Skip to content

url.parse does not strip leading C0 control characters, returning protocol: null for '\x00javascript:...' #79

Open

Description

@trackoor

In url 0.11.4 (and master at 455a3e2, as well as the bundled copy in node-stdlib-browser 1.3.1), url.parse() uses String.prototype.trim() before matching the scheme, which does not strip C0 control characters (U+0000–U+001F other than \t\n\v\f\r). When a URL starts with a C0 control character, url.parse() fails to recognize the scheme or host (returning protocol: null and host: null), whereas both Node.js node:url and browsers (WHATWG URL) strip leading C0 controls and parse the scheme/host normally—allowing javascript: URI or host-check sanitizers built on url.parse() in browser bundles to be bypassed.

const url = require('url/');
const nodeUrl = require('node:url');

console.log(url.parse('\x00javascript:alert(1)').protocol);     // null
console.log(url.parse('\x1fjavascript:alert(1)').protocol);     // null
console.log(url.parse('\x00\x00http://evil.com/').host);        // null

console.log(nodeUrl.parse('\x00javascript:alert(1)').protocol); // 'javascript:'
console.log(nodeUrl.parse('\x00\x00http://evil.com/').host);    // 'evil.com'
console.log(new URL('\x00javascript:alert(1)').protocol);       // 'javascript:'

Expected: url.parse('\x00javascript:alert(1)').protocol returns 'javascript:' and url.parse('\x00\x00http://evil.com/').host returns 'evil.com', matching Node.js url.parse and browser URL parsing.

Actual: url.parse('\x00javascript:alert(1)').protocol returns null, url.parse('\x1fjavascript:alert(1)').protocol returns null, and url.parse('\x00\x00http://evil.com/').host returns null.

Reference: defunctzombie/node-url README states the module is meant to have feature parity with Node.js core url, which strips leading and trailing C0 controls and spaces per nodejs/node#42196 and the WHATWG URL Standard (URL parsing).

In url.js (Url.prototype.parse), rest = rest.trim() only strips ECMAScript whitespace, leaving leading C0 controls in rest so protocolPattern (/^([a-z0-9.+-]+:)/i) fails to match; stripping leading and trailing code points <= 0x20 (as in nodejs/node#42196) resolves the mismatch.

Credit: https://ucsbplse.github.io/

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions