Commit graph

15 commits

Author SHA1 Message Date
João "Pisco" Fernandes
67d0c250d7 TUN-10798: Harden protected Quick Tunnel CLI behavior
Redact recipient rules, improve protected tunnel startup output, and reject --allowed-mail for named tunnels.
Use state-specific cookies so concurrent login flows remain independent, with callbacks selecting and clearing only the matching state cookie.
2026-09-24 14:46:10 +01:00
Miguel da Costa Martins Marcelino
a20f569675 TUN-10926: Broker JWKS cache expiration
Adds an expiration for cached JWKS. Expired caches refresh even when kid still matches. All retired keys are rejected after expiration.
2026-09-24 14:44:13 +01:00
Miguel da Costa Martins Marcelino
cdcd702aa4 chore: Add upper bounds to supplied emails
This prevents users from abusing `--allowed-mail` policies by limiting the nr. of rules to 100 and defining a max per rule of 320 bytes. Validation errors now identify only the rule position and category, preventing recipient values from appearing in logs. Parsing also stops once limits are exceeded while preserving normalisation and deduplication.
2026-09-23 15:59:32 +00:00
Miguel da Costa Martins Marcelino
0b37a912a2 TUN-10805: Strip authentication data before forwarding requests
To properly secure authorized requests, we need to strip any process-local session and authentication-state cookies before forwarding them. On protected responses, we also enforce `Cache-Control: private, no-store` and `Referrer-Policy: no-referrer` directives, to prevent sensitive data from being cached locally or by intermediaries and to block referrer URLs from leaking across requests.

Response headers and cookies for Public Quick Tunnel are preserved.
2026-09-22 15:13:07 +00:00
Alessandro Frigerio
9c49bb94af TUN-10804: Enforce authentication before origin access
This adds a shared authorization boundary for protected Quick Tunnels before ingress selection. Unauthenticated requests are redirected or rejected, while requests with a valid local session continue to the origin after the internal authentication cookie is removed.

Before these changes, we could already login and validate sessions, but the request handler could not allow authenticated requests to continue to the origin. This change adds that authorization decision.

Public Quick Tunnels remain unchanged.
2026-09-21 16:54:54 +01:00
Alessandro Frigerio
3e1febbda1 TUN-10803: Authorize Quick Tunnel callbacks
Validates callback-bound broker assertions, enforces recipient rules, and issues local sessions before redirecting with HTTP 303. Callback replay is bounded by the short-lived signed artifacts.
2026-09-21 12:28:07 +00:00
Alessandro Frigerio
05d053acec TUN-10803: Add Quick Tunnel recipient policy
## Summary

Adds a process-local recipient policy for normalized exact-email and wildcard-domain rules.
2026-09-21 11:47:36 +01:00
Alessandro Frigerio
be3ac12702 TUN-10802: Manage process-local authentication sessions
## Summary

- generate a cryptographically random process-local HMAC-SHA256 session signing key
- issue host-only, Secure, HttpOnly, SameSite=Lax Quick Tunnel session cookies
- cap sessions at the earlier of 4 hours or the Access identity expiry
- validate canonical cookie encoding, signatures, nonces, and expiry
- invalidate sessions across cloudflared restarts through process-local signing keys
- use `github.com/cloudflare/backoff` for tunnel retry jitter while preserving retry limits, retry-forever caps, and grace-period resets

## Security

- session payloads contain only a random nonce and expiry; no visitor identity or recipient rules
- malformed, tampered, expired, and previous-process sessions fail closed
- HMAC signatures are compared in constant time

## Validation

- `make test lint`
- `go test -race ./retry ./quicktunnelauth`
2026-09-17 17:08:56 +00:00
Miguel da Costa Martins Marcelino
4f80fc1c23 Revert "chore: Fix unbounded channel"
This reverts merge request !1952

Reverting this change. This was an unbuffered channel before, which does not mean "unbounded". The channel was effectively blocking until a receiver read the value.
2026-09-17 15:15:52 +00:00
Miguel da Costa Martins Marcelino
8985ebd469 chore: Fix unbounded channel
For the refresh JWKS operation, a worker fiber was reading from a notification channel that had no buffering.

This has two issues. The first issue has to do with memory growth, but the second is more subtle. When we don't supply any bounds, the code needs to be capable to handle concurrent requests and the worker must be able to dedup notifications. While this is what happens now, a future iteration might introduce a bugthat removes the dedup logic.
2026-09-17 08:21:12 +00:00
Alessandro Frigerio
a1cb032df2 TUN-10801: Validate quick tunnel broker assertions
## Summary

Validates callback-bound Quick Tunnel broker assertions using the verification keys cached in !1937. The validator accepts only ES256 JWTs from the fixed broker issuer and audience, verifies signatures before claims, enforces the two-minute handoff lifetime and Access identity expiry, binds the callback hostname and browser state, validates and normalizes the verified email, and returns a trusted identity for recipient authorization.

The temporary hostname validator mirrors TUN-10800 and is documented for consolidation after that stack merges.

## Stack

- Depends on: !1937
- Part 2/2: this MR

## Validation

- `make test lint`
- focused `go test -race ./connection` coverage for JWT headers, signatures, claims, and identity binding
2026-09-16 18:30:13 +00:00
Alessandro Frigerio
156742e3ac TUN-10801: Cache quick tunnel broker JWKS
## Summary

Adds the process-local broker JWKS cache used by protected Quick Tunnel assertion validation. Fetches keys only from the fixed broker endpoint with a five-second timeout, refuses redirects, bounds and validates responses, accepts only public P-256 ES256 signing keys, reuses cached keys, and refreshes once for an unknown key ID. Concurrent cache misses and refreshes are deduplicated.

## Stack

- Part 1/2: this MR
- Followed by: `afrigerio/TUN-10801-assertion-validation`

## Validation

- `make test lint`
- focused `go test -race ./connection` coverage for JWKS caching and refresh
2026-09-16 17:26:55 +00:00
Alessandro Frigerio
5539e4590c TUN-10800: Wire Quick Tunnel authentication redirects
Wires protected Quick Tunnel authentication into the request path so unauthenticated requests are intercepted before ingress selection. Broker callbacks are handled locally and fail closed
2026-09-16 16:30:46 +01:00
Alessandro Frigerio
eeac161daa TUN-10800: Consume authentication callback state
This adds callback handling for the browser-bound Quick Tunnel authentication state, consuming the POST to /.cloudflared/qt-auth/callback that the broker sends after login. The handler accepts a bounded application/x-www-form-urlencoded body containing exactly one state and one assertion, verifies the HMAC-signed state cookie, and binds it to the submitted state. 

It fails closed and clears the state cookie once consumption succeeds. On success, the handler returns the broker assertion and the validated return path for later use.
2026-09-15 13:52:36 +00:00
Alessandro Frigerio
5cf510d7cc TUN-10800: Add browser-bound login state
This adds QuickTunnelAuthStateManager, which issues browser-bound authentication states for protected Quick Tunnels. Each login generates a random state and a signed, host-only cookie that binds the browser to that state. The cookie is Secure, scoped to the callback path, valid for 10 minutes, and uses SameSite=None because the broker's callback is a cross-site POST from login.trycloudflare.com back to .trycloudflare.com. 

The design is stateless: the cookie is signed with a process-local HMAC key rather than tracked server-side, so there's nothing to store or clean up, and state values are verified rather than looked up. Return-path validation rejects unsafe schemes, hosts, and paths to prevent open redirects.
2026-09-15 09:29:48 +00:00