[security] fix(auth): reject cross-site auth POSTs#2740
Merged
WillemJiang merged 3 commits intobytedance:mainfrom May 6, 2026
Merged
[security] fix(auth): reject cross-site auth POSTs#2740WillemJiang merged 3 commits intobytedance:mainfrom
WillemJiang merged 3 commits intobytedance:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens DeerFlow’s CSRF-exempt auth endpoints (login/register/logout/initialize) by adding an Origin-based browser boundary check, preventing cross-site form POSTs from creating attacker-controlled sessions (login CSRF / session fixation), while preserving same-origin behavior and explicitly configured CORS origins.
Changes:
- Added origin normalization + proxy-aware request-origin reconstruction (including
X-Forwarded-*and RFC 7239Forwarded) and enforced403for hostile/malformed browserOriginvalues on CSRF-exempt auth POSTs. - Preserved existing double-submit CSRF enforcement for non-auth state-changing routes.
- Added focused regression tests covering hostile origins, malformed origins, forwarded proxy headers, explicit CORS origins, wildcard handling, and double-submit behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| backend/app/gateway/csrf_middleware.py | Adds origin parsing/normalization and rejects cross-site browser Origins for CSRF-exempt auth POST endpoints. |
| backend/tests/test_csrf_middleware.py | Adds regression tests for the new auth-origin restriction and confirms non-auth CSRF behavior remains enforced. |
WillemJiang
approved these changes
May 6, 2026
Wingxxx
pushed a commit
to Wingxxx/deer-flow
that referenced
this pull request
May 7, 2026
* fix(security): reject cross-site auth posts * fix(auth): align secure cookie proxy scheme handling --------- Co-authored-by: Willem Jiang <willem.jiang@gmail.com>
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
This PR hardens the browser authentication boundary around CSRF-exempt auth endpoints.
Auth routes such as
/api/v1/auth/login/localintentionally skip the double-submit CSRF token because first-time browser clients do not have a CSRF cookie yet. Before this change, those same endpoints also accepted browser POSTs with a hostileOrigin, which allowed login CSRF/session fixation: a malicious site could submit the victim browser to DeerFlow with attacker-controlled credentials and cause later victim activity to be saved under the attacker account.This PR:
Originis neither same-origin nor explicitly configured inGATEWAY_CORS_ORIGINS;Originnon-browser clients;Security issues covered
Before this PR
/api/v1/auth/login/local,/register,/initialize, and/logoutwere exempt from CSRF token validation./api/v1/auth/login/localaccepts browser-form-postable credentials throughOAuth2PasswordRequestForm.Originhandling on CSRF-exempt auth routes.After this PR
403.GATEWAY_CORS_ORIGINScontinue to work for split frontend/backend deployments.Originheader continue to work for non-browser clients such as curl/mobile integrations.Why this matters
Cookie
SameSite=Laxdoes not prevent a top-level cross-site form POST from causing the target site to set cookies on the response. Because login is a session-creating endpoint, a malicious page can submit attacker-known DeerFlow credentials and silently switch the victim browser into the attacker account.The attacker does not steal the victim's existing DeerFlow account. The risk is session confusion and data capture: after the forced login, the victim may enter prompts, upload files, or generate artifacts that the attacker can later access by logging into the same attacker-controlled account.
Attack flow
Affected code
backend/app/gateway/csrf_middleware.py,backend/app/gateway/routers/auth.pyRoot cause
Issue: login CSRF / session fixation
CVSS assessment
CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:U/C:H/I:N/A:NRationale:
Safe reproduction steps
On vulnerable code, host this minimal page on another origin:
Have a victim browser visit the page.
The browser submits a cross-site form POST to DeerFlow.
On vulnerable code, the auth route processes the request because it is CSRF-exempt.
The victim browser receives DeerFlow auth cookies for the attacker account.
Any later work the victim performs in DeerFlow is associated with that attacker account.
The regression test
test_auth_post_rejects_cross_origin_browser_requestcaptures the safe proof without requiring real credentials.Expected vulnerable behavior
/api/v1/auth/login/localwere accepted.Changes in this PR
X-Forwarded-*and RFC 7239Forwardedproxy headers.GATEWAY_CORS_ORIGINSentries.*) for auth-origin bypass purposes.Originbehavior for non-browser clients.Files changed
backend/app/gateway/csrf_middleware.py403rejection path for hostile auth POST originsbackend/tests/test_csrf_middleware.pyMaintainer impact
GATEWAY_CORS_ORIGINS.Originremain compatible.Suggested fix rationale
Origin checking is the appropriate compensating control for session-creating endpoints that cannot require a pre-existing CSRF token. It blocks browser-driven cross-site login/register/setup requests while avoiding a bootstrap problem for legitimate first-time auth flows.
The helper fails closed for malformed browser Origin values, handles common reverse-proxy headers, and keeps ordinary double-submit checks unchanged for all other state-changing routes.
Type of change
Test plan
Executed with:
cd backend uv run pytest -q tests/test_csrf_middleware.py tests/test_auth_middleware.py tests/test_auth.py uv run ruff check app/gateway/csrf_middleware.py tests/test_csrf_middleware.py uv run ruff format --check app/gateway/csrf_middleware.py tests/test_csrf_middleware.py uv run python -m compileall -q app/gateway/csrf_middleware.py tests/test_csrf_middleware.py git diff --checkResult:
111 passed, 1 warningfor the targeted pytest command.git diff --checkpassed.test_auth_middleware.pycookie-use deprecation and is unrelated to this patch.Token usage
Disclosure notes