Repository navigation
Conversation
jhrozek
requested review from
ChrisJBurns,
JAORMX,
amirejaz,
rdimitrov and
tgrunnagle
as code owners
October 22, 2026 10:34
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #6908 +/- ##
==========================================
- Coverage 79.94% 79.91% -0.04%
==========================================
Files 833 833
Lines 84977 85086 +109
==========================================
+ Hits 67938 67999 +61
- Misses 17034 17082 +48
Partials 5 5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
ChrisJBurns
previously approved these changes
Oct 22, 2026
Clients that host sessions for a user, such as Mecatl, need to know
which person at the company identity provider a token belongs to.
Access tokens only carried the auth server's own sub.
Add an RFC 9493 sub_id claim ({"format":"iss_sub"}) with the issuer and
subject of the validated upstream OIDC ID token to access tokens from
authorization-code and device-flow logins and their refreshes.
Disclosure is off by default and enabled per audience. Every entry
must be a member of the allowed audiences, and the claims are only
added when every audience of the token is listed.
The raw upstream sub is not stable everywhere: Microsoft Entra ID
issues a different sub per app registration. Alongside sub_id, add
https://toolhive.dev/upstream_subject = {iss, claim, value}, where
claim is the upstream's subject_claim (or "sub") and value is that
claim's value from the same ID token, the value ToolHive already keys
its local user on. With subject_claim: oid on Entra, it stays the same
across app registrations. Consumers that need a stable key should
compare all three members, not sub_id. claim and value are fixed at
login; the disclosure setting applies at every refresh. Both claims
use one opt-in, and since oid correlates a user across apps in a
tenant, only first-party audiences should be listed.
The setting is reachable from Go only: runner callers pass
runner.WithSubjectIdentifierAudiences, and authserver.New callers set
Config.SubjectIdentifierAudiences. It is deliberately not a RunConfig
field, so it stays out of the published API schema, the SDK, config
files and operator CRDs while the feature settles.
Promoting it to RunConfig later is additive: add an omitempty
subject_identifier_audiences field and its validation to RunConfig,
map it into Config in the runner, regenerate the API docs and SDK, and
fail construction if both the field and the option are set. Deprecate
the option, move Mecatl to the field, then remove the option. Older
binaries ignore the unknown field, so disclosure stays off until they
are upgraded. A CRD field can follow in a separate change.
The verified upstream identity comes only from the first upstream
login that establishes the user, never from client input. Synthetic
and pure OAuth2 identities never get either claim. The identity is
stored with the pending authorization, pending device confirmation,
device request and token session even when disclosure is off, and
every token request clears and recomputes both claims, so policy
changes apply at the next refresh. Token exchange never carries either
claim over from a subject token.
MarkDeviceRequestAuthorized now takes a DeviceAuthorization struct, so
storage decorators that forward it must be updated.
Fixes: #6900
Co-authored-by: Mecatl <[email protected]>
A SingleLeg authorization connects one backend and never establishes the local user, so the upstream it authenticates against must not supply the sub_id provenance carried into issued tokens. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
ChrisJBurns
approved these changes
Oct 22, 2026
This was referenced Oct 26, 2026
1 task
This was referenced Oct 27, 2026
rdimitrov
pushed a commit
that referenced
this pull request
Oct 27, 2026
Since #6908 the auth server stored the verified upstream identity (issuer, subject, stable claim and value) at every login, even when SubjectIdentifierAudiences was empty. Its only reader turns it into the sub_id and upstream_subject claims, and only for listed audiences. With the default empty list, every write was stored data nobody reads, and deployments that keep upstream identity out of storage had to strip it in their own storage layer. Record the identity at login only when SubjectIdentifierAudiences is not empty. Both login paths, the authorization-code callback and the device verification flow, now ask one helper, Handler.recordedUpstreamIdentity, so a future login path cannot forget the check. Nothing downstream changes: a zero value already means "no claim". Behaviour with disclosure on is unchanged. This reverses a choice made in #6908, which stored the identity so that a policy change applied at the next refresh. Now, going from an empty list to a non-empty one adds the claims only to sessions whose login happened while the list was non-empty. Other users get them from their next login. Adding an audience to a list that is already non-empty still applies at the next refresh, and emptying the list still stops the claims at the next refresh. Sessions created on v0.57.0 already hold the stored identity and keep it until their refresh token expires. There is no cleanup step. Update the SubjectIdentifierAudiences docs and the auth server storage architecture page to describe both cases, tell consumers not to switch a user's key based on whether sub_id is present, and note that a rolling restart leaves some logins unrecorded. Fixes #6955 Co-authored-by: Mecatl <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Clients that host sessions for a user, such as Mecatl, need to know which person at the company identity provider an access token belongs to, so they can tie sessions and grants to that person. Until now the embedded auth server's access tokens only carried the auth server's own
sub.This PR adds two claims to access tokens. Both are off by default and enabled per audience:
sub_id, the RFC 9493 identifier:{"format":"iss_sub","iss":...,"sub":...}. It holds the exactissand rawsubof the validated upstream OIDC ID token.https://toolhive.dev/upstream_subject:{"iss":...,"claim":...,"value":...}.claimis the upstream'ssubject_claim(or"sub"), andvalueis that claim from the same ID token, the value ToolHive already keys its local user on. The rawsubis not stable on every IdP: Microsoft Entra ID issues a differentsubper app registration. Withsubject_claim: oidthis claim stays the same across app registrations. Consumers that need a stable key compare all three members.What changed:
authserver.Config.SubjectIdentifierAudiencesforauthserver.Newcallers, andrunner.WithSubjectIdentifierAudiencesfor runner callers. Every entry must be one of the allowed audiences. The claims are added only when every audience of the token is listed.RunConfigfield, so it stays out of the published API schema, the SDK, config files and operator CRDs while the feature settles. The commit message describes how to move it toRunConfiglater without breaking anything.MarkDeviceRequestAuthorizednow takes aDeviceAuthorizationstruct.docs/arch/11-auth-server-storage.mddescribes both claims, the rules for consumers, IdP caveats and upgrade behavior.Fixes #6900
Type of change
Test plan
task test)task test-e2e)task lint-fix)The new tests cover:
subject_claim.Changes
pkg/authserver/config.go,server/audience.goSubjectIdentifierAudiencessetting and its validation against the allowed audiencespkg/authserver/runner/embeddedauthserver.goWithSubjectIdentifierAudiencesrunner optionpkg/authserver/upstream/oidc.go,types.gopkg/authserver/server/handlers/callback.go,device_verification.gopkg/authserver/server/handlers/token.gopkg/authserver/server/session/session.goUpstreamSubjecttype, claim keys, set and clear helpers, clonepkg/authserver/storage/*DeviceAuthorizationstructdocs/arch/11-auth-server-storage.md,docs/redis-storage.mdDoes this introduce a user-facing change?
Only for Go callers that opt in. For
thv, vMCP and operator users nothing changes: the setting is not reachable from config files or CRDs, and it is off by default.sub.SubjectIdentifierAudiences, access tokens issued only to listed audiences also carrysub_idandhttps://toolhive.dev/upstream_subject.Disclosing
oidlets anyone who sees the token match the same user across apps in the tenant, so only first-party audiences should be listed.Go API break:
storage.Storage.MarkDeviceRequestAuthorized(ctx, deviceCode, DeviceAuthorization)replaces the earlier positional arguments. Storage decorators outside this repo that forward the method must be updated. Nothing in this repo forwards it.Implementation plan
The plan covered the second claim,
upstream_subject, which was added after the firstsub_iddraft. This is its design section.Approved implementation plan
Problem
The commit adds an RFC 9493
sub_idclaim to access tokens from the embedded auth server:{"format":"iss_sub","iss":<upstream ID token iss>,"sub":<upstream ID token raw sub>}. It is opt-in peraudience from Go (
runner.WithSubjectIdentifierAudiences,authserver.Config.SubjectIdentifierAudiences).With Microsoft Entra ID as the upstream, the ID token
subis pairwise per (client ID, tenant, user).It changes when ToolHive's upstream app is registered again, and a second Entra app (for example a
gateway's AS-2) sees a different
subfor the same person. Microsoft recommendsoid+tidas thecross-app key. The consumer, Mecatl, keys session ownership on this identity and expects it to be stable,
and its AS-2 consistency check compares identities across two Entra apps.
Decision
Keep
sub_idexactly as it is: a bareiss_subwith the raw upstreamissandsub. Do not switchto RFC 9493
aliases(it would changesub_id's format depending on config and break consumers thatcheck
format == "iss_sub"), and do not putoidin thesubmember (it is not the issuer'ssub).Add one claim with a collision-resistant (public) claim name (RFC 7519 §4.2; same
https://toolhive.dev/prefix as the existing
NoUpstreamSessionClaimKey), emitted together withsub_idunder the same opt-in:issis the same validated upstream issuer as insub_id.claimis the upstream's existingsubject_claimsetting, or"sub"when it is unset.valueis that claim's value from the validated ID token. This is exactly the value ToolHive alreadyresolves to key its own local user (
OIDCProviderImpl.resolveSubject), so no new config is needed.claimis"sub"and the value duplicatessub_id.sub(Okta, Google, Keycloak). Consumers always key on one claim, and the key does notsilently change shape when an operator sets
subject_claimlater.iss: the upstream issuer check is strict (noSkipIssuerCheck), so an Entraupstream must use a tenant-specific issuer, and
isspinstidand the endpoint version. Notidmember. A v1/v2 endpoint switch changes
issand therefore the identifier; this is documented.Policy: no new setting. The claim is cleared and recomputed with
sub_idon every token request andonly appears when
sub_iddoes. Disclosingoidis a stronger cross-app correlator than a pairwisesub; the docs say so.Storage: replace the two provenance string fields (
VerifiedUpstreamIssuer,VerifiedUpstreamSubject)on every type with one value-type struct that also stores the stable claim name and value. No released
version has the old fields, so changing the shape now is free. Adding members later would make replicas
running this version drop them on every rewrite and force re-logins.
Capture rules are unchanged: only from a validated upstream OIDC ID token, only on the callback that
establishes the local user (first leg), never from userInfo, OAuth2
identityFromToken, refreshresponses, or client input. Synthetic and pure OAuth2 identities get neither claim.
(claim, value) is fixed at login. A later
subject_claimconfig change applies at the user's nextlogin (it also re-keys ToolHive's own
sub; the next upstream refresh under the new config alreadyfails with
ErrSubjectMismatch), never at refresh. Recomputingclaimat refresh would pair a newclaim name with an old value. The disclosure policy, by contrast, applies at the next refresh.
The value is only a safe ownership key when the claim is an immutable, never-reassigned,
IdP-controlled identifier (
sub; Entraoid).email,preferred_username,upnandunique_namecan be reassigned to another person (Entra
emailis not even verified). This is not new risk insideToolHive (the same value already keys the local user) but consumers will treat an exported key as
authoritative, so the docs tell them to allowlist (iss, claim) pairs and fail closed. No code change
restricts
subject_claimin this PR.Consumers compare the full (iss, claim, value) as exact strings, never
valuealone.issmust beidentical on both sides: Entra v1 vs v2, Okta org vs custom authorization server, custom URL domains and
B2C
tfpissuers all give a differentissfor the same person. Multi-tenant Entra authorities(
common,organizations) cannot be used as an upstream at all, because the issuer check is strict.Storage member names (
stable_claim,stable_value) differ from the emitted ones (claim,value) onpurpose: the stored object also holds the raw
sub, sovaluealone would be ambiguous there.Why a value-type struct and
omitzeroAll four members are strings, so a value type copies safely with plain assignment (no pointer aliasing in
memory storage clones, no nil checks).
go.modisgo 1.27, soencoding/jsonsupports theomitzerotag option, which omits a zero struct.
Special notes for reviewers
sub_idalready present in a stored refresh session again without checking the disclosure setting. The safe order is to roll out with disclosure off and enable it once no older replica is left. To roll back, turn disclosure off first. Details are in Storage and upgrades.sub_id.subis the raw ID-tokensubeven whensubject_claimmaps the local user to another claim. RFC 9493iss_subrequires the issuer's ownsub. The stable key is the job ofupstream_subject.