Files
0pt/tests/support/st0re-realinfra.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

67 lines
2.5 KiB
TypeScript

type St0reTestEnv = Readonly<Record<string, string | undefined>>;
export interface St0reRealinfraProbe {
target?: string;
reachable: boolean;
reason: string;
}
/** Keep the real client's subsequent writes inside the selected test origin too. */
export function isolatedSt0reFetch(target: string, fetchImpl: typeof fetch): typeof fetch {
const origin = resolveSt0reRealinfraTarget({ ST0RE_REALINFRA_TESTS: "1", ST0RE_TEST_URL: target });
return async (input, init) => {
const url = new URL(typeof input === "string" ? input : input instanceof URL ? input.href : input.url);
if (url.origin !== origin) throw new Error("ST0RE realinfra request escaped its isolated test origin");
return fetchImpl(input, { ...init, redirect: "error" });
};
}
/** Only an explicit, isolated loopback origin can enable real-infrastructure tests. */
export function resolveSt0reRealinfraTarget(env: St0reTestEnv): string | undefined {
if (env.ST0RE_REALINFRA_TESTS !== "1") return undefined;
const target = env.ST0RE_TEST_URL;
const configError = () => new Error(
"ST0RE realinfra configuration error: ST0RE_REALINFRA_TESTS=1 requires ST0RE_TEST_URL to be an HTTP(S) loopback origin (localhost, 127.0.0.1 or [::1]), without credentials, path, query or fragment. Remote/production targets are forbidden."
);
// Check the original spelling too: URL parsing normalizes paths and IP aliases.
if (!target || !/^https?:\/\/(localhost|127\.0\.0\.1|\[::1\])(?::[0-9]+)?\/?$/i.test(target)) {
throw configError();
}
try {
return new URL(target).origin;
} catch {
throw configError();
}
}
export async function probeSt0reRealinfra(
env: St0reTestEnv,
fetchImpl: typeof fetch,
): Promise<St0reRealinfraProbe> {
// Configuration failures must escape the reachability catch and fail closed.
const target = resolveSt0reRealinfraTarget(env);
if (!target) {
return { reachable: false, reason: "disabled: ST0RE_REALINFRA_TESTS is not 1" };
}
try {
const response = await fetchImpl(`${target}/v1/st0re/lakehouse/namespaces`, {
method: "GET",
redirect: "error",
headers: {
"X-Tenant-Id": env.ST0RE_TEST_TENANT_ID ?? "t_0pt_realinfra_local",
"X-Actor-Id": "0pt-realinfra-test",
},
signal: AbortSignal.timeout(5000),
});
return {
target,
reachable: response.ok,
reason: response.ok ? "" : `ST0RE responded HTTP ${response.status}`,
};
} catch {
return { target, reachable: false, reason: "Local ST0RE reachability probe failed" };
}
}