Skip to content

core: key a declaring module's calls on the credentials they borrowed, not the session - #15238

Merged
sipsma merged 4 commits into
mainfrom
sipsma/git-clients-borrowed-credentials
Nov 20, 2026
Merged

sipsma merged 4 commits into
mainfrom
sipsma/git-clients-borrowed-credentials

Conversation

@sipsma

@sipsma sipsma commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Since #14493, a module's code can serve a git client that its dagger.toml declares, and the engine resolves it with the git credentials of the session that called it. To keep a result built with one session's credentials away from other sessions, #14493 put the session id in the cache key of every function of a module that declares any git client. It did so whether or not the function served a client or used a credential.

So no new session is ever served those functions' results, on any engine, warm or fresh. On a copy of dagger/dagger's own pull-request checks, 19 checks call such a function directly, and on a same-commit rerun on a warm engine they run it again: 355 s of rerun time across them. go-client/test's rerun on a warm engine takes 25 to 27 s, against about 4 s for a check that is served whole.

What changes

The key keeps the declared clients' digest, so a changed or removed declaration still misses, as #14493 intended. The session id goes. In its place, a call's result requires exactly the credentials its work actually borrowed, through the session-resource check the cache already uses for secrets and sockets:

  • Serving a declared client records the module source it resolved. That is on the function call whose work did the serving, including a process a module entrypoint starts. The source requires the token or SSH agent it was fetched with, and nothing for a repository anyone can read.
  • Every module call result the call's work obtains is recorded too, whether it executed or came from cache, null results included. Another module that borrowed can hand back a plain string, which carries no dependency on how it was computed.
  • When the call returns, its result requires the session resources those results require. A session that hasn't loaded the same credential computes its own result. A session that has, and every session for a call that borrowed nothing, is served it.
  • A module entrypoint keys the process it starts on the call's recipe digest, which no longer changes per session. For a call of such a module, the process is keyed on the session too, so a session refused the call's result can't get the process's output either. It runs only when the call itself misses.

The mechanism is a small dagql hook, RequireSessionResourcesOf, beside DeclinePersistence and ExpireBy. A resolver calls it with the results it borrowed through, and the cache gives the call's result a dependency on each session resource in their closures. That happens in the same critical section that indexes the call's key, so no lookup is served the result before it carries the requirement. A resolver that borrows nothing changes nothing.

Results

  • go-client/test on a warm engine, rerun in a new session: from 25 to 27 s to 0.8 to 1.0 s, served at its top call.
  • Cost: none measured. On the same warm machine, four alternating fills of go-client/test with each engine empty took a mean of 248.1 s without the change and 246.6 s with it, inside their spread.

What a later session gets

Each case was tested on this change and on main. A first session with the right token runs a call that serves a private declared client, plus two calls above it that return its string. One is in a module that declares a git client, the other in a module that doesn't. A new session then runs each call:

Later session holds The module's own call A declaring call above it A non-declaring call above it
No credential runs, auth fails runs, auth fails served the first result
A wrong token from its credential helper runs, auth fails runs, auth fails served the first result
The lent token's secret name, with a garbage value runs, auth fails runs, auth fails served the first result

Main and this change read the same in all nine cells. The last column is how main already behaves: a module that declares no git client has no session in its key. This change neither opens that nor closes it.

The secret the engine creates for a lent token is created in the module's own client, so its handle is scoped to the module's implementation (core/client_resource.go). A session outside that module's code can't bind it, which is why the forged secret name misses.

One behavior does change. If module code catches serveModule's authentication failure and returns normally, that outcome is now cached across sessions, so a later session that has the credential is served the failure. Before, the session in the key kept them apart. Generated clients return the error instead, so the function fails and nothing is cached.

Tests

  • core/integration/module_serve_git_trust_test.go, new subtests:
    • a call that borrows no credential hits in another session (fails on main);
    • a result built with credentials hits in a session holding them (fails on main);
    • a result built with credentials misses in a session without them, when the function returns an object the engine holds under its own key;
    • a string relayed from a module that used credentials misses in a session without them, including when that module's result came from cache or returned nothing;
    • an entrypoint's process that used credentials misses in a session without them.
  • The existing subtests, among them "a removed declaration misses the cache" and "another session without credentials misses the cache", pass unchanged.
  • dagql/cache_borrowed_resources_test.go: a borrowed resource gates the result; a borrowed result that requires nothing adds nothing; a returned shared result gains the requirement for its other callers too; a detached result is refused.

#15130 also edits core/modfunc.go, next to the git clients digest. It changes how local clients are keyed, not this, and the two don't interact.

@sipsma
sipsma force-pushed the sipsma/git-clients-borrowed-credentials branch from ad32cdb to fb62c2e Compare November 20, 2026 00:44
@sipsma
sipsma force-pushed the sipsma/git-clients-borrowed-credentials branch from fb62c2e to 838bfba Compare November 20, 2026 01:36
A call's result requires a session resource, such as a secret or socket,
when it depends on one, which mostly comes through the call's inputs: its
key names the resource, so a session without it can't even ask. But a
resolver can reach a session's resources during its work, and a key
computed before the work can't name those: credentials the session lends
to code the call runs are one case. Until now the only way to protect such
a result was to put the whole session in the key, so no other session was
ever served it, whether or not the work used anything the session held.

RequireSessionResourcesOf lets the resolver say, as it returns, which
results it used. The call's result gets a dependency on each session
resource in their closures, added in the same critical section that
indexes the call's key, so the usual check refuses the result to a session
that hasn't loaded the same resources and serves it to one that has. A
result that borrowed nothing requiring a resource is unchanged.

When the call returns a result the cache already holds under another key,
that result is the one that gains the requirement, so its other callers
are refused it too unless they hold the resource.

Signed-off-by: Erik Sipsma <[email protected]>
…session

Since a module's config could declare git clients, every function of such
a module had the session in its cache key, because its code may serve a
declared client with credentials the session lends. Whether or not a
function served a client or used a credential, no new session was served
its result, on any engine.

The key keeps the declared clients' digest, so a changed or removed
declaration still misses. In place of the session, a call of such a
module tracks what its work borrows, and its result requires the session
resources those results required (dagql.RequireSessionResourcesOf):

- Serving a declared client records the module source it resolved, which
  requires the token or SSH agent it was fetched with, and nothing for a
  repository anyone can read. It records on the function call whose work
  the serving client does, the nearest of the client and its ancestors
  that a call created, so a module entrypoint's process counts too.
- Every module call result the work obtains, executed or from cache, null
  results included, is recorded as well: another module that borrowed can
  hand back a string, which carries no dependency on how it was computed.
- A module entrypoint keys the process it starts on the call's recipe
  digest, which no longer changes per session. For a call that tracks
  borrowing the process is keyed on the session too, so a session refused
  the call's result doesn't get the process's output either. It runs only
  when the call misses.

A session that hasn't loaded the same credential computes its own result,
one that has is served it, and calls that borrowed nothing are served to
every session. An entrypoint member's call, such as prebuild's, tracks
nothing, since its result isn't a module function's.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma
sipsma force-pushed the sipsma/git-clients-borrowed-credentials branch from 838bfba to 18c7eb4 Compare November 20, 2026 02:09
@sipsma
sipsma merged commit e475f88 into main Nov 20, 2026
76 checks passed
@sipsma
sipsma deleted the sipsma/git-clients-borrowed-credentials branch November 20, 2026 02:42
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