72 lines
2.1 KiB
TypeScript
72 lines
2.1 KiB
TypeScript
import { test, expect } from "bun:test";
|
|
import { spawn } from "node:child_process";
|
|
import { once } from "node:events";
|
|
import http from "node:http";
|
|
|
|
function fetchRaw(
|
|
port: number,
|
|
path: string,
|
|
): Promise<{ status: number | undefined; body: string }> {
|
|
return new Promise((resolve, reject) => {
|
|
http
|
|
.get({ host: "127.0.0.1", port, path }, (response) => {
|
|
let body = "";
|
|
response.on("data", (chunk) => {
|
|
body += chunk;
|
|
});
|
|
response.on("end", () =>
|
|
resolve({ status: response.statusCode, body }),
|
|
);
|
|
})
|
|
.on("error", reject);
|
|
});
|
|
}
|
|
|
|
test("Bun preview serves browser JavaScript and blocks private files/encoded traversal", async () => {
|
|
const child = spawn(process.execPath, ["scripts/serve.ts"], {
|
|
env: { ...process.env, PORT: "0" },
|
|
stdio: ["ignore", "pipe", "pipe"],
|
|
});
|
|
try {
|
|
const output = await Promise.race([
|
|
once(child.stdout, "data").then(([value]) => String(value)),
|
|
once(child, "exit").then(() => {
|
|
throw new Error("Preview failed to start");
|
|
}),
|
|
]);
|
|
const port = Number(output.match(/127\.0\.0\.1:(\d+)/)?.[1]);
|
|
expect(port).toBeGreaterThan(0);
|
|
for (const path of [
|
|
"/",
|
|
"/account",
|
|
"/account/",
|
|
"/src/app.js",
|
|
"/src/styles.css",
|
|
"/assets/vnox-mark.png",
|
|
]) {
|
|
expect((await fetchRaw(port, path)).status).toBe(200);
|
|
}
|
|
for (const path of [
|
|
"/src/%2e%2e%2fpackage.json",
|
|
"/src/%2e%2e%2f.env",
|
|
"/src/%2e%2e%2fLICENSE",
|
|
]) {
|
|
expect((await fetchRaw(port, path)).status).toBe(403);
|
|
}
|
|
for (const path of [
|
|
"/package.json",
|
|
"/src/app.ts",
|
|
"/src/api.ts",
|
|
"/scripts/serve.ts",
|
|
]) {
|
|
expect((await fetchRaw(port, path)).status).toBe(404);
|
|
}
|
|
expect((await fetchRaw(port, "/v1/auth/session")).status).toBe(503);
|
|
const javascript = await fetchRaw(port, "/src/app.js");
|
|
expect(javascript.body).not.toContain("import type");
|
|
expect(javascript.body).toContain("vnox-web-language");
|
|
} finally {
|
|
child.kill("SIGTERM");
|
|
await once(child, "exit");
|
|
}
|
|
}, 15000);
|