-
Notifications
You must be signed in to change notification settings - Fork 8.8k
[security] fix(auth): reject cross-site auth POSTs #2740
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
Merged
WillemJiang
merged 3 commits into
bytedance:main
from
Hinotoi-agent:fix/auth-origin-csrf
May 6, 2026
+331
−1
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,218 @@ | ||
| """Tests for CSRF middleware.""" | ||
|
|
||
| from fastapi import FastAPI | ||
| from starlette.testclient import TestClient | ||
|
|
||
| from app.gateway.csrf_middleware import CSRFMiddleware | ||
|
|
||
|
|
||
| def _make_app() -> FastAPI: | ||
| app = FastAPI() | ||
| app.add_middleware(CSRFMiddleware) | ||
|
|
||
| @app.post("/api/v1/auth/login/local") | ||
| async def login_local(): | ||
| return {"ok": True} | ||
|
|
||
| @app.post("/api/v1/auth/register") | ||
| async def register(): | ||
| return {"ok": True} | ||
|
|
||
| @app.post("/api/threads/abc/runs/stream") | ||
| async def protected_mutation(): | ||
| return {"ok": True} | ||
|
|
||
| return app | ||
|
|
||
|
|
||
| def test_auth_post_rejects_cross_origin_browser_request(): | ||
| """CSRF-exempt auth routes must not accept hostile browser origins. | ||
|
|
||
| Login/register endpoints intentionally skip the double-submit token because | ||
| first-time callers do not have a token yet. They still set an auth session, | ||
| so a hostile cross-site form POST must be rejected to avoid login CSRF / | ||
| session fixation. | ||
| """ | ||
| client = TestClient(_make_app(), base_url="https://deerflow.example") | ||
|
|
||
| response = client.post( | ||
| "/api/v1/auth/login/local", | ||
| headers={"Origin": "https://evil.example"}, | ||
| ) | ||
|
|
||
| assert response.status_code == 403 | ||
| assert response.json()["detail"] == "Cross-site auth request denied." | ||
|
|
||
|
|
||
| def test_auth_post_allows_same_origin_browser_request(): | ||
| client = TestClient(_make_app(), base_url="https://deerflow.example") | ||
|
|
||
| response = client.post( | ||
| "/api/v1/auth/login/local", | ||
| headers={"Origin": "https://deerflow.example"}, | ||
| ) | ||
|
|
||
| assert response.status_code == 200 | ||
| assert response.cookies.get("csrf_token") | ||
|
|
||
|
|
||
| def test_auth_post_rejects_malformed_origin_with_path(): | ||
| client = TestClient(_make_app(), base_url="https://deerflow.example") | ||
|
|
||
| response = client.post( | ||
| "/api/v1/auth/login/local", | ||
| headers={"Origin": "https://deerflow.example/path"}, | ||
| ) | ||
|
|
||
| assert response.status_code == 403 | ||
| assert response.json()["detail"] == "Cross-site auth request denied." | ||
| assert response.cookies.get("csrf_token") is None | ||
|
|
||
|
|
||
| def test_auth_post_rejects_malformed_origin_with_invalid_port(): | ||
| client = TestClient(_make_app(), base_url="https://deerflow.example") | ||
|
|
||
| response = client.post( | ||
| "/api/v1/auth/login/local", | ||
| headers={"Origin": "https://deerflow.example:bad"}, | ||
| ) | ||
|
|
||
| assert response.status_code == 403 | ||
| assert response.json()["detail"] == "Cross-site auth request denied." | ||
| assert response.cookies.get("csrf_token") is None | ||
|
|
||
|
|
||
| def test_auth_post_allows_same_origin_default_port_equivalence(): | ||
| client = TestClient(_make_app(), base_url="https://deerflow.example") | ||
|
|
||
| response = client.post( | ||
| "/api/v1/auth/login/local", | ||
| headers={"Origin": "https://deerflow.example:443"}, | ||
| ) | ||
|
|
||
| assert response.status_code == 200 | ||
| assert response.cookies.get("csrf_token") | ||
|
|
||
|
|
||
| def test_auth_post_allows_forwarded_same_origin(): | ||
| client = TestClient(_make_app(), base_url="http://internal:8000") | ||
|
|
||
| response = client.post( | ||
| "/api/v1/auth/login/local", | ||
| headers={ | ||
| "Origin": "https://deerflow.example", | ||
| "X-Forwarded-Proto": "https", | ||
| "X-Forwarded-Host": "deerflow.example, internal:8000", | ||
| }, | ||
| ) | ||
|
|
||
| assert response.status_code == 200 | ||
| assert response.cookies.get("csrf_token") | ||
|
|
||
|
|
||
| def test_auth_post_allows_rfc_forwarded_same_origin(): | ||
| client = TestClient(_make_app(), base_url="http://internal:8000") | ||
|
|
||
| response = client.post( | ||
| "/api/v1/auth/login/local", | ||
| headers={ | ||
| "Origin": "https://deerflow.example", | ||
| "Forwarded": "proto=https;host=deerflow.example", | ||
| }, | ||
| ) | ||
|
|
||
| assert response.status_code == 200 | ||
| assert response.cookies.get("csrf_token") | ||
|
Hinotoi-agent marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def test_auth_post_allows_explicit_configured_origin(monkeypatch): | ||
| monkeypatch.setenv("GATEWAY_CORS_ORIGINS", "https://app.example") | ||
| client = TestClient(_make_app(), base_url="https://api.example") | ||
|
|
||
| response = client.post( | ||
| "/api/v1/auth/register", | ||
| headers={"Origin": "https://app.example"}, | ||
| ) | ||
|
|
||
| assert response.status_code == 200 | ||
| assert response.cookies.get("csrf_token") | ||
|
|
||
|
|
||
| def test_auth_post_does_not_treat_wildcard_cors_as_allowed_origin(monkeypatch): | ||
| monkeypatch.setenv("GATEWAY_CORS_ORIGINS", "*") | ||
| client = TestClient(_make_app(), base_url="https://api.example") | ||
|
|
||
| response = client.post( | ||
| "/api/v1/auth/login/local", | ||
| headers={"Origin": "https://evil.example"}, | ||
| ) | ||
|
|
||
| assert response.status_code == 403 | ||
| assert response.json()["detail"] == "Cross-site auth request denied." | ||
|
|
||
|
|
||
| def test_auth_post_sets_strict_samesite_csrf_cookie(): | ||
| client = TestClient(_make_app(), base_url="https://deerflow.example") | ||
|
|
||
| response = client.post( | ||
| "/api/v1/auth/login/local", | ||
| headers={"Origin": "https://deerflow.example"}, | ||
| ) | ||
|
|
||
| assert response.status_code == 200 | ||
| set_cookie = response.headers["set-cookie"].lower() | ||
| assert "csrf_token=" in set_cookie | ||
| assert "samesite=strict" in set_cookie | ||
| assert "secure" in set_cookie | ||
|
|
||
|
|
||
| def test_auth_post_without_origin_still_allows_non_browser_clients(): | ||
| client = TestClient(_make_app(), base_url="https://deerflow.example") | ||
|
|
||
| response = client.post("/api/v1/auth/login/local") | ||
|
|
||
| assert response.status_code == 200 | ||
| assert response.cookies.get("csrf_token") | ||
|
|
||
|
|
||
| def test_non_auth_mutation_still_requires_double_submit_token(): | ||
| client = TestClient(_make_app(), base_url="https://deerflow.example") | ||
|
|
||
| response = client.post( | ||
| "/api/threads/abc/runs/stream", | ||
| headers={"Origin": "https://deerflow.example"}, | ||
| ) | ||
|
|
||
| assert response.status_code == 403 | ||
| assert response.json()["detail"] == "CSRF token missing. Include X-CSRF-Token header." | ||
|
|
||
|
|
||
| def test_non_auth_mutation_allows_valid_double_submit_token(): | ||
| client = TestClient(_make_app(), base_url="https://deerflow.example") | ||
| client.cookies.set("csrf_token", "known-token") | ||
|
|
||
| response = client.post( | ||
| "/api/threads/abc/runs/stream", | ||
| headers={ | ||
| "Origin": "https://deerflow.example", | ||
| "X-CSRF-Token": "known-token", | ||
| }, | ||
| ) | ||
|
|
||
| assert response.status_code == 200 | ||
|
|
||
|
|
||
| def test_non_auth_mutation_rejects_mismatched_double_submit_token(): | ||
| client = TestClient(_make_app(), base_url="https://deerflow.example") | ||
| client.cookies.set("csrf_token", "cookie-token") | ||
|
|
||
| response = client.post( | ||
| "/api/threads/abc/runs/stream", | ||
| headers={ | ||
| "Origin": "https://deerflow.example", | ||
| "X-CSRF-Token": "header-token", | ||
| }, | ||
| ) | ||
|
|
||
| assert response.status_code == 403 | ||
| assert response.json()["detail"] == "CSRF token mismatch." | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.