Skip to content

Events: refuse an http Origin on an https request - #865

Merged
JuroOravec merged 4 commits into
mainfrom
events/refuse-http-origin
Nov 24, 2026
Merged

JuroOravec merged 4 commits into
mainfrom
events/refuse-http-origin

Conversation

@JuroOravec

@JuroOravec JuroOravec commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

(Opus 5.5)

Closes #769.

What changes

The Events CSRF check (enforce_floor) compared only the Origin header's host and port with Host. It now also refuses a call whose Origin is not https: when the server reports that the call arrived over https. This stops a plain-http page on the same host, which anyone on the network can rewrite, from calling an https app.

  • RouteRequest gets a new scheme field (last, default ""). The ASGI adapter reads the scope's scheme (default "http" per the ASGI spec), the WSGI adapter reads wsgi.url_scheme, and the Django adapter reads request.scheme, which follows SECURE_PROXY_SSL_HEADER. The preview host fills it the same way as the ASGI adapter. Flask and FastAPI go through the WSGI and ASGI adapters.
  • An http request accepts an Origin of either scheme. Behind a proxy that serves the site over https and passes plain http to the app, the app sees http while the browser sends an https: origin, so that setup keeps working. The check runs there only when the server reads the forwarded scheme (Django SECURE_PROXY_SSL_HEADER, Uvicorn --forwarded-allow-ips for a proxy not on 127.0.0.1, Werkzeug ProxyFix(x_proto=1)); the new "Read the proxy's scheme" docs section says how.
  • An empty scheme (a hand-built RouteRequest) skips the scheme check. The comparison ignores case.
  • An Origin that urlsplit cannot parse is now refused through the normal CSRF path. Before, it raised ValueError, which the dispatcher already answered as csrf_failed but logged with a traceback; only the log changes.

Tests

  • packages/py/citry/tests/test_events_origin_scheme.py: the same call through WSGI, ASGI and Django for https+http Origin (403), https+https (200), the proxy case http+https (200), and http+http (200); Django with SECURE_PROXY_SSL_HEADER; a no-JS form post from an http page (403); the csrf_failed wire error; how the ASGI and WSGI adapters read and lowercase the scheme.
  • TestCsrfFloor in test_events_routes.py: the scheme matrix, uppercase values, no Origin, and an unparseable Origin.
  • The adapter, Django-proxy and unit refusal tests fail on main without the fix.

Local checks: the focused Events, contrib, Django, route and preview-host suites (2677 passed before the review fixes; the changed files again after them), ruff check ., ruff format --check ., mypy on the gate's full path set on macOS and with --platform linux, uv lock --check, and scripts/validate.py. The cargo phases were not run locally (Python-only change).

Docs

security.md ("What Citry always checks"), advanced/web-frameworks.md ("Run behind a proxy", and new "Fix an http Origin" and "Read the proxy's scheme" sections), the Events v1 protocol spec, docs/design/events.md 7.4, docs/design/security_csrf.md, and the CSRF row of docs/design/trust_boundaries.md.

Versioning

  • RouteRequest.scheme is a new public field with a default, added last, so positional and keyword callers keep working: a minor-release addition, targeted at 1.3.0.
  • Refusing these calls refuses requests 1.2.x accepted. This is a security tightening, which the 1.x rules allow with a changelog note; the CHANGELOG.md entry under v1.3.0 says so. Citry already refused these calls from browsers that send Sec-Fetch-Site, so browser traffic on a correctly configured site should not change. A site served to browsers over plain http while its server reports https now fails every call that is not a GET; the docs describe the fix.
  • The citry-events/1 wire format does not change.

Review

An independent reviewer ran a technical, security (Mechanism 7), advisory-survey (Mechanism 8), versioning and prose pass, and a docs cold reader read the changed pages. No hole was found. Fixed during the review: a case-sensitive scheme comparison for hand-built requests, a missing form-post test, missing adapter-reading tests, a stale statement in docs/design/security_csrf.md, stale line numbers in trust_boundaries.md, docs that did not say the check is skipped behind a TLS-ending proxy unless the server reads the forwarded scheme, and (from a second prose pass) a wrong Uvicorn flag, a missing Flask ProxyFix ordering note, and an inaccurate code comment. Not checked in a real browser: the exact Sec-Fetch-Site value each engine sends for an http page posting to https on the same host (Citry refuses both same-site and cross-site).

🤖 Generated with Claude Code

JuroOravec and others added 4 commits November 24, 2026 19:10
The Events CSRF floor compared only the Origin's host and port with Host.
RouteRequest now carries the scheme each adapter reports (ASGI scope,
wsgi.url_scheme, Django request.scheme), and enforce_floor refuses an
Origin that is not https: when that scheme is https. An http request
does not compare schemes, so a proxy that ends TLS keeps working. An
Origin that urlsplit cannot read is refused as a plain CSRF failure.

Closes #769

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Compare the request scheme case-insensitively, add a form-post test
for the attack the check stops and unit tests for how the ASGI and WSGI
adapters read the scheme, explain the TLS-ending proxy case and how to
turn the check on there, give the http-Origin failure its own section,
and refresh docs/design/security_csrf.md and the trust-boundary line
numbers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Name Uvicorn's --forwarded-allow-ips (proxy headers are already on),
keep the Flask ProxyFix ordering rule, give reading the proxy's scheme
its own section and link to it, drop a wrong rationale comment in
csrf.py, and tighten the changelog and RouteRequest.scheme wording.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JuroOravec
JuroOravec merged commit 32ad422 into main Nov 24, 2026
0 of 12 checks passed
@JuroOravec
JuroOravec deleted the events/refuse-http-origin branch November 24, 2026 19:39
JuroOravec added a commit that referenced this pull request Nov 24, 2026
#871 (#878)

Records the Mechanism 7 security reviews of six merged Events changes, in
the form docs/security/reviews/README.md describes. Each record is built
from the pull request description, the merged diff and its tests; where a
description does not record something the template asks for, the record
says so.

Co-authored-by: Claude Sonnet 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Events: refuse an http Origin on an https request

1 participant