Skip to content
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

fix(parseURL, hasProtocol, isScriptProtocol): ignore leading whitespaces #170

Merged
merged 20 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ export interface ParsedHost {
* @returns A parsed URL object.
*/
export function parseURL(input = "", defaultProto?: string): ParsedURL {
const dataMatch = input.match(/^(data:|blob:)/);
const dataMatch = input.match(/^(blob|data|javascript|vbscript):/);
pi0 marked this conversation as resolved.
Show resolved Hide resolved
if (dataMatch) {
const proto = dataMatch[1];
const proto = dataMatch[0];
return {
protocol: proto,
pathname: input.slice(proto.length),
Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export function isRelative(inputString: string) {
return ["./", "../"].some((string_) => inputString.startsWith(string_));
}

const PROTOCOL_STRICT_REGEX = /^\w{2,}:([/\\]{1,2})/;
const PROTOCOL_REGEX = /^\w{2,}:([/\\]{2})?/;
const PROTOCOL_STRICT_REGEX = /^[\s\w\0]{2,}:([/\\]{1,2})/;
const PROTOCOL_REGEX = /^[\s\w\0]{2,}:([/\\]{2})?/;
const PROTOCOL_RELATIVE_REGEX = /^([/\\]\s*){2,}[^/\\]/;

export interface HasProtocolOptions {
Expand Down
12 changes: 12 additions & 0 deletions test/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ describe("parseURL", () => {
search: "?url=http://0.0.0.0/2.svg",
},
},
{
input: "javascript:alert('hello')",
out: {
protocol: "javascript:",
auth: "",
host: "",
href: "javascript:alert('hello')",
pathname: "alert('hello')",
search: "",
hash: "",
},
},
{
input: "https://domain.test:3000#owo",
out: {
Expand Down
3 changes: 3 additions & 0 deletions test/utilities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ describe("hasProtocol", () => {

// Has protocol (non strict)
{ input: "tel:", out: [true, false, true] },
{ input: "javascript:alert(true)", out: [true, false, true] },
{ input: " javascript:alert(true)", out: [true, false, true] },
{ input: "\0javascript:alert(true)", out: [true, false, true] },
{ input: "tel:123456", out: [true, false, true] },
{ input: "mailto:support@example.com", out: [true, false, true] },

Expand Down