@malloy-publisher/server 0.0.203 → 0.0.205

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/build.ts +10 -1
  2. package/dist/app/api-doc.yaml +146 -0
  3. package/dist/app/assets/{EnvironmentPage-BVQ7glKP.js → EnvironmentPage-CAge6UHD.js} +1 -1
  4. package/dist/app/assets/HomePage-DhTe8qpa.js +1 -0
  5. package/dist/app/assets/{MainPage-bYOWcgDP.js → MainPage-CeTxxGex.js} +2 -2
  6. package/dist/app/assets/MaterializationsPage-CpDHB70t.js +1 -0
  7. package/dist/app/assets/ModelPage-D9sSMb75.js +1 -0
  8. package/dist/app/assets/PackagePage-LRqQWrFY.js +1 -0
  9. package/dist/app/assets/{RouteError-_J-EBz7W.js → RouteError-xT6kuCNw.js} +1 -1
  10. package/dist/app/assets/{WorkbookPage-Bjs9Nm-_.js → WorkbookPage-DsIh9svZ.js} +1 -1
  11. package/dist/app/assets/{core-BPLlx5VM.es-C2ARtwWI.js → core-C2sQrwVu.es-Bjem0hym.js} +1 -1
  12. package/dist/app/assets/{index-CqUWJELr.js → index-BdOZDcce.js} +2 -2
  13. package/dist/app/assets/index-DHHAcY5o.js +1812 -0
  14. package/dist/app/assets/index-RX3QOTde.js +455 -0
  15. package/dist/app/assets/index.umd-D2WH3D-f.js +2469 -0
  16. package/dist/app/index.html +1 -1
  17. package/dist/package_load_worker.mjs +392 -67
  18. package/dist/runtime/publisher.js +318 -0
  19. package/dist/server.mjs +982 -346
  20. package/package.json +15 -14
  21. package/scripts/bake-duckdb-extensions.js +104 -0
  22. package/src/controller/watch-mode.controller.ts +176 -46
  23. package/src/ducklake_version.spec.ts +43 -0
  24. package/src/ducklake_version.ts +26 -0
  25. package/src/errors.spec.ts +21 -0
  26. package/src/errors.ts +18 -1
  27. package/src/mcp/error_messages.spec.ts +35 -0
  28. package/src/mcp/error_messages.ts +14 -1
  29. package/src/mcp/handler_utils.ts +12 -0
  30. package/src/package_load/package_load_pool.ts +0 -5
  31. package/src/package_load/package_load_worker.ts +41 -99
  32. package/src/package_load/protocol.ts +1 -7
  33. package/src/runtime/publisher.js +318 -0
  34. package/src/server.ts +479 -2
  35. package/src/service/annotations.spec.ts +118 -0
  36. package/src/service/annotations.ts +91 -0
  37. package/src/service/authorize.spec.ts +132 -0
  38. package/src/service/authorize.ts +241 -0
  39. package/src/service/authorize_integration.spec.ts +932 -0
  40. package/src/service/compile_authorize.spec.ts +85 -0
  41. package/src/service/connection.ts +1 -1
  42. package/src/service/environment.ts +67 -9
  43. package/src/service/environment_store.ts +142 -11
  44. package/src/service/filter.spec.ts +14 -3
  45. package/src/service/filter.ts +5 -1
  46. package/src/service/filter_bypass.spec.ts +418 -0
  47. package/src/service/given.ts +37 -12
  48. package/src/service/givens_integration.spec.ts +34 -7
  49. package/src/service/materialization_service.ts +25 -20
  50. package/src/service/materialized_table_gc.spec.ts +6 -5
  51. package/src/service/materialized_table_gc.ts +2 -50
  52. package/src/service/model.spec.ts +203 -8
  53. package/src/service/model.ts +349 -155
  54. package/src/service/package.ts +17 -6
  55. package/src/service/package_worker_path.spec.ts +113 -0
  56. package/src/service/quoting.ts +0 -20
  57. package/src/service/restricted_mode.spec.ts +299 -0
  58. package/src/service/source_extraction.ts +226 -0
  59. package/src/storage/StorageManager.ts +73 -0
  60. package/src/storage/duckdb/DuckDBConnection.ts +70 -124
  61. package/tests/fixtures/authorize-compile/model.malloy +9 -0
  62. package/tests/fixtures/authorize-compile/publisher.json +4 -0
  63. package/tests/fixtures/html-pages-nopublic/model.malloy +1 -0
  64. package/tests/fixtures/html-pages-nopublic/publisher.json +5 -0
  65. package/tests/fixtures/html-pages-test/data.csv +3 -0
  66. package/tests/fixtures/html-pages-test/public/assets/app.css +3 -0
  67. package/tests/fixtures/html-pages-test/public/data.json +1 -0
  68. package/tests/fixtures/html-pages-test/public/index.html +9 -0
  69. package/tests/fixtures/html-pages-test/public/sub/page2.html +9 -0
  70. package/tests/fixtures/html-pages-test/publisher.json +5 -0
  71. package/tests/fixtures/html-pages-test/report.malloy +1 -0
  72. package/tests/integration/authorize/compile_authorize_http.integration.spec.ts +92 -0
  73. package/tests/integration/duckdb_storage/duckdb_storage.integration.spec.ts +138 -0
  74. package/tests/integration/html_pages/html_pages.integration.spec.ts +378 -0
  75. package/tests/integration/watch-mode/watch_mode.integration.spec.ts +421 -0
  76. package/tests/unit/duckdb/attached_databases.test.ts +111 -0
  77. package/tests/unit/duckdb/duckdb_connection.test.ts +181 -0
  78. package/tests/unit/duckdb/repositories.test.ts +208 -0
  79. package/dist/app/assets/HomePage-D9drXoZX.js +0 -1
  80. package/dist/app/assets/ModelPage-DT0gjNy1.js +0 -1
  81. package/dist/app/assets/PackagePage-N1ZBNJul.js +0 -1
  82. package/dist/app/assets/index-BeNwIeYQ.js +0 -454
  83. package/dist/app/assets/index-Dx7qi2LO.js +0 -1803
  84. package/dist/app/assets/index.umd-BXm2lnUO.js +0 -1145
@@ -0,0 +1,92 @@
1
+ import { afterAll, beforeAll, describe, expect, it } from "bun:test";
2
+ import path from "path";
3
+ import { fileURLToPath } from "url";
4
+ import { type RestE2EEnv, startRestE2E } from "../../harness/rest_e2e";
5
+
6
+ // HTTP end-to-end for the /compile authorize gate: proves AccessDeniedError
7
+ // surfaces as a real 403 body through the express route + internalErrorToHttpError,
8
+ // and that the 403 names only the source (never the gate expression).
9
+
10
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
11
+ const PROJECT = "authorize-compile-test-project";
12
+ const PKG = "authorize-compile";
13
+ const MODEL = "model.malloy";
14
+
15
+ describe("compile authorize gate (HTTP E2E)", () => {
16
+ let env: (RestE2EEnv & { stop(): Promise<void> }) | null = null;
17
+ let baseUrl: string;
18
+
19
+ beforeAll(async () => {
20
+ env = await startRestE2E();
21
+ baseUrl = env.baseUrl;
22
+ const fixtureDir = path.resolve(
23
+ __dirname,
24
+ "../../fixtures/authorize-compile",
25
+ );
26
+ const createRes = await fetch(`${baseUrl}/api/v0/projects`, {
27
+ method: "POST",
28
+ headers: { "Content-Type": "application/json" },
29
+ body: JSON.stringify({
30
+ name: PROJECT,
31
+ packages: [{ name: PKG, location: fixtureDir }],
32
+ connections: [],
33
+ }),
34
+ });
35
+ if (!createRes.ok) {
36
+ throw new Error(
37
+ `Failed to create test project (${createRes.status}): ${await createRes.text()}`,
38
+ );
39
+ }
40
+ const deadline = Date.now() + 30_000;
41
+ while (Date.now() < deadline) {
42
+ const res = await fetch(
43
+ `${baseUrl}/api/v0/projects/${PROJECT}/packages/${PKG}`,
44
+ );
45
+ if (res.ok) break;
46
+ await new Promise((r) => setTimeout(r, 250));
47
+ }
48
+ });
49
+
50
+ afterAll(async () => {
51
+ await env?.stop();
52
+ });
53
+
54
+ const compile = (body: unknown) =>
55
+ fetch(
56
+ `${baseUrl}/api/v0/environments/${PROJECT}/packages/${PKG}/models/${MODEL}/compile`,
57
+ {
58
+ method: "POST",
59
+ headers: { "Content-Type": "application/json" },
60
+ body: JSON.stringify(body),
61
+ },
62
+ );
63
+
64
+ it("returns 403 with a source-only message when the gate is not satisfied", async () => {
65
+ const res = await compile({
66
+ source: "run: gated -> { aggregate: c }",
67
+ givens: {},
68
+ });
69
+ expect(res.status).toBe(403);
70
+ const json = (await res.json()) as { code: number; message: string };
71
+ expect(json.code).toBe(403);
72
+ expect(json.message).toBe('Access denied for source "gated".');
73
+ // Redaction: the runtime 403 must NOT leak the authorize expression.
74
+ expect(json.message).not.toContain("ROLE");
75
+ expect(json.message).not.toContain("analyst");
76
+ });
77
+
78
+ it("returns 200 when a satisfying given is supplied", async () => {
79
+ const res = await compile({
80
+ source: "run: gated -> { aggregate: c }",
81
+ givens: { ROLE: "analyst" },
82
+ });
83
+ expect(res.status).toBe(200);
84
+ });
85
+
86
+ it("compiles an ungated source without any given", async () => {
87
+ const res = await compile({
88
+ source: "run: open_src -> { aggregate: c }",
89
+ });
90
+ expect(res.status).toBe(200);
91
+ });
92
+ });
@@ -0,0 +1,138 @@
1
+ /// <reference types="bun-types" />
2
+
3
+ // Integration test for the DuckDB storage stack end-to-end:
4
+ // StorageManager -> ResourceRepository -> DuckDBConnection (@duckdb/node-api)
5
+ // -> a real on-disk publisher.db
6
+ //
7
+ // Unlike the repository unit tests (which construct a single repo against a bare
8
+ // connection) this drives the same path the server uses -- StorageManager.initialize()
9
+ // wires the schema + repositories over the migrated connection, and getRepository()
10
+ // is what the service layer calls. It also reopens a second StorageManager against
11
+ // the same file to prove data persists across a restart, which is the real
12
+ // end-to-end guarantee for the migration.
13
+
14
+ import { afterEach, beforeEach, describe, expect, it } from "bun:test";
15
+ import fs from "fs/promises";
16
+ import os from "os";
17
+ import path from "path";
18
+ import { StorageManager } from "../../../src/storage/StorageManager";
19
+
20
+ describe("DuckDB storage stack (integration)", () => {
21
+ let dbDir: string;
22
+ let dbPath: string;
23
+ let sm: StorageManager;
24
+
25
+ beforeEach(async () => {
26
+ dbDir = await fs.mkdtemp(path.join(os.tmpdir(), "duckdb-storage-int-"));
27
+ dbPath = path.join(dbDir, "publisher.db");
28
+ sm = new StorageManager({ type: "duckdb", duckdb: { path: dbPath } });
29
+ await sm.initialize();
30
+ });
31
+
32
+ afterEach(async () => {
33
+ try {
34
+ await sm.close();
35
+ } catch {
36
+ // ignore
37
+ }
38
+ await fs.rm(dbDir, { recursive: true, force: true });
39
+ });
40
+
41
+ it("initializes the schema and reports ready", () => {
42
+ expect(sm.isInitialized()).toBe(true);
43
+ });
44
+
45
+ it("creates an environment with a package and connection through the repository", async () => {
46
+ const repo = sm.getRepository();
47
+
48
+ const env = await repo.createEnvironment({
49
+ name: "prod",
50
+ path: "/srv/prod",
51
+ metadata: { owner: "platform" },
52
+ });
53
+ expect(env.id).toBeTruthy();
54
+
55
+ const pkg = await repo.createPackage({
56
+ environmentId: env.id,
57
+ name: "sales",
58
+ manifestPath: "/srv/prod/sales/publisher.json",
59
+ });
60
+
61
+ const conn = await repo.createConnection({
62
+ environmentId: env.id,
63
+ name: "warehouse",
64
+ type: "snowflake",
65
+ config: { account: "acct", warehouse: "wh" },
66
+ });
67
+
68
+ // Read everything back through the same repository.
69
+ expect((await repo.getEnvironmentByName("prod"))?.id).toBe(env.id);
70
+ expect((await repo.listPackages(env.id)).map((p) => p.name)).toEqual([
71
+ "sales",
72
+ ]);
73
+ const conns = await repo.listConnections(env.id);
74
+ expect(conns.map((c) => c.name)).toEqual(["warehouse"]);
75
+ expect(conns[0].config).toEqual({ account: "acct", warehouse: "wh" });
76
+ expect(pkg.environmentId).toBe(env.id);
77
+ });
78
+
79
+ it("persists data across a StorageManager restart (reopen the same file)", async () => {
80
+ const repo = sm.getRepository();
81
+ const env = await repo.createEnvironment({
82
+ name: "persisted",
83
+ path: "/srv/persisted",
84
+ metadata: { keep: true },
85
+ });
86
+ await repo.createConnection({
87
+ environmentId: env.id,
88
+ name: "pg",
89
+ type: "postgres",
90
+ config: { host: "db.internal", port: 5432 },
91
+ });
92
+
93
+ // Close the manager (releases the file), then open a fresh one on the
94
+ // same path -- the data must still be there.
95
+ await sm.close();
96
+
97
+ const reopened = new StorageManager({
98
+ type: "duckdb",
99
+ duckdb: { path: dbPath },
100
+ });
101
+ await reopened.initialize();
102
+ try {
103
+ const repo2 = reopened.getRepository();
104
+ const env2 = await repo2.getEnvironmentByName("persisted");
105
+ expect(env2).not.toBeNull();
106
+ expect(env2!.metadata).toEqual({ keep: true });
107
+ expect(env2!.createdAt).toBeInstanceOf(Date);
108
+
109
+ const conns = await repo2.listConnections(env2!.id);
110
+ expect(conns.map((c) => c.name)).toEqual(["pg"]);
111
+ expect(conns[0].config).toEqual({ host: "db.internal", port: 5432 });
112
+ } finally {
113
+ await reopened.close();
114
+ }
115
+
116
+ // Point afterEach's close() at the already-closed original without error.
117
+ sm = reopened;
118
+ });
119
+
120
+ it("deleteEnvironment also removes the environment's connections", async () => {
121
+ const repo = sm.getRepository();
122
+ const env = await repo.createEnvironment({
123
+ name: "to-delete",
124
+ path: "/srv/del",
125
+ });
126
+ await repo.createConnection({
127
+ environmentId: env.id,
128
+ name: "c1",
129
+ type: "duckdb",
130
+ config: {},
131
+ });
132
+
133
+ await repo.deleteEnvironment(env.id);
134
+
135
+ expect(await repo.getEnvironmentById(env.id)).toBeNull();
136
+ expect(await repo.listConnections(env.id)).toEqual([]);
137
+ });
138
+ });
@@ -0,0 +1,378 @@
1
+ /// <reference types="bun-types" />
2
+
3
+ /**
4
+ * E2E coverage for in-package HTML data apps:
5
+ * - static-file serving (`serveFromPackage`) from the package's public/
6
+ * directory only, with realpath containment and HTML-only CSP framing,
7
+ * - the `/pages` list endpoint (bare `Page[]`, the house list shape),
8
+ * - the `/events` SSE stream and its input validation.
9
+ *
10
+ * These routes touch the live filesystem and carry the security-relevant
11
+ * branches (403 containment, 404 for files outside public/, 400 name
12
+ * validation), so they're exercised against the real Express app.
13
+ */
14
+
15
+ import { afterAll, beforeAll, describe, expect, it } from "bun:test";
16
+ import fs from "fs";
17
+ import path from "path";
18
+ import { fileURLToPath } from "url";
19
+ import { RestE2EEnv, startRestE2E } from "../../harness/rest_e2e";
20
+
21
+ const __filename = fileURLToPath(import.meta.url);
22
+ const __dirname = path.dirname(__filename);
23
+
24
+ const ENV_NAME = "html-pages-test-env";
25
+ const PACKAGE_NAME = "html-pages-test";
26
+ // A second package in the same env that ships NO public/ directory, to pin the
27
+ // "package without public/" behavior (file requests 404, /pages returns []).
28
+ const NOPUBLIC_PACKAGE = "html-pages-nopublic";
29
+
30
+ const fixtureDir = path.resolve(__dirname, "../../fixtures/html-pages-test");
31
+ const nopublicFixtureDir = path.resolve(
32
+ __dirname,
33
+ "../../fixtures/html-pages-nopublic",
34
+ );
35
+ // The "malicious package" escape class: a symlink inside the served public/
36
+ // directory that points outside it. We plant it in the *served* copy under
37
+ // publisher_data AFTER the package mounts — never in the source fixture — so it
38
+ // never gets routed through env-creation's `fs.cp`. (Copying an absolute-target
39
+ // symlink is not portable across platforms and was breaking CI on Linux.) The
40
+ // package is served from a copy (no --watch-env), at <SERVER_ROOT>/
41
+ // publisher_data/<env>/<pkg>/public; SERVER_ROOT defaults to the server package
42
+ // dir under `bun test`.
43
+ const serverPkgRoot = path.resolve(__dirname, "../../..");
44
+ const servedEscapeLink = path.join(
45
+ serverPkgRoot,
46
+ "publisher_data",
47
+ ENV_NAME,
48
+ PACKAGE_NAME,
49
+ "public",
50
+ "escape.html",
51
+ );
52
+ // A second planted symlink: the realistic "escape public/" vector, a link
53
+ // inside public/ pointing at a package-root sibling (../report.malloy). It must
54
+ // be rejected (403) just like the absolute /etc/hosts escape above.
55
+ const servedSiblingLink = path.join(
56
+ serverPkgRoot,
57
+ "publisher_data",
58
+ ENV_NAME,
59
+ PACKAGE_NAME,
60
+ "public",
61
+ "leak.html",
62
+ );
63
+
64
+ interface PageItem {
65
+ resource?: string;
66
+ packageName?: string;
67
+ path?: string;
68
+ title?: string;
69
+ }
70
+
71
+ // Creating a symlink that escapes the package needs privileges the Windows CI
72
+ // runner lacks (SeCreateSymbolicLinkPrivilege), and the escape target
73
+ // (/etc/hosts) is Unix-only — so the one symlink-escape case is skipped on
74
+ // Windows (see `itEscape` below). The rest of the suite (serving, manifest
75
+ // blocking, 404s, /pages) runs on every platform and is the valuable Windows
76
+ // coverage of serveFromPackage's path handling (separators, drive letters,
77
+ // case-insensitive manifest match, realpath containment).
78
+ const isWindows = process.platform === "win32";
79
+ const itEscape = isWindows ? it.skip : it;
80
+
81
+ describe("In-package HTML data apps (E2E)", () => {
82
+ let env: (RestE2EEnv & { stop(): Promise<void> }) | null = null;
83
+ let baseUrl: string;
84
+
85
+ const pkgUrl = (sub: string) =>
86
+ `${baseUrl}/environments/${ENV_NAME}/packages/${PACKAGE_NAME}${sub}`;
87
+ const apiUrl = (sub: string) =>
88
+ `${baseUrl}/api/v0/environments/${ENV_NAME}/packages/${PACKAGE_NAME}${sub}`;
89
+
90
+ beforeAll(async () => {
91
+ env = await startRestE2E();
92
+ baseUrl = env.baseUrl;
93
+
94
+ const createRes = await fetch(`${baseUrl}/api/v0/environments`, {
95
+ method: "POST",
96
+ headers: { "Content-Type": "application/json" },
97
+ body: JSON.stringify({
98
+ name: ENV_NAME,
99
+ packages: [
100
+ { name: PACKAGE_NAME, location: fixtureDir },
101
+ { name: NOPUBLIC_PACKAGE, location: nopublicFixtureDir },
102
+ ],
103
+ connections: [],
104
+ }),
105
+ });
106
+ if (!createRes.ok) {
107
+ const body = await createRes.text();
108
+ throw new Error(
109
+ `Failed to create test environment (${createRes.status}): ${body}`,
110
+ );
111
+ }
112
+
113
+ const waitForPackage = async (pkg: string) => {
114
+ const deadline = Date.now() + 30_000;
115
+ while (Date.now() < deadline) {
116
+ try {
117
+ const res = await fetch(
118
+ `${baseUrl}/api/v0/environments/${ENV_NAME}/packages/${pkg}`,
119
+ );
120
+ if (res.ok) return;
121
+ } catch {
122
+ // not ready yet
123
+ }
124
+ await new Promise((r) => setTimeout(r, 500));
125
+ }
126
+ throw new Error(`Package ${pkg} did not become available in time`);
127
+ };
128
+ await waitForPackage(PACKAGE_NAME);
129
+ await waitForPackage(NOPUBLIC_PACKAGE);
130
+
131
+ // Now that the package is mounted, plant the escape symlinks directly in
132
+ // the served public/ copy (post-fs.cp): one pointing fully outside the
133
+ // package (/etc/hosts) and one at a package-root sibling (../report.malloy),
134
+ // the realistic "escape public/" vector. Unlink any stale link first, then
135
+ // create fresh WITHOUT swallowing errors, so a failed plant fails the suite
136
+ // loudly instead of silently skipping the security-critical assertions.
137
+ // Skipped on Windows, where the matching tests (itEscape) are skipped too.
138
+ if (!isWindows) {
139
+ for (const link of [servedEscapeLink, servedSiblingLink]) {
140
+ try {
141
+ fs.unlinkSync(link);
142
+ } catch {
143
+ // no stale link to remove
144
+ }
145
+ }
146
+ fs.symlinkSync("/etc/hosts", servedEscapeLink);
147
+ fs.symlinkSync("../report.malloy", servedSiblingLink);
148
+ }
149
+ });
150
+
151
+ afterAll(async () => {
152
+ // Always tear down the env so a partially-set-up run can't leave residue
153
+ // in the shared EnvironmentStore for later test files in this process.
154
+ if (baseUrl) {
155
+ try {
156
+ await fetch(`${baseUrl}/api/v0/environments/${ENV_NAME}`, {
157
+ method: "DELETE",
158
+ });
159
+ } catch {
160
+ // best-effort
161
+ }
162
+ }
163
+ // DELETE removes the served dir (and the symlink within it); this is a
164
+ // belt-and-suspenders unlink in case the env was never created.
165
+ for (const link of [servedEscapeLink, servedSiblingLink]) {
166
+ try {
167
+ fs.unlinkSync(link);
168
+ } catch {
169
+ // best-effort
170
+ }
171
+ }
172
+ await env?.stop();
173
+ env = null;
174
+ });
175
+
176
+ // ── static-file serving ──────────────────────────────────────────
177
+
178
+ it("serves index.html at the package root (directory index)", async () => {
179
+ const res = await fetch(pkgUrl("/"));
180
+ expect(res.status).toBe(200);
181
+ const body = await res.text();
182
+ expect(body).toContain("Hello from the in-package data app");
183
+ });
184
+
185
+ it("308-redirects the package root (no trailing slash) to the canonical path", async () => {
186
+ // The redirect target is rebuilt from the route params + parsed query
187
+ // (canonical, same-origin), not the raw request URL.
188
+ const res = await fetch(
189
+ `${baseUrl}/environments/${ENV_NAME}/packages/${PACKAGE_NAME}`,
190
+ { redirect: "manual" },
191
+ );
192
+ expect(res.status).toBe(308);
193
+ expect(res.headers.get("location")).toBe(
194
+ `/environments/${ENV_NAME}/packages/${PACKAGE_NAME}/`,
195
+ );
196
+ // The query string is preserved, placed before the appended slash.
197
+ const withQuery = await fetch(
198
+ `${baseUrl}/environments/${ENV_NAME}/packages/${PACKAGE_NAME}?embed_token=abc`,
199
+ { redirect: "manual" },
200
+ );
201
+ expect(withQuery.status).toBe(308);
202
+ expect(withQuery.headers.get("location")).toBe(
203
+ `/environments/${ENV_NAME}/packages/${PACKAGE_NAME}/?embed_token=abc`,
204
+ );
205
+ });
206
+
207
+ it("sets frame-ancestors CSP on HTML responses and clears X-Frame-Options", async () => {
208
+ const res = await fetch(pkgUrl("/index.html"));
209
+ expect(res.status).toBe(200);
210
+ expect(res.headers.get("content-security-policy")).toBe(
211
+ "frame-ancestors *",
212
+ );
213
+ expect(res.headers.get("x-frame-options")).toBeNull();
214
+ expect(res.headers.get("x-content-type-options")).toBe("nosniff");
215
+ });
216
+
217
+ it("does NOT set the framing CSP on non-HTML assets", async () => {
218
+ const res = await fetch(pkgUrl("/assets/app.css"));
219
+ expect(res.status).toBe(200);
220
+ // CSP framing is only meaningful on documents; assets keep their default.
221
+ expect(res.headers.get("content-security-policy")).toBeNull();
222
+ expect(res.headers.get("x-content-type-options")).toBe("nosniff");
223
+ });
224
+
225
+ it("404s a missing file", async () => {
226
+ const res = await fetch(pkgUrl("/does-not-exist.html"));
227
+ expect(res.status).toBe(404);
228
+ });
229
+
230
+ it("serves a page from a subdirectory", async () => {
231
+ const res = await fetch(pkgUrl("/sub/page2.html"));
232
+ expect(res.status).toBe(200);
233
+ expect(await res.text()).toContain("A page in a subdirectory");
234
+ });
235
+
236
+ it("serves only files under public/; package internals are never served", async () => {
237
+ // The manifest, models, and data live at the package root, outside
238
+ // public/. Each exists in the fixture (asserted), so the 404 proves the
239
+ // public/ boundary blocked it, not a missing file. This is what keeps
240
+ // raw data, model source, and secrets off the static route and behind
241
+ // the per-model #(authorize) and query controls.
242
+ const blocked = ["publisher.json", "report.malloy", "data.csv"];
243
+ for (const name of blocked) {
244
+ expect(fs.existsSync(path.join(fixtureDir, name))).toBe(true);
245
+ const res = await fetch(pkgUrl(`/${name}`));
246
+ expect(res.status).toBe(404);
247
+ }
248
+ });
249
+
250
+ it("serves any file type placed under public/ (no extension filter)", async () => {
251
+ // public/ is the boundary, not a file-extension allowlist: a data-typed
252
+ // file the author deliberately put under public/ is served. (Raw data at
253
+ // the package root is still never served, per the test above.)
254
+ const res = await fetch(pkgUrl("/data.json"));
255
+ expect(res.status).toBe(200);
256
+ });
257
+
258
+ it("404s file requests for a package with no public/ directory", async () => {
259
+ const res = await fetch(
260
+ `${baseUrl}/environments/${ENV_NAME}/packages/${NOPUBLIC_PACKAGE}/index.html`,
261
+ );
262
+ expect(res.status).toBe(404);
263
+ });
264
+
265
+ it("lists no pages for a package with no public/ directory", async () => {
266
+ const res = await fetch(
267
+ `${baseUrl}/api/v0/environments/${ENV_NAME}/packages/${NOPUBLIC_PACKAGE}/pages`,
268
+ );
269
+ expect(res.status).toBe(200);
270
+ expect(await res.json()).toEqual([]);
271
+ });
272
+
273
+ it("rejects URL-encoded path traversal out of public/", async () => {
274
+ // Pre-encoded so the segments aren't normalized away before reaching the
275
+ // server. Whatever the rejection mode (safeJoinUnderRoot 400, realpath
276
+ // containment 403, or normalize-then-missing 404), package internals must
277
+ // never be served (never 200).
278
+ const encoded = [
279
+ "..%2f..%2freport.malloy",
280
+ "%2e%2e%2f%2e%2e%2fpublisher.json",
281
+ "..%2f..%2fdata.csv",
282
+ ];
283
+ for (const p of encoded) {
284
+ const res = await fetch(
285
+ `${baseUrl}/environments/${ENV_NAME}/packages/${PACKAGE_NAME}/${p}`,
286
+ );
287
+ expect([400, 403, 404]).toContain(res.status);
288
+ }
289
+ });
290
+
291
+ itEscape(
292
+ "rejects a symlink in public/ that escapes the package with 403",
293
+ async () => {
294
+ // Precondition: the plant succeeded, so a 403 means realpath
295
+ // containment caught the escape, not a missing-file 404.
296
+ expect(fs.lstatSync(servedEscapeLink).isSymbolicLink()).toBe(true);
297
+ const res = await fetch(pkgUrl("/escape.html"));
298
+ expect(res.status).toBe(403);
299
+ },
300
+ );
301
+
302
+ itEscape(
303
+ "rejects a symlink from public/ to a package-root sibling with 403",
304
+ async () => {
305
+ // The realistic escape: public/leak.html -> ../report.malloy reaches a
306
+ // file at the package root, outside public/. Must be 403, not served.
307
+ expect(fs.lstatSync(servedSiblingLink).isSymbolicLink()).toBe(true);
308
+ const res = await fetch(pkgUrl("/leak.html"));
309
+ expect(res.status).toBe(403);
310
+ },
311
+ );
312
+
313
+ // ── /pages list endpoint ─────────────────────────────────────────
314
+
315
+ it("lists pages as a bare Page[] (not a {pages} envelope)", async () => {
316
+ const res = await fetch(apiUrl("/pages"));
317
+ expect(res.status).toBe(200);
318
+ const body = (await res.json()) as unknown;
319
+ expect(Array.isArray(body)).toBe(true);
320
+
321
+ const pages = body as PageItem[];
322
+ const paths = pages.map((p) => p.path).sort();
323
+ // Only HTML files under public/ are listed; the toEqual pins the exact
324
+ // set, so non-public files (manifest, models, data) can't appear.
325
+ expect(paths).toEqual(["index.html", "sub/page2.html"]);
326
+
327
+ const index = pages.find((p) => p.path === "index.html");
328
+ expect(index?.title).toBe("Carrier Dashboard");
329
+ expect(index?.packageName).toBe(PACKAGE_NAME);
330
+ expect(index?.resource).toBe(
331
+ `/environments/${ENV_NAME}/packages/${PACKAGE_NAME}/index.html`,
332
+ );
333
+ });
334
+
335
+ it("400s a malformed environment/package name on /pages", async () => {
336
+ // getEnvironment runs assertSafePackageName, so a name outside
337
+ // IdentifierPattern is a 400 (now documented on list-pages in api-doc).
338
+ const res = await fetch(
339
+ `${baseUrl}/api/v0/environments/bad%20name/packages/${PACKAGE_NAME}/pages`,
340
+ );
341
+ expect(res.status).toBe(400);
342
+ });
343
+
344
+ // ── /events SSE stream ───────────────────────────────────────────
345
+
346
+ it("400s an illegal environment/package name on /events", async () => {
347
+ // A space is outside IdentifierPattern → assertSafePackageName rejects.
348
+ const res = await fetch(
349
+ `${baseUrl}/api/v0/environments/bad%20name/packages/${PACKAGE_NAME}/events`,
350
+ );
351
+ expect(res.status).toBe(400);
352
+ });
353
+
354
+ it("404s an unknown package on /events", async () => {
355
+ const res = await fetch(
356
+ `${baseUrl}/api/v0/environments/${ENV_NAME}/packages/no-such-pkg/events`,
357
+ );
358
+ expect(res.status).toBe(404);
359
+ });
360
+
361
+ it("opens an SSE stream announcing hello + mode", async () => {
362
+ const controller = new AbortController();
363
+ const res = await fetch(apiUrl("/events"), {
364
+ signal: controller.signal,
365
+ });
366
+ expect(res.status).toBe(200);
367
+ expect(res.headers.get("content-type")).toContain("text/event-stream");
368
+
369
+ const reader = res.body!.getReader();
370
+ const { value } = await reader.read();
371
+ const chunk = new TextDecoder().decode(value);
372
+ expect(chunk).toContain("event: hello");
373
+ expect(chunk).toContain("event: mode");
374
+
375
+ await reader.cancel();
376
+ controller.abort();
377
+ });
378
+ });