Repository navigation
Security: HMAC-sign login_user cookie + drop Origin/Referer fallback in frontend URL - #174
Merged
Merged
Conversation
…r XSS ## Bug 1 — Forgeable login_user cookie (CodeQL #56, user-controlled-bypass) The OAuth callback flow read req.cookies.login_user, base64-decoded the JSON, and trusted the resulting 'profile' as the authenticated user. The cookie was set without a secret in cookieParser(), so it was NOT HMAC-signed — anyone who knew a target user's id/email could forge a profile, set the cookie in their browser, navigate to /callback, and log in as that user. Fix: - main.ts: initialise cookieParser(secret) with COOKIE_SECRET (or JWT_SECRET as fallback). Throws at startup if neither is present. Logs a warning in production when the fallback is used. - login.controller.ts: set the login_user cookie with { signed: true }. - local-oauth.strategy.ts: read from req.signedCookies (not req.cookies). Unsigned/forged cookies are silently rejected → redirected to /login. - .env.example: document COOKIE_SECRET as an optional separate secret for defense in depth. ## Bug 2 — XSS in password-reset email via Origin header (CodeQL #11) auth.controller.ts#getFrontendUrl() preferred req.headers.origin over the FRONTEND_URL env var. That value was then interpolated into the password-reset email HTML (email.service.ts:46) inside an <a href> attribute. An attacker crafting an Origin header like \" onmouseover=\"alert(...)\" could inject HTML attribute content into the recipient's email. Fix: drop the Origin/Referer fallback entirely. Frontend URL must come from server-side config (FRONTEND_URL → SERVER_URL → localhost) which is operator-controlled, not attacker-controlled.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two real CodeQL findings, both auth-adjacent, both pre-launch blockers.
1. `login_user` cookie was forgeable (CodeQL #56 `js/user-controlled-bypass`)
`local-oauth.strategy.ts` read `req.cookies.login_user`, base64-decoded the JSON, and trusted the resulting profile as the authenticated user. The cookie was set without a secret in `cookieParser()`, so it was NOT HMAC-signed — anyone who knew a target user's id/email could forge a profile, set the cookie in their browser, navigate to `/callback`, and log in as that user.
Fix:
2. XSS in password-reset email via Origin header (CodeQL #11 `js/xss`)
`auth.controller.ts#getFrontendUrl()` preferred `req.headers.origin` over the `FRONTEND_URL` env var. That value was then interpolated into the password-reset email HTML inside an `` attribute. Attacker crafting an Origin header like `" onmouseover="alert(...)" ` could inject HTML attribute content into the recipient's email.
Fix: drop the Origin/Referer fallback entirely. Frontend URL must come from server-side config (`FRONTEND_URL` → `SERVER_URL` → `localhost`), which is operator-controlled.
Test plan
Migration note for self-hosters
No env-var change required for existing deployments (falls back to `JWT_SECRET` if `COOKIE_SECRET` not set). Strongly recommended to add a separate `COOKIE_SECRET=<32+ chars>` in production .env for defense in depth.