Repository navigation
fix: apply script protocol guard to navigateTo open option
#1930
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
Open
wattanx
wants to merge
1
commit into
main
Choose a base branch
from
fix/navigate-to-script-protocol
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { isScriptProtocol } from 'ufo' | ||
|
|
||
| const PROBE_BASE_A = 'https://a.invalid/probe/' | ||
| const PROBE_BASE_B = 'https://b.invalid/probe/' | ||
|
|
||
| function parseAnchorURL (value: string, base: string = PROBE_BASE_A): URL | null { | ||
| try { | ||
| return new URL(value, base) | ||
| } catch { | ||
| return null | ||
| } | ||
| } | ||
|
|
||
| // a relative value inherits whichever base it resolves against; an absolute one ignores both | ||
| function resolvesWithoutBase (value: string): boolean { | ||
| const resolved = parseAnchorURL(value) | ||
| return !!resolved && resolved.href === parseAnchorURL(value, PROBE_BASE_B)?.href | ||
| } | ||
|
|
||
| /** Whether a URL string is a path rooted at the current origin, so carries no protocol. */ | ||
| export function isRootedPath (value: string): boolean { | ||
| if (value[0] !== '/' || value[1] === '/' || value[1] === '\\') { | ||
| return false | ||
| } | ||
| // the URL parser drops ASCII tab and newline, which can join the leading `/` to a host | ||
| return (value[1] !== '\t' && value[1] !== '\n' && value[1] !== '\r') || !resolvesWithoutBase(value) | ||
| } | ||
|
|
||
| /** Whether a URL string carries its own protocol or authority. */ | ||
| export function isAbsoluteHref (value: string): boolean { | ||
| return !!value && !isRootedPath(value) && resolvesWithoutBase(value) | ||
| } | ||
|
|
||
| /** The script-capable protocol a URL string would navigate to, or `null` when it is safe. */ | ||
| export function getScriptProtocol (value: string): string | null { | ||
| if (!value || isRootedPath(value)) { | ||
| return null | ||
| } | ||
| // browser URL parsers ignore whitespace and control characters around a scheme | ||
| // eslint-disable-next-line no-control-regex | ||
| let resolved = parseAnchorURL(value.replace(/[\u0000-\u001F\u007F\s]+/g, '')) | ||
| // Chromium resolves `view-source:` transparently to the inner URL | ||
| while (resolved?.protocol === 'view-source:') { | ||
| resolved = parseAnchorURL(resolved.pathname) | ||
| } | ||
| if (!resolved) { | ||
| return '' | ||
| } | ||
| return isScriptProtocol(resolved.protocol) ? resolved.protocol : null | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { getScriptProtocol, isAbsoluteHref, isRootedPath } from '../src/runtime/utils/url' | ||
|
|
||
| describe('isRootedPath', () => { | ||
| it.each([ | ||
| '/', | ||
| '/foo/bar?a=1#b', | ||
| '/\tfoo', | ||
| '/\n', | ||
| '/\t/', | ||
| '/foo\t//bar' | ||
| ])('should treat %j as rooted', (value) => { | ||
| expect(isRootedPath(value)).toBe(true) | ||
| }) | ||
|
|
||
| it.each([ | ||
| '', | ||
| 'foo', | ||
| '//evil.com', | ||
| '/\\evil.com', | ||
| '/\t/evil.com', | ||
| '/\n/evil.com', | ||
| '/\r/evil.com', | ||
| '/\t\\evil.com', | ||
| '/\t\r\n/evil.com' | ||
| ])('should not treat %j as rooted', (value) => { | ||
| expect(isRootedPath(value)).toBe(false) | ||
| }) | ||
| }) | ||
|
|
||
| describe('isAbsoluteHref', () => { | ||
| it.each([ | ||
| '', | ||
| '/', | ||
| '/foo/bar?a=1#b', | ||
| '/\tfoo', | ||
| '/foo:bar', | ||
| '#hash', | ||
| 'foo', | ||
| './foo', | ||
| '../foo', | ||
| 'foo bar:baz' | ||
| ])('should treat %j as relative', (value) => { | ||
| expect(isAbsoluteHref(value)).toBe(false) | ||
| }) | ||
|
|
||
| it.each([ | ||
| 'https://example.com', | ||
| 'HTTP://example.com', | ||
| 'mailto:a@b.c', | ||
| '//evil.com', | ||
| '\\\\evil.com', | ||
| '/\\evil.com', | ||
| '/\t/evil.com', | ||
| '/\n\\evil.com', | ||
| 'javascript:alert(1)', | ||
| '\u0001javascript:alert(1)', | ||
| 'java\tscript:alert(1)', | ||
| 'c:\\windows' | ||
| ])('should treat %j as absolute', (value) => { | ||
| expect(isAbsoluteHref(value)).toBe(true) | ||
| }) | ||
| }) | ||
|
|
||
| describe('getScriptProtocol', () => { | ||
| it.each([ | ||
| 'javascript:alert(1)', | ||
| 'JaVaScRiPt:alert(1)', | ||
| '\u0001javascript:alert(1)', | ||
| '\u0000javascript:alert(1)', | ||
| '\tjavascript:alert(1)', | ||
| ' javascript:alert(1)', | ||
| 'java\tscript:alert(1)', | ||
| 'data:text/html,<script>alert(1)</script>', | ||
| 'vbscript:alert(1)', | ||
| 'blob:https://example.com/x', | ||
| 'view-source:javascript:alert(1)', | ||
| '\u0001view-source:\u0001javascript:alert(1)' | ||
| ])('should reject %j', (value) => { | ||
| expect(getScriptProtocol(value)).not.toBeNull() | ||
| }) | ||
|
|
||
| it.each([ | ||
| '', | ||
| '/foo/bar?a=1#b', | ||
| '#hash', | ||
| '/view-source:javascript:alert(1)', | ||
| '%01javascript:alert(1)', | ||
| 'https://example.com', | ||
| 'mailto:a@b.c', | ||
| 'tel:123', | ||
| 'about:blank' | ||
| ])('should allow %j', (value) => { | ||
| expect(getScriptProtocol(value)).toBeNull() | ||
| }) | ||
| }) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π― Functional Correctness | π‘ Minor | β‘ Quick win
Classify HTTPS scheme-relative URLs on HTTP sites.
For
https:evil.exampleon an HTTP site, the browser resolves the input as an absolute HTTPS URL. Both HTTPS probes resolve it relative to their own hosts, soisAbsoluteHrefreturnsfalse.navigateTothen sends the input torouter.pushinstead of requiring{ external: true }. Detect an explicit scheme independently of the probe bases, and add this case to the classifier tests. (url.spec.whatwg.org)π€ Prompt for AI Agents