Files
hoelee-check/test/net-guard.test.mjs
hoelee b2707f49f7 hoelee-check: v1 skeleton — net-guard, cache, ratelimit, router, pages, three checks
Zero-dependency Node service mounted at www.hoelee.com/check/.
net-guard pins the resolved IP, re-validates every redirect hop, and refuses
private ranges; the certificate is read off the same socket as the request.
Pages: hub, certificate, email auth, redirects/preview, about, report.
Argon review applied: optional-hardening wording, DKIM/SHA-1 root rules,
neutral not-evaluated, cache-before-scan, single-connection TLS.
2026-11-10 12:56:48 +08:00

107 lines
4.4 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {
isBlockedAddress,
parseTarget,
resolvePublic,
guardHop,
NetGuardError
} from '../src/lib/net-guard.mjs';
test('isBlockedAddress blocks every private and reserved range', () => {
const blocked = [
'127.0.0.1', '127.1.2.3', '10.0.0.1', '10.255.255.255', '172.16.0.1', '172.31.255.254',
'192.168.1.1', '169.254.169.254', '0.0.0.0', '100.64.0.1', '192.0.2.5', '198.51.100.1',
'203.0.113.9', '224.0.0.1', '255.255.255.255',
'::1', '::', 'fe80::1', 'fc00::1', 'fd12:3456::1', 'ff02::1', '::ffff:127.0.0.1',
'::ffff:192.168.1.6', '64:ff9b::7f00:1', '2002:7f00:1::1', '2001:db8::1'
];
for (const ip of blocked) {
assert.equal(isBlockedAddress(ip), true, `${ip} must be blocked`);
}
});
test('isBlockedAddress allows real public addresses', () => {
const allowed = ['8.8.8.8', '1.1.1.1', '180.73.34.45', '2606:4700:4700::1111', '2a00:1450:4001:80f::200e'];
for (const ip of allowed) {
assert.equal(isBlockedAddress(ip), false, `${ip} must be allowed`);
}
});
test('isBlockedAddress fails closed on junk', () => {
for (const junk of ['', 'not-an-ip', '1234.1.1.1', '1.2.3', null, undefined, '2130706433']) {
assert.equal(isBlockedAddress(junk), true, `${String(junk)} must be blocked`);
}
});
test('parseTarget accepts plain domains and full URLs', () => {
assert.deepEqual(parseTarget('example.com'), { host: 'example.com', port: 443 });
assert.deepEqual(parseTarget('Hoelee.COM'), { host: 'hoelee.com', port: 443 });
assert.deepEqual(parseTarget('https://www.hoelee.com/advisory?x=1'), { host: 'www.hoelee.com', port: 443 });
assert.deepEqual(parseTarget('http://example.com/path'), { host: 'example.com', port: 80 });
assert.deepEqual(parseTarget('example.com:443'), { host: 'example.com', port: 443 });
assert.deepEqual(parseTarget('example.com.'), { host: 'example.com', port: 443 });
});
test('parseTarget refuses what must never be fetched', () => {
const cases = [
['127.0.0.1', 'ip_literal'],
['1.2.3.4', 'ip_literal'],
['[::1]', 'ip_literal'],
['localhost', 'bad_input'],
['printer.local', 'private_host'],
['example.com:22', 'bad_port'],
['example.com:8080', 'bad_port'],
['ftp://example.com', 'bad_scheme'],
['https://user:[email protected]', 'bad_input'],
['', 'bad_input'],
['no spaces allowed.com', 'bad_input']
];
for (const [input, code] of cases) {
assert.throws(() => parseTarget(input), (err) => {
assert.ok(err instanceof NetGuardError, `${input} should throw NetGuardError`);
assert.equal(err.code, code, `${input} → expected ${code}, got ${err.code}`);
return true;
});
}
});
test('resolvePublic refuses a host that answers with a private address', async () => {
const lookup = async () => [{ address: '192.168.1.6', family: 4 }];
await assert.rejects(() => resolvePublic('evil.example', { lookup }), (err) => err.code === 'private_address');
});
test('resolvePublic refuses a mixed public + private answer set (rebinding shape)', async () => {
const lookup = async () => [
{ address: '93.184.216.34', family: 4 },
{ address: '10.0.0.7', family: 4 }
];
await assert.rejects(() => resolvePublic('sneaky.example', { lookup }), (err) => err.code === 'private_address');
});
test('resolvePublic returns public answers untouched', async () => {
const lookup = async () => [{ address: '93.184.216.34', family: 4 }];
const out = await resolvePublic('example.com', { lookup });
assert.equal(out.length, 1);
assert.equal(out[0].address, '93.184.216.34');
});
test('resolvePublic refuses an empty answer', async () => {
const lookup = async () => [];
await assert.rejects(() => resolvePublic('nope.example', { lookup }), (err) => err.code === 'dns_nxdomain');
});
test('guardHop refuses a redirect into the private network or a strange port', () => {
assert.equal(guardHop('https://example.com/ok'), null);
const priv = guardHop('http://192.168.1.6/admin');
assert.ok(priv instanceof NetGuardError);
assert.equal(priv.code, 'ip_literal'); // an IP is refused before any range check runs
const port = guardHop('https://example.com:8443/');
assert.ok(port instanceof NetGuardError);
assert.equal(port.code, 'bad_port');
const meta = guardHop('http://169.254.169.254/latest/meta-data/');
assert.ok(meta instanceof NetGuardError);
const host = guardHop('http://localhost/');
assert.ok(host instanceof NetGuardError);
});