mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-10-26 21:26:39 +00:00
* fix(gateway): reject non-ASCII tokens instead of raising in constant-time compares
hmac.compare_digest raises TypeError when a str operand holds non-ASCII
characters. Starlette decodes header bytes as latin-1 and query values as
UTF-8, so a single 0xE9 byte in a CSRF token, GitHub webhook signature,
internal auth token, or OIDC state turned the expected 403/401 into a 500.
Route the five client-facing comparisons through a new
app.gateway.utils.constant_time_equals helper that compares the UTF-8
encodings ("surrogatepass" so even a lone surrogate cannot raise). ASCII
results are unchanged, and no bypass was possible before the fix.
* fix(provisioner): reject non-ASCII API keys instead of raising; drive OIDC test over HTTP
The standalone provisioner compared X-API-Key with secrets.compare_digest on
str operands, so a non-ASCII header raised TypeError (500) the same way the
Gateway sites did. It cannot import app.gateway, so encode inline.
The OIDC state regression test now mounts the auth router and sends the
percent-encoded state through a real signed state cookie instead of stubbing
get_state_cookie and calling the handler directly.
* docs(changelog): reference #6076
---------
Co-authored-by: Willem Jiang <willem.jiang@gmail.com>
307 lines
9.2 KiB
Python
307 lines
9.2 KiB
Python
"""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}
|
|
|
|
@app.post("/api/v1/auth/log{gap}in/local")
|
|
async def control_gap(gap: str):
|
|
return {"gap": gap}
|
|
|
|
@app.post("/api/v1/auth/me{suffix}")
|
|
async def delimiter_suffix(suffix: str):
|
|
return {"suffix": suffix}
|
|
|
|
return app
|
|
|
|
|
|
def test_url_reconstruction_cannot_create_a_csrf_exemption():
|
|
client = TestClient(_make_app(), base_url="https://deerflow.example")
|
|
|
|
for encoded_path in (
|
|
"/api/v1/auth/log%0Ain/local",
|
|
"/api/v1/auth/log%0Din/local",
|
|
"/api/v1/auth/log%09in/local",
|
|
"/api/v1/auth/me%23private",
|
|
"/api/v1/auth/me%3Fprivate",
|
|
):
|
|
response = client.post(encoded_path)
|
|
assert response.status_code == 403, encoded_path
|
|
|
|
|
|
def test_csrf_uses_the_same_root_path_projection_as_the_router():
|
|
child = FastAPI()
|
|
child.add_middleware(CSRFMiddleware)
|
|
|
|
@child.post("/api/v1/auth/login/local")
|
|
async def login_local():
|
|
return {"ok": True}
|
|
|
|
parent = FastAPI()
|
|
parent.mount("/prefix", child)
|
|
|
|
response = TestClient(
|
|
parent,
|
|
base_url="https://deerflow.example",
|
|
).post("/prefix/api/v1/auth/login/local")
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
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_forwarded_same_origin_with_non_default_port():
|
|
client = TestClient(_make_app(), base_url="http://internal:8000")
|
|
|
|
response = client.post(
|
|
"/api/v1/auth/login/local",
|
|
headers={
|
|
"Origin": "http://localhost:2026",
|
|
"X-Forwarded-Proto": "http",
|
|
"X-Forwarded-Host": "localhost:2026",
|
|
},
|
|
)
|
|
|
|
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")
|
|
assert "secure" in response.headers["set-cookie"].lower()
|
|
|
|
|
|
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."
|
|
|
|
|
|
def test_non_auth_mutation_rejects_non_ascii_double_submit_token():
|
|
# Starlette decodes header bytes as latin-1, so a raw 0xE9 reaches the
|
|
# comparison as "é"; that must be a 403 mismatch, not a 500.
|
|
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": "cookie-tok\xe9n".encode("latin-1"),
|
|
},
|
|
)
|
|
|
|
assert response.status_code == 403
|
|
assert response.json()["detail"] == "CSRF token mismatch."
|
|
|
|
|
|
def test_channel_posts_require_double_submit_csrf():
|
|
client = TestClient(_make_app(), base_url="https://deerflow.example")
|
|
|
|
response = client.post(
|
|
"/api/channels/slack/connect",
|
|
headers={"Origin": "https://deerflow.example"},
|
|
)
|
|
|
|
assert response.status_code == 403
|
|
assert response.json()["detail"] == "CSRF token missing. Include X-CSRF-Token header."
|