-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix(cli): redact URL query secrets in the error envelope #2488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+177
−1
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import { redactUrlQuery } from "@inspector/core/mcp/fetchTracking.js"; | ||
| import { awaitableError } from "./utils/awaitable-log.js"; | ||
|
|
||
| /** | ||
|
|
@@ -138,14 +139,77 @@ function statusOf(error: unknown): number | undefined { | |
| const UNREACHABLE_PATTERN = | ||
| /ENOTFOUND|ECONNREFUSED|ECONNRESET|EAI_AGAIN|ETIMEDOUT|fetch failed|getaddrinfo|connect(?:ion)? timed out|aborted/i; | ||
|
|
||
| /** | ||
| * An `http(s)://` URL embedded in free text. Stops at whitespace, at the | ||
| * double-quote/angle-bracket characters that commonly delimit a URL inside a | ||
| * message, and where a second `http(s)://` begins — so two URLs joined by a | ||
| * comma are redacted separately rather than the second one's query being read | ||
| * as part of the first one's last value (Copilot). An apostrophe is kept in the | ||
| * match because it is legal inside a query value; a *trailing* one is peeled | ||
| * off as punctuation below, which still handles a `'…'`-quoted URL. | ||
| * Case-insensitive because URI schemes are: `HTTPS://…?code=…` is the same | ||
| * URL and must not slip past the redaction (Copilot). | ||
| */ | ||
| const EMBEDDED_URL_PATTERN = /\bhttps?:\/\/(?:(?!https?:\/\/)[^\s"<>])+/gi; | ||
|
|
||
| /** Sentence punctuation (or a closing quote) a message may put right after a URL. */ | ||
| const TRAILING_PUNCTUATION = /[.,;:!?)\]']+$/; | ||
|
|
||
| /** | ||
| * Apply {@link redactUrlQuery} to every URL embedded in `text`. Trailing | ||
| * sentence punctuation is split off first and re-appended, so a URL ending a | ||
| * sentence (`…?code=abc.`) keeps its full stop instead of having it folded into | ||
| * the redacted parameter value. | ||
| */ | ||
| function redactUrlsInText(text: string): string { | ||
| return text.replace(EMBEDDED_URL_PATTERN, (match) => { | ||
| const trailing = TRAILING_PUNCTUATION.exec(match)?.[0] ?? ""; | ||
| const url = match.slice(0, match.length - trailing.length); | ||
| return redactUrlQuery(url) + trailing; | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Scrub query-string secrets out of every envelope field that can carry a URL | ||
| * (#2423). The envelope is written verbatim to stderr — a terminal, a CI log, | ||
| * a pipe into another tool — so it gets the same {@link redactUrlQuery} | ||
| * guarantee the web client's Network log and `OAuthRequestTimeoutError` | ||
| * already have: an OAuth `code`, `access_token` or `client_secret` in a server | ||
| * URL is replaced, while the path and non-sensitive parameters stay readable. | ||
| * | ||
| * Applied to the finished envelope rather than to the inputs, so | ||
| * classification still reads the error's own text: redaction rewrites | ||
| * parameter values, and a pattern test run on the rewritten copy could land a | ||
| * different exit code. | ||
| */ | ||
| function redactEnvelope(envelope: ErrorEnvelope): ErrorEnvelope { | ||
| return { | ||
| ...envelope, | ||
| message: redactUrlsInText(envelope.message), | ||
| ...(envelope.cause !== undefined && { | ||
| cause: redactUrlsInText(envelope.cause), | ||
| }), | ||
| ...(envelope.url !== undefined && { url: redactUrlQuery(envelope.url) }), | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Declined, with no change.
|
||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Classify an arbitrary error into an exit code and envelope. Used both by the | ||
| * binary's {@link handleError} and by callers that want to throw a | ||
| * {@link CliExitCodeError} with the right code up front. | ||
| * {@link CliExitCodeError} with the right code up front. Every URL in the | ||
| * returned envelope is query-redacted (see {@link redactEnvelope}). | ||
| */ | ||
| export function classifyError( | ||
| error: unknown, | ||
| context?: { url?: string }, | ||
| ): { exitCode: number; envelope: ErrorEnvelope } { | ||
| const { exitCode, envelope } = classifyUnredacted(error, context); | ||
| return { exitCode, envelope: redactEnvelope(envelope) }; | ||
| } | ||
|
|
||
| function classifyUnredacted( | ||
| error: unknown, | ||
| context?: { url?: string }, | ||
| ): { exitCode: number; envelope: ErrorEnvelope } { | ||
| const message = | ||
| error instanceof Error | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.