masterplan-lock / check (push) Successful in 7s
CI (SIAX Cloud) / security (push) Successful in 19s
Security Scan (SIAX Cloud) / security (push) Successful in 27s
CI (SIAX Cloud) / quality (push) Successful in 33s
CI (SIAX Cloud) / integration (push) Successful in 46s
masterplan-lock / check (pull_request) Successful in 4s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 16s
CI (SIAX Cloud) / security (pull_request) Successful in 17s
CI (SIAX Cloud) / quality (pull_request) Successful in 35s
CI (SIAX Cloud) / integration (pull_request) Successful in 1m11s
connection_config.sqlEndpoint on POST /api/0pt/sources/{sourceId}/query
was passed straight from the request body to the mssql TDS driver as the
outbound connection target, with only its http(s):// scheme stripped —
no hostname allowlist or private/loopback/link-local/cloud-metadata
guard. This route already requires a Zitadel bearer token + tenant match
(P014 tenant guard), so this was not anonymously exploitable, but it is
the exact SSRF shape found and exploited-in-principle elsewhere in this
estate tonight (press0), so it's worth closing as defense-in-depth.
Adds src/lib/security/ssrf-guard.ts: resolves the endpoint's hostname
and rejects any address in a private/loopback/link-local/cloud-metadata/
unspecified range (RFC1918, 127.0.0.0/8, 169.254.0.0/16 incl. the
169.254.169.254 metadata address, ::1, fc00::/7, fe80::/10, IPv4-mapped
IPv6), failing closed on DNS resolution errors. The range-check logic is
ported 1:1 from press0's src/lib/security/ssrf-guard.ts built the same
night for the same bug class against a fetch()-based URL there; this
module's surface differs because sqlEndpoint is a bare host[:port], not
a fetchable URL. The DNS lookup is injectable (mirrors this adapter's
existing fetchImpl pattern) so tests are hermetic.
FabricOneLakeAdapter.getPool() now calls the guard before loadDriver()/
getToken()/opening the TDS pool, so a blocked target never reaches a
real outbound connection attempt or spends an AAD token exchange.
Tests: new tests/p0/fabric-onelake-ssrf-guard.test.ts unit-tests the
guard module directly (range boundaries, IPv4-mapped-IPv6 bypass
attempt, fail-closed on DNS failure/empty answer). New adapter-level
tests in tests/p0/fabric-onelake-adapter.test.ts prove (a) a
private-IP-resolving sqlEndpoint is rejected before fetch/connect is
ever called, (b) a literal loopback sqlEndpoint is rejected the same
way, and (c) a normal external Fabric hostname still passes the guard
and completes the query — the guard doesn't false-block legitimate
targets. Existing adapter tests are updated to inject a deterministic
DNS resolver so they stay hermetic instead of depending on real DNS.
Full suite: 997 passed, 0 failed, 184 skipped (self-skipping realinfra
tests with no live backend this run). typecheck and lint clean on all
touched files.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
159 lines
6.2 KiB
TypeScript
159 lines
6.2 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
|
import {
|
|
assertConnectionHostIsSafe,
|
|
checkHostnameSafety,
|
|
extractConnectionHostname,
|
|
isBlockedV4,
|
|
isBlockedV6,
|
|
SsrfBlockedHostError,
|
|
type DnsLookupFn,
|
|
} from "@/src/lib/security/ssrf-guard";
|
|
|
|
function lookupReturning(address: string, family: 4 | 6 = 4): DnsLookupFn {
|
|
return () => Promise.resolve([{ address, family }]);
|
|
}
|
|
|
|
function lookupFailing(): DnsLookupFn {
|
|
return () => Promise.reject(new Error("ENOTFOUND"));
|
|
}
|
|
|
|
function lookupEmpty(): DnsLookupFn {
|
|
return () => Promise.resolve([]);
|
|
}
|
|
|
|
describe("extractConnectionHostname — parses the bare host[:port] shape sqlEndpoint takes", () => {
|
|
it("strips an http(s):// scheme", () => {
|
|
expect(extractConnectionHostname("https://ep.datawarehouse.fabric.microsoft.com")).toBe(
|
|
"ep.datawarehouse.fabric.microsoft.com",
|
|
);
|
|
expect(extractConnectionHostname("http://10.0.0.5")).toBe("10.0.0.5");
|
|
});
|
|
|
|
it("strips a trailing port", () => {
|
|
expect(extractConnectionHostname("ep.fabric.microsoft.com:1433")).toBe("ep.fabric.microsoft.com");
|
|
expect(extractConnectionHostname("http://127.0.0.1:1433")).toBe("127.0.0.1");
|
|
});
|
|
|
|
it("strips a trailing path/query/fragment defensively", () => {
|
|
expect(extractConnectionHostname("https://ep.fabric.microsoft.com/whatever?x=1#y")).toBe("ep.fabric.microsoft.com");
|
|
});
|
|
|
|
it("unwraps a bracketed IPv6 literal, with or without a port", () => {
|
|
expect(extractConnectionHostname("[::1]")).toBe("::1");
|
|
expect(extractConnectionHostname("[::1]:1433")).toBe("::1");
|
|
expect(extractConnectionHostname("[fe80::1]:1433")).toBe("fe80::1");
|
|
});
|
|
|
|
it("does not mistake a bare (unbracketed) IPv6 literal's colons for a port separator", () => {
|
|
expect(extractConnectionHostname("fe80::1")).toBe("fe80::1");
|
|
});
|
|
|
|
it("trims whitespace and a trailing slash", () => {
|
|
expect(extractConnectionHostname(" https://ep.fabric.microsoft.com/ ".trim())).toBe("ep.fabric.microsoft.com");
|
|
});
|
|
});
|
|
|
|
describe("isBlockedV4 — RFC1918 / loopback / link-local / unspecified ranges", () => {
|
|
it.each([
|
|
["10.0.0.1", true],
|
|
["10.255.255.255", true],
|
|
["172.16.0.1", true],
|
|
["172.31.255.255", true],
|
|
["172.15.255.255", false], // just outside 172.16.0.0/12
|
|
["172.32.0.0", false], // just outside 172.16.0.0/12
|
|
["192.168.0.1", true],
|
|
["127.0.0.1", true],
|
|
["169.254.169.254", true], // cloud-metadata address
|
|
["169.254.0.1", true],
|
|
["0.0.0.0", true],
|
|
["8.8.8.8", false],
|
|
["20.91.151.2", false], // a real public Azure IP (used elsewhere in these fixtures)
|
|
["1.1.1.1", false],
|
|
])("isBlockedV4(%s) === %s", (ip, expected) => {
|
|
expect(isBlockedV4(ip)).toBe(expected);
|
|
});
|
|
|
|
it("returns false for a non-IPv4 string instead of throwing", () => {
|
|
expect(isBlockedV4("not-an-ip")).toBe(false);
|
|
expect(isBlockedV4("::1")).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("isBlockedV6 — loopback / unique-local / link-local, plus IPv4-mapped bypass", () => {
|
|
it.each([
|
|
["::1", true],
|
|
["::", true],
|
|
["fc00::1", true],
|
|
["fd12:3456::1", true],
|
|
["fe80::1", true],
|
|
["2001:4860:4860::8888", false], // public (Google DNS v6)
|
|
])("isBlockedV6(%s) === %s", (ip, expected) => {
|
|
expect(isBlockedV6(ip)).toBe(expected);
|
|
});
|
|
|
|
it("unwraps an IPv4-mapped IPv6 address and re-checks it as v4 (filter-bypass form)", () => {
|
|
expect(isBlockedV6("::ffff:127.0.0.1")).toBe(true);
|
|
expect(isBlockedV6("::ffff:169.254.169.254")).toBe(true);
|
|
expect(isBlockedV6("::ffff:8.8.8.8")).toBe(false);
|
|
});
|
|
|
|
it("returns false for a non-IPv6 string instead of throwing", () => {
|
|
expect(isBlockedV6("10.0.0.1")).toBe(false);
|
|
expect(isBlockedV6("not-an-ip")).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("checkHostnameSafety — resolves via the injected lookup and fails closed", () => {
|
|
it("blocks a hostname whose resolved address is a private IP (the DNS-rebinding case a plain string allowlist would miss)", async () => {
|
|
const result = await checkHostnameSafety("looks-public.example.com", lookupReturning("10.1.2.3"));
|
|
expect(result).toEqual({ ok: false, reason: "blocked_address_range" });
|
|
});
|
|
|
|
it("blocks the cloud-metadata address specifically", async () => {
|
|
const result = await checkHostnameSafety("metadata.internal", lookupReturning("169.254.169.254"));
|
|
expect(result).toEqual({ ok: false, reason: "blocked_address_range" });
|
|
});
|
|
|
|
it("allows a hostname that resolves to a normal public IP", async () => {
|
|
const result = await checkHostnameSafety("ep.datawarehouse.fabric.microsoft.com", lookupReturning("20.91.151.2"));
|
|
expect(result).toEqual({ ok: true });
|
|
});
|
|
|
|
it("fails closed when DNS resolution errors", async () => {
|
|
const result = await checkHostnameSafety("does-not-resolve.invalid", lookupFailing());
|
|
expect(result).toEqual({ ok: false, reason: "dns_resolution_failed" });
|
|
});
|
|
|
|
it("fails closed when DNS resolution returns no addresses", async () => {
|
|
const result = await checkHostnameSafety("no-answers.invalid", lookupEmpty());
|
|
expect(result).toEqual({ ok: false, reason: "dns_resolution_failed" });
|
|
});
|
|
|
|
it("fails closed on an empty hostname", async () => {
|
|
const result = await checkHostnameSafety("", lookupReturning("20.91.151.2"));
|
|
expect(result).toEqual({ ok: false, reason: "empty_hostname" });
|
|
});
|
|
});
|
|
|
|
describe("assertConnectionHostIsSafe — the guard fabric-onelake-adapter.ts calls", () => {
|
|
it("throws SsrfBlockedHostError for a private-IP sqlEndpoint", async () => {
|
|
await expect(assertConnectionHostIsSafe("internal-db.corp.example.com", lookupReturning("192.168.1.50"))).rejects.toBeInstanceOf(
|
|
SsrfBlockedHostError,
|
|
);
|
|
});
|
|
|
|
it("throws for a literal loopback sqlEndpoint with no DNS lookup needed at all", async () => {
|
|
// Real dns.lookup() on a literal IP just returns that IP without a
|
|
// network round-trip; simulate that here via the injected lookup.
|
|
await expect(assertConnectionHostIsSafe("http://127.0.0.1:1433", lookupReturning("127.0.0.1"))).rejects.toBeInstanceOf(
|
|
SsrfBlockedHostError,
|
|
);
|
|
});
|
|
|
|
it("resolves without throwing for a normal external Fabric/OneLake hostname", async () => {
|
|
await expect(
|
|
assertConnectionHostIsSafe("https://my-tenant.datawarehouse.fabric.microsoft.com/", lookupReturning("20.91.151.2")),
|
|
).resolves.toBeUndefined();
|
|
});
|
|
});
|