Skip to content

Add support for application capabilities (grants) - #135

Open
olistrik wants to merge 3 commits into
tailscale:mainfrom
olistrik:tailscale-grants
Open

olistrik wants to merge 3 commits into
tailscale:mainfrom
olistrik:tailscale-grants

Conversation

@olistrik

@olistrik olistrik commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

Resolves #93.

Exposes Tailscale application capabilities (grants) from the peer's CapMap as the {tailscale.grants} placeholder, and as {http.auth.user.tailscale_grants} in the user metadata.

  • In CEL expressions (expression matchers) it is a map, so you can index into it and use in.
  • Anywhere else (e.g. header_up) it renders as a JSON object. This is the same shape as the Tailscale-App-Capabilities header from tailscale serve: each capability name maps to an array of its values.
  • With no grants, it is an empty map in CEL and {} as a string.

Changes

  • auth.go
    • Extracts grants from the WhoIs CapMap and parses each value as JSON (non-JSON values are kept as strings).
    • Adds a Grants map type with a String() method that renders JSON with non-ASCII escaped as \uXXXX, so it is always a valid header value.
    • Registers a replacer for {tailscale.grants} and sets tailscale_grants in the user metadata.
  • cel_test.go: tests for CEL use (indexing, in, deep field access, empty grants), header rendering (including empty, non-JSON and non-ASCII values), and a non-ASCII round trip.
  • README.md: new "Application Capabilities" section with CEL and header examples, and user.tailscale_grants added to the user fields list.
  • .gitignore: ignore *.test and *.test.exe from go test -c.

Differences from tailscale serve

  • {tailscale.grants} contains all of the peer's capabilities. tailscale serve only forwards those in --accept-app-caps.
  • Non-ASCII is JSON-escaped (\uXXXX), not RFC 2047 Q-encoded. The value is plain ASCII and valid JSON, and decoding it as RFC 2047 leaves it unchanged.
    • Why not Q-encode: Caddy placeholders have no encoding hook, so the encoding would have to happen in the placeholder's String(). Q-encoding there would make the value invalid JSON until decoded, and consumers that parse the header as JSON (like a JSONPath extractor) would have to decode it first. The earlier discussion on expose Tailscale App Capabilities (grants) using caddy.Replacer #93 also suggested that header encoding is arguably out of scope for caddy-tailscale and could be left to Caddy.
    • JSON escaping satisfies the same constraint that motivates Q-encoding in tailscale serve (a header value that is always ASCII), while staying valid JSON. A backend written for tailscale serve that Q-decodes and then JSON-parses should work unchanged, since the escaped value has nothing for the Q-decoder to change. That is untested, see below.
  • tailscale serve strips client-sent Tailscale-* headers. Here header_up overwrites the header, but routes that skip tailscale_auth should strip them (e.g. header_up -Tailscale-*). This is documented in the README.

I've been running this on my homelab for a couple of months now, also with both applications that are simply gated with it, and also one that consumes the grants from the forwarded headers and extracts them with JSONPath. I haven't tested it with a backend written for tailscale serve, nor with non-ASCII values in my own grants.

Usage

tailscale_auth

# user has admin: true
@admin expression `{tailscale.grants}["example.com/app"][0].admin == true`
handle @admin {
    respond "Hello, admin {http.auth.user.id}"
}

# user has the capability, value doesn't matter
@user expression `"example.com/app" in {tailscale.grants}`
handle @user {
    respond "Hello, user {http.auth.user.id}"
}

handle {
    respond "Access denied" 401
}

# forward to a backend
reverse_proxy localhost:3000 {
    header_up Tailscale-App-Capabilities {tailscale.grants}
}

Signed-off-by: Oli Strik <strik@olii.nl>
The placeholder returned a bare map[string]any, which caddy.ToString
formats with %+v, so `header_up X {tailscale.grants}` sent Go debug
output such as `map[example.com/app:[map[admin:true]]]`.

Return a named Grants map type that implements fmt.Stringer. CEL still
sees a map, while headers and other string contexts get the same JSON
object that tailscale serve sends in Tailscale-App-Capabilities, or {}
when there are no grants. Non-ASCII characters are escaped as \uXXXX
so the header value is always ASCII and valid JSON.

The tailscale_grants user metadata now uses the same encoding, and the
tests exercise the real replacer func instead of a copy of it.

Signed-off-by: olistrik <strik@olii.nl>
Signed-off-by: olistrik <strik@olii.nl>
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.

expose Tailscale App Capabilities (grants) using caddy.Replacer

1 participant