Spaces:
Runtime error
Runtime error
File size: 14,470 Bytes
cd8bd0a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | /**
* T-07 β embed proxy route handler tests.
*
* Tests GET/POST/PUT/PATCH/DELETE handlers in
* /dashboard/providers/services/[name]/embed/[...path]/route.ts.
*
* Uses registerSupervisor to inject fake supervisors (ESM live bindings
* can't be reassigned, so direct module patching is not possible).
*/
import { describe, it, afterEach } from "node:test";
import assert from "node:assert/strict";
import { registerSupervisor, unregisterSupervisor } from "../../../src/lib/services/registry.ts";
import type { ServiceSupervisor } from "../../../src/lib/services/ServiceSupervisor.ts";
import {
GET,
POST,
PUT,
PATCH,
DELETE,
HEAD,
OPTIONS,
} from "../../../src/app/(dashboard)/dashboard/providers/services/[name]/embed/[...path]/route.ts";
const originalFetch = globalThis.fetch;
afterEach(() => {
globalThis.fetch = originalFetch;
unregisterSupervisor("9router");
});
// βββ helpers ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function makeFakeParams(
name: string,
path: string[]
): { params: Promise<{ name: string; path: string[] }> } {
return { params: Promise.resolve({ name, path }) };
}
function registerFake(state: string, port: number): void {
const fake = {
getStatus: () => ({
tool: "9router",
state,
port,
pid: null,
health: "unknown" as const,
startedAt: null,
lastError: null,
}),
};
registerSupervisor(fake as unknown as ServiceSupervisor);
}
// βββ tests βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe("embed proxy route", () => {
it("returns 404 for unknown service", async () => {
// No supervisor registered β getSupervisor returns null.
const req = new Request("http://localhost/dashboard/providers/services/unknown/embed/");
const resp = await GET(req, makeFakeParams("unknown", []));
assert.equal(resp.status, 404);
});
it("returns 503 when service exists but is not running", async () => {
registerFake("stopped", 20130);
const req = new Request("http://localhost/dashboard/providers/services/9router/embed/");
const resp = await GET(req, makeFakeParams("9router", []));
assert.equal(resp.status, 503);
});
it("proxies GET to the upstream service", async () => {
registerFake("running", 20130);
let capturedUrl = "";
globalThis.fetch = async (input: string | URL | Request) => {
capturedUrl = String(input);
return new Response("<html>9router UI</html>", {
status: 200,
headers: { "content-type": "text/html" },
});
};
const req = new Request(
"http://localhost/dashboard/providers/services/9router/embed/ui/index.html"
);
const resp = await GET(req, makeFakeParams("9router", ["ui", "index.html"]));
assert.equal(resp.status, 200);
assert.ok(capturedUrl.startsWith("http://127.0.0.1:20130/ui/index.html"));
assert.ok((await resp.text()).includes("9router UI"));
});
it("forwards query string to upstream", async () => {
registerFake("running", 20130);
let capturedUrl = "";
globalThis.fetch = async (input: string | URL | Request) => {
capturedUrl = String(input);
return new Response("{}", { status: 200 });
};
const req = new Request(
"http://localhost/dashboard/providers/services/9router/embed/api/models?page=2"
);
await GET(req, makeFakeParams("9router", ["api", "models"]));
assert.ok(capturedUrl.includes("?page=2"));
});
it("proxies POST and forwards body", async () => {
registerFake("running", 20130);
let capturedMethod = "";
globalThis.fetch = async (_input: string | URL | Request, init?: RequestInit) => {
capturedMethod = init?.method ?? "UNKNOWN";
return new Response('{"ok":true}', { status: 200 });
};
const req = new Request("http://localhost/dashboard/providers/services/9router/embed/api/v1", {
method: "POST",
body: JSON.stringify({ test: 1 }),
headers: { "content-type": "application/json" },
});
const resp = await POST(req, makeFakeParams("9router", ["api", "v1"]));
assert.equal(resp.status, 200);
assert.equal(capturedMethod, "POST");
});
it("strips hop-by-hop headers from the upstream response", async () => {
registerFake("running", 20130);
globalThis.fetch = async () =>
new Response("body", {
status: 200,
headers: {
"content-type": "text/plain",
"transfer-encoding": "chunked",
connection: "keep-alive",
"x-custom": "kept",
},
});
const req = new Request("http://localhost/dashboard/providers/services/9router/embed/");
const resp = await GET(req, makeFakeParams("9router", []));
assert.equal(resp.headers.get("x-custom"), "kept");
assert.equal(resp.headers.get("transfer-encoding"), null);
assert.equal(resp.headers.get("connection"), null);
});
it("returns 502 on upstream network error", async () => {
registerFake("running", 20130);
globalThis.fetch = async () => {
throw new Error("ECONNREFUSED");
};
const req = new Request("http://localhost/dashboard/providers/services/9router/embed/");
const resp = await GET(req, makeFakeParams("9router", []));
assert.equal(resp.status, 502);
});
it("PUT, PATCH, DELETE are handled", async () => {
registerFake("running", 20130);
globalThis.fetch = async (_input: string | URL | Request, init?: RequestInit) =>
new Response(null, { status: 204 });
const params = makeFakeParams("9router", ["resource", "1"]);
const reqUrl = "http://localhost/dashboard/providers/services/9router/embed/resource/1";
assert.equal((await PUT(new Request(reqUrl, { method: "PUT" }), params)).status, 204);
assert.equal((await PATCH(new Request(reqUrl, { method: "PATCH" }), params)).status, 204);
assert.equal((await DELETE(new Request(reqUrl, { method: "DELETE" }), params)).status, 204);
});
// βββ G-05: cookie/auth strip + response header strip + HTML rewrite ββββββββββ
it("G-05: strips cookie header before forwarding to upstream", async () => {
registerFake("running", 20130);
let capturedHeaders: Record<string, string> = {};
globalThis.fetch = async (_input: string | URL | Request, init?: RequestInit) => {
for (const [k, v] of new Headers(init?.headers as HeadersInit).entries()) {
capturedHeaders[k.toLowerCase()] = v;
}
return new Response("ok", { status: 200 });
};
const req = new Request("http://localhost/dashboard/providers/services/9router/embed/", {
headers: { cookie: "session=abc123; jwt=secret" },
});
await GET(req, makeFakeParams("9router", []));
assert.equal(capturedHeaders["cookie"], undefined, "cookie must not be forwarded upstream");
});
it("G-05: sets Authorization: Bearer on upstream request", async () => {
registerFake("running", 20130);
let capturedAuth: string | undefined;
globalThis.fetch = async (_input: string | URL | Request, init?: RequestInit) => {
capturedAuth = new Headers(init?.headers as HeadersInit).get("authorization") ?? undefined;
return new Response("ok", { status: 200 });
};
const req = new Request("http://localhost/dashboard/providers/services/9router/embed/");
await GET(req, makeFakeParams("9router", []));
assert.ok(capturedAuth, "authorization header must be present");
assert.ok(capturedAuth!.startsWith("Bearer "), "authorization must be a Bearer token");
});
it("G-05: strips client Authorization before forwarding, injects service key instead", async () => {
registerFake("running", 20130);
let capturedAuth: string | undefined;
globalThis.fetch = async (_input: string | URL | Request, init?: RequestInit) => {
capturedAuth = new Headers(init?.headers as HeadersInit).get("authorization") ?? undefined;
return new Response("ok", { status: 200 });
};
const req = new Request("http://localhost/dashboard/providers/services/9router/embed/", {
headers: { authorization: "Bearer client-token-should-not-leak" },
});
await GET(req, makeFakeParams("9router", []));
assert.ok(capturedAuth, "authorization header must be set");
assert.notEqual(
capturedAuth,
"Bearer client-token-should-not-leak",
"client authorization must not be forwarded as-is"
);
});
it("G-05: strips set-cookie from upstream response", async () => {
registerFake("running", 20130);
globalThis.fetch = async () =>
new Response("ok", {
status: 200,
headers: { "set-cookie": "session=upstream; Path=/", "content-type": "text/plain" },
});
const resp = await GET(
new Request("http://localhost/dashboard/providers/services/9router/embed/"),
makeFakeParams("9router", [])
);
assert.equal(resp.headers.get("set-cookie"), null, "set-cookie must be stripped from response");
});
it("G-05: strips x-frame-options from upstream response", async () => {
registerFake("running", 20130);
globalThis.fetch = async () =>
new Response("ok", {
status: 200,
headers: { "x-frame-options": "DENY", "content-type": "text/plain" },
});
const resp = await GET(
new Request("http://localhost/dashboard/providers/services/9router/embed/"),
makeFakeParams("9router", [])
);
assert.equal(resp.headers.get("x-frame-options"), null, "x-frame-options must be stripped");
});
it("G-05: strips content-security-policy from upstream response", async () => {
registerFake("running", 20130);
globalThis.fetch = async () =>
new Response("ok", {
status: 200,
headers: {
"content-security-policy": "default-src 'none'",
"content-type": "text/plain",
},
});
const resp = await GET(
new Request("http://localhost/dashboard/providers/services/9router/embed/"),
makeFakeParams("9router", [])
);
assert.equal(
resp.headers.get("content-security-policy"),
null,
"content-security-policy must be stripped"
);
});
it("G-05: strips cross-origin-* headers from upstream response", async () => {
registerFake("running", 20130);
globalThis.fetch = async () =>
new Response("ok", {
status: 200,
headers: {
"cross-origin-embedder-policy": "require-corp",
"cross-origin-opener-policy": "same-origin",
"cross-origin-resource-policy": "same-site",
"content-type": "text/plain",
},
});
const resp = await GET(
new Request("http://localhost/dashboard/providers/services/9router/embed/"),
makeFakeParams("9router", [])
);
assert.equal(
resp.headers.get("cross-origin-embedder-policy"),
null,
"cross-origin-embedder-policy must be stripped"
);
assert.equal(
resp.headers.get("cross-origin-opener-policy"),
null,
"cross-origin-opener-policy must be stripped"
);
assert.equal(
resp.headers.get("cross-origin-resource-policy"),
null,
"cross-origin-resource-policy must be stripped"
);
});
it("G-05: HTML response is rewritten β contains injected <base href>", async () => {
registerFake("running", 20130);
globalThis.fetch = async () =>
new Response('<html><head></head><body><a href="/dashboard">link</a></body></html>', {
status: 200,
headers: { "content-type": "text/html; charset=utf-8" },
});
const resp = await GET(
new Request("http://localhost/dashboard/providers/services/9router/embed/"),
makeFakeParams("9router", [])
);
const body = await resp.text();
assert.ok(
body.includes('<base href="/dashboard/providers/services/9router/embed/">'),
`Expected <base href> in rewritten HTML. Got: ${body.substring(0, 200)}`
);
});
it("G-05: HTML response rewrites path-absolute links to go through proxy", async () => {
registerFake("running", 20130);
globalThis.fetch = async () =>
new Response('<html><head></head><body><a href="/ui/page">x</a></body></html>', {
status: 200,
headers: { "content-type": "text/html" },
});
const resp = await GET(
new Request("http://localhost/dashboard/providers/services/9router/embed/"),
makeFakeParams("9router", [])
);
const body = await resp.text();
assert.ok(
body.includes('href="/dashboard/providers/services/9router/embed/ui/page"'),
`Expected rewritten href. Got: ${body.substring(0, 300)}`
);
});
it("G-05: JSON response is NOT rewritten (streaming pass-through)", async () => {
registerFake("running", 20130);
const jsonPayload = '{"models":["gpt-4","claude-3"]}';
globalThis.fetch = async () =>
new Response(jsonPayload, {
status: 200,
headers: { "content-type": "application/json" },
});
const resp = await GET(
new Request("http://localhost/dashboard/providers/services/9router/embed/api/models"),
makeFakeParams("9router", ["api", "models"])
);
const body = await resp.text();
assert.equal(body, jsonPayload, "JSON response must pass through unchanged");
});
it("G-05: HEAD method is handled", async () => {
registerFake("running", 20130);
globalThis.fetch = async () =>
new Response(null, { status: 200, headers: { "content-type": "text/html" } });
const req = new Request("http://localhost/dashboard/providers/services/9router/embed/", {
method: "HEAD",
});
const resp = await HEAD(req, makeFakeParams("9router", []));
assert.equal(resp.status, 200);
});
it("G-05: OPTIONS method is handled", async () => {
registerFake("running", 20130);
globalThis.fetch = async () =>
new Response(null, {
status: 204,
headers: { allow: "GET, HEAD, POST, OPTIONS" },
});
const req = new Request("http://localhost/dashboard/providers/services/9router/embed/", {
method: "OPTIONS",
});
const resp = await OPTIONS(req, makeFakeParams("9router", []));
assert.equal(resp.status, 204);
});
});
|