Repository navigation
fix(auth): bind a credential response to the request that asked for it - #775
Conversation
#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
left a comment
There was a problem hiding this comment.
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.
| 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; | ||
| } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
AmaadMartin
left a comment
There was a problem hiding this comment.
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.
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.
Link to Issue or Description of Change
Closes: #772
Problem
#771 closed the provenance half of the credential resume path: an
adk_request_credentialrequest is honoured only if this agent raised it. Whatthe response then says was still taken at face value. Only
credentialKeywaspinned back from the request, and only when the request happened to carry one:
Everything else on the
AuthConfig— the scheme, the client identity, thecredential 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.
authSchemeis the load-bearing field, because it decides what happens next:apiKeyparseAndStoreAuthResponsetakes the branch that stores the supplied credential verbatim — an OAuth2 request answered with no exchange at alloauth2A response bearing an
accessTokenskipped the exchange regardless, because theexchanger returns early on any token it finds.
The CSRF check had the same shape.
exchangeAuthorizationCodecompares thestateinauthResponseUriagainst thestateon the credential — both ofwhich arrived in the same client message, so it compared the sender against
itself, and a response that simply omitted
stateskipped the check entirely.Scope note, since #772 overstated this: the token endpoint is caller-choosable
but not an unrestricted SSRF primitive.
fetchOAuth2Tokensalready runsvalidateDiscoveryUrl(HTTPS only; loopback, private ranges, CGNAT, IPv6ULA/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:
authUri→ an authorization-code flow is pending, soonly
authResponseUriandauthCodeare read from the response; the rest ofthe credential is the agent's;
service account — and the supplied credential is the answer.
Pinning
statethis way is what makes the CSRF check mean something: the statecompared 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.
processAuthResumeon the workflow path had the same three lines and gets thesame treatment. Its plain-value branch already built from the request and is
unchanged, so the
auth_api_keysample flow ({result: '...'}) is untouched.Three judgement calls worth a reviewer's attention:
accessTokenis deliberately not an accepted answer to a pendingauthorization-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 thatbreaks them, and I would rather find that out here.
statepinning is a behaviour change. A client that previously sent itsown
statenow gets aCredentialExchangeErrorinstead of silent success.That is the point, but it is the most likely source of a surprise.
Partial<AuthConfig>rather thanAuthConfig. Therequest 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
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:
The last two are worth reading together with the first five.
stores the credential for a scheme with no exchangepasses both before and after — it isthe no-regression guard for the API-key flow.
exchanges an authorization code the client answers withfails before the fix only because today a client mustecho 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:
fetchis stubbed, and the realAuthHandlerand
OAuth2CredentialExchangerrun.Checklist
Additional context
Test fixtures in
auth_preprocessor_test.tsthat asked for a credential withoutsaying what kind were under-specifying a real session — both producers of an
adk_request_credentialcall put the whole config in the args — and have beencompleted, the same way #771 completed the gate fixtures. Those tests mock
AuthHandler, so they never noticed; the new file does not mock it, which iswhy it catches this.
Review round 1 (
d6b7446): on the authorization-code path, a response whoseexchangedAuthCredentialwas present but answered nothing — noauthResponseUri, noauthCode— was not refused. It reached the exchanger andthrew 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.
bindCredentialnow 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
ToolConfirmationon the request-input resume path.