Commit Graph

810 Commits

Author SHA1 Message Date
Kristoffer Dalby 6a0f67d56e state: resolve changed peers through adjacency
ListPeers now filters named peers against the recipient's adjacency, so
a node the policy hides is never delivered.

Updates #3417

(cherry picked from commit 4c6a2dff52)
2026-09-23 21:19:22 +02:00
Kristoffer Dalby 237dc74e73 state: reuse peer adjacency for payload-only writes
A write that cannot move visibility carries the previous adjacency
forward; policy and user changes rebuild it explicitly.

Updates #3417

(cherry picked from commit e3c4c81b18)
2026-09-23 21:19:22 +02:00
Kristoffer Dalby 938c2bd753 policy,state: key the peer map by node ID
Adjacency becomes immutable, so a snapshot can resolve peers through its
own fresh views instead of storing them.

Updates #3417

(cherry picked from commit 95ba787417)
2026-09-23 21:19:22 +02:00
Kristoffer Dalby 9fbf7b9b60 mapper: take peer visibility from the peer map only
Fixes #3408

0.29's BuildPeerMap returns node views, not node IDs, so the peer map
assertion compares IDs read off the views.

(cherry picked from commit e48bc46cc6)
2026-09-23 21:19:22 +02:00
Kristoffer Dalby 5aded15bf2 policy/v2: suggest approved exit nodes by default
Matches SaaS; Apple clients hide the exit-node list without a suggestion.

Fixes #3415

0.29's tailscale has no tailcfg/nodecap package, so the constant keeps its
tailcfg name here and in the test.

(cherry picked from commit c604874dba)
2026-09-23 21:19:22 +02:00
Kristoffer Dalby c7d9d03500 hscontrol: replace tailscale line refs with doc links
Line numbers drift on every upstream bump.

0.29's tailscale has no tailcfg/nodecap package, so the constants keep their
tailcfg names here.

(cherry picked from commit 06fa3075da)
2026-09-23 21:19:22 +02:00
Kristoffer Dalby 694bafee0a policy/v2: compare peer CapMap against SaaS route captures
SaaS stamps suggest-exit-node on approved exit peers without nodeAttrs.

Updates #3415

(cherry picked from commit 0e8a3bba54)
2026-09-23 21:19:22 +02:00
Kristoffer Dalby 8490631f06 policy/v2: add via exit node capture
Updates #3408

(cherry picked from commit 5861005ef6)
2026-09-23 21:19:22 +02:00
Kristoffer Dalby a7f7e6a401 servertest: compare user-owned nodes in via compat tests
Updates #3408

(cherry picked from commit 23f7eedac6)
2026-09-23 21:19:22 +02:00
Andrei Korviakov ebe18cebff hscontrol: keep the ACME error body readable in acmeLogger
acmeLogger drained and closed the body of every ACME error response before
handing the response back to golang.org/x/crypto/acme. The client parses that
body to classify errors, so badNonce was no longer recognised: isBadNonce
returned false, clearNonces was never called and Client.post treated the 400 as
non-retriable.

One badNonce reply therefore stops certificate renewal for good. autocert
retries every 30-60 minutes, each attempt reuses a nonce stored from the
previous failed response, that nonce has already expired, and the loop repeats
until the process is restarted or the certificate expires.

Restore the body with a fresh reader after logging it.

(cherry picked from commit 7472e98f6c)
2026-09-23 21:19:22 +02:00
Kristoffer Dalby aa4c952bca policy: ignore '#' metadata fields across the whole policy
The filter lived in ACL.UnmarshalJSON, so grants, ssh and nodeAttrs still
hit RejectUnknownMembers. Strip the members in the HuJSON AST instead, at
the single decode entrypoint. Grant "app" payloads are left untouched.

Fixes #3479

(cherry picked from commit 5401edb6a8)
2026-09-23 21:19:22 +02:00
Kristoffer Dalby e6c0a8adee types: regenerate node view
Updates #3470

(cherry picked from commit 804954f5bc)
2026-09-23 21:19:22 +02:00
Kristoffer Dalby a91f708adc state: mark expired nodes offline without ending the session
Online now requires a live session and an unexpired key, derived in one
place by Node.ShouldBeOnline so every writer agrees what online means.

Fixes #3470

Connect keeps 0.29's NodeOnlineFor peer patch; only the not-online branch
is new here. The updateChanges hunk is dropped: 0.29 has no caller for it.

(cherry picked from commit 80c863b15a)
2026-09-23 21:19:22 +02:00
Kristoffer Dalby 8f7ee375e9 state: skip node health writes that change nothing
An all-unchanged probe cycle no longer publishes a snapshot.

Updates #3417

(cherry picked from commit 1b820b7ebe)
2026-09-23 21:19:22 +02:00
Kristoffer Dalby b972da3fe9 state: stop broadcasting a whole peer on disconnect
Going offline changes nothing the policy reads, so the row write skips
the policy refresh and peers get only the offline patch.

Updates #3417

(cherry picked from commit dfe0d3f2e5)
2026-09-23 21:19:22 +02:00
Kristoffer Dalby 8973347d22 state: classify map requests before broadcasting
Each request is reduced to the narrowest change it justifies, so a
keepalive or endpoint bump no longer resends the whole node to peers.

Updates #3417

0.29 pins an older tailscale where Hostinfo.NetInfo.PreferredDERP is an int,
so the DERPRegionID types are int here.

(cherry picked from commit 59f3ff12a7)
2026-09-23 21:19:22 +02:00
Kristoffer Dalby b8da1c2d6f mapper: drop empty changes before fan-out
An empty change carries no work for any recipient, so it never becomes
a pending entry. Adds headscale_mapper_changes_dropped_total.

Updates #3417

(cherry picked from commit 286f1d5a12)
2026-09-23 21:19:22 +02:00
Kristoffer Dalby 4411264ef9 types: detect policy change on user identity and exit routes
Updates #3417

(cherry picked from commit 905ab92fe0)
2026-09-23 21:19:22 +02:00
Kristoffer Dalby bc5b9d56b2 policy,types: skip recompile when the user list is unchanged
SetUsers now also reports whether user-derived peer adjacency moved.

Updates #3417

(cherry picked from commit be322e8ea7)
2026-09-23 21:19:22 +02:00
Kristoffer Dalby fdb782daa5 state: rename persist helpers to say what they do
Updates #3417

(cherry picked from commit 1bbe59b98d)
2026-09-23 21:19:22 +02:00
Kristoffer Dalby cbb9357d1c oidc: harden reloadable confirmation flow
Updates #3365

(cherry picked from commit 10dd38fcef)
2026-09-23 21:19:22 +02:00
Sean Reifschneider 2da47a77d6 oidc: serve the registration confirmation page from a reloadable URL
The interstitial was the body of /oidc/callback, the URL carrying the
single-use code, so any reload re-entered the spent exchange. Redirect to
GET /register/confirm/{auth_id}, also missing from the route table.

0.29 lacks the authPathURL helper from main, so it is added here.

(cherry picked from commit 6d377b5348)
2026-09-23 21:19:22 +02:00
Kristoffer Dalby 2b28f5756b oidc: harden callback CSRF cookies, state reuse, and issuer config
Defence-in-depth fixes to the OIDC login flow.

Set the state/nonce cookie Secure flag from the configured server_url
scheme rather than req.TLS, so the cookies stay Secure behind a
TLS-terminating reverse proxy where the proxy-to-Headscale hop is plain
HTTP. Deriving it from config avoids trusting a spoofable
X-Forwarded-Proto header.

Make the OIDC state single-use: consume it from the cache on the
callback and clear the state/nonce cookies once validated, so a replayed
callback cannot resolve the same session and the cookies do not linger
until expiry.

Bound OIDC discovery to the caller's context so a slow or unreachable
issuer fails startup within the timeout instead of hanging, and validate
the issuer URL and required client_id/client_secret at config load so an
unworkable setup fails fast.

(cherry picked from commit 622e08f5e6)
2026-09-23 21:19:22 +02:00
Kristoffer Dalby d388244025 noise: make deleted-node expiry clock independent
Updates #3410

(cherry picked from commit afb3020ef0)
2026-09-23 21:19:22 +02:00
Kristoffer Dalby 974becf658 change, mapper: distinguish deleted nodes
Updates #3410

(cherry picked from commit a91c0519c2)
2026-09-23 21:19:22 +02:00
Kristoffer Dalby 0fca2bb646 state: preserve committed node deletion changes
Updates #3410

(cherry picked from commit fc6a16fdfc)
2026-09-23 21:19:22 +02:00
Kristoffer Dalby 83eff4000a poll: interrupt blocked map writes
Updates #3410

(cherry picked from commit ef456542ce)
2026-09-23 21:19:22 +02:00
Kristoffer Dalby fb197db163 poll, noise: tell a deleted node to re-authenticate
A bare 404 is indistinguishable from any other map-path error to a Tailscale
client: it retries forever, still logged in. Only a self node with a past
KeyExpiry reaches NeedsLogin. Also skip the reconnect grace wait, which a
deleted node can never satisfy.

Fixes #3410

(cherry picked from commit b1fb6ed2e6)
2026-09-23 21:19:22 +02:00
Kristoffer Dalby 2b6cb3a2cc types/change: drop unused VisibilityChange
It fills PeersRemoved without deleting anything. The batcher now tears down
the session behind every removed id, so a caller would kill a live poll.

Updates #3410

(cherry picked from commit 42bf00523a)
2026-09-23 21:19:22 +02:00
Kristoffer Dalby 397149e9be mapper: stop a deleted node's map session
Dropping the batcher entry left serveLongPoll streaming to a node that no
longer exists: Close ranges b.nodes and can no longer reach it, so shutdown
blocks and the client keeps polling instead of re-authenticating.

Updates #3410

(cherry picked from commit 0b69e844f5)
2026-09-23 21:19:22 +02:00
Saleh b4f991b381 types: lowercase DNS extra record names
DNS names are case-insensitive, but clients match extra records against
the lowercased query name, so records with mixed-case names (for example
"Printer.fritz.box" in an extra_records_path file) never resolved and
queries fell through to the global nameserver.

Normalize record names to lowercase where the records enter the tailcfg
DNS config, covering both dns.extra_records and extra_records_path.

Fixes #2782

(cherry picked from commit 95ba1f0566)
2026-09-23 21:19:22 +02:00
Kristoffer Dalby 2b8dbea412 mapper: assert no map response lists the recipient as its own peer
Covers every change shape under four policy shapes, plus connect churn. The
zero-matcher shape is the gap: buildTailPeers skips ReduceNodes there, so the
peer lookup is the only self filter left.

(cherry picked from commit 8995d8a558)
2026-09-23 21:19:22 +02:00
Kristoffer Dalby d4b073e830 state: exclude self from peers on the named peer-ID path
ListPeers with explicit IDs filtered every node, not every peer, so a change
batch naming the recipient returned it as its own peer. db.ListPeers keeps
this out with `id <> nodeID`; the NodeStore rewrite dropped it.

(cherry picked from commit d9aebf472d)
2026-09-23 21:19:22 +02:00
Sebastien Tardif 46a80ea8f9 dns: cancel extra-records retry on shutdown and close watcher on setup error
After Remove/Rename, the extra-records filewatcher retried with
context.Background and the default 15-minute backoff budget, so Close
could not stop Run. Cancel that retry when closeCh closes. If the file
is still missing after the budget, watch the parent directory so a later
recreate is seen.

Close the fsnotify watcher on NewExtraRecordsManager error paths after
NewWatcher succeeds.

Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>

(cherry picked from commit a48a42baf4)
2026-09-23 21:19:22 +02:00
Igor Serganov fc894aa9da linting issue fix
(cherry picked from commit c26b2fd0a4)
2026-09-23 21:19:22 +02:00
Igor Serganov 032470a5d3 hscontrol: cancel tailsql on graceful shutdown
Serve stored a Background context for tailsql and called
context.Done() during signal shutdown. Done only returns the
done channel and does not cancel, so tailsql never stopped
when Headscale shut down.

Create a cancellable child of the Serve context, run
`runTailSQLService` in the listener errgroup so its error is
surfaced, and call the cancel func on shutdown - tailsql
unblocks on ctx.Done.

(cherry picked from commit a9f80d8802)
2026-09-23 21:19:22 +02:00
Paulo Luna 33db8c6b29 fix(metrics): collect metrics for non-OPTIONS requests
Fix the inverted HTTP metrics skip condition, which caused the collector to ignore all requests except OPTIONS.

Apply the correction to both the main and Noise routers so regular HTTP traffic is included in http_requests_total and http_request_duration_seconds.

(cherry picked from commit 63ce8f2295)
2026-09-23 21:19:22 +02:00
Igor Bernstein e3dba1fdf9 templates: apply the tvOS setup reorder to the served /apple page
The instructions are duplicated between the docs and the HTML page
headscale serves at /apple. Reorder the tvOS steps there to match, so
the VPN configuration is installed before the alternate coordination
server URL is set.

(cherry picked from commit efe947507b)
2026-09-23 21:19:22 +02:00
Kristoffer Dalby bdc3e996de hscontrol: prefer completed auth over expired ctx in followup wait
waitForFollowup selected on ctx.Done() and the verdict channel with equal
priority; when both were ready, select picked at random and discarded a
successful registration as a spurious 401 timeout. Check for a completed
verdict first, race the deadline only if none is ready.

Fixes #3385

(cherry picked from commit d28a6b111a)
2026-07-29 14:35:31 +02:00
Igor Serganov 5fb514e6e0 poll: do not cancel ephemeral GC until Connect succeeds
With node.ephemeral.inactivity_timeout set, ephemeral nodes are
usually deleted after they go offline, but under reconnect churn some
departed nodes stayed in the node list as disconnected indefinitely
until removed manually or until Headscale restarted.

Ephemeral cleanup is timer-based via EphemeralGarbageCollector, not a
periodic LastSeen scan. serveLongPoll cancelled any pending GC timer
at the very start of a long-poll attempt and only rescheduled on a
clean disconnect after Connect. If a reconnect cancelled the timer and
then failed before Connect (for example an UpdateNodeFromMapRequest
error), the deferred cleanup saw connectGen == 0 and returned without
Schedule. The node remained offline with no deletion timer and no
reconciler to recover it.

Cancel the ephemeral GC timer only after a successful Connect, so a
failed reconnect leaves an already-armed inactivity timer intact.
Successful reconnects still cancel GC once the node is online, and a
later disconnect reschedules as before.

Add TestFailedReconnectDoesNotCancelEphemeralGC to lock in the
ordering, plus IsScheduled and DeleteNodeFromStoreForTest helpers for
the test.

Fixes #3382

Co-authored-by: Cursor <cursoragent@cursor.com>
(cherry picked from commit cfd845cb53)
2026-07-29 14:35:31 +02:00
Kristoffer Dalby 4a1e77359d policy,state: authorize reauth tags against the authenticating user
Re-authenticating a tagged node with --advertise-tags checked the tag-owned
node, not the authenticating user, so every tag was rejected.

Fixes #3374

(cherry picked from commit 6275e3a356)
2026-07-29 14:35:31 +02:00
Kristoffer Dalby 1fccdb18bd state: apply a new pre-auth key's tags on re-registration
Re-registering a tagged node with a different key discarded the new key's
tags and left a stale auth-key reference; retag on key change and persist it.

Fixes #3370

(cherry picked from commit fc16cc6905)
2026-07-29 14:35:31 +02:00
Kristoffer Dalby d202883200 state: do not expire tagged nodes on logout
Tagged nodes never expire, but handleLogout stamped a past expiry on them,
leaving them stuck expired and unable to re-authenticate.

Fixes #3371

(cherry picked from commit 1ed5693fa4)
2026-07-29 14:35:31 +02:00
Kristoffer Dalby 9609a0b87d hscontrol: gate /key on supported capability version
/key handed out the Noise public key for any v>=39, a floor unrelated
to the handshake's capver.MinSupportedCapabilityVersion. Reject below
the supported floor, matching /ts2021, and drop the stale constant.

Fixes #3380

(cherry picked from commit 5b6e1e17be)
2026-07-29 14:35:31 +02:00
Arpit Jain fba84ca232 auth: check machine key on the followup registration path
waitForFollowup returns nodeToRegisterResponse for a completed
registration without checking that the Noise session polling for the
result was started with the machine key that opened the registration.
That response carries the registering user's User and Login, so the auth
ID in the followup URL is the only thing protecting it.

handleRegister and handleLogout both call machineKeyMismatch before
handing back a node, so this is the one path of the three that does not.
The key is already available: HandleNodeFromAuthPath resolves the node
from the MachineKey cached in RegistrationData, so on the normal path the
node and the session agree and the check is a no-op.

The auth ID is 96 bits of randomness and is not guessable, so this is not
reachable by brute force. It is logged at info level when a registration
is created, which makes log access the realistic way to obtain one.

The existing followup_registration_success case built its node with
CreateNodeForTest, which picks a random machine key that no real
registration would produce. Set the registering machine key so the
fixture matches the production path.

Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
(cherry picked from commit 0ce3356b89)
2026-07-29 14:35:31 +02:00
Kristoffer Dalby 1ec7b7fb72 hscontrol: register /ts2021 for WebSocket GET
The chi migration dropped GET; JS/WASM control clients open /ts2021 as a WebSocket GET and were rejected with 405 before reaching NoiseUpgradeHandler.

Fixes #3357

(cherry picked from commit fc6f216b61)
2026-07-01 14:33:03 +00:00
Kristoffer Dalby fd154fdb66 state: log nodes with map-breaking data at startup
Scan a node-health check registry at boot and log each node whose name
can't form a valid FQDN, with the rename fix. Log-only, no mutation.

Updates #3346

(cherry picked from commit 4946d1c88d)
2026-07-01 14:33:03 +00:00
Kristoffer Dalby 9c9206a92b state: reject renames whose FQDN exceeds the hostname limit
A valid label can still overflow 255 chars under a long base_domain; gate
RenameNode with the new types.ValidateGivenName.

Updates #3346

(cherry picked from commit c497612c99)
2026-07-01 14:33:03 +00:00
Kristoffer Dalby 5fb76eb231 poll: return an HTTP error on long-poll setup failure
A bare return sent an empty 200 the client read as "unexpected EOF" and
retried forever; emit a real error instead.

Updates #3346

(cherry picked from commit 4e4512c4b7)
2026-07-01 14:33:03 +00:00
Kristoffer Dalby ec67197368 mapper: skip peers with invalid names instead of failing the map
A peer whose GivenName fails GetFQDN aborted the whole map for every node
that could see it. Drop and log it; SSH policy errors degrade too.

Fixes #3346

(cherry picked from commit 08956d51a4)
2026-07-01 14:33:03 +00:00