Files
WebSpeak-client-for-TeamSpeak/scripts/acceleration-poc/target-guard-parity.test.mjs
T
LainHE 96f7fd6783 fix(acceleration-poc): align the relay's target guard with the gateway policy
The PoC relay carried its own copy of the restricted-address check and had
drifted well behind src/security/open-target-policy.ts: it missed multicast,
192.0.0.0/24 and the other special-use ranges, 198.18.0.0/15, the TEST-NET
blocks, fec0::/10, ff00::/8 and 2001:db8::/32. A relay started without
allowPrivate would therefore forward to addresses the gateway refuses, and
nothing exercised the guard, so the gap could not be noticed.

The relay must stay a plain .mjs file that the README documents copying to a
remote host without a build step, so it cannot import the TypeScript policy. The
predicate is mirrored instead and pinned by a parity test that fails the moment
the two classifications disagree.

The parity test lives beside the PoC rather than under src/: .dockerignore
excludes scripts/acceleration-poc from the Docker build context while the
Dockerfile runs `tsc` over src/**, so a copy in src/ would resolve locally and
still break the image build with TS2307.

- mirror isRestrictedAddress() exactly, including fail-closed handling of any
  input that is not a literal public address
- add scripts/acceleration-poc/target-guard-parity.test.mjs comparing both
  implementations across private, loopback, link-local, carrier-grade NAT,
  special-use, IPv6 and IPv4-mapped cases
- assert the guard in the PoC smoke run: a relay without allowPrivate must
  reject loopback, private and IPv4-mapped targets while still serving a public
  one, and the run now fails with the offending target instead of passing
  silently

Verified: tsc clean; `npm run build` and `npm run web:build` succeed with
scripts/acceleration-poc moved aside to reproduce the Docker build context; the
parity test fails when either side drifts; the smoke run reports ssrf_guard_ok
and ACCELERATION_POC_OK after the fix, and fails with a LEAK line per unblocked
target once the guard is weakened; the IPv6 smoke run still reports
IPV6_ACCELERATION_POC_OK.
2026-10-15 21:26:22 +08:00

89 lines
3.3 KiB
JavaScript

import assert from "node:assert/strict";
import test from "node:test";
import { isRestrictedAddress } from "../../src/security/open-target-policy.js";
import { isRestrictedAddress as isPocRestrictedAddress } from "./relay-server.mjs";
/**
* The acceleration PoC relay keeps its own copy of the restricted-address
* predicate because it is a standalone `.mjs` file that the README documents
* copying to a remote host without a build step, so it cannot import the
* gateway's TypeScript module. Duplication is only safe while the two stay
* identical: these cases pin both classifications to the same answer, so a
* change to either side without the other fails here instead of silently
* widening the PoC's SSRF boundary.
*
* This test lives beside the PoC because `.dockerignore` excludes
* `scripts/acceleration-poc` from the Docker build context while the Dockerfile
* runs `tsc` over `src/**`; a copy under `src/` would make the image build fail
* with TS2307.
*/
const CASES = [
// Public addresses that must stay reachable.
["8.8.8.8", false],
["1.1.1.1", false],
["172.32.0.1", false],
["100.128.0.1", false],
["2001:4860:4860::8888", false],
["2606:4700:4700::1111", false],
// Private, loopback and link-local.
["10.0.0.1", true],
["172.16.0.1", true],
["172.31.255.255", true],
["192.168.1.1", true],
["127.0.0.1", true],
["169.254.169.254", true],
["0.0.0.0", true],
["0.1.2.3", true],
// Carrier-grade NAT.
["100.64.0.1", true],
["100.127.255.255", true],
// Special-use ranges: the PoC's first implementation missed every one of these.
["192.0.0.1", true],
["192.0.2.5", true],
["198.18.0.1", true],
["198.19.5.5", true],
["198.51.100.7", true],
["203.0.113.9", true],
["224.0.0.1", true],
["239.1.1.1", true],
["255.255.255.255", true],
// IPv6 loopback, unspecified, unique-local, link-local, site-local, multicast.
["::1", true],
["::", true],
["fc00::1", true],
["fd12::1", true],
["fe80::1", true],
["fec0::1", true],
["ff02::1", true],
["2001:db8::1", true],
// IPv4-mapped forms must unwrap before the IPv4 rules apply.
["::ffff:127.0.0.1", true],
["::ffff:10.0.0.1", true],
["::ffff:8.8.8.8", false],
// Anything that is not a literal address fails closed.
["localhost", true],
["public.example.invalid", true],
["", true],
];
test("the acceleration PoC relay classifies restricted addresses exactly like the gateway policy", () => {
const mismatches = [];
for (const [address, expected] of CASES) {
const policy = isRestrictedAddress(address);
const poc = isPocRestrictedAddress(address);
assert.equal(policy, expected, `gateway policy disagrees with the pinned expectation for ${address}`);
if (poc !== policy) {
mismatches.push(`${address}: policy=${policy} poc=${poc}`);
}
}
assert.deepEqual(mismatches, [], "the PoC relay's restricted-address guard drifted from the gateway policy");
});
test("the acceleration PoC relay fails closed on a non-literal address", () => {
// `isIP()` returns 0 for a hostname, so the guard must not treat an
// unresolved name as public just because it is not a literal address.
for (const hostname of ["localhost", "internal.example.invalid", "attacker.example.invalid", ""]) {
assert.equal(isPocRestrictedAddress(hostname), true, `${hostname} must fail closed`);
}
});