-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #187 from salesforce/new-embed-ip-regex
New embed ip regex
- Loading branch information
Showing
3 changed files
with
99 additions
and
58 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -32,7 +32,6 @@ | |
const punycode = require("punycode"); | ||
const urlParse = require("url").parse; | ||
const util = require("util"); | ||
const ipRegex = require("ip-regex")({ exact: true }); | ||
const pubsuffix = require("./pubsuffix-psl"); | ||
const Store = require("./store").Store; | ||
const MemoryCookieStore = require("./memstore").MemoryCookieStore; | ||
|
@@ -94,6 +93,12 @@ const PrefixSecurityEnum = Object.freeze({ | |
DISABLED: "unsafe-disabled" | ||
}); | ||
|
||
// Dumped from [email protected], with the following changes: | ||
// * all capturing groups converted to non-capturing -- "(?:)" | ||
// * support for IPv6 Scoped Literal ("%eth1") removed | ||
// * lowercase hexadecimal only | ||
var IP_REGEX_LOWERCASE =/(?:^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$)|(?:^(?:(?:[a-f\d]{1,4}:){7}(?:[a-f\d]{1,4}|:)|(?:[a-f\d]{1,4}:){6}(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|:[a-f\d]{1,4}|:)|(?:[a-f\d]{1,4}:){5}(?::(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-f\d]{1,4}){1,2}|:)|(?:[a-f\d]{1,4}:){4}(?:(?::[a-f\d]{1,4}){0,1}:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-f\d]{1,4}){1,3}|:)|(?:[a-f\d]{1,4}:){3}(?:(?::[a-f\d]{1,4}){0,2}:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-f\d]{1,4}){1,4}|:)|(?:[a-f\d]{1,4}:){2}(?:(?::[a-f\d]{1,4}){0,3}:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-f\d]{1,4}){1,5}|:)|(?:[a-f\d]{1,4}:){1}(?:(?::[a-f\d]{1,4}){0,4}:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-f\d]{1,4}){1,6}|:)|(?::(?:(?::[a-f\d]{1,4}){0,5}:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-f\d]{1,4}){1,7}|:)))$)/; | ||
|
||
/* | ||
* Parses a Natural number (i.e., non-negative integer) with either the | ||
* <min>*<max>DIGIT ( non-digit *OCTET ) | ||
|
@@ -325,38 +330,42 @@ function domainMatch(str, domStr, canonicalize) { | |
} | ||
|
||
/* | ||
* "The domain string and the string are identical. (Note that both the | ||
* S5.1.3: | ||
* "A string domain-matches a given domain string if at least one of the | ||
* following conditions hold:" | ||
* | ||
* " o The domain string and the string are identical. (Note that both the | ||
* domain string and the string will have been canonicalized to lower case at | ||
* this point)" | ||
*/ | ||
if (str == domStr) { | ||
return true; | ||
} | ||
|
||
/* "All of the following [three] conditions hold:" (order adjusted from the RFC) */ | ||
|
||
/* "* The string is a host name (i.e., not an IP address)." */ | ||
if (ipRegex.test(str)) { | ||
return false; | ||
} | ||
/* " o All of the following [three] conditions hold:" */ | ||
|
||
/* "* The domain string is a suffix of the string" */ | ||
const idx = str.indexOf(domStr); | ||
if (idx <= 0) { | ||
return false; // it's a non-match (-1) or prefix (0) | ||
} | ||
|
||
// e.g "a.b.c".indexOf("b.c") === 2 | ||
// next, check it's a proper suffix | ||
// e.g., "a.b.c".indexOf("b.c") === 2 | ||
// 5 === 3+2 | ||
if (str.length !== domStr.length + idx) { | ||
// it's not a suffix | ||
return false; | ||
return false; // it's not a suffix | ||
} | ||
|
||
/* " * The last character of the string that is not included in the | ||
* domain string is a %x2E (".") character." */ | ||
if (str.substr(idx-1,1) !== '.') { | ||
return false; // doesn't align on "." | ||
} | ||
|
||
/* "* The last character of the string that is not included in the domain | ||
* string is a %x2E (".") character." */ | ||
if (str.substr(idx - 1, 1) !== ".") { | ||
return false; | ||
/* " * The string is a host name (i.e., not an IP address)." */ | ||
if (IP_REGEX_LOWERCASE.test(str)) { | ||
return false; // it's an IP address | ||
} | ||
|
||
return true; | ||
|
This file contains 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 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