Something went wrong. Try again.
llm chat done the right way™
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129import { afterEach, expect, test } from "bun:test";import { mkdtempSync, rmSync } from "node:fs";import { tmpdir } from "node:os";import { join } from "node:path";import { FsBlobStore } from "../src/blobs";import { apiRoutes } from "../src/http";import { sandboxRoutes } from "../src/sandbox";import { loadConfig, setConfig } from "../src/settings";import { shareRoutes } from "../src/share";import { Store } from "../src/store";
/** * The artifact origin exists to be a place with nothing in it. * * So the tests worth having are the refusals: that it serves nothing but the * pages it is for, that the app refuses to answer there, and that a deployment * which never configured it is exactly as it was. */const ARTIFACTS = "https://artifacts.kloe.test";
afterEach(() => { setConfig(null);});
/** A deployment that knows where it lives and (maybe) where its artifacts run. */function configure(artifactOrigin: string, appOrigin = "https://kloe.test"): void { const base = loadConfig({ path: "does-not-exist.json", env: {} }); setConfig({ ...base, server: { ...base.server, artifactOrigin }, auth: { ...base.auth, baseUrl: appOrigin }, });}
function serve(store: Store, blobs: FsBlobStore) { return Bun.serve({ port: 0, routes: { ...sandboxRoutes({ store, blobs }), ...shareRoutes({ store, blobs }), ...apiRoutes({ store, blobs }), }, });}
async function withServer( fn: (base: string, store: Store, blobs: FsBlobStore) => Promise<void>,): Promise<void> { const tmp = mkdtempSync(join(tmpdir(), "kloe-sandbox-")); const store = new Store(":memory:"); const blobs = new FsBlobStore(join(tmp, "blobs")); const server = serve(store, blobs); try { await fn(server.url.origin, store, blobs); } finally { server.stop(true); store.db.close(); rmSync(tmp, { recursive: true, force: true }); }}
/** Stores bytes as an artifact-shaped blob and returns its address. */async function putHtml(store: Store, blobs: FsBlobStore, html: string): Promise<string> { const { sha256, size } = await blobs.put(new TextEncoder().encode(html)); store.recordBlob(sha256, "text/html", size); return sha256;}
test("with no artifact origin configured, the door is not there", async () => { configure(""); await withServer(async (base, store, blobs) => { const sha = await putHtml(store, blobs, "<h1>hello</h1>"); expect((await fetch(`${base}/a/${sha}`)).status).toBe(404); });});
test("the artifact origin serves a page, and says who may frame it", async () => { await withServer(async (base, store, blobs) => { // This server IS the artifact host for the length of the test; what the // route matches on is the host the request arrived at. configure(base); const sha = await putHtml(store, blobs, "<h1>a page</h1>"); const res = await fetch(`${base}/a/${sha}`); expect(res.status).toBe(200); expect(res.headers.get("content-type")).toContain("text/html"); expect(await res.text()).toBe("<h1>a page</h1>"); // Not the `sandbox` CSP the app's own blob route sets: this page has to be // able to run, which is the whole reason this origin exists. const csp = res.headers.get("content-security-policy") ?? ""; expect(csp).not.toContain("sandbox"); // Only the app may frame it. expect(csp).toContain("frame-ancestors https://kloe.test"); });});
test("it serves only HTML, and only on its own host", async () => { await withServer(async (base, store, blobs) => { configure(base); const { sha256, size } = await blobs.put(new TextEncoder().encode("not a page")); store.recordBlob(sha256, "text/plain", size); // Anything that isn't a page has a home already, behind the session. expect((await fetch(`${base}/a/${sha256}`)).status).toBe(404); expect((await fetch(`${base}/a/nonsense`)).status).toBe(404);
// And on any other host, the door isn't there at all. const html = await putHtml(store, blobs, "<h1>a page</h1>"); configure(ARTIFACTS); expect((await fetch(`${base}/a/${html}`)).status).toBe(404); });});
test("the app does not answer on the artifact host", async () => { // The isolation is worth what its weakest statement is worth. A proxy that // sent the whole app to that hostname must not make it the app's origin — // with auth off, that would be an unauthenticated API served to the very // pages this arrangement exists to contain. await withServer(async (base, store, blobs) => { configure("http://localhost:1"); // not this server: the app answers normally expect((await fetch(`${base}/api/conversations`)).status).toBe(200);
configure(base); // now this server IS the artifact host expect((await fetch(`${base}/api/conversations`)).status).toBe(404); expect((await fetch(`${base}/api/me`)).status).toBe(404); // …but the one thing it is for still works. const sha = await putHtml(store, blobs, "<h1>still here</h1>"); expect((await fetch(`${base}/a/${sha}`)).status).toBe(200); });});