-
Notifications
You must be signed in to change notification settings - Fork 14
fix: harden security — configurable CORS origin, role escalation, unauthenticated seed #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -356,8 +356,8 @@ def verify_token(raw: str, secret: str): | |
| # Response helpers | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| _CORS_ORIGIN_INITIALISED = False | ||
| _CORS = { | ||
| "Access-Control-Allow-Origin": "*", | ||
| "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS", | ||
| "Access-Control-Allow-Headers": "Content-Type, Authorization", | ||
| } | ||
|
|
@@ -1061,12 +1061,11 @@ async def api_join(req, env): | |
| return bad_resp | ||
|
|
||
| act_id = body.get("activity_id") | ||
| role = (body.get("role") or "participant").strip() | ||
|
|
||
| if not act_id: | ||
| return err("activity_id is required") | ||
| if role not in ("participant", "instructor", "organizer"): | ||
| role = "participant" | ||
|
|
||
| role = "participant" | ||
|
Comment on lines
1063
to
+1068
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick | 🔵 Trivial Privilege-escalation fix looks solid 🛡️ — please refresh the stale join tests. Hardcoding One downstream cleanup: the existing tests in 🧪 Sketch of the hardened assertionasync def test_join_ignores_client_supplied_role(self):
tok = _token()
act_row = MockRow(id="act-1")
insert_stmt = make_stmt() # capture bound params
env = make_env(db=MockDB([make_stmt(first=act_row), insert_stmt]))
r = await worker.api_join(
self._req({"activity_id": "act-1", "role": "instructor"}, token=tok), env
)
assert r.status == 200
# The 4th bound parameter to the INSERT is the role column.
assert insert_stmt.bound_args[-1] == "participant"As per coding guidelines: "Verify tests cover the key logic paths." 🤖 Prompt for AI Agents |
||
|
|
||
| act = await env.DB.prepare( | ||
| "SELECT id FROM activities WHERE id=?" | ||
|
|
@@ -1335,6 +1334,12 @@ async def serve_static(path: str, env): | |
| # --------------------------------------------------------------------------- | ||
|
|
||
| async def _dispatch(request, env): | ||
| global _CORS_ORIGIN_INITIALISED | ||
| if not _CORS_ORIGIN_INITIALISED: | ||
| _CORS_ORIGIN_INITIALISED = True | ||
| origin = (getattr(env, "ALLOWED_ORIGIN", "") or "").strip() | ||
| _CORS["Access-Control-Allow-Origin"] = origin if origin else "*" | ||
|
zikunz marked this conversation as resolved.
|
||
|
|
||
|
zikunz marked this conversation as resolved.
|
||
| path = urlparse(request.url).path | ||
| method = request.method.upper() | ||
| admin_path = _clean_path(getattr(env, "ADMIN_URL", "")) | ||
|
|
@@ -1357,6 +1362,8 @@ async def _dispatch(request, env): | |
| return err("Database init failed — check D1 binding", 500) | ||
|
|
||
| if path == "/api/seed" and method == "POST": | ||
| if not _is_basic_auth_valid(request, env): | ||
| return _unauthorized_basic() | ||
|
zikunz marked this conversation as resolved.
|
||
| try: | ||
| await init_db(env) | ||
| await seed_db(env, env.ENCRYPTION_KEY) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.