fix(acceleration-relay): validate resolved targets to close SSRF

The relay's private-target guard ran on the caller-supplied hostname while the
connection was made to the DNS-resolved address, so any hostname could reach
loopback or an internal host even with allowPrivate=false. IPv4-mapped IPv6
literals such as ::ffff:127.0.0.1 slipped through the IPv6 branch as well.

- reuse isRestrictedAddress() instead of the weaker local isPrivateAddress()
- resolve first, then reject when any resolved address is restricted, so a
  mixed public/private DNS answer cannot be used to reach an internal host
- release the freshly created session before rejecting a blocked target
- add an injectable lookupAddresses option for deterministic tests
- mirror the same resolve-then-validate ordering in the acceleration PoC relay
- add src/server/acceleration-relay.test.ts (6 tests) to the test script

Verified: tsc clean; the 6 new tests fail 4/6 before the fix and pass 6/6 after;
the acceleration PoC still reports ACCELERATION_POC_OK.
This commit is contained in:
LainHE committed 2026-10-15 16:34:07 +08:00
1 parent 62af82f253
commit 4816c7ea62
4 files changed
+179 -31

No files matched your search

+17 -7
View File
@@ -16,10 +16,17 @@ function isPrivateAddress(host) {
|| octets[0] === 192 && octets[1] === 168
|| octets[0] === 127
|| octets[0] === 169 && octets[1] === 254
|| octets[0] === 100 && octets[1] >= 64 && octets[1] <= 127
|| octets[0] === 0;
}
if (family === 6) {
const normalized = host.toLowerCase();
// IPv4-mapped addresses must be unwrapped before the IPv4 rules apply:
// ::ffff:10.0.0.1 is loopback/private traffic in IPv6 clothing.
if (normalized.startsWith("::ffff:")) {
const mapped = normalized.slice("::ffff:".length);
return isIP(mapped) !== 4 || isPrivateAddress(mapped);
}
return normalized === "::1"
|| normalized === "::"
|| normalized.startsWith("fc")
@@ -34,11 +41,11 @@ function isPrivateAddress(host) {
async function resolveTarget(host) {
const family = isIP(host);
if (family) return { host, family };
if (family) return { host, family, resolved: [host] };
const records = await lookup(host, { all: true, verbatim: true });
const record = records.find((item) => item.family === 4) ?? records[0];
if (!record) throw new Error("TARGET_DNS_EMPTY");
return { host: record.address, family: record.family };
return { host: record.address, family: record.family, resolved: records.map((item) => item.address) };
}
export async function createRelayServer({
@@ -71,11 +78,6 @@ export async function createRelayServer({
if (!request || typeof request.targetHost !== "string" || !Number.isInteger(request.targetPort)) {
return;
}
if (!allowPrivate && (isPrivateAddress(request.targetHost) || request.targetHost === "localhost")) {
const error = { code: "PRIVATE_TARGET_BLOCKED" };
socket.send(encodeFrame(FrameType.OPEN_ERR, frame.sessionId, 0, jsonPayload(error), token), client.port, client.address);
return;
}
const existing = sessions.get(frame.sessionId);
if (existing) cleanup(existing);
@@ -92,6 +94,14 @@ export async function createRelayServer({
try {
const target = await resolveTarget(request.targetHost);
// Validate the resolved addresses, not the caller-supplied name: a DNS
// answer can point any hostname at loopback or an internal address.
if (!allowPrivate && target.resolved.some((address) => isPrivateAddress(address))) {
const error = { code: "PRIVATE_TARGET_BLOCKED" };
socket.send(encodeFrame(FrameType.OPEN_ERR, frame.sessionId, 0, jsonPayload(error), token), client.port, client.address);
cleanup(session);
return;
}
const targetSocket = createSocket(target.family === 6 ? "udp6" : "udp4");
session.targetSocket = targetSocket;
targetSocket.on("message", (payload) => {