Skip to content

Commit cbef508

Browse files
theofidrymanuelMarkDenverscottgigante-hubflowhenri-extravagantWikiRik
authored
fix(isURL): improve protocol detection. Resolves CVE-2025-56200 (#2608)
Co-authored-by: Théo FIDRY <5175937+theofidry@users.noreply.github.com> Co-authored-by: manuelMarkDenver <manuel.markdenver@gmail.com> Co-authored-by: scottgigante-hubflow <scott@gethubflow.ai> Co-authored-by: Henri Holopainen <henri.holopainen@extravagant.limited> Co-authored-by: Rik Smale <13023439+WikiRik@users.noreply.github.com>
1 parent 6f436be commit cbef508

File tree

4 files changed

+178
-9
lines changed

4 files changed

+178
-9
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
runs-on: ubuntu-latest
1010
strategy:
1111
matrix:
12-
node-version: [22, 20, 18, 16, 14, 12, 10, 8, 6]
12+
node-version: [22, 20, 18, 16, 14, 12, 10, 8]
1313
name: Run tests on Node.js ${{ matrix.node-version }}
1414
steps:
1515
- name: Setup Node.js ${{ matrix.node-version }}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ Validator | Description
167167
**isStrongPassword(str [, options])** | check if the string can be considered a strong password or not. Allows for custom requirements or scoring rules. If `returnScore` is true, then the function returns an integer score for the password rather than a boolean.<br/>Default options: <br/>`{ minLength: 8, minLowercase: 1, minUppercase: 1, minNumbers: 1, minSymbols: 1, returnScore: false, pointsPerUnique: 1, pointsPerRepeat: 0.5, pointsForContainingLower: 10, pointsForContainingUpper: 10, pointsForContainingNumber: 10, pointsForContainingSymbol: 10 }`
168168
**isTime(str [, options])** | check if the string is a valid time e.g. [`23:01:59`, new Date().toLocaleTimeString()].<br/><br/> `options` is an object which can contain the keys `hourFormat` or `mode`.<br/><br/>`hourFormat` is a key and defaults to `'hour24'`.<br/><br/>`mode` is a key and defaults to `'default'`. <br/><br/>`hourFormat` can contain the values `'hour12'` or `'hour24'`, `'hour24'` will validate hours in 24 format and `'hour12'` will validate hours in 12 format. <br/><br/>`mode` can contain the values `'default', 'withSeconds', withOptionalSeconds`, `'default'` will validate `HH:MM` format, `'withSeconds'` will validate the `HH:MM:SS` format, `'withOptionalSeconds'` will validate `'HH:MM'` and `'HH:MM:SS'` formats.
169169
**isTaxID(str, locale)** | check if the string is a valid Tax Identification Number. Default locale is `en-US`.<br/><br/>More info about exact TIN support can be found in `src/lib/isTaxID.js`.<br/><br/>Supported locales: `[ 'bg-BG', 'cs-CZ', 'de-AT', 'de-DE', 'dk-DK', 'el-CY', 'el-GR', 'en-CA', 'en-GB', 'en-IE', 'en-US', 'es-AR', 'es-ES', 'et-EE', 'fi-FI', 'fr-BE', 'fr-CA', 'fr-FR', 'fr-LU', 'hr-HR', 'hu-HU', 'it-IT', 'lb-LU', 'lt-LT', 'lv-LV', 'mt-MT', 'nl-BE', 'nl-NL', 'pl-PL', 'pt-BR', 'pt-PT', 'ro-RO', 'sk-SK', 'sl-SI', 'sv-SE', 'uk-UA']`.
170-
**isURL(str [, options])** | check if the string is a URL.<br/><br/>`options` is an object which defaults to `{ protocols: ['http','https','ftp'], require_tld: true, require_protocol: false, require_host: true, require_port: false, require_valid_protocol: true, allow_underscores: false, host_whitelist: false, host_blacklist: false, allow_trailing_dot: false, allow_protocol_relative_urls: false, allow_fragments: true, allow_query_components: true, disallow_auth: false, validate_length: true }`.<br/><br/>`protocols` - valid protocols can be modified with this option.<br/>`require_tld` - If set to false isURL will not check if the URL's host includes a top-level domain.<br/>`require_protocol` - if set to true isURL will return false if protocol is not present in the URL.<br/>`require_host` - if set to false isURL will not check if host is present in the URL.<br/>`require_port` - if set to true isURL will check if port is present in the URL.<br/>`require_valid_protocol` - isURL will check if the URL's protocol is present in the protocols option.<br/>`allow_underscores` - if set to true, the validator will allow underscores in the URL.<br/>`host_whitelist` - if set to an array of strings or regexp, and the domain matches none of the strings defined in it, the validation fails.<br/>`host_blacklist` - if set to an array of strings or regexp, and the domain matches any of the strings defined in it, the validation fails.<br/>`allow_trailing_dot` - if set to true, the validator will allow the domain to end with a `.` character.<br/>`allow_protocol_relative_urls` - if set to true protocol relative URLs will be allowed.<br/>`allow_fragments` - if set to false isURL will return false if fragments are present.<br/>`allow_query_components` - if set to false isURL will return false if query components are present.<br/>`disallow_auth` - if set to true, the validator will fail if the URL contains an authentication component, e.g. `http://username:password@example.com`.<br/>`validate_length` - if set to false isURL will skip string length validation. `max_allowed_length` will be ignored if this is set as `false`.<br/>`max_allowed_length` - if set, isURL will not allow URLs longer than the specified value (default is 2084 that IE maximum URL length).<br/>
170+
**isURL(str [, options])** | check if the string is a URL.<br/><br/>`options` is an object which defaults to `{ protocols: ['http','https','ftp'], require_tld: true, require_protocol: false, require_host: true, require_port: false, require_valid_protocol: true, allow_underscores: false, host_whitelist: false, host_blacklist: false, allow_trailing_dot: false, allow_protocol_relative_urls: false, allow_fragments: true, allow_query_components: true, disallow_auth: false, validate_length: true }`.<br/><br/>`protocols` - valid protocols can be modified with this option.<br/>`require_tld` - If set to false isURL will not check if the URL's host includes a top-level domain.<br/>`require_protocol` - **RECOMMENDED** if set to true isURL will return false if protocol is not present in the URL. Without this setting, some malicious URLs cannot be distinguishable from a valid URL with authentication information.<br/>`require_host` - if set to false isURL will not check if host is present in the URL.<br/>`require_port` - if set to true isURL will check if port is present in the URL.<br/>`require_valid_protocol` - isURL will check if the URL's protocol is present in the protocols option.<br/>`allow_underscores` - if set to true, the validator will allow underscores in the URL.<br/>`host_whitelist` - if set to an array of strings or regexp, and the domain matches none of the strings defined in it, the validation fails.<br/>`host_blacklist` - if set to an array of strings or regexp, and the domain matches any of the strings defined in it, the validation fails.<br/>`allow_trailing_dot` - if set to true, the validator will allow the domain to end with a `.` character.<br/>`allow_protocol_relative_urls` - if set to true protocol relative URLs will be allowed.<br/>`allow_fragments` - if set to false isURL will return false if fragments are present.<br/>`allow_query_components` - if set to false isURL will return false if query components are present.<br/>`disallow_auth` - if set to true, the validator will fail if the URL contains an authentication component, e.g. `http://username:password@example.com`.<br/>`validate_length` - if set to false isURL will skip string length validation. `max_allowed_length` will be ignored if this is set as `false`.<br/>`max_allowed_length` - if set, isURL will not allow URLs longer than the specified value (default is 2084 that IE maximum URL length).<br/>
171171
**isULID(str)** | check if the string is a [ULID](https://github.com/ulid/spec).
172172
**isUUID(str [, version])** | check if the string is an RFC9562 UUID.<br/>`version` is one of `'1'`-`'8'`, `'nil'`, `'max'`, `'all'` or `'loose'`. The `'loose'` option checks if the string is a UUID-like string with hexadecimal values, ignoring RFC9565.
173173
**isVariableWidth(str)** | check if the string contains a mixture of full and half-width chars.

src/lib/isURL.js

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,94 @@ export default function isURL(url, options) {
8383
split = url.split('?');
8484
url = split.shift();
8585

86-
split = url.split('://');
87-
if (split.length > 1) {
88-
protocol = split.shift().toLowerCase();
86+
// Replaced the 'split("://")' logic with a regex to match the protocol.
87+
// This correctly identifies schemes like `javascript:` which don't use `//`.
88+
// However, we need to be careful not to confuse authentication credentials (user:password@host)
89+
// with protocols. A colon before an @ symbol might be part of auth, not a protocol separator.
90+
const protocol_match = url.match(/^([a-z][a-z0-9+\-.]*):/i);
91+
let had_explicit_protocol = false;
92+
93+
const cleanUpProtocol = (potential_protocol) => {
94+
had_explicit_protocol = true;
95+
protocol = potential_protocol.toLowerCase();
96+
8997
if (options.require_valid_protocol && options.protocols.indexOf(protocol) === -1) {
98+
// The identified protocol is not in the allowed list.
9099
return false;
91100
}
101+
102+
// Remove the protocol from the URL string.
103+
return url.substring(protocol_match[0].length);
104+
};
105+
106+
if (protocol_match) {
107+
const potential_protocol = protocol_match[1];
108+
const after_colon = url.substring(protocol_match[0].length);
109+
110+
// Check if what follows looks like authentication credentials (user:password@host)
111+
// rather than a protocol. This happens when:
112+
// 1. There's no `//` after the colon (protocols like `http://` have this)
113+
// 2. There's an `@` symbol before any `/`
114+
// 3. The part before `@` contains only valid auth characters (alphanumeric, -, _, ., %, :)
115+
const starts_with_slashes = after_colon.slice(0, 2) === '//';
116+
117+
if (!starts_with_slashes) {
118+
const first_slash_position = after_colon.indexOf('/');
119+
const before_slash = first_slash_position === -1
120+
? after_colon
121+
: after_colon.substring(0, first_slash_position);
122+
const at_position = before_slash.indexOf('@');
123+
124+
if (at_position !== -1) {
125+
const before_at = before_slash.substring(0, at_position);
126+
const valid_auth_regex = /^[a-zA-Z0-9\-_.%:]*$/;
127+
const is_valid_auth = valid_auth_regex.test(before_at);
128+
129+
if (is_valid_auth) {
130+
// This looks like authentication (e.g., user:password@host), not a protocol
131+
if (options.require_protocol) {
132+
return false;
133+
}
134+
135+
// Don't consume the colon; let the auth parsing handle it later
136+
} else {
137+
// This looks like a malicious protocol (e.g., javascript:alert();@host)
138+
url = cleanUpProtocol(potential_protocol);
139+
140+
if (url === false) {
141+
return false;
142+
}
143+
}
144+
} else {
145+
// No @ symbol, this is definitely a protocol
146+
url = cleanUpProtocol(potential_protocol);
147+
148+
if (url === false) {
149+
return false;
150+
}
151+
}
152+
} else {
153+
// Starts with '//', this is definitely a protocol like http://
154+
url = cleanUpProtocol(potential_protocol);
155+
156+
if (url === false) {
157+
return false;
158+
}
159+
}
92160
} else if (options.require_protocol) {
93161
return false;
94-
} else if (url.slice(0, 2) === '//') {
95-
if (!options.allow_protocol_relative_urls) {
162+
}
163+
164+
// Handle leading '//' only as protocol-relative when there was NO explicit protocol.
165+
// If there was an explicit protocol, '//' is the normal separator
166+
// and should be stripped unconditionally.
167+
if (url.slice(0, 2) === '//') {
168+
if (!had_explicit_protocol && !options.allow_protocol_relative_urls) {
96169
return false;
97170
}
98-
split[0] = url.slice(2);
171+
172+
url = url.slice(2);
99173
}
100-
url = split.join('://');
101174

102175
if (url === '') {
103176
return false;

test/validators.test.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,12 @@ describe('Validators', () => {
424424
'http://[2010:836B:4179::836B:4179]',
425425
'http://example.com/example.json#/foo/bar',
426426
'http://1337.com',
427+
// TODO: those probably should not be marked as valid URLs; CVE-2025-56200
428+
/* eslint-disable no-script-url */
429+
'javascript:%61%6c%65%72%74%28%31%29@example.com',
430+
'http://evil-site.com@example.com/',
431+
'javascript:alert(1)@example.com',
432+
/* eslint-enable no-script-url */
427433
],
428434
invalid: [
429435
'http://localhost:3000/',
@@ -466,6 +472,18 @@ describe('Validators', () => {
466472
'////foobar.com',
467473
'http:////foobar.com',
468474
'https://example.com/foo/<script>alert(\'XSS\')</script>/',
475+
// the following tests are because of CVE-2025-56200
476+
/* eslint-disable no-script-url */
477+
"javascript:alert(1);a=';@example.com/alert(1)'",
478+
'JaVaScRiPt:alert(1)@example.com',
479+
'javascript:/* comment */alert(1)@example.com',
480+
'javascript:var a=1; alert(a);@example.com',
481+
'javascript:alert(1)@user@example.com',
482+
'javascript:alert(1)@example.com?q=safe',
483+
'data:text/html,<script>alert(1)</script>@example.com',
484+
'vbscript:msgbox("XSS")@example.com',
485+
'//evil-site.com/path@example.com',
486+
/* eslint-enable no-script-url */
469487
],
470488
});
471489
});
@@ -478,9 +496,11 @@ describe('Validators', () => {
478496
}],
479497
valid: [
480498
'rtmp://foobar.com',
499+
'rtmp:foobar.com',
481500
],
482501
invalid: [
483502
'http://foobar.com',
503+
'tel:+15551234567',
484504
],
485505
});
486506
});
@@ -533,6 +553,9 @@ describe('Validators', () => {
533553
'rtmp://foobar.com',
534554
'http://foobar.com',
535555
'test://foobar.com',
556+
// Dangerous! This allows to mark malicious URLs as a valid URL (CVE-2025-56200)
557+
// eslint-disable-next-line no-script-url
558+
'javascript:alert(1);@example.com',
536559
],
537560
invalid: [
538561
'mailto:test@example.com',
@@ -704,6 +727,61 @@ describe('Validators', () => {
704727
});
705728
});
706729

730+
it('should validate authentication strings if a protocol is not required', () => {
731+
test({
732+
validator: 'isURL',
733+
args: [{
734+
require_protocol: false,
735+
}],
736+
valid: [
737+
'user:pw@foobar.com/',
738+
],
739+
invalid: [
740+
'user:pw,@foobar.com/',
741+
],
742+
});
743+
});
744+
745+
it('should reject authentication strings if a protocol is required', () => {
746+
test({
747+
validator: 'isURL',
748+
args: [{
749+
require_protocol: true,
750+
}],
751+
valid: [
752+
'http://user:pw@foobar.com/',
753+
'https://user:password@example.com',
754+
'ftp://admin:pass@ftp.example.com/',
755+
],
756+
invalid: [
757+
'user:pw@foobar.com/',
758+
'user:password@example.com',
759+
'admin:pass@ftp.example.com/',
760+
],
761+
});
762+
});
763+
764+
it('should reject invalid protocols when require_valid_protocol is enabled', () => {
765+
test({
766+
validator: 'isURL',
767+
args: [{
768+
require_valid_protocol: true,
769+
protocols: ['http', 'https', 'ftp'],
770+
}],
771+
valid: [
772+
'http://example.com',
773+
'https://example.com',
774+
'ftp://example.com',
775+
],
776+
invalid: [
777+
// eslint-disable-next-line no-script-url
778+
'javascript:alert(1);@example.com',
779+
'data:text/html,<script>alert(1)</script>@example.com',
780+
'file:///etc/passwd@example.com',
781+
],
782+
});
783+
});
784+
707785
it('should let users specify a host whitelist', () => {
708786
test({
709787
validator: 'isURL',
@@ -782,6 +860,24 @@ describe('Validators', () => {
782860
});
783861
});
784862

863+
it('GHSA-9965-vmph-33xx vulnerability - protocol delimiter parsing difference', () => {
864+
const DOMAIN_WHITELIST = ['example.com'];
865+
866+
test({
867+
validator: 'isURL',
868+
args: [{
869+
protocols: ['https'],
870+
host_whitelist: DOMAIN_WHITELIST,
871+
require_host: false,
872+
}],
873+
valid: [],
874+
invalid: [
875+
// eslint-disable-next-line no-script-url
876+
"javascript:alert(1);a=';@example.com/alert(1)",
877+
],
878+
});
879+
});
880+
785881
it('should allow rejecting urls containing authentication information', () => {
786882
test({
787883
validator: 'isURL',

0 commit comments

Comments
 (0)