Skip to content

id(web): Origin check ignores the port — a different localhost port passes as same-origin (CSRF/CSWSH gap in security.rs) #814

Closed

Description

@dezren39

Problem

The id web UI's Origin check (pkgs/id/src/web/security.rs) compares only the host of the Origin header against the host of the Host header — the port is stripped from both sides before comparison, so an origin on a different port of the same host passes as "same-origin".

This defeats the layer the module docs say it provides (security.rs:12-15): defeating cross-site request forgery and cross-site WebSocket hijacking for browser requests. An origin is scheme + host + port: a page served from http://localhost:9999 (any other local web app, dev server, or attacker page proxied/served on another local port) is a different origin from the id UI on http://localhost:3000, but the guard treats them as the same. WebSocket handshakes carry no CORS preflight, so the Origin check is the only browser-side defence for the /ws/* routes when no token is configured (the default loopback setup relies on exactly these host/origin layers).

Evidence (verified on main 1161bcff)

  • security.rs:73-83 — host_only() strips an optional :port (and lower-cases) from a Host/Origin authority: host_only("localhost:9999") == host_only("localhost:3000") == "localhost".
  • security.rs:138-145 — the guard's same-origin test is exactly that comparison:
    let same = origin_authority(origin).is_some_and(|a| host_only(a) == host_only(&host_header));
    So a request with Host: localhost:3000 and Origin: http://localhost:9999 evaluates same == true and is not refused.
  • The existing test same_origin_is_allowed_cross_origin_is_not (security.rs:278) only exercises a same-port origin (http://localhost:3000 vs Host localhost:3000) and a different-host origin (https://evil.example); no test covers a different-port origin.
  • No other layer compensates: grep -rn Origin pkgs/id/src/web shows this guard is the only Origin check in the web stack.

Repro (verified by running the guard): a test added to the security tests in a scratch worktree driving guard with Host: localhost:3000 and Origin: http://localhost:9999, asserting the current behaviour, passed on main 1161bcff — i.e. the different-port origin was answered 200 OK where a true same-origin check would refuse with 403. Full run: cargo test --features web --lib web::security:: → 12 passed, 0 failed (11 pre-existing + the repro case).

Why it matters

  • The loopback default has no token, so layers 2+3 (host allow-list + origin check) are the whole browser-attack defence. Layer 3 currently does not distinguish local origins, which is precisely the neighbour it exists to distinguish: other content reachable in the user's browser on another local port.
  • The collab/world WebSocket routes are state-changing and long-lived; a cross-site WebSocket from a different local origin inherits the user's ambient access to the UI.

Suggested fix

Compare the full authority (host and port) for the Origin check, while keeping the host allow-list check host-only as it is today:

  • Keep host_only() for the allow-list comparison (step 2).
  • For step 3, compare a port-preserving normalised authority (lower-cased host, explicit port when present; IPv6 brackets handled like host_only does) of origin_authority(origin) against the Host header authority.
  • Decide and document the no-port cases: an Origin with no explicit port vs a Host with an explicit port (e.g. behind a proxy / default-port serving) — treat missing port as the scheme default, or refuse; either is defensible, but it must be a deliberate, tested choice, not an accident of stripping.

Acceptance criteria

  • Request with Host: localhost:3000 and Origin: http://localhost:9999 (and the IPv4/[::1] equivalents) is refused with 403.
  • Same-authority origins (same host, same port, any case) still pass; different-host origins still 403; Origin: null still refused.
  • New regression tests cover different-port origin refusal and same-port acceptance; existing security tests pass (cargo test --features web web::security).
  • Module docs updated so the stated Origin guarantee matches the implemented comparison.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions