|
1 | 1 | /** ATTN: SECURITY RESEARCHERS
|
2 | 2 | * To Security researchers about to submit an SSRF report CVE - please don't.
|
3 | 3 | * We are aware that the code below is does not defend against any of the thousands of ways
|
4 |
| - * you can map a hostname to another IP. The code below does not have intention of blocking this |
| 4 | + * you can map a hostname to another IP via tunneling, hosts editing, etc. The code below does not have intention of blocking this |
5 | 5 | * and is simply to prevent the user from accidentally putting in non-valid websites, which is all this protects
|
6 | 6 | * since _all urls must be submitted by the user anyway_ and cannot be done with authentication and manager or admin roles.
|
7 | 7 | * If an attacker has those roles then the system is already vulnerable and this is not a primary concern.
|
|
14 | 14 | const VALID_PROTOCOLS = ["https:", "http:"];
|
15 | 15 | const INVALID_OCTETS = [192, 172, 10, 127];
|
16 | 16 |
|
| 17 | +/** |
| 18 | + * If an ip address is passed in the user is attempting to collector some internal service running on internal/private IP. |
| 19 | + * This is not a security feature and simply just prevents the user from accidentally entering invalid IP addresses. |
| 20 | + * @param {URL} param0 |
| 21 | + * @param {URL['hostname']} param0.hostname |
| 22 | + * @returns {boolean} |
| 23 | + */ |
17 | 24 | function isInvalidIp({ hostname }) {
|
18 | 25 | const IPRegex = new RegExp(
|
19 | 26 | /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/gi
|
20 | 27 | );
|
| 28 | + |
| 29 | + // Not an IP address at all - passthrough |
21 | 30 | if (!IPRegex.test(hostname)) return false;
|
22 | 31 | const [octetOne, ..._rest] = hostname.split(".");
|
23 | 32 |
|
24 | 33 | // If fails to validate to number - abort and return as invalid.
|
25 | 34 | if (isNaN(Number(octetOne))) return true;
|
| 35 | + |
| 36 | + // Allow localhost loopback and 0.0.0.0 for scraping convenience |
| 37 | + // for locally hosted services or websites |
| 38 | + if (["127.0.0.1", "0.0.0.0"].includes(hostname)) return false; |
| 39 | + |
26 | 40 | return INVALID_OCTETS.includes(Number(octetOne));
|
27 | 41 | }
|
28 | 42 |
|
|
0 commit comments