fix: restrict 127.* loopback matching to real IPv4 addresses
A prefix test also accepted DNS names like 127.attacker.example, which would pass both the devUrl eligibility check and the Host header guard (review finding from macroscope). Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
1 parent
03a9e682f1
commit
9faf5533eb
2 files changed
+25
-7
No files matched your search
@@ -36,6 +36,8 @@ describe("isLoopbackHostHeader", () => {
|
||||
assert.isFalse(isLoopbackHostHeader("evil.example"));
|
||||
assert.isFalse(isLoopbackHostHeader("evil.example:13773"));
|
||||
assert.isFalse(isLoopbackHostHeader("localhost.evil.example"));
|
||||
assert.isFalse(isLoopbackHostHeader("127.attacker.example"));
|
||||
assert.isFalse(isLoopbackHostHeader("127.attacker.example:13773"));
|
||||
assert.isFalse(isLoopbackHostHeader("10.0.0.5:13773"));
|
||||
assert.isFalse(isLoopbackHostHeader("[::2]:13773"));
|
||||
assert.isFalse(isLoopbackHostHeader(undefined));
|
||||
@@ -56,4 +58,14 @@ describe("isLoopbackHost", () => {
|
||||
assert.isFalse(isLoopbackHost("0.0.0.0"));
|
||||
assert.isFalse(isLoopbackHost("192.168.1.10"));
|
||||
});
|
||||
|
||||
it("only accepts real 127.0.0.0/8 addresses, not 127.* DNS names", () => {
|
||||
assert.isFalse(isLoopbackHost("127.attacker.example"));
|
||||
assert.isFalse(isLoopbackHost("127.0.0.999"));
|
||||
assert.isFalse(isLoopbackHost("127.0.0"));
|
||||
assert.isFalse(isLoopbackHost("127.0.0.0.1"));
|
||||
assert.isFalse(isLoopbackHost("127.0.0.-1"));
|
||||
assert.isTrue(isLoopbackHost("127.0.0.0"));
|
||||
assert.isTrue(isLoopbackHost("127.255.255.255"));
|
||||
});
|
||||
});
|
||||
@@ -7,18 +7,24 @@
|
||||
* @module netHost
|
||||
*/
|
||||
|
||||
// Strict 127.0.0.0/8 check: a prefix test would also accept DNS names like
|
||||
// "127.attacker.example", which must not count as loopback anywhere this is
|
||||
// used as a security boundary.
|
||||
const isLoopbackIpv4 = (host: string): boolean => {
|
||||
const octets = host.split(".");
|
||||
if (octets.length !== 4) {
|
||||
return false;
|
||||
}
|
||||
const values = octets.map((octet) => (/^\d{1,3}$/.test(octet) ? Number(octet) : -1));
|
||||
return values[0] === 127 && values.every((value) => value >= 0 && value <= 255);
|
||||
};
|
||||
|
||||
export const isLoopbackHost = (host: string | undefined): boolean => {
|
||||
if (!host || host.length === 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return (
|
||||
host === "localhost" ||
|
||||
host === "127.0.0.1" ||
|
||||
host === "::1" ||
|
||||
host === "[::1]" ||
|
||||
host.startsWith("127.")
|
||||
);
|
||||
return host === "localhost" || host === "::1" || host === "[::1]" || isLoopbackIpv4(host);
|
||||
};
|
||||
|
||||
export const isWildcardHost = (host: string | undefined): boolean =>
|
||||
|
||||
Reference in new issue
Block a user