Skip to content

fix(auth): bind a credential response to the request that asked for it - #775

Merged
ScottMansfield merged 2 commits into
mainfrom
fix/credential-response-binding
Aug 20, 2026
Merged

ScottMansfield merged 2 commits into
mainfrom
fix/credential-response-binding

Conversation

@ScottMansfield

@ScottMansfield ScottMansfield commented Aug 20, 2026 •

Copy link
Copy Markdown
Member

Link to Issue or Description of Change

Closes: #772

Problem

#771 closed the provenance half of the credential resume path: an
adk_request_credential request is honoured only if this agent raised it. What
the response then says was still taken at face value. Only credentialKey was
pinned back from the request, and only when the request happened to carry one:

const authConfig = authResponses[fcId] as AuthConfig;
if (request.config.credentialKey) {
  authConfig.credentialKey = request.config.credentialKey;
}

Everything else on the AuthConfig — the scheme, the client identity, the
credential itself — came from the client. The destination was trusted and the
credential was not, so a session writer who is not the user could file a
credential of its choosing under the key a waiting tool reads. That tool then
authenticates with it: reads and writes landing in someone else's account while
the agent believes it is acting for the user.

authScheme is the load-bearing field, because it decides what happens next:

restated as consequence
apiKey parseAndStoreAuthResponse takes the branch that stores the supplied credential verbatim — an OAuth2 request answered with no exchange at all
oauth2 still names the token endpoint, so the same message chose where the exchange posted

A response bearing an accessToken skipped the exchange regardless, because the
exchanger returns early on any token it finds.

The CSRF check had the same shape. exchangeAuthorizationCode compares the
state in authResponseUri against the state on the credential — both of
which arrived in the same client message, so it compared the sender against
itself, and a response that simply omitted state skipped the check entirely.

Scope note, since #772 overstated this: the token endpoint is caller-choosable
but not an unrestricted SSRF primitive. fetchOAuth2Tokens already runs
validateDiscoveryUrl (HTTPS only; loopback, private ranges, CGNAT, IPv6
ULA/link-local and cloud-metadata hosts blocked) and sets redirect: 'error'.
It is a public HTTPS host of the sender's choosing. Worth fixing, not the
metadata-server reach the issue implied — I've corrected the issue too.

Solution

Reconcile request and response before storing anything, in one new module,
auth/credential_response_binding.ts.

The request defines the question — scheme, client identity, credential key — and
is now the only source for it. The response answers, and what counts as an
answer depends on what was asked:

  • the request carries an authUri → an authorization-code flow is pending, so
    only authResponseUri and authCode are read from the response; the rest of
    the credential is the agent's;
  • otherwise nothing was pinned to answer — an API key, an HTTP credential, a
    service account — and the supplied credential is the answer.

Pinning state this way is what makes the CSRF check mean something: the state
compared against the redirect is now the one the agent issued, and it can no
longer be skipped by omission.

A request too incomplete to be an authority, or a response carrying no
credential material, is refused rather than guessed at.

processAuthResume on the workflow path had the same three lines and gets the
same treatment. Its plain-value branch already built from the request and is
unchanged, so the auth_api_key sample flow ({result: '...'}) is untouched.

Three judgement calls worth a reviewer's attention:

  • accessToken is deliberately not an accepted answer to a pending
    authorization-code flow. A client that has already done its own exchange and
    wants to hand over a token can still do so through a request that never issued
    an authUri. If anyone is relying on the other shape, this is the line that
    breaks them, and I would rather find that out here.
  • state pinning is a behaviour change. A client that previously sent its
    own state now gets a CredentialExchangeError instead of silent success.
    That is the point, but it is the most likely source of a surprise.
  • The binder takes Partial<AuthConfig> rather than AuthConfig. The
    request is read back off the wire out of a function call's args, so it can
    arrive missing the fields its type promises; typing it honestly is what makes
    the two guard clauses legitimate rather than dead code.

Testing Plan

Unit Tests

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.
npm run test:unit          239 files, 3559 passed
npm run test:integration    77 files,  240 passed | 8 skipped
npm run ts:check / lint / format:check    all clean

Manual End-to-End (E2E) Tests

The demonstration is that the new assertions fail against the current tree.
Revert just the call site and run the file:

git show HEAD~1:core/src/auth/auth_preprocessor.ts > core/src/auth/auth_preprocessor.ts
npx vitest run --project unit:core core/test/auth/credential_response_binding_test.ts
× ignores an auth scheme supplied by the response
  → expected 'attacker-key' to be undefined
× does not store an access token in place of an authorization code
  → expected 'attacker-token' not to be 'attacker-token'
× exchanges against the token endpoint from the request
  → expected [ Array(1) ] to not include 'https://attacker.example.com/token'
× checks the returned state against the state the agent issued
  → expected undefined to be defined
× refuses a request that names no credential key
  → expected { authType: 'apiKey', …(1) } to be undefined
× exchanges an authorization code the client answers with
  → expected "spy" to be called once, but got 0 times
✓ stores the credential for a scheme with no exchange

The last two are worth reading together with the first five. stores the credential for a scheme with no exchange passes both before and after — it is
the no-regression guard for the API-key flow. exchanges an authorization code the client answers with fails before the fix only because today a client must
echo the scheme back for the resume to work at all; after the fix the scheme is
pinned, so the clean response shape works.

No credentials or network needed: fetch is stubbed, and the real AuthHandler
and OAuth2CredentialExchanger run.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Additional context

Test fixtures in auth_preprocessor_test.ts that asked for a credential without
saying what kind were under-specifying a real session — both producers of an
adk_request_credential call put the whole config in the args — and have been
completed, the same way #771 completed the gate fixtures. Those tests mock
AuthHandler, so they never noticed; the new file does not mock it, which is
why it catches this.

Review round 1 (d6b7446): on the authorization-code path, a response whose
exchangedAuthCredential was present but answered nothing — no
authResponseUri, no authCode — was not refused. It reached the exchanger and
threw there, aborting the invocation, where every other unusable response gets a
warn and a skip. Nothing client-controlled was ever stored, so it was safe rather
than a gap, but it reported one condition two different ways. bindCredential
now refuses that shape at the same place as the rest. The no-authorization-code
path is untouched, and there is a test for each side of the split.

Still open from the #771 review, unaffected by this change: #773, the fabricated
ToolConfirmation on the request-input resume path.

#771 closed the provenance half of the credential resume path: a request is
honoured only if this agent raised it. What the response then says was still
taken at face value. Only `credentialKey` was pinned back from the request,
and only when the request happened to carry one:

    const authConfig = authResponses[fcId] as AuthConfig;
    if (request.config.credentialKey) {
      authConfig.credentialKey = request.config.credentialKey;
    }

Everything else on the `AuthConfig` — the scheme, the client identity, the
credential itself — came from the client. So the destination was trusted and
the credential was not, and a session writer who is not the user could put a
credential of its choosing under the key a waiting tool reads. That tool then
authenticates with it: reads and writes landing in someone else's account
while the agent believes it is acting for the user.

The scheme is the load-bearing field, because it decides what happens next.
Restated as `apiKey`, it sends `parseAndStoreAuthResponse` down the branch
that stores the supplied credential verbatim, so an OAuth2 request could be
answered without any exchange at all. Left as `oauth2`, it still names the
token endpoint, so the same message chose where the exchange posted — within
the SSRF blocklist `fetchOAuth2Tokens` already applies, so a public HTTPS host
of the sender's choosing. And a response bearing an `accessToken` skipped the
exchange regardless, because the exchanger returns early on any token it
finds.

The CSRF check had the same shape. `exchangeAuthorizationCode` compares the
`state` in `authResponseUri` against the `state` on the credential — both of
which arrived in the same client message, so it compared the sender against
itself, and a response that simply omitted `state` skipped the check.

Reconcile the two before storing anything. The request defines the question —
scheme, client identity, credential key — and is now the only source for it.
The response answers, and what counts as an answer depends on what was asked:
if the request carries an `authUri` an authorization-code flow is pending, so
only `authResponseUri` and `authCode` are read from the response and the rest
of the credential is the agent's; otherwise nothing was pinned to answer — an
API key, an HTTP credential, a service account — and the supplied credential
is the answer. Pinning `state` this way is what makes the CSRF check mean
something, since the state compared against the redirect is now the one the
agent issued.

A request too incomplete to be an authority, or a response carrying no
credential material, is refused rather than guessed at.

`processAuthResume` on the workflow path had the same three lines and gets
the same treatment; its plain-value branch already built from the request and
is unchanged.

Test fixtures that asked for a credential without saying what kind were
under-specifying a real session — both producers of an `adk_request_credential`
call put the whole config in the args — and have been completed.

Fixes #772

@AmaadMartin AmaadMartin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Verified against the exchanger at the head SHA. Dropping the response accessToken and pinning the scheme, key, client identity and state from the request closes the bypass and makes the CSRF check mean something. The fix is correct and well-tested on both the LLM-agent and workflow paths, and I found no blocking issue. I am not approving only because the run-tests matrix (ubuntu, macOS, Windows) is still pending; the required checks (cla/google, check-license, scan-pr) pass.

Comment on lines +60 to +69
const suppliedOAuth2 = readField(supplied, 'oauth2');
const answer: Partial<OAuth2Auth> = {};
const authResponseUri = readString(suppliedOAuth2, 'authResponseUri');
if (authResponseUri !== undefined) {
answer.authResponseUri = authResponseUri;
}
const authCode = readString(suppliedOAuth2, 'authCode');
if (authCode !== undefined) {
answer.authCode = authCode;
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nit, optional. On the authorization-code path a response with exchangedAuthCredential present but no authResponseUri and no authCode is not refused. answer stays empty, the agent credential returns unchanged, and the exchanger later throws instead of the warn-and-skip a missing credential gets. This is safe (nothing client-controlled is stored), but it does not match "refused rather than guessed at" for this shape.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in d6b7446 — and you are right that it did not match the promise, which is the part worth fixing even though nothing client-controlled was reachable.

Worth naming why it bothered me once you pointed at it: the module reported one condition two different ways depending on which field was missing. A response with no exchangedAuthCredential at all got a warn and a skip; a response with one that answered nothing got a CredentialExchangeError from deep in the exchanger and took the invocation down with it. Same situation — the client did not answer the question — and an operator reading logs would have seen two unrelated-looking failures.

bindCredential now returns undefined for that shape, so it lands at the same warn-and-skip as the rest.

The no-authorization-code path is deliberately untouched: a credential answering an API key, an HTTP scheme or a service account has no code fields by definition, so the new check is scoped to the branch where a code is actually pending. There is a test for each side of that split, and the access-token case now asserts the refusal is clean rather than merely not storing the token — no exchange attempted, nothing stored, nothing thrown.

…anging it

Review catch. On the authorization-code path a response whose
`exchangedAuthCredential` was present but carried neither `authResponseUri`
nor `authCode` was not refused. The answer stayed empty, the agent's own
credential came back unchanged, and the exchanger threw further down —
aborting the invocation where every other unusable response gets a warn and a
skip.

Nothing client-controlled was ever stored, so this was safe rather than a
gap. It just was not the "refused rather than guessed at" the rest of the
module promises, and it reported the same condition in two different ways
depending on which field was missing.

`bindCredential` now returns `undefined` for that shape, so it joins the
others at the caller's warn-and-skip. The no-authorization-code path is
untouched: a credential answering an API key, an HTTP scheme or a service
account has no code fields by definition and is still accepted.

Two direct tests for the two sides of the new branch, and the access-token
case now asserts the refusal is clean — no exchange attempted, nothing
stored, nothing thrown.

@AmaadMartin AmaadMartin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Re-review of d6b7446. My only open item, the optional nit about a pending authorization-code flow that answers nothing, is fixed: bindCredential returns undefined for that shape (core/src/auth/credential_response_binding.ts:74), both call sites warn and skip, and a new test pins it. I verified the fix and the request-over-response binding against the head source; the diff adds no any, no suppression, and no instanceof, and the internal helper is correctly not exported. Required checks (cla/google, check-license, scan-pr) are green and ubuntu run-tests passed; windows and macos run-tests were still running at review time.

@ScottMansfield
ScottMansfield merged commit 10ae7bd into main Aug 20, 2026
12 checks passed
@ScottMansfield
ScottMansfield deleted the fix/credential-response-binding branch August 20, 2026 18:33
@kalenkevich kalenkevich mentioned this pull request Aug 20, 2026

@AmaadMartin AmaadMartin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Re-review at d6b7446. The prior nit is fixed: bindCredential now refuses an authorization-code response that carries no authResponseUri and no authCode (credential_response_binding.ts:74), and a direct test covers it. I verified the guard in the source at the head SHA, not the commit message. No new type suppressions, no instanceof, and the internal binder is correctly kept out of index.ts/common.ts. CI was green at review time.

prasanna8585 pushed a commit to prasanna8585/adk-js that referenced this pull request Nov 10, 2026
google#775)

* fix(auth): bind a credential response to the request that asked for it

google#771 closed the provenance half of the credential resume path: a request is
honoured only if this agent raised it. What the response then says was still
taken at face value. Only `credentialKey` was pinned back from the request,
and only when the request happened to carry one:

    const authConfig = authResponses[fcId] as AuthConfig;
    if (request.config.credentialKey) {
      authConfig.credentialKey = request.config.credentialKey;
    }

Everything else on the `AuthConfig` — the scheme, the client identity, the
credential itself — came from the client. So the destination was trusted and
the credential was not, and a session writer who is not the user could put a
credential of its choosing under the key a waiting tool reads. That tool then
authenticates with it: reads and writes landing in someone else's account
while the agent believes it is acting for the user.

The scheme is the load-bearing field, because it decides what happens next.
Restated as `apiKey`, it sends `parseAndStoreAuthResponse` down the branch
that stores the supplied credential verbatim, so an OAuth2 request could be
answered without any exchange at all. Left as `oauth2`, it still names the
token endpoint, so the same message chose where the exchange posted — within
the SSRF blocklist `fetchOAuth2Tokens` already applies, so a public HTTPS host
of the sender's choosing. And a response bearing an `accessToken` skipped the
exchange regardless, because the exchanger returns early on any token it
finds.

The CSRF check had the same shape. `exchangeAuthorizationCode` compares the
`state` in `authResponseUri` against the `state` on the credential — both of
which arrived in the same client message, so it compared the sender against
itself, and a response that simply omitted `state` skipped the check.

Reconcile the two before storing anything. The request defines the question —
scheme, client identity, credential key — and is now the only source for it.
The response answers, and what counts as an answer depends on what was asked:
if the request carries an `authUri` an authorization-code flow is pending, so
only `authResponseUri` and `authCode` are read from the response and the rest
of the credential is the agent's; otherwise nothing was pinned to answer — an
API key, an HTTP credential, a service account — and the supplied credential
is the answer. Pinning `state` this way is what makes the CSRF check mean
something, since the state compared against the redirect is now the one the
agent issued.

A request too incomplete to be an authority, or a response carrying no
credential material, is refused rather than guessed at.

`processAuthResume` on the workflow path had the same three lines and gets
the same treatment; its plain-value branch already built from the request and
is unchanged.

Test fixtures that asked for a credential without saying what kind were
under-specifying a real session — both producers of an `adk_request_credential`
call put the whole config in the args — and have been completed.

Fixes google#772

* fix(auth): refuse a credential that answers nothing, rather than exchanging it

Review catch. On the authorization-code path a response whose
`exchangedAuthCredential` was present but carried neither `authResponseUri`
nor `authCode` was not refused. The answer stayed empty, the agent's own
credential came back unchanged, and the exchanger threw further down —
aborting the invocation where every other unusable response gets a warn and a
skip.

Nothing client-controlled was ever stored, so this was safe rather than a
gap. It just was not the "refused rather than guessed at" the rest of the
module promises, and it reported the same condition in two different ways
depending on which field was missing.

`bindCredential` now returns `undefined` for that shape, so it joins the
others at the caller's warn-and-skip. The no-authorization-code path is
untouched: a credential answering an API key, an HTTP scheme or a service
account has no code fields by definition and is still accepted.

Two direct tests for the two sides of the new branch, and the access-token
case now asserts the refusal is clean — no exchange attempted, nothing
stored, nothing thrown.
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.

fix(auth): a credential response's AuthConfig is adopted wholesale apart from credentialKey

3 participants