Repository navigation
fix(auth): redact OAuth2 authorization code from malformed-callback-URI log - #640
Conversation
…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.
|
Hi @prasanna8585, We noticed that a few of the pre-checks are failing on this PR. Could you please resolve them? |
ScottMansfield
left a comment
There was a problem hiding this comment.
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:
core/src/auth/oauth2/oauth2_utils.ts:106— drop${e}(or interpolate onlye.name). It is the last engine-controlled path by which the raw URI can reach this log line.core/src/utils/redact_uri.ts— either handle fragment parameters or narrow the new comment, so the statedaccess_token/id_token/refresh_tokencoverage matches what the query-only loop actually does.core/test/utils/redact_uri_test.ts:126-130— addexpect(warnSpy).toHaveBeenCalledOnce()plus a positive assertion, so the test cannot pass vacuously.- Optional: add
code_verifierto the set, cover all five new names with a table-driven test, and refresh the now-stale comment atredact_uri.ts:8-9. - 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:176already has adescribe('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.
Co-authored-by: Scott Mansfield <[email protected]>
Co-authored-by: Scott Mansfield <[email protected]>
Co-authored-by: Scott Mansfield <[email protected]>
Co-authored-by: Scott Mansfield <[email protected]>
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.
…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]>
Summary
PR #602 did a thorough sweep of connection-URI password leaks across
sessions/andartifacts/-- including catching its own initial miss (getArtifactServiceFromUri) and covering both userinfo and query-parameter credential forms. That sweep never touchedauth/oauth2/, since an OAuth2 callback URI isn't a "connection URI" -- butparseAuthorizationCode()has the identical bug class.Its
catchbranch (reached when the OAuth2 callback/authorization-response URI fails to parse) logs the raw URI atwarnlevel:By this function's own stated purpose -- extracting the
codequery 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:
parseAuthorizationCodeis called fromoauth2_credential_exchanger.tswith the real OAuth2authResponseUri, in the real credential-exchange flow.Fix
redactUriPassword()helper at the vulnerable log line. Its owncatchbranch already safely handles the failed-to-parse case (returning<unparseable URI, redacted>orscheme://<unparseable URI, redacted>) regardless of which query-param names it recognizes, so this alone closes the vulnerable line. - ExtendSECRET_QUERY_PARAMSwith 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
parseAuthorizationCodedirectly with a malformed callback URI carrying a recognizable authorization code, confirming it never reaches the logger; one confirmingredactUriPasswordmasks OAuth2 query-parameter secrets alongside the existing password forms.npm run build: compiles clean.redact_uri_test.ts: 16 passed (14 existing + 2 new).unit:coreproject (2700+ tests): passes except two files (sessions/db/operations_test.ts,sessions/database_session_service_test.ts) that depend on a nativesqlite3binding unavailable in the sandbox I used -- confirmed viagit stashthat these exact same tests fail identically against the unmodified upstream code, i.e. unrelated to this change.