Skip to content

Isolate capability cache by identity binding - #7350

Merged
jhrozek merged 1 commit into
stacklok:mainfrom
jhrozek:credential-read-1-cache-isolation
Nov 27, 2026
Merged

jhrozek merged 1 commit into
stacklok:mainfrom
jhrozek:credential-read-1-cache-isolation

Conversation

@jhrozek

@jhrozek jhrozek commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Summary

The vMCP capability cache and its shared in-flight discovery sweep were keyed on the caller's Subject string alone. Two callers with the same subject but different issuers, or an interactive caller and a delegated one (different client or actor), got the same key. The second caller could be served the first caller's capability view from the cache, or by joining the first caller's sweep. What a backend returns depends on the credentials presented to it, so those views can differ.

  • The cache and the shared sweep now key on the session binding (sessionbinding.FromIdentity: issuer and subject, plus route, client and optional actor for delegated identities) and Identity.PlatformUserID, together with the forwarded headers and backend IDs as before. The binding and platform user ID are length-framed so the two fields cannot run into each other.
  • An identity that cannot be bound is discovered directly: no cache lookup, no shared sweep, nothing stored. The reason is logged at DEBUG. If the request was cancelled during the sweep, the call returns the context error instead of a partial view, as the shared path does.
  • A request with no identity binds to the unauthenticated sentinel and stays cacheable.
  • docs/arch/10 said Modern discovery is never cached; it now describes the 30 second per-identity cache and the bypass. docs/arch/17 notes that the cache uses the same binding as session ownership.
  • The cache's comments now say that the identity's own tokens are not part of the key, and which kinds of aggregator are unsafe to wrap in it.

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Dependency update
  • Documentation
  • Other (describe):

Test plan

  • Unit tests (task test)
  • E2E tests (task test-e2e)
  • Linting (task lint-fix)
  • Manual testing (describe below)

New tests cover the key (delegated with and without an actor, empty and set platform user ID, the binding and platform user ID boundary, nil identity, unbindable identity) and cancellation on the bypass path. The final cache-hit assertions in an existing test now run through the bounded helpers, so a regression fails quickly instead of hanging.

Does this introduce a user-facing change?

Yes, for the capability cache:

  • Before: callers with the same subject shared one cached view and one in-flight sweep, even across issuers or between interactive and delegated callers.
  • After: each issuer, subject, platform user, delegated client and actor gets its own view.
  • Before: an identity with a subject but no issuer claim was cached by subject.
  • After: such an identity is swept on every call and is not rejected. This can happen with token introspection responses that omit iss, or with JWT validators that have no issuer configured. Requests with no identity at all are still cached.

Special notes for reviewers

  • Accepted trade-off. Identities without an issuer lose caching. Falling back to the subject for them would bring back the collision this PR fixes, so they take the direct path instead. The cost is one backend sweep per call from those callers.
  • No panic recovery on the direct path. The shared sweep recovers panics only because DoChan runs it on its own goroutine. The direct path behaves like running without the cache, where a panic unwinds the request goroutine. A comment says so.
  • Known limit, not fixed here. The key does not include the caller's tokens (bearer or upstream). Two requests with the same binding and platform user ID share a view for up to 30 seconds even if they present different tokens to a backend. This is not new, and the comments and docs now say it. Putting credentials into the key is a separate change with its own trade-offs, planned as its own PR.
  • Nearby PR. Answer vMCP list calls from one aggregation #7338 (Answer vMCP list calls from one aggregation) is open and changes sharedSweep in the same file, plus the same test and docs/arch/10. The hunks are in different places, so I expect a clean merge either way.

Key capability views and in-flight discovery by the session binding and
platform user ID, not the subject alone, so callers that share a subject
across issuers, or across interactive and delegated bindings, cannot be
served each other's view.

An identity that cannot be bound (for example one with no issuer claim,
which introspection responses may omit) now discovers directly on every
call instead of sharing a subject-only key. The bypass logs the reason at
DEBUG and still returns the context error if the request was cancelled
during the sweep. Requests with no identity stay cacheable.

The key still does not include the identity's own tokens, so two requests
with the same binding can share a view for up to the 30 second TTL. The
cache docs now say so and name the aggregators that are unsafe to wrap.
The architecture docs describe the cache and the bypass.

Co-authored-by: Mecatl <[email protected]>
@jhrozek
jhrozek requested a review from ChrisJBurns as a code owner November 27, 2026 09:07
@codecov

codecov Bot commented Nov 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.57%. Comparing base (df5aed7) to head (a40b5bc).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7350      +/-   ##
==========================================
+ Coverage   82.56%   82.57%   +0.01%     
==========================================
  Files         941      941              
  Lines       96689    96701      +12     
==========================================
+ Hits        79829    79852      +23     
+ Misses      16855    16844      -11     
  Partials        5        5              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@rdimitrov rdimitrov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving. Checked against the session-binding design (#6888) and the delegated read in #6407: the cache now splits on the same binding that session ownership uses, so delegated and interactive callers can't share a view.

Verified locally (merged with main, -race on ./pkg/vmcp/...), plus probes against the real middleware:

  • An identity from AnonymousMiddleware stays cacheable (5 calls, 1 sweep). A nil identity does too.
  • The same sub under two issuers gets two sweeps.
  • An identity with no iss is swept on every call with no dedup (20 concurrent calls, 20 sweeps). Legacy session creation already fails closed for these identities, so the bypass only costs Modern requests. That's fine as the accepted trade-off.

Non-blocking, for the token-keyed follow-up: an interactive token and a sessionless token (no tsid, unspecified route) for the same user still share one key. I confirmed this with a probe. The sessionless caller loads no upstream tokens, so backends that need them can drop out of its sweep. Whichever caller sweeps first then sets the view the other one sees for up to 30s. It doesn't leak anything, but it's the concrete case that follow-up should cover. Keying on whether upstream credentials were loaded might be enough, without hashing tokens.

@rdimitrov rdimitrov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. The new key closes the collisions across issuers and between interactive and delegated callers. A nil identity and an identity with an empty sub no longer share a key: the nil identity binds to the sentinel and the empty-sub identity bypasses the cache. The sentinel can't collide with a real binding, because every real binding contains a NUL. The cancellation and error handling on the bypass path match the shared path. go test -race ./pkg/vmcp/... passes locally.

I also checked the interaction with #7338: git merge-tree reports no conflict, the merged tree compiles, and the two sharedSweep changes work together. The merged tree does fail TestLookupBackendCapabilities_OneAggregation, but #7338 fails it on its own base too. #7338 just needs a rebase over #6903.

The no-cache path is narrower than the description suggests. FromIdentity reads the token's own iss claim, so a JWT validator without a configured issuer still caches as long as the token carries iss. Only tokens without an iss claim bypass the cache. docs/arch/10 already says this correctly. The downstream embedders I checked both set an issuer, so neither takes the bypass.

@jhrozek

jhrozek commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the careful review, and for probing the sessionless case with a real probe.

I've started a branch for the follow-up locally and will push it once this one is in, so it can sit on main. The plan is to add the names of the upstream providers that have a token loaded to the cache key (never the token values). That separates an interactive token from a sessionless one for the same user, and it doesn't re-key on every token refresh the way hashing would.

I'm leaving the 30 second window after a connect or disconnect on stored credentials as it is for now.

CI is green, so I'll merge this one.

@jhrozek
jhrozek merged commit d168ec2 into stacklok:main Nov 27, 2026
44 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.

2 participants