Skip to content

fix(auth): redact OAuth2 authorization code from malformed-callback-URI log - #640

Merged
ScottMansfield merged 7 commits into
google:mainfrom
prasanna8585:fix/redact-oauth2-authorization-code-in-logs
Aug 24, 2026
Merged

ScottMansfield merged 7 commits into
google:mainfrom
prasanna8585:fix/redact-oauth2-authorization-code-in-logs

Conversation

@prasanna8585

@prasanna8585 prasanna8585 commented Aug 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary

PR #602 did a thorough sweep of connection-URI password leaks across sessions/ and artifacts/ -- including catching its own initial miss (getArtifactServiceFromUri) and covering both userinfo and query-parameter credential forms. That sweep never touched auth/oauth2/, since an OAuth2 callback URI isn't a "connection URI" -- but parseAuthorizationCode() has the identical bug class.

Its catch branch (reached when the OAuth2 callback/authorization-response URI fails to parse) logs the raw URI at warn level:

export function parseAuthorizationCode(uri: string): string | undefined {
  try {
    const url = new URL(uri);
    return url.searchParams.get('code') || undefined;
  } catch (e) {
    logger.warn(`Failed to parse authorization URI ${uri}: ${e}`);  // <-- raw uri
    return undefined;
  }
}

By this function's own stated purpose -- extracting the code query parameter -- that URI is expected to carry a real, single-use, bearer-equivalent authorization code, which then reaches application logs and error-tracking services (the same trust-boundary concern PR #602 addressed).

Confirmed reachable: parseAuthorizationCode is called from oauth2_credential_exchanger.ts with the real OAuth2 authResponseUri, in the real credential-exchange flow.

Fix

  • Reuse the existing redactUriPassword() helper at the vulnerable log line. Its own catch branch already safely handles the failed-to-parse case (returning <unparseable URI, redacted> or scheme://<unparseable URI, redacted>) regardless of which query-param names it recognizes, so this alone closes the vulnerable line. - Extend SECRET_QUERY_PARAMS with OAuth2-specific secret names (code, access_token, id_token, refresh_token, client_secret) for defense-in-depth on any future successfully-parsed OAuth URI logging elsewhere.

Testing

  • 2 new regression tests: one exercising parseAuthorizationCode directly with a malformed callback URI carrying a recognizable authorization code, confirming it never reaches the logger; one confirming redactUriPassword masks OAuth2 query-parameter secrets alongside the existing password forms.
  • Real npm run build: compiles clean.
  • Real vitest run of redact_uri_test.ts: 16 passed (14 existing + 2 new).
  • Full unit:core project (2700+ tests): passes except two files (sessions/db/operations_test.ts, sessions/database_session_service_test.ts) that depend on a native sqlite3 binding unavailable in the sandbox I used -- confirmed via git stash that these exact same tests fail identically against the unmodified upstream code, i.e. unrelated to this change.

…I log

parseAuthorizationCode()'s catch branch (reached when the OAuth2
callback/authorization-response URI fails to parse) logged the raw URI
at warn level. By this function's own purpose -- extracting the code
query parameter -- that URI is expected to carry a real, single-use,
bearer-equivalent authorization code, which then reaches application
logs and error-tracking services, the same trust-boundary concern
PR google#602 addressed for connection-URI passwords.

Confirmed reachable: parseAuthorizationCode is called from
oauth2_credential_exchanger.ts with the real OAuth2 authResponseUri, in
the real credential-exchange flow.

Fix: reuse the existing redactUriPassword() helper at the vulnerable
log line (its own catch branch already safely handles the
failed-to-parse case), and extend SECRET_QUERY_PARAMS with OAuth2
secret names (code, access_token, id_token, refresh_token,
client_secret) for defense-in-depth on any future successfully-parsed
OAuth URI logging.

Verified: real npm run build compiles clean; real vitest suite for
redact_uri_test.ts (16 tests, including 2 new ones) all pass; the
broader unit:core project passes except two files depending on a
native sqlite3 binding unavailable in this environment, confirmed via
git stash to fail identically on unmodified upstream code.
@Varun-S10

Copy link
Copy Markdown
Contributor

Hi @prasanna8585, We noticed that a few of the pre-checks are failing on this PR. Could you please resolve them?

@Varun-S10 Varun-S10 self-assigned this Aug 13, 2026
@Varun-S10 Varun-S10 added the request clarification [Status] The maintainer need clarification or more information from the author label Aug 13, 2026

@ScottMansfield ScottMansfield 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.

Nice find, and the fix is in the right layer. parseAuthorizationCode is reachable with a live callback URI from the ordinary authorization-code exchange (core/src/auth/oauth2/oauth2_credential_exchanger.ts:164), so this is a real leak rather than a theoretical one. I reproduced it against current main and against this branch:

# current main (10ae7bd), input 'not-a-valid-scheme?code=SECRET_AUTH_CODE&state=xyz'
warn : Failed to parse authorization URI not-a-valid-scheme?code=SECRET_AUTH_CODE&state=xyz: TypeError: Invalid URL
leaks SECRET_AUTH_CODE? true

# this PR (56ae6d1), same input
warn : Failed to parse authorization URI <unparseable URI, redacted>: TypeError: Invalid URL
leaks SECRET_AUTH_CODE? false

Reusing redactUriPassword instead of hand-rolling a scrubber is the right call, and the new regression test really does fail without the fix — the main line above is exactly what its not.toContain would be handed. The helper's existing "unparseable ⇒ drop everything" fallback also means the fixed line is safe regardless of which parameter names the set knows about, which is a nice property.

@kalenkevich has already approved this, and nothing I found below is a regression against main — on every input I tried, this PR is strictly better than what's on main today. So I'm not blocking. The first item is still worth doing while the file is open; it's a one-line edit on the very line this PR rewrites.

Scope note the description doesn't mention. redactUriPassword has three other callers — sessions/registry.ts:36, sessions/db/operations.ts:44, artifacts/registry.ts:34 — which put its output into thrown Error messages. Adding code to the global set means a database URI that happens to carry ?code=… now surfaces as ?code=*** in those errors. Harmless, but it is a behaviour change at three call sites, and worth a line in the description.

Same pattern nearby, deliberately out of scope. core/src/auth/oauth2/oauth2_discovery.ts has three instances of the identical shape — the raw URL plus ${e} interpolated straight into a logger.warn template literal — at line 59 (issuerUrl), line 134 (resourceUrl) and line 255 (urlStr). Those carry configuration URLs rather than credentials, so the severity is much lower; I mention them only because a follow-up that closes the bug class rather than one line would want them.

CI. The only genuinely failing job is run-tests (macos-latest), whose three failures are all 60-second timeouts in tests/integration/app_loader/app_loader_test.ts — unrelated to this PR. run-tests (windows-latest) shows red but was cancelled by matrix fail-fast, not failed. Everything else (run-tests (ubuntu-latest), scan-pr, check-license, check-changes, cla/google) is green, so @Varun-S10's request about failing pre-checks looks overtaken by events. The branch is MERGEABLE but its merge base is 91 commits behind main; a rebase before merge is worth it.

Worth resolving:

  1. core/src/auth/oauth2/oauth2_utils.ts:106 — drop ${e} (or interpolate only e.name). It is the last engine-controlled path by which the raw URI can reach this log line.
  2. core/src/utils/redact_uri.ts — either handle fragment parameters or narrow the new comment, so the stated access_token/id_token/refresh_token coverage matches what the query-only loop actually does.
  3. core/test/utils/redact_uri_test.ts:126-130 — add expect(warnSpy).toHaveBeenCalledOnce() plus a positive assertion, so the test cannot pass vacuously.
  4. Optional: add code_verifier to the set, cover all five new names with a table-driven test, and refresh the now-stale comment at redact_uri.ts:8-9.
  5. Optional: the new test sits under describe('connection-URI errors do not leak the password'), a heading that no longer fits an OAuth2 authorization code. core/test/auth/oauth2/oauth2_utils_test.ts:176 already has a describe('parseAuthorizationCode') block (its third case, line 187, is 'returns undefined and logs warning for invalid URI') — that is where a maintainer touching this function will look.

Comment thread core/src/auth/oauth2/oauth2_utils.ts Resolved
Comment thread core/src/utils/redact_uri.ts Resolved
Comment thread core/src/utils/redact_uri.ts Resolved
Comment thread core/test/utils/redact_uri_test.ts Resolved
Comment thread core/test/utils/redact_uri_test.ts Resolved
prasanna8585 and others added 5 commits August 21, 2026 14:59
Addresses the one remaining item from review: parseAuthorizationCode's
catch block already redacts the URI itself via redactUriPassword(),
but still interpolated the caught Error object (${e}) into the same
log line.

On Node this is inert -- a URL parse failure's String(e) is just
"TypeError: Invalid URL", no raw input included. But this package
ships a browser build (core/package.json's "browser" field, built via
core/build.js, reachable from index_web.ts -> OAuth2CredentialExchanger
-> parseAuthorizationCode), and per Gecko's own source, Firefox's URL
constructor throws with the raw input string embedded directly in the
error message -- so on that engine ${e} would put the authorization
code straight back into the log this fix exists to keep it out of.

Stops interpolating the error entirely, since the message doesn't add
anything the already-redacted URI didn't imply, and drops the now-
unused e binding (catch { is already the idiom in this file's own
neighbour, redact_uri.ts).

Verified: tsc --noEmit, eslint, and prettier --check all clean.
core/test/auth/oauth2/oauth2_utils_test.ts and
core/test/utils/redact_uri_test.ts both pass (42/42), including
confirming the resulting warning log reads exactly "Failed to parse
authorization URI <unparseable URI, redacted>" with no error content
appended.
@ScottMansfield ScottMansfield changed the title security: redact OAuth2 authorization code from malformed-callback-URI log fix(auth): redact OAuth2 authorization code from malformed-callback-URI log Aug 24, 2026
@ScottMansfield
ScottMansfield merged commit 08f4f24 into google:main Aug 24, 2026
13 of 15 checks passed
@kalenkevich kalenkevich mentioned this pull request Aug 24, 2026
prasanna8585 added a commit to prasanna8585/adk-js that referenced this pull request Nov 10, 2026
…RI log (google#640)

* security: redact OAuth2 authorization code from malformed-callback-URI log

parseAuthorizationCode()'s catch branch (reached when the OAuth2
callback/authorization-response URI fails to parse) logged the raw URI
at warn level. By this function's own purpose -- extracting the code
query parameter -- that URI is expected to carry a real, single-use,
bearer-equivalent authorization code, which then reaches application
logs and error-tracking services, the same trust-boundary concern
PR google#602 addressed for connection-URI passwords.

Confirmed reachable: parseAuthorizationCode is called from
oauth2_credential_exchanger.ts with the real OAuth2 authResponseUri, in
the real credential-exchange flow.

Fix: reuse the existing redactUriPassword() helper at the vulnerable
log line (its own catch branch already safely handles the
failed-to-parse case), and extend SECRET_QUERY_PARAMS with OAuth2
secret names (code, access_token, id_token, refresh_token,
client_secret) for defense-in-depth on any future successfully-parsed
OAuth URI logging.

Verified: real npm run build compiles clean; real vitest suite for
redact_uri_test.ts (16 tests, including 2 new ones) all pass; the
broader unit:core project passes except two files depending on a
native sqlite3 binding unavailable in this environment, confirmed via
git stash to fail identically on unmodified upstream code.

* chore: apply prettier formatting

* Update core/test/utils/redact_uri_test.ts

Co-authored-by: Scott Mansfield <[email protected]>

* Update core/test/utils/redact_uri_test.ts

Co-authored-by: Scott Mansfield <[email protected]>

* Update core/src/utils/redact_uri.ts

Co-authored-by: Scott Mansfield <[email protected]>

* Update core/src/utils/redact_uri.ts

Co-authored-by: Scott Mansfield <[email protected]>

* fix: stop interpolating the raw Error into the redacted-URI warning log

Addresses the one remaining item from review: parseAuthorizationCode's
catch block already redacts the URI itself via redactUriPassword(),
but still interpolated the caught Error object (${e}) into the same
log line.

On Node this is inert -- a URL parse failure's String(e) is just
"TypeError: Invalid URL", no raw input included. But this package
ships a browser build (core/package.json's "browser" field, built via
core/build.js, reachable from index_web.ts -> OAuth2CredentialExchanger
-> parseAuthorizationCode), and per Gecko's own source, Firefox's URL
constructor throws with the raw input string embedded directly in the
error message -- so on that engine ${e} would put the authorization
code straight back into the log this fix exists to keep it out of.

Stops interpolating the error entirely, since the message doesn't add
anything the already-redacted URI didn't imply, and drops the now-
unused e binding (catch { is already the idiom in this file's own
neighbour, redact_uri.ts).

Verified: tsc --noEmit, eslint, and prettier --check all clean.
core/test/auth/oauth2/oauth2_utils_test.ts and
core/test/utils/redact_uri_test.ts both pass (42/42), including
confirming the resulting warning log reads exactly "Failed to parse
authorization URI <unparseable URI, redacted>" with no error content
appended.

---------

Co-authored-by: Scott Mansfield <[email protected]>
Co-authored-by: prasanna8585 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

request clarification [Status] The maintainer need clarification or more information from the author

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants