Files
0pt/tests/p0/st0re-realinfra-guard.test.ts
T
admin 0cac0266e0
masterplan-lock / check (push) Successful in 6s
CI (SIAX Cloud) / sonar (pull_request) Skipped
masterplan-lock / check (pull_request) Successful in 4s
CI (SIAX Cloud) / Component conformance (blocking) (pull_request) Successful in 9s
CI (SIAX Cloud) / security (pull_request) Successful in 16s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 17s
CI (SIAX Cloud) / quality (pull_request) Successful in 1m13s
CI (SIAX Cloud) / integration (pull_request) Successful in 1m15s
test(safety): require an isolated opt-in target for ST0RE integration
2026-11-10 03:49:33 +01:00

117 lines
5.0 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import { isolatedSt0reFetch, probeSt0reRealinfra, resolveSt0reRealinfraTarget } from "@/tests/support/st0re-realinfra";
describe("ST0RE realinfra safety guard", () => {
it("uses real HTTP for local writes while forbidding redirect following", async () => {
const fetchSpy = vi.fn<typeof fetch>().mockResolvedValue(new Response("{}"));
const transport = isolatedSt0reFetch("http://127.0.0.1:4620", fetchSpy);
await transport("http://127.0.0.1:4620/v1/st0re/lakehouse/tables", { method: "POST", body: "{}", redirect: "follow" });
expect(fetchSpy).toHaveBeenCalledWith("http://127.0.0.1:4620/v1/st0re/lakehouse/tables",
{ method: "POST", body: "{}", redirect: "error" });
});
it.each(["https://api.st0re.siax.io/v1/st0re/lakehouse/tables", "http://127.0.0.1:4621/v1/st0re/lakehouse/tables"])
("refuses a write outside the exact test origin without sending it (%s)", async (url) => {
const fetchSpy = vi.fn<typeof fetch>();
const transport = isolatedSt0reFetch("http://127.0.0.1:4620", fetchSpy);
await expect(transport(new Request(url, { method: "POST" }))).rejects.toThrow(/isolated test origin/);
expect(fetchSpy).not.toHaveBeenCalled();
});
it.each([
{},
{ ST0RE_URL: "https://api.st0re.siax.io" },
{ ST0RE_TEST_URL: "http://localhost:4620" },
{ ST0RE_TEST_URL: "https://api.st0re.siax.io" },
{ ST0RE_REALINFRA_TESTS: "true", ST0RE_TEST_URL: "http://localhost:4620" },
{ ST0RE_REALINFRA_TESTS: "0", ST0RE_TEST_URL: "http://localhost:4620" },
])("stays disabled without exact opt-in and makes no request (%j)", async (env) => {
const fetchSpy = vi.fn<typeof fetch>();
expect(resolveSt0reRealinfraTarget(env)).toBeUndefined();
expect(await probeSt0reRealinfra(env, fetchSpy)).toEqual({
reachable: false,
reason: expect.stringContaining("disabled"),
});
expect(fetchSpy).not.toHaveBeenCalled();
});
it.each([
undefined,
"",
"https://api.st0re.siax.io",
"https://st0re.siax.io",
"https://example.com",
"http://192.168.1.1",
"http://localhost.example.com",
"http://127.1",
"http://2130706433",
"http://127.0.0.2",
"http://[::ffff:127.0.0.1]",
"ftp://localhost",
"not-a-url",
"http://localhost:65536",
["http://", "user:password", "@localhost"].join(""),
["http://", "@localhost"].join(""),
"http://localhost/path",
"http://localhost/..",
"http://localhost?query=1",
"http://localhost?",
"http://localhost#fragment",
"http://localhost#",
"http://localhost\\remote",
" http://localhost",
])("fails closed before fetch for missing or invalid target (%s)", async (target) => {
const env = {
ST0RE_REALINFRA_TESTS: "1",
ST0RE_TEST_URL: target,
ST0RE_URL: "http://localhost:4620",
};
const fetchSpy = vi.fn<typeof fetch>();
expect(() => resolveSt0reRealinfraTarget(env)).toThrow(/ST0RE realinfra configuration error/);
await expect(probeSt0reRealinfra(env, fetchSpy)).rejects.toThrow(/ST0RE_TEST_URL/);
expect(fetchSpy).not.toHaveBeenCalled();
});
it.each([
"http://localhost:4620",
"http://127.0.0.1:4620",
"http://[::1]:4620",
"https://localhost:4620/",
"https://127.0.0.1",
"https://[::1]",
])("probes an explicit loopback target with one GET (%s)", async (target) => {
const env = {
ST0RE_REALINFRA_TESTS: "1",
ST0RE_TEST_URL: target,
ST0RE_URL: "https://api.st0re.siax.io",
};
const origin = new URL(target).origin;
const fetchSpy = vi.fn<typeof fetch>().mockResolvedValue(new Response("[]", { status: 200 }));
expect(resolveSt0reRealinfraTarget(env)).toBe(origin);
expect(await probeSt0reRealinfra(env, fetchSpy)).toEqual({ target: origin, reachable: true, reason: "" });
expect(fetchSpy).toHaveBeenCalledTimes(1);
expect(fetchSpy).toHaveBeenCalledWith(`${origin}/v1/st0re/lakehouse/namespaces`, {
method: "GET",
redirect: "error",
headers: { "X-Tenant-Id": "t_0pt_realinfra_local", "X-Actor-Id": "0pt-realinfra-test" },
signal: expect.any(AbortSignal),
});
});
it("reports HTTP 500 as unreachable", async () => {
const target = "http://localhost:4620";
const fetchSpy = vi.fn<typeof fetch>().mockResolvedValue(new Response(null, { status: 500 }));
expect(await probeSt0reRealinfra({ ST0RE_REALINFRA_TESTS: "1", ST0RE_TEST_URL: target }, fetchSpy))
.toEqual({ target, reachable: false, reason: "ST0RE responded HTTP 500" });
expect(fetchSpy).toHaveBeenCalledTimes(1);
});
it("reports a throwing fetch as unreachable", async () => {
const target = "http://localhost:4620";
const fetchSpy = vi.fn<typeof fetch>().mockRejectedValue(new Error("synthetic connection failure"));
expect(await probeSt0reRealinfra({ ST0RE_REALINFRA_TESTS: "1", ST0RE_TEST_URL: target }, fetchSpy))
.toEqual({ target, reachable: false, reason: "Local ST0RE reachability probe failed" });
expect(fetchSpy).toHaveBeenCalledTimes(1);
});
});