Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions lib/web/fetch/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,17 +554,11 @@ function stripURLForReferrer (url, originOnly = false) {
return url
}

const potentialleTrustworthyIPv4RegExp = new RegExp('^(?:' +
'(?:127\\.)' +
'(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\\.){2}' +
'(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])' +
')$')

const potentialleTrustworthyIPv6RegExp = new RegExp('^(?:' +
'(?:(?:0{1,4}):){7}(?:(?:0{0,3}1))|' +
'(?:(?:0{1,4}):){1,6}(?::(?:0{0,3}1))|' +
'(?:::(?:0{0,3}1))|' +
')$')
const isPotentialleTrustworthyIPv4 = RegExp.prototype.test
.bind(/^127\.(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)\.){2}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)$/)

const isPotentiallyTrustworthyIPv6 = RegExp.prototype.test
.bind(/^(?:(?:0{1,4}:){7}|(?:0{1,4}:){1,6}:|::)0{0,3}1$/)

/**
* Check if host matches one of the CIDR notations 127.0.0.0/8 or ::1/128.
Expand All @@ -579,11 +573,11 @@ function isOriginIPPotentiallyTrustworthy (origin) {
if (origin[0] === '[' && origin[origin.length - 1] === ']') {
origin = origin.slice(1, -1)
}
return potentialleTrustworthyIPv6RegExp.test(origin)
return isPotentiallyTrustworthyIPv6(origin)
}

// IPv4
return potentialleTrustworthyIPv4RegExp.test(origin)
return isPotentialleTrustworthyIPv4(origin)
}

/**
Expand Down
11 changes: 8 additions & 3 deletions test/fetch/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ describe('isValidHeaderValue', () => {

describe('isOriginIPPotentiallyTrustworthy()', () => {
[
['', false],
['0000:0000:0000:0000:0000:0000:0000:0001', true],
['0001:0000:0000:0000:0000:0000:0000:0001', false],
['0000:0000:0000:0000:0000:0000::0001', true],
Expand All @@ -292,16 +293,20 @@ describe('isOriginIPPotentiallyTrustworthy()', () => {
['0000:0000:0000::0001', true],
['0000:0000::0001', true],
['0000::0001', true],
['::1001', false],
['::0001', true],
['::0011', false],
['::1', true],
['[::1]', true],
['[::1', false],
['::1]', false],
['::2', false],
['::', false],
['126.0.0.0', false],
['127.0.0.0', true],
['127.0.0.1', true],
['127.255.255.255', true],
['128.255.255.255', false],
['127.0.0.1', true],
['127.0.0.0', false]
['128.255.255.255', false]
].forEach(([ip, expected]) => {
test(`${ip} is ${expected ? '' : 'not '}potentially trustworthy`, () => {
assert.strictEqual(util.isOriginIPPotentiallyTrustworthy(ip), expected)
Expand Down
Loading