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();
|
|
});
|
|
});
|