Skip to content

Pairing cleanup after #7: Serve-mode warning, replay test, subtle, per-client challenge limit - #8

Merged
VirtualMachinist merged 3 commits into
mainfrom
fix/pendant-pairing-nits
Nov 14, 2026
Merged

VirtualMachinist merged 3 commits into
mainfrom
fix/pendant-pairing-nits

Conversation

@VirtualMachinist

@VirtualMachinist VirtualMachinist commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Follow-up to #7 at 72fcdd4. Four items from the security re-review of that head plus one QA gap. Two commits, plus a merge of main e092c2f (app Lapis pin bump). No schema change.

Rows

C1 — Serve mode warning (README). Adds a sentence next to the existing Serve paragraph: only Serve's HTTP-proxy mode is supported (tailscale serve --bg --https=8443 http://127.0.0.1:8780); a raw TCP forward, a TLS-terminated TCP forward, or any proxy that does not add X-Forwarded-For defeats the local-only gate.

C2 — challenge replay test. a_challenge_replay_is_refused_and_leaves_state_alone: a paired device opens a grant with a challenge, then sends the identical request again. The second use is 403 bad_signature, and a signed write on the grant opened by the first use still returns 200, so the refusal released no grant and dropped no device.

C3 — constant-time compare. The hand-rolled ct_eq is gone. take_token and take_ticket now compare with subtle::ConstantTimeEq and still scan every entry; only the length leaks, as before. subtle was already in Cargo.lock transitively through p256, so the direct dependency adds no crate. cargo deny check: advisories, bans, licenses and sources all ok.

C4 — per-client challenge rate limit (done, not deferred). The rate bucket is keyed per client instead of one global bucket, so a flood from one caller cannot delay another caller's challenge. Key order: the right-most element of X-Forwarded-For, trimmed, with any host:port port dropped. The proxy appends the trusted hop and strips client-supplied forwarding headers first, so everything to the left of that element is caller-written and is now ignored. Next, Tailscale-User-Login / -Name / -Profile / -Id, lower-cased before the 64-character truncation, so case variants of one identity share a bucket. Finally local for a direct caller that sends no forwarding header. 429 and its body are unchanged.
Tests, all at a limit of 2:

  • challenge_rate_is_per_client_not_global — one address on three ports gets 200, 200, 429; a second address and an identity each still get 200 in the same window.
  • challenge_rate_ignores_a_rotated_leading_forwarded_element — a caller that rotates the leading element of a two-hop header is still refused with 429. This test fails on the previous left-most keying (200 where 429 is required).
  • challenge_rate_folds_identity_case — two case variants of one identity fill one bucket and the third is 429.

What a full map does. recent holds at most 1024 keys, each with at most the rate window's timestamps (32 by default); expired buckets are pruned on every issue. When the map is full, a key that is not already present is refused with the same 429 until buckets age out of the window, while keys already present keep working. So a caller that can mint distinct keys can keep a new client out for as long as it keeps the map full, but memory stays bounded and no existing bucket is taken from its client. Reaching that state through the proxy needs distinct trusted identities; a direct loopback caller is the host operator. A request carrying only a bare Forwarded header, with no X-Forwarded-For and no identity, keys as local, so all such callers share one bucket and are refused together rather than bypassing the limit.

C4 stayed small (about 75 implementation lines, no new crate), so it is in this PR.

Also in this diff

  • rustix 1.1.5 → 1.1.6 in Cargo.lock. A newly published advisory (RUSTSEC-2026-0486, unsound xattr buffer handling) fails the cargo-deny job on the base lock. The crate arrives through tempfile as a dev-dependency; no runtime path changes.

Verification

Run on the build host, workspace at this head:

  • cargo check --workspace --all-targets — ok.
  • cargo test --workspace — ok (39 bridge tests, including the four pairing-rate and replay tests; cargo test -p pendant-bridge also run alone).
  • Mutation check: restoring the left-most X-Forwarded-For element in a scratch copy makes challenge_rate_ignores_a_rotated_leading_forwarded_element fail (200 where 429 is required), so the test pins the new keying.
  • cargo deny check — advisories ok, bans ok, licenses ok, sources ok.
  • rustfmt --edition 2021 --check on both touched source files — clean.
  • After merging main e092c2f: cargo check --workspace --all-targets and cargo test --workspace ok; cargo check --manifest-path app/Cargo.toml --all-targets --locked and cargo test --manifest-path app/Cargo.toml --locked ok (12 passed, 1 ignored, 3 doctests); cargo deny check and cargo deny --manifest-path app/Cargo.toml check both ok. app/ matches main; the merge added only that pin bump.

Follow-up proposal: defence in depth for the local-only gate

The gate reads the presence of a forwarding header, which is correct for the documented Serve mode and is defeated by any other proxy (now warned about in the README). Two options, either of which removes reliance on the header:

  1. Host-only secret. Generate a random secret per install, store it owner-only beside the pairings state, and require it on GET /v1/pair/qr, the signed revoke and the host revoke. Serve would inject it as a header through a proxy rule; the bridge compares it constant-time. Cost: one config knob and a Serve rule; no wire change for the phone that fetches pair/qr because Serve adds it.
  2. A loopback listener Serve does not target. Keep the current 127.0.0.1:8780 as the proxied endpoint and require a token for it, or move the pairing routes to a second loopback port that Serve never forwards to, so only a local process can reach them. Cost: a second listen address in config and a second bind in the accept loop; the PWA gets no phone-facing change.

Option 2 is more robust (no header trust at all) and option 1 is smaller. Neither is in this PR; it is a design change and needs your call.

CI on this head

Run 43559838582 at 146df9b: check pass, banned-names pass, cargo-deny pass. The earlier red step, cargo deny check (app), is cleared by the merge of main e092c2f, which moves the app's pinned Lapis rev to v0.5.0 (libsqlite3-sys 0.40.0) and drops RUSTSEC-2026-0459.

Out of scope, untouched

app/src/notes/store.rs, app/src/platform/keystore.rs, port 443, the Serve config, serve-apply.sh, the bridge wire schema, and ci.yml.

- README: only Serve's HTTP-proxy mode satisfies the local-only gate; a raw
  or TLS-terminated TCP forward, or any proxy without X-Forwarded-For,
  defeats it.
- Test a challenge replay: the second use is refused and no grant or pairing
  state changes.
- Replace the hand-rolled constant-time compare with subtle::ConstantTimeEq;
  take_token and take_ticket still scan every entry.
- Key the challenge rate limit on the proxied client (X-Forwarded-For, then
  the Tailscale identity headers) instead of one global bucket; the 429 is
  unchanged.
- Bump rustix 1.1.5 to 1.1.6 for RUSTSEC-2026-0486 so cargo deny passes.
The caller writes everything to the left of the hop the proxy appends, so the
right-most X-Forwarded-For element is the only trusted one; key on it, trimmed
and with any port dropped. Earlier elements no longer mint a new bucket.

Fold the Tailscale identity headers to lower case before truncating, so case
variants of one identity share a bucket.

Tests: a rate-2 caller rotating the leading element of a two-hop header still
gets 429, and two case variants of one identity share a bucket. The rotation
test fails on the previous keying.
@VirtualMachinist
VirtualMachinist marked this pull request as ready for review November 14, 2026 05:13
@VirtualMachinist
VirtualMachinist merged commit 34a4fa3 into main Nov 14, 2026
3 checks passed
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.

1 participant