Spaces:
Configuration error
Configuration error
fix: CORS for HuggingFace Spaces + OPTIONS preflight + auth banner visibility
Browse filesFiles changed (2)
- src/app.py+57-9
- src/viewer/index.html+16-14
144 unmodified lines145146147148149148149150151152153154155151156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190156157158159160161191192193194195196197198199200201202203204205206207208209210211212213214215216144 unmodified lines return send_from_directory(os.path.join(_base_dir, "viewer"), "favicon.svg")
# 7. CORS after_request — D2.1: configurable via AGENTMEMORY_CORS_ORIGINS env var
# Default allows localhost, 127.0.0.1, vscode-webview://, and chrome-extension://
_default_cors = "http://localhost,http://127.0.0.1,vscode-webview://,chrome-extension://"
# Default allows localhost, 127.0.0.1, HuggingFace Spaces, vscode-webview://, chrome-extension://
# Wildcard entries like "*.hf.space" match any subdomain via suffix check.
_default_cors = (
"http://localhost,http://127.0.0.1,"
"https://huggingface.co,https://*.hf.space,"
"vscode-webview://,chrome-extension://"
)
_cors_origins_raw = os.getenv("AGENTMEMORY_CORS_ORIGINS", _default_cors)
_allowed_origins = [o.strip().rstrip("*") for o in _cors_origins_raw.split(",") if o.strip()]
def _parse_cors_origins(raw: str):
"""Return (exact_set, suffix_list) for efficient origin matching."""
exact, suffixes = set(), []
for entry in raw.split(","):
entry = entry.strip()
if not entry:
continue
if entry.startswith("*."):
# *.hf.space → match anything ending with .hf.space
suffixes.append(entry[1:].lower()) # keep the leading dot: ".hf.space"
elif "*" in entry:
# generic prefix wildcard: strip trailing * and treat as prefix
suffixes.append(("prefix:", entry.rstrip("*").lower()))
else:
exact.add(entry.lower())
return exact, suffixes
_cors_exact, _cors_suffixes = _parse_cors_origins(_cors_origins_raw)
def _origin_allowed(origin: str) -> bool:
lo = origin.lower()
if lo in _cors_exact:
return True
for s in _cors_suffixes:
if isinstance(s, tuple) and s[0] == "prefix:":
if lo.startswith(s[1]):
return True
elif lo.endswith(s):
return True
return False
@flask_app.after_request
def _cors(response):
origin = request.headers.get("Origin")
if origin:
lo = origin.lower()
if any(lo == allowed.lower() or lo.startswith(allowed.lower())
for allowed in _allowed_origins):
response.headers["Access-Control-Allow-Origin"] = origin
response.headers["Access-Control-Allow-Credentials"] = "true"
if origin and _origin_allowed(origin):
response.headers["Access-Control-Allow-Origin"] = origin
response.headers["Access-Control-Allow-Credentials"] = "true"
response.headers.add("Access-Control-Allow-Headers", "Content-Type, Authorization")
response.headers.add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
return response
# Handle CORS preflight OPTIONS requests globally
from flask import Response as _FlaskResponse
@flask_app.before_request
def _handle_options():
if request.method == "OPTIONS":
origin = request.headers.get("Origin", "")
if origin and _origin_allowed(origin):
resp = _FlaskResponse("", status=204)
resp.headers["Access-Control-Allow-Origin"] = origin
resp.headers["Access-Control-Allow-Credentials"] = "true"
resp.headers["Access-Control-Allow-Headers"] = "Content-Type, Authorization"
resp.headers["Access-Control-Allow-Methods"] = "GET, POST, PUT, DELETE, OPTIONS"
resp.headers["Access-Control-Max-Age"] = "86400"
return resp
# 8. Background workers
from workers import start_background_workers
start_background_workers(kv)878 unmodified lines879880881882882883884885886886887888889890891891892893894895896897898899894895896897898899900901902903903904905906907908909910909910911912913914915914916917918917919920921922878 unmodified lines
.viewer-auth { display: none; padding: 0 24px 10px 24px; padding: 0; background: var(--bg); flex: 0 0 auto; position: relative; z-index: 1; z-index: 10; } .viewer-auth.open { display: block; } .viewer-auth-panel { display: grid; grid-template-columns: minmax(0, 1fr) minmax(180px, 320px) auto; grid-template-columns: minmax(0, 1fr) minmax(200px, 360px) auto; gap: 10px; align-items: center; width: min(960px, 100%); max-width: 960px; padding: 10px 14px; border: 1px solid var(--border); border-left: 3px solid var(--accent); background: var(--bg-subtle); width: 100%; max-width: 100%; padding: 12px 24px; border-top: none; border-bottom: 3px solid var(--accent); background: #fff8f0; font-family: var(--font-ui); font-size: 12px; box-shadow: 0 2px 8px rgba(204,0,0,0.10); } .viewer-auth-title { font-weight: 600; color: var(--ink); margin-bottom: 2px; } html[data-theme="dark"] .viewer-auth-panel { background: #2a1a1a; } .viewer-auth-title { font-weight: 700; color: var(--accent); margin-bottom: 2px; font-size: 13px; } .viewer-auth-desc { color: var(--ink-muted); line-height: 1.4; } .viewer-auth-desc code { font-family: var(--font-mono); font-size: 10px; color: var(--ink); } .viewer-auth input { width: 100%; min-width: 0; padding: 7px 9px; border: 1px solid var(--border); padding: 8px 10px; border: 1px solid var(--accent); background: var(--bg); color: var(--ink); font-family: var(--font-mono); font-size: 12px; font-size: 13px; } @media (max-width: 900px) { .viewer-auth-panel { grid-template-columns: 1fr; } .viewer-auth-panel { grid-template-columns: 1fr; padding: 12px 16px; } }
/* Viewer footer */src/app.py+57-9
src/viewer/index.html+16-14