fix(generator): harden SPA path handler and cache patch_settings read

- Drop the os.path.isfile pre-check in the SPA catch-all; let
  send_from_directory be the only gatekeeper so raw user-supplied
  path segments never reach os.path.isfile on a joined path.
- Reuse the first db.get_settings result in patch_settings so a
  race-induced None doesn't raise an unhandled AttributeError.
This commit is contained in:
shay
2026-04-17 17:01:56 -04:00
parent d448825bed
commit c50393467e
+8 -5
View File
@@ -28,9 +28,11 @@ def require_auth(fn):
def spa(path: str):
if path.startswith("api/"):
return jsonify({"error": "not found"}), 404
full = os.path.join(_STATIC_DIR, path) if path else os.path.join(_STATIC_DIR, "index.html")
if path and os.path.isfile(full):
return send_from_directory(_STATIC_DIR, path)
if path:
try:
return send_from_directory(_STATIC_DIR, path)
except Exception:
pass
index = os.path.join(_STATIC_DIR, "index.html")
if os.path.isfile(index):
return send_from_directory(_STATIC_DIR, "index.html")
@@ -113,10 +115,11 @@ def get_settings(username: str):
@app.route("/api/<username>/settings", methods=["PATCH"])
@require_auth
def patch_settings(username: str):
if db.get_settings(username) is None:
current = db.get_settings(username)
if current is None:
return jsonify({"error": "not_enrolled"}), 404
body = request.get_json(silent=True) or {}
merged = {**db.get_settings(username)["settings"], **body}
merged = {**current["settings"], **body}
job_id = db.update_settings(username, merged)
return jsonify({"updated": True, "job_id": job_id})