Skip to content

Chrome DevTools for coding agents

fix(url): unwrap view-source: before all scheme checks in validateUrl (#3335)

Abhijith S7275268bf052
diff --git a/src/utils/url.ts b/src/utils/url.ts
index 1d0b5203..b0024d00 100644
--- a/src/utils/url.ts
+++ b/src/utils/url.ts
@@ -235,32 +235,43 @@ export function validateUrlPatterns(
}
/**
- * Reports whether a URL ultimately targets the `file:` scheme.
+ * Unwraps nested `view-source:` prefixes so every scheme check below runs
+ * against the URL Chrome will actually resolve and load — not the
+ * wrapper. `view-source:` wraps another URL and Chrome resolves the inner
+ * target (potentially through multiple levels of wrapping), so
+ * `view-source:file:///etc/passwd` reads the file just as
+ * `file:///etc/passwd` does, and `view-source:devtools://devtools/...`
+ * loads under the `devtools://devtools` origin just as the bare
+ * `devtools:` URL would. The inner URL is re-parsed rather than
+ * string-matched so that an uppercase inner scheme
+ * (`view-source:FILE:///etc/passwd`) is normalised.
*
- * `view-source:` wraps another URL and Chrome resolves the inner target, so
- * `view-source:file:///etc/passwd` reads the file just as `file:///etc/passwd`
- * does. The inner URL is re-parsed rather than string-matched so that an
- * uppercase inner scheme (`view-source:FILE:///etc/passwd`) is normalised.
- *
- * @param url The already-parsed URL to inspect.
- * @returns true if the URL, after unwrapping any `view-source:` prefixes, uses `file:`.
+ * @param url The already-parsed URL to unwrap.
+ * @returns The fully unwrapped URL, or the original URL unchanged if it
+ * is not `view-source:`. Returns null if any wrapped layer is not itself
+ * a valid URL.
*/
-function targetsFileScheme(url: URL): boolean {
+function unwrapViewSource(url: URL): URL | null {
let current = url;
// Bounded in case of a pathologically nested `view-source:` chain.
for (let i = 0; i < 5 && current.protocol === 'view-source:'; i++) {
try {
current = new URL(current.pathname);
} catch {
- return false;
+ return null;
}
}
- return current.protocol === 'file:';
+ return current.protocol === 'view-source:' ? null : current;
}
/**
* Validates a URL string by parsing it with `new URL` and checking for disallowed protocols and restricted schemes.
*
+ * Any `view-source:` wrapping is unwrapped first (see {@link unwrapViewSource}),
+ * since Chrome resolves the wrapper at navigation time and loads the page
+ * under the wrapped URL's own origin, so every scheme check below must run
+ * against the unwrapped target rather than the outer `view-source:` URL.
+ *
* @param url The URL string to validate.
* @param options Options object containing javascriptEvaluation, categoryExtensions and fileNavigations.
* @returns The parsed URL.
@@ -279,32 +290,39 @@ export function validateUrl(url: string, options: ValidateUrlOptions): URL {
);
}
+ const target = unwrapViewSource(parsed);
+ if (!target) {
+ throw new Error(
+ `Invalid URL: "${url}". The view-source: wrapped content is not a valid URL.`,
+ );
+ }
+
if (
javascriptEvaluation === false &&
- DISALLOWED_PROTOCOLS.has(parsed.protocol)
+ DISALLOWED_PROTOCOLS.has(target.protocol)
) {
throw new Error(
- `Navigating to ${parsed.protocol} URLs is not allowed when JavaScript evaluation is disabled.`,
+ `Navigating to ${target.protocol} URLs is not allowed when JavaScript evaluation is disabled.`,
);
}
- if (fileNavigations === false && targetsFileScheme(parsed)) {
+ if (fileNavigations === false && target.protocol === 'file:') {
throw new Error(
`Navigating to file: URLs is not allowed when --file-navigations is disabled.`,
);
}
- if (parsed.protocol === 'devtools:') {
+ if (target.protocol === 'devtools:') {
throw new Error(`Navigating to devtools: URLs is not allowed.`);
}
- if (!isAllowedUrl(url, {categoryExtensions})) {
- if (parsed.protocol === 'chrome-extension:') {
+ if (!isAllowedUrl(target.href, {categoryExtensions})) {
+ if (target.protocol === 'chrome-extension:') {
throw new Error(
`Navigating to chrome-extension: URLs is not allowed without --categoryExtensions.`,
);
}
- throw new Error(`Navigating to ${parsed.protocol} URLs is not allowed.`);
+ throw new Error(`Navigating to ${target.protocol} URLs is not allowed.`);
}
return parsed;
diff --git a/tests/utils/url.test.ts b/tests/utils/url.test.ts
index 3ec1d9a4..3a702b1e 100644
--- a/tests/utils/url.test.ts
+++ b/tests/utils/url.test.ts
@@ -438,6 +438,104 @@ describe('validateUrl', () => {
);
});
+ it('should reject view-source nesting deeper than the unwrap limit', () => {
+ const noFileNavigations = {
+ javascriptEvaluation: undefined,
+ categoryExtensions: undefined,
+ fileNavigations: false,
+ };
+ const notValid = /The view-source: wrapped content is not a valid URL\./;
+ // Past the unwrap limit the target can no longer be inspected, so the
+ // validator must fail closed instead of checking the outer wrapper.
+ assert.throws(
+ () =>
+ validateUrl(
+ 'view-source:'.repeat(6) + 'file:///etc/passwd',
+ noFileNavigations,
+ ),
+ notValid,
+ );
+ assert.throws(
+ () =>
+ validateUrl(
+ 'view-source:'.repeat(6) + 'chrome://settings',
+ noFileNavigations,
+ ),
+ notValid,
+ );
+ // Exactly at the limit still unwraps to the real target.
+ assert.throws(
+ () =>
+ validateUrl(
+ 'view-source:'.repeat(5) + 'file:///etc/passwd',
+ noFileNavigations,
+ ),
+ /Navigating to file: URLs is not allowed when --file-navigations is disabled\./,
+ );
+ });
+
+ it('should reject view-source URLs whose wrapped content is not a valid URL', () => {
+ assert.throws(
+ () =>
+ validateUrl('view-source:example.com', {
+ javascriptEvaluation: undefined,
+ categoryExtensions: undefined,
+ fileNavigations: false,
+ }),
+ /The view-source: wrapped content is not a valid URL\./,
+ );
+ });
+
+ it('should reject view-source URLs that target devtools:, chrome:, chrome-untrusted:, and chrome-extension:', () => {
+ // These previously bypassed validateUrl entirely: the devtools:/chrome:
+ // checks were evaluated against the outer `view-source:` protocol
+ // instead of the wrapped target, even though Chrome strips the
+ // `view-source:` prefix at navigation time and loads the page under
+ // the wrapped URL's own origin.
+ assert.throws(
+ () =>
+ validateUrl(
+ 'view-source:devtools://devtools/bundled/devtools_app.html',
+ defaultOptions,
+ ),
+ /Navigating to devtools: URLs is not allowed\./,
+ );
+ assert.throws(
+ () => validateUrl('view-source:chrome://settings', defaultOptions),
+ /Navigating to chrome: URLs is not allowed\./,
+ );
+ // chrome://inspect is intentionally allowlisted by isAllowedUrl, so
+ // unwrapping view-source: and reaching chrome://inspect is expected
+ // to succeed, matching the direct chrome://inspect behavior above.
+ assert.strictEqual(
+ validateUrl('view-source:chrome://inspect', defaultOptions).protocol,
+ 'view-source:',
+ );
+ assert.throws(
+ () =>
+ validateUrl('view-source:chrome-untrusted://terminal', defaultOptions),
+ /Navigating to chrome-untrusted: URLs is not allowed\./,
+ );
+ assert.throws(
+ () =>
+ validateUrl(
+ 'view-source:chrome-extension://abcdef/popup.html',
+ defaultOptions,
+ ),
+ /Navigating to chrome-extension: URLs is not allowed without --categoryExtensions\./,
+ );
+ // Still rejected even with categoryExtensions enabled, mirroring the
+ // bare chrome-extension: behavior being unaffected by the unwrap.
+ assert.strictEqual(
+ validateUrl('view-source:chrome-extension://abcdef/popup.html', {
+ javascriptEvaluation: undefined,
+ categoryExtensions: true,
+ fileNavigations: undefined,
+ }).protocol,
+ 'view-source:',
+ );
+ });
+
it('should not reject view-source URLs that target other schemes', () => {
const noFileNavigations = {
javascriptEvaluation: undefined,