Repository navigation
fix(sessions): redact connection-URI password in unsupported-URI errors - #602
Conversation
getSessionServiceFromUri and getConnectionOptionsFromUri interpolated the raw connection URI into their "Unsupported ... URI" error messages. A URI such as postgres://user:password@host/db embeds the password in its userinfo, so an unsupported or mistyped scheme surfaced the password verbatim in a thrown Error, which typically reaches application logs and error-tracking services. Add a redactUriPassword helper that masks the userinfo password (keeping the rest of the URI for debugging, like Go's net/url.URL.Redacted) and use it at both sites.
secretlint's database-connection-string rule flags the literal postgres:// URI in the test as a real credential because "s3cr3t" isn't on its placeholder allowlist. Use "pass", matching the convention already used by the sibling operations_test.ts fixtures.
|
Pushed a follow-up commit to fix the failing @kalenkevich the 4 workflow runs on the new commit are stuck in "awaiting approval" since this is a fork PR could you approve them when you get a chance so CI can finish? |
format:check only surfaced this now that the earlier secretlint failure stopped masking it — the ternary in the catch branch wasn't prettier-formatted.
|
Pushed another follow-up commit — after the secretlint fix let @kalenkevich could you approve the pending workflow runs on this commit too? Should be green after this. |
AmaadMartin
left a comment
There was a problem hiding this comment.
The helper is right and the tests are the ones worth having — the two .rejects.not.toThrow(/hunter2/) assertions test the actual property (the secret is absent) rather than the formatting. new URL() does parse userinfo for non-special schemes like postgres:/oracle:, so the two fixed call sites behave as the tests claim, and the unparseable-input branch fails closed.
One missed call site below — same line, same threat, not covered.
getArtifactServiceFromUri had the same unredacted-URI-in-error-message gap as the two sessions/ call sites fixed earlier in this PR, missed by the original sweep. An artifact store URI (e.g. s3://key:secret@bucket) carries credentials in userinfo just as readily as a session connection string.
AmaadMartin
left a comment
There was a problem hiding this comment.
Verified the helper's behaviour rather than reading it: I ran the old and new shapes over 300k+ random inputs and the documented cases, and the three exact-equality assertions in the new test hold with no URL.toString() normalization surprises for these schemes. I also grepped for other sites that interpolate a connection URI into an error — the three this PR changes are the complete set of "Unsupported ... URI" throws, so the sweep is done.
One real gap below (query-string credentials), plus a stale description. Neither is a blocker; the PR is a strict improvement as-is.
On CI, so it isn't mistaken for your bug: both red checks are environmental. GitHub Actions Scan flags three unpinned-action findings in .github/workflows/validation.yaml, a file this PR doesn't touch — that job scans the head ref, and this branch is 74 commits behind main, so its stale copy of that workflow shifts the uses: lines and misses the suppressions every up-to-date PR gets. validation failed on macOS only, on app_loader_test.ts timing out at 40000ms; that file doesn't exist on this branch at all (it arrives via the merge ref) and it observed 41.3s and 44.7s against a 40s budget, so it is a borderline-timeout on a slow runner, not anything this change did. Ubuntu ran the identical tree green: 2689 passed, 0 failed, including your redact_uri_test.ts (7 tests). Updating the branch onto main should clear the zizmor one.
| const schemeEnd = uri.indexOf('://'); | ||
| return schemeEnd === -1 | ||
| ? '<redacted>' | ||
| : `${uri.slice(0, schemeEnd)}://<redacted>`; |
There was a problem hiding this comment.
Nit, optional — the unparseable fallback is right, and I want to flag one consequence you may already have intended.
: `${uri.slice(0, schemeEnd)}://<redacted>`;This throws away the host and database name for any URI that new URL() rejects, so a genuine typo like postgres//host/db (missing colon) produces <redacted> and the operator loses every clue about what they mistyped. That is the safe direction to err in and I would not change the default, but the error message at the call site now reads Unsupported database URI: <redacted>, which is close to useless for debugging. Worth a sentence in the thrown error telling the user the value was redacted, so they don't think the URI itself was empty.
There was a problem hiding this comment.
Agreed on both halves: the fallback should keep erring safe, and the message it produced was unhelpful. Fixed in 065210c by making the placeholder say what happened rather than adding a sentence at each call site, so the three throws stay one line each:
Unsupported database URI: <unparseable URI, redacted>
Unsupported database URI: postgres://<unparseable URI, redacted>
Your postgres//host/db example hits the first form, since there is no :// to keep a scheme from. It still costs the operator the host and database name, which is the tradeoff you described and I did not want to trade away, but at least it no longer reads as though the value was empty. Test added pinning both forms.
…-uri-password-in-errors
…meters A credential can reach the same error paths as a query parameter rather than in the userinfo component, for example postgres://user@host/db?password=secret, a form several drivers accept. url.password only covers userinfo, so those were returned verbatim. Masks password, passwd, pwd and sslpassword, matched case-insensitively, and returns the input unchanged when it carries no credential at all. Also makes the unparseable-input fallback say so, since the previous <redacted> read as though the URI had been empty.
|
Thanks @AmaadMartin, the 300k-input differential run and the sweep for other sites are more than I expected and both were useful. All four points addressed in 065210c. Query-string credentials: closed rather than documented out of scope, with the normalization tradeoff measured and written up in the thread. Unparseable fallback: the placeholder now names itself, so the message no longer reads as an empty URI. Stale description: rewritten. It said "both error sites" from before the third one was found, and it did not mention the sweep result or the query-parameter coverage. CI: merged Locally on the merged tree: @kalenkevich this is a fork PR again, so the workflow runs on 065210c are sitting in "awaiting approval". Could you approve them when you have a moment? |
…rs (google#602) * fix(sessions): redact connection-URI password in unsupported-URI errors getSessionServiceFromUri and getConnectionOptionsFromUri interpolated the raw connection URI into their "Unsupported ... URI" error messages. A URI such as postgres://user:password@host/db embeds the password in its userinfo, so an unsupported or mistyped scheme surfaced the password verbatim in a thrown Error, which typically reaches application logs and error-tracking services. Add a redactUriPassword helper that masks the userinfo password (keeping the rest of the URI for debugging, like Go's net/url.URL.Redacted) and use it at both sites. * fix(sessions): avoid secretlint false positive in redact_uri test secretlint's database-connection-string rule flags the literal postgres:// URI in the test as a real credential because "s3cr3t" isn't on its placeholder allowlist. Use "pass", matching the convention already used by the sibling operations_test.ts fixtures. * style: run prettier on redact_uri.ts format:check only surfaced this now that the earlier secretlint failure stopped masking it — the ternary in the catch branch wasn't prettier-formatted. * fix(artifacts): redact connection-URI password in unsupported-URI error getArtifactServiceFromUri had the same unredacted-URI-in-error-message gap as the two sessions/ call sites fixed earlier in this PR, missed by the original sweep. An artifact store URI (e.g. s3://key:secret@bucket) carries credentials in userinfo just as readily as a session connection string. * fix(sessions): redact credentials passed as connection-URI query parameters A credential can reach the same error paths as a query parameter rather than in the userinfo component, for example postgres://user@host/db?password=secret, a form several drivers accept. url.password only covers userinfo, so those were returned verbatim. Masks password, passwd, pwd and sslpassword, matched case-insensitively, and returns the input unchanged when it carries no credential at all. Also makes the unparseable-input fallback say so, since the previous <redacted> read as though the URI had been empty.
…RI log (#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 #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]>
…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]>
getSessionServiceFromUri(sessions/registry.ts),getConnectionOptionsFromUri(sessions/db/operations.ts) andgetArtifactServiceFromUri(artifacts/registry.ts) interpolate the raw connection URI into their "Unsupported ... URI" error messages. A connection URI such aspostgres://user:password@host/dbcarries the password in its userinfo component, so an unsupported or mistyped scheme surfaces the password verbatim in the thrown Error, which typically propagates to application logs and error-tracking services, a different trust boundary from whoever provisioned the connection string. Those three throws are the complete set of sites with this shape incore/srcanddev/src.This adds a
redactUriPasswordhelper that masks the credential while keeping the rest of the URI intact for debugging, mirroring the semantics of Gonet/url.URL.Redactedand therender_as_string(hide_password=True)redaction already used in the adk-pythonDatabaseSessionService, and applies it at all three error sites.The same credential can also arrive as a query parameter,
postgres://user@host/db?password=secret, a form several of the guarded drivers accept and which reaches the same error path.url.passwordcovers only the userinfo, sopassword,passwd,pwdandsslpasswordare matched case-insensitively in the query string as well. A URI carrying no credential in either position is returned unchanged, byte for byte.Unparseable inputs fall back to the scheme prefix plus
<unparseable URI, redacted>, so a credential in a non-URL string is not leaked and the resulting message does not read as though the URI had been empty.Unit tests cover both credential positions, the case-insensitive match, non-secret parameters surviving intact, and the unparseable fallback. Regression tests cover all three call sites, asserting the secret is absent from the thrown message rather than asserting the formatting.