@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,932 @@
1
+ import { DuckDBConnection } from "@malloydata/db-duckdb";
2
+ import { Connection, GivenValue } from "@malloydata/malloy";
3
+ import { afterAll, beforeAll, describe, expect, it } from "bun:test";
4
+ import fs from "fs/promises";
5
+ import os from "os";
6
+ import path from "path";
7
+ import { AccessDeniedError } from "../errors";
8
+ import { Model } from "./model";
9
+
10
+ // Introspection, compile-time validation, and the runtime gate for
11
+ // #(authorize) / ##(authorize).
12
+
13
+ const TEST_DIR = path.join(os.tmpdir(), "authorize-integration-tests");
14
+ const TEST_DB_DIR = path.join(TEST_DIR, "db");
15
+ const TEST_DB_PATH = path.join(TEST_DB_DIR, "test.duckdb");
16
+ const TEST_PKG_DIR = path.join(TEST_DIR, "pkg");
17
+
18
+ let duckdbConnection: DuckDBConnection;
19
+
20
+ const SEED_SQL = `
21
+ CREATE TABLE IF NOT EXISTS customers (
22
+ id INTEGER,
23
+ name VARCHAR,
24
+ region VARCHAR
25
+ );
26
+ INSERT INTO customers VALUES (1, 'a', 'us-west'), (2, 'b', 'us-east');
27
+ `;
28
+
29
+ function getConnections(): Map<string, Connection> {
30
+ const map = new Map<string, Connection>();
31
+ map.set("duckdb", duckdbConnection);
32
+ return map;
33
+ }
34
+
35
+ async function writeModel(filename: string, content: string): Promise<void> {
36
+ await fs.writeFile(path.join(TEST_PKG_DIR, filename), content, "utf-8");
37
+ }
38
+
39
+ function sourceNamed(model: Model, name: string) {
40
+ return model.getSources()?.find((s) => s.name === name);
41
+ }
42
+
43
+ beforeAll(async () => {
44
+ await fs.mkdir(TEST_DB_DIR, { recursive: true });
45
+ await fs.mkdir(TEST_PKG_DIR, { recursive: true });
46
+ duckdbConnection = new DuckDBConnection("duckdb", TEST_DB_PATH, TEST_DB_DIR);
47
+ for (const stmt of SEED_SQL.trim().split(";").filter(Boolean)) {
48
+ await duckdbConnection.runSQL(stmt.trim() + ";");
49
+ }
50
+ });
51
+
52
+ afterAll(async () => {
53
+ try {
54
+ await duckdbConnection.close();
55
+ await new Promise((resolve) => setTimeout(resolve, 100));
56
+ await fs.rm(TEST_DIR, { recursive: true, force: true });
57
+ } catch {
58
+ // ignore cleanup errors
59
+ }
60
+ });
61
+
62
+ describe("authorize annotation introspection", () => {
63
+ it("collects file-level then source-level expressions as one list", async () => {
64
+ await writeModel(
65
+ "disjunction.malloy",
66
+ `##! experimental.givens
67
+
68
+ given:
69
+ ROLE :: string
70
+ REGION :: string
71
+
72
+ ##(authorize) "$ROLE = 'admin'"
73
+
74
+ #(authorize) "$REGION = 'us-west'"
75
+ source: regional is duckdb.table('customers')
76
+ `,
77
+ );
78
+ const model = await Model.create(
79
+ "test-pkg",
80
+ TEST_PKG_DIR,
81
+ "disjunction.malloy",
82
+ getConnections(),
83
+ );
84
+
85
+ // File-level first, then the source's own.
86
+ expect(model.getAuthorize("regional")).toEqual([
87
+ "$ROLE = 'admin'",
88
+ "$REGION = 'us-west'",
89
+ ]);
90
+ expect(sourceNamed(model, "regional")?.authorize).toEqual([
91
+ "$ROLE = 'admin'",
92
+ "$REGION = 'us-west'",
93
+ ]);
94
+ });
95
+
96
+ it("does NOT inherit a base source's authorize through extend", async () => {
97
+ await writeModel(
98
+ "extend.malloy",
99
+ `##! experimental.givens
100
+
101
+ given:
102
+ ROLE :: string
103
+
104
+ // Locked base.
105
+ #(authorize) "false"
106
+ source: customers_raw is duckdb.table('customers')
107
+
108
+ // Extension with its own gate — must NOT pick up the base's "false".
109
+ #(authorize) "$ROLE = 'analyst'"
110
+ source: customers_marketing is customers_raw extend {
111
+ measure: customer_count is count()
112
+ }
113
+ `,
114
+ );
115
+ const model = await Model.create(
116
+ "test-pkg",
117
+ TEST_PKG_DIR,
118
+ "extend.malloy",
119
+ getConnections(),
120
+ );
121
+
122
+ // Base keeps its own lock.
123
+ expect(model.getAuthorize("customers_raw")).toEqual(["false"]);
124
+ // Extension is governed ONLY by its own gate — the base "false" is gone.
125
+ expect(model.getAuthorize("customers_marketing")).toEqual([
126
+ "$ROLE = 'analyst'",
127
+ ]);
128
+ });
129
+
130
+ it("applies a file-level gate to a source with no own authorize", async () => {
131
+ await writeModel(
132
+ "file_only.malloy",
133
+ `##! experimental.givens
134
+
135
+ given:
136
+ ROLE :: string
137
+
138
+ ##(authorize) "$ROLE = 'admin'"
139
+
140
+ source: plain is duckdb.table('customers')
141
+ `,
142
+ );
143
+ const model = await Model.create(
144
+ "test-pkg",
145
+ TEST_PKG_DIR,
146
+ "file_only.malloy",
147
+ getConnections(),
148
+ );
149
+
150
+ expect(model.getAuthorize("plain")).toEqual(["$ROLE = 'admin'"]);
151
+ });
152
+
153
+ it("applies a file-level gate inherited from an imported model", async () => {
154
+ // Regression for the hand-rolled model-annotation fold (malloy 0.0.405+
155
+ // removed ModelDef.annotation): a `##(authorize)` declared in an
156
+ // imported file must flow into the importing file's file-level gate even
157
+ // when the importer declares no `##` of its own. The fold must match
158
+ // malloy's getModelAnnotations (skip empty-ownNotes links) or `.notes`
159
+ // returns [] here and the gate silently drops — fail-open.
160
+ await writeModel(
161
+ "auth_base.malloy",
162
+ `##! experimental.givens
163
+
164
+ given:
165
+ ROLE :: string
166
+
167
+ ##(authorize) "$ROLE = 'admin'"
168
+ `,
169
+ );
170
+ await writeModel(
171
+ "auth_importer.malloy",
172
+ `import "auth_base.malloy"
173
+
174
+ source: inherited_gate is duckdb.table('customers') extend {
175
+ measure: c is count()
176
+ }
177
+ `,
178
+ );
179
+ const model = await Model.create(
180
+ "test-pkg",
181
+ TEST_PKG_DIR,
182
+ "auth_importer.malloy",
183
+ getConnections(),
184
+ );
185
+
186
+ expect(model.getAuthorize("inherited_gate")).toEqual(["$ROLE = 'admin'"]);
187
+ });
188
+
189
+ it("fails model load on a malformed authorize annotation (no silent drop)", async () => {
190
+ await writeModel(
191
+ "malformed.malloy",
192
+ `#(authorize) notquoted
193
+ source: broken is duckdb.table('customers')
194
+ `,
195
+ );
196
+ const model = await Model.create(
197
+ "test-pkg",
198
+ TEST_PKG_DIR,
199
+ "malformed.malloy",
200
+ getConnections(),
201
+ );
202
+
203
+ // A malformed gate must surface as a compilation error, not vanish.
204
+ const err = model.getNotebookError();
205
+ expect(err).toBeDefined();
206
+ expect(err?.message).toMatch(/quote/i);
207
+ // No sources surfaced for a failed compile — the gate is not silently
208
+ // reported as unrestricted.
209
+ expect(model.getSources()).toBeUndefined();
210
+ });
211
+
212
+ it("treats a source with no authorize annotations as unrestricted", async () => {
213
+ await writeModel(
214
+ "none.malloy",
215
+ `source: open_source is duckdb.table('customers')
216
+ `,
217
+ );
218
+ const model = await Model.create(
219
+ "test-pkg",
220
+ TEST_PKG_DIR,
221
+ "none.malloy",
222
+ getConnections(),
223
+ );
224
+
225
+ expect(model.getAuthorize("open_source")).toEqual([]);
226
+ expect(sourceNamed(model, "open_source")?.authorize).toBeUndefined();
227
+ });
228
+ });
229
+
230
+ describe("authorize annotation compile-time validation", () => {
231
+ it("loads a valid expression that references a value-less given", async () => {
232
+ // The probe is compiled, not run, so a given with no default/value does
233
+ // NOT cause a false failure (the original getSQL approach would have).
234
+ await writeModel(
235
+ "valid_valueless.malloy",
236
+ `##! experimental.givens
237
+
238
+ given:
239
+ ROLE :: string
240
+
241
+ #(authorize) "$ROLE = 'analyst'"
242
+ source: gated is duckdb.table('customers')
243
+ `,
244
+ );
245
+ const model = await Model.create(
246
+ "test-pkg",
247
+ TEST_PKG_DIR,
248
+ "valid_valueless.malloy",
249
+ getConnections(),
250
+ );
251
+
252
+ expect(model.getNotebookError()).toBeUndefined();
253
+ expect(model.getAuthorize("gated")).toEqual(["$ROLE = 'analyst'"]);
254
+ });
255
+
256
+ it("fails model load when an expression references an unknown given", async () => {
257
+ await writeModel(
258
+ "unknown_given.malloy",
259
+ `##! experimental.givens
260
+
261
+ given:
262
+ ROLE :: string
263
+
264
+ #(authorize) "$NOPE = 'x'"
265
+ source: gated is duckdb.table('customers')
266
+ `,
267
+ );
268
+ const model = await Model.create(
269
+ "test-pkg",
270
+ TEST_PKG_DIR,
271
+ "unknown_given.malloy",
272
+ getConnections(),
273
+ );
274
+
275
+ const err = model.getNotebookError();
276
+ expect(err).toBeDefined();
277
+ // Names the source and surfaces the underlying Malloy reason.
278
+ expect(err?.message).toContain("gated");
279
+ expect(err?.message).toMatch(/NOPE|not declared/i);
280
+ // Redaction policy (pinned): the model-load 424 is author-facing, so it
281
+ // KEEPS the full expression text (needed to fix a malformed annotation).
282
+ // Only the runtime 403 redacts to the source name. If this assertion ever
283
+ // flips, the redaction split was changed — make it a conscious decision.
284
+ expect(err?.message).toContain("$NOPE = 'x'");
285
+ });
286
+
287
+ it("fails model load when an expression references a source field", async () => {
288
+ await writeModel(
289
+ "field_ref.malloy",
290
+ `#(authorize) "some_field = 1"
291
+ source: gated is duckdb.table('customers')
292
+ `,
293
+ );
294
+ const model = await Model.create(
295
+ "test-pkg",
296
+ TEST_PKG_DIR,
297
+ "field_ref.malloy",
298
+ getConnections(),
299
+ );
300
+
301
+ const err = model.getNotebookError();
302
+ expect(err).toBeDefined();
303
+ expect(err?.message).toContain("gated");
304
+ });
305
+
306
+ it("does not reject a type-mismatched comparison (not a Malloy compile error)", async () => {
307
+ // Documents the boundary: `$ROLE = 5` is not a compile error; such a gate
308
+ // simply evaluates per the warehouse at the runtime gate.
309
+ await writeModel(
310
+ "type_mismatch.malloy",
311
+ `##! experimental.givens
312
+
313
+ given:
314
+ ROLE :: string
315
+
316
+ #(authorize) "$ROLE = 5"
317
+ source: gated is duckdb.table('customers')
318
+ `,
319
+ );
320
+ const model = await Model.create(
321
+ "test-pkg",
322
+ TEST_PKG_DIR,
323
+ "type_mismatch.malloy",
324
+ getConnections(),
325
+ );
326
+
327
+ expect(model.getNotebookError()).toBeUndefined();
328
+ });
329
+
330
+ it("fails model load when a file-level ##(authorize) references an unknown given", async () => {
331
+ await writeModel(
332
+ "file_unknown_given.malloy",
333
+ `##! experimental.givens
334
+
335
+ given:
336
+ ROLE :: string
337
+
338
+ ##(authorize) "$NOPE = 'x'"
339
+
340
+ source: gated is duckdb.table('customers')
341
+ `,
342
+ );
343
+ const model = await Model.create(
344
+ "test-pkg",
345
+ TEST_PKG_DIR,
346
+ "file_unknown_given.malloy",
347
+ getConnections(),
348
+ );
349
+
350
+ const err = model.getNotebookError();
351
+ expect(err).toBeDefined();
352
+ expect(err?.message).toContain("gated");
353
+ expect(err?.message).toMatch(/NOPE|not declared/i);
354
+ });
355
+
356
+ it("validates expressions over number and list givens, not just strings", async () => {
357
+ await writeModel(
358
+ "given_types.malloy",
359
+ `##! experimental.givens
360
+
361
+ given:
362
+ AGE :: number
363
+ TENANT :: string
364
+ ALLOWED :: string[]
365
+
366
+ #(authorize) "$AGE > 18"
367
+ #(authorize) "$TENANT in $ALLOWED"
368
+ source: gated is duckdb.table('customers')
369
+ `,
370
+ );
371
+ const model = await Model.create(
372
+ "test-pkg",
373
+ TEST_PKG_DIR,
374
+ "given_types.malloy",
375
+ getConnections(),
376
+ );
377
+
378
+ expect(model.getNotebookError()).toBeUndefined();
379
+ expect(model.getAuthorize("gated")).toEqual([
380
+ "$AGE > 18",
381
+ "$TENANT in $ALLOWED",
382
+ ]);
383
+ });
384
+ });
385
+
386
+ describe("authorize runtime gate", () => {
387
+ // Helper: run an ad-hoc query through the full getQueryResults path (which
388
+ // is where the gate fires). Returns the result or throws AccessDeniedError.
389
+ async function runGated(
390
+ modelFile: string,
391
+ query: string,
392
+ givens?: Record<string, GivenValue>,
393
+ bypassFilters?: boolean,
394
+ ) {
395
+ const model = await Model.create(
396
+ "test-pkg",
397
+ TEST_PKG_DIR,
398
+ modelFile,
399
+ getConnections(),
400
+ );
401
+ return model.getQueryResults(
402
+ undefined,
403
+ undefined,
404
+ query,
405
+ undefined,
406
+ bypassFilters,
407
+ givens,
408
+ );
409
+ }
410
+
411
+ const SINGLE_GATE = `##! experimental.givens
412
+
413
+ given:
414
+ ROLE :: string
415
+
416
+ #(authorize) "$ROLE = 'analyst'"
417
+ source: gated is duckdb.table('customers') extend { measure: c is count() }
418
+ `;
419
+
420
+ it("allows the query when a given satisfies the gate", async () => {
421
+ await writeModel("rt_single.malloy", SINGLE_GATE);
422
+ const { result } = await runGated(
423
+ "rt_single.malloy",
424
+ "run: gated -> { aggregate: c }",
425
+ { ROLE: "analyst" },
426
+ );
427
+ expect(result.data).toBeDefined();
428
+ });
429
+
430
+ it("denies (403) when no given satisfies the gate", async () => {
431
+ await writeModel("rt_single.malloy", SINGLE_GATE);
432
+ await expect(
433
+ runGated("rt_single.malloy", "run: gated -> { aggregate: c }", {
434
+ ROLE: "intern",
435
+ }),
436
+ ).rejects.toBeInstanceOf(AccessDeniedError);
437
+ });
438
+
439
+ it("denies when the referenced given has no value (fail closed)", async () => {
440
+ await writeModel("rt_single.malloy", SINGLE_GATE);
441
+ await expect(
442
+ runGated("rt_single.malloy", "run: gated -> { aggregate: c }", {}),
443
+ ).rejects.toBeInstanceOf(AccessDeniedError);
444
+ });
445
+
446
+ it("still enforces the gate when bypassFilters is true (authorize is not a filter)", async () => {
447
+ await writeModel("rt_single.malloy", SINGLE_GATE);
448
+ await expect(
449
+ runGated(
450
+ "rt_single.malloy",
451
+ "run: gated -> { aggregate: c }",
452
+ { ROLE: "intern" },
453
+ true,
454
+ ),
455
+ ).rejects.toBeInstanceOf(AccessDeniedError);
456
+ });
457
+
458
+ // A model that declares no `##` of its own and inherits its file-level gate
459
+ // entirely from an imported model. Exercises the model-annotation fold end
460
+ // to end: parse → fold → fileLevelAuthorize → runtime gate.
461
+ const IMPORT_BASE = `##! experimental.givens
462
+
463
+ given:
464
+ ROLE :: string
465
+
466
+ ##(authorize) "$ROLE = 'admin'"
467
+ `;
468
+ const IMPORT_GATED = `import "rt_auth_base.malloy"
469
+
470
+ source: inherited_gate is duckdb.table('customers') extend {
471
+ measure: c is count()
472
+ }
473
+ `;
474
+
475
+ it("enforces a file-level gate inherited from an imported model (allow with role)", async () => {
476
+ await writeModel("rt_auth_base.malloy", IMPORT_BASE);
477
+ await writeModel("rt_import.malloy", IMPORT_GATED);
478
+ const { result } = await runGated(
479
+ "rt_import.malloy",
480
+ "run: inherited_gate -> { aggregate: c }",
481
+ { ROLE: "admin" },
482
+ );
483
+ expect(result.data).toBeDefined();
484
+ });
485
+
486
+ it("enforces a file-level gate inherited from an imported model (deny without role — not fail-open)", async () => {
487
+ await writeModel("rt_auth_base.malloy", IMPORT_BASE);
488
+ await writeModel("rt_import.malloy", IMPORT_GATED);
489
+ await expect(
490
+ runGated(
491
+ "rt_import.malloy",
492
+ "run: inherited_gate -> { aggregate: c }",
493
+ {
494
+ ROLE: "intern",
495
+ },
496
+ ),
497
+ ).rejects.toBeInstanceOf(AccessDeniedError);
498
+ });
499
+
500
+ const DISJUNCTION = `##! experimental.givens
501
+
502
+ given:
503
+ ROLE :: string
504
+ REGION :: string
505
+
506
+ ##(authorize) "$ROLE = 'admin'"
507
+
508
+ #(authorize) "$REGION = 'us-west'"
509
+ source: regional is duckdb.table('customers') extend { measure: c is count() }
510
+ `;
511
+
512
+ it("grants on the file-level gate even when the OTHER disjunct's given is missing", async () => {
513
+ // The key OR-semantics case: admin supplies only ROLE; REGION is absent.
514
+ // A disjunct that can't evaluate (missing given) must not sink the whole
515
+ // request — the satisfied $ROLE='admin' branch still grants.
516
+ await writeModel("rt_disj.malloy", DISJUNCTION);
517
+ const { result } = await runGated(
518
+ "rt_disj.malloy",
519
+ "run: regional -> { aggregate: c }",
520
+ { ROLE: "admin" },
521
+ );
522
+ expect(result.data).toBeDefined();
523
+ });
524
+
525
+ it("grants on the source-level gate even when the file-level disjunct's given is missing", async () => {
526
+ await writeModel("rt_disj.malloy", DISJUNCTION);
527
+ const { result } = await runGated(
528
+ "rt_disj.malloy",
529
+ "run: regional -> { aggregate: c }",
530
+ { REGION: "us-west" },
531
+ );
532
+ expect(result.data).toBeDefined();
533
+ });
534
+
535
+ it("denies when neither disjunct is satisfied", async () => {
536
+ await writeModel("rt_disj.malloy", DISJUNCTION);
537
+ await expect(
538
+ runGated("rt_disj.malloy", "run: regional -> { aggregate: c }", {
539
+ ROLE: "nobody",
540
+ REGION: "nowhere",
541
+ }),
542
+ ).rejects.toBeInstanceOf(AccessDeniedError);
543
+ });
544
+
545
+ it("gates a named query that targets a gated source (no sourceName supplied)", async () => {
546
+ await writeModel(
547
+ "rt_namedq.malloy",
548
+ `##! experimental.givens
549
+
550
+ given:
551
+ ROLE :: string
552
+
553
+ #(authorize) "$ROLE = 'analyst'"
554
+ source: gated is duckdb.table('customers') extend { measure: c is count() }
555
+
556
+ query: secret is gated -> { aggregate: c }
557
+ `,
558
+ );
559
+ const model = await Model.create(
560
+ "test-pkg",
561
+ TEST_PKG_DIR,
562
+ "rt_namedq.malloy",
563
+ getConnections(),
564
+ );
565
+ // Named query, no sourceName — must still resolve to `gated` and gate it.
566
+ await expect(
567
+ model.getQueryResults(
568
+ undefined,
569
+ "secret",
570
+ undefined,
571
+ undefined,
572
+ false,
573
+ {
574
+ ROLE: "intern",
575
+ },
576
+ ),
577
+ ).rejects.toBeInstanceOf(AccessDeniedError);
578
+ // And it runs when the gate passes.
579
+ const { result } = await model.getQueryResults(
580
+ undefined,
581
+ "secret",
582
+ undefined,
583
+ undefined,
584
+ false,
585
+ { ROLE: "analyst" },
586
+ );
587
+ expect(result.data).toBeDefined();
588
+ });
589
+
590
+ it("gates an ad-hoc query even when a blank sourceName is supplied", async () => {
591
+ await writeModel("rt_single.malloy", SINGLE_GATE);
592
+ const model = await Model.create(
593
+ "test-pkg",
594
+ TEST_PKG_DIR,
595
+ "rt_single.malloy",
596
+ getConnections(),
597
+ );
598
+ // Blank sourceName must not skip the gate while the query-builder treats
599
+ // it as absent and runs the ad-hoc query.
600
+ await expect(
601
+ model.getQueryResults(
602
+ "",
603
+ undefined,
604
+ "run: gated -> { aggregate: c }",
605
+ undefined,
606
+ false,
607
+ { ROLE: "intern" },
608
+ ),
609
+ ).rejects.toBeInstanceOf(AccessDeniedError);
610
+ });
611
+
612
+ it("gates the source the query actually RUNS, not a decoy leading statement", async () => {
613
+ // Malloy runs the LAST `run:`. A multi-statement ad-hoc query that names
614
+ // an ungated source first and the gated source last must be gated on the
615
+ // gated source (the one that executes), not fooled by the leading one.
616
+ await writeModel(
617
+ "rt_multi.malloy",
618
+ `##! experimental.givens
619
+
620
+ given:
621
+ ROLE :: string
622
+
623
+ source: ungated is duckdb.table('customers') extend { measure: c is count() }
624
+
625
+ #(authorize) "$ROLE = 'analyst'"
626
+ source: gated is duckdb.table('customers') extend { measure: c is count() }
627
+ `,
628
+ );
629
+ await expect(
630
+ runGated(
631
+ "rt_multi.malloy",
632
+ "run: ungated -> { aggregate: c }\nrun: gated -> { aggregate: c }",
633
+ { ROLE: "intern" },
634
+ ),
635
+ ).rejects.toBeInstanceOf(AccessDeniedError);
636
+ });
637
+
638
+ it("leaves a source with no authorize annotations unrestricted", async () => {
639
+ await writeModel(
640
+ "rt_open.malloy",
641
+ `source: open_src is duckdb.table('customers') extend { measure: c is count() }
642
+ `,
643
+ );
644
+ const { result } = await runGated(
645
+ "rt_open.malloy",
646
+ "run: open_src -> { aggregate: c }",
647
+ );
648
+ expect(result.data).toBeDefined();
649
+ });
650
+
651
+ const FILE_LEVEL = `##! experimental.givens
652
+
653
+ given:
654
+ ROLE :: string
655
+
656
+ ##(authorize) "$ROLE = 'admin'"
657
+
658
+ source: declared is duckdb.table('customers') extend { measure: c is count() }
659
+ `;
660
+
661
+ it("applies a file-level gate to an ad-hoc query (no gate bypass)", async () => {
662
+ await writeModel("rt_filelevel.malloy", FILE_LEVEL);
663
+ // The model-wide file-level gate must apply to any ad-hoc query against
664
+ // the model, denying a non-admin with a 403.
665
+ await expect(
666
+ runGated("rt_filelevel.malloy", "run: declared -> { aggregate: c }", {
667
+ ROLE: "user",
668
+ }),
669
+ ).rejects.toBeInstanceOf(AccessDeniedError);
670
+ // An admin (file-level gate satisfied) can run it.
671
+ const { result } = await runGated(
672
+ "rt_filelevel.malloy",
673
+ "run: declared -> { aggregate: c }",
674
+ { ROLE: "admin" },
675
+ );
676
+ expect(result.data).toBeDefined();
677
+ });
678
+
679
+ it("rejects an ad-hoc inline-SQL query (restricted mode closes the raw-warehouse path)", async () => {
680
+ // Pre-#807 the file-level #(authorize) gate was the only thing between a
681
+ // caller and raw `duckdb.sql(...)`. #807's restricted mode now rejects
682
+ // inline raw SQL outright while resolving the compiled source — before the
683
+ // gate is reached — so the raw-warehouse path is closed at the compile
684
+ // layer regardless of givens (even for an admin who satisfies the gate).
685
+ await writeModel("rt_filelevel.malloy", FILE_LEVEL);
686
+ await expect(
687
+ runGated(
688
+ "rt_filelevel.malloy",
689
+ `run: duckdb.sql("SELECT 1 AS id") -> { aggregate: c is count() }`,
690
+ { ROLE: "admin" },
691
+ ),
692
+ ).rejects.toThrow(/raw SQL is not permitted/);
693
+ });
694
+
695
+ const MIXED_SOURCE_GATE = `##! experimental.givens
696
+
697
+ given:
698
+ ROLE :: string
699
+
700
+ #(authorize) "$ROLE = 'analyst'"
701
+ source: gated is duckdb.table('customers') extend { measure: c is count() }
702
+
703
+ source: open_src is duckdb.table('customers') extend { measure: c is count() }
704
+ `;
705
+
706
+ it("does not over-gate: a source-level gate is not model-wide", async () => {
707
+ // Control: a per-source gate applies only to that source, not the whole
708
+ // model. An ad-hoc query against an ungated declared source in the same
709
+ // model runs without any given.
710
+ await writeModel("rt_mixed.malloy", MIXED_SOURCE_GATE);
711
+ const { result } = await runGated(
712
+ "rt_mixed.malloy",
713
+ "run: open_src -> { aggregate: c }",
714
+ );
715
+ expect(result.data).toBeDefined();
716
+ });
717
+
718
+ it("gates a quoted-identifier source BEFORE compilation (no schema oracle)", async () => {
719
+ // A gated source whose Malloy name must be quoted (here, a hyphen) must be
720
+ // recognized by the early gate too. Otherwise a denied caller could probe
721
+ // a non-existent field and learn the schema from a pre-compilation Malloy
722
+ // field error instead of a clean 403.
723
+ await writeModel(
724
+ "rt_quoted.malloy",
725
+ `##! experimental.givens
726
+
727
+ given:
728
+ ROLE :: string
729
+
730
+ #(authorize) "$ROLE = 'analyst'"
731
+ source: \`gated-source\` is duckdb.table('customers') extend {
732
+ measure: c is count()
733
+ }
734
+ `,
735
+ );
736
+ // Probing a field that doesn't exist must deny (403) before compilation,
737
+ // not surface a Malloy "field not found" error.
738
+ await expect(
739
+ runGated(
740
+ "rt_quoted.malloy",
741
+ "run: `gated-source` -> { group_by: no_such_field }",
742
+ { ROLE: "viewer" },
743
+ ),
744
+ ).rejects.toBeInstanceOf(AccessDeniedError);
745
+ });
746
+
747
+ it("gates a notebook cell that runs a NAMED QUERY targeting a gated source", async () => {
748
+ // `run: secret` has no `->`, so source resolution must come from the
749
+ // compiled query, not a text regex — otherwise the gate is bypassed.
750
+ await writeModel(
751
+ "rt_nb.malloynb",
752
+ `>>>malloy
753
+ ##! experimental.givens
754
+
755
+ given:
756
+ ROLE :: string
757
+
758
+ #(authorize) "$ROLE = 'analyst'"
759
+ source: gated is duckdb.table('customers') extend { measure: c is count() }
760
+ query: secret is gated -> { aggregate: c }
761
+
762
+ >>>malloy
763
+ run: secret
764
+ `,
765
+ );
766
+ const model = await Model.create(
767
+ "test-pkg",
768
+ TEST_PKG_DIR,
769
+ "rt_nb.malloynb",
770
+ getConnections(),
771
+ );
772
+ // Cell 1 is `run: secret` — must be denied without the gate-passing given.
773
+ await expect(
774
+ model.executeNotebookCell(1, undefined, false, { ROLE: "intern" }),
775
+ ).rejects.toBeInstanceOf(AccessDeniedError);
776
+ // ...and allowed when the gate passes.
777
+ const ok = await model.executeNotebookCell(1, undefined, false, {
778
+ ROLE: "analyst",
779
+ });
780
+ expect(ok.result).toBeDefined();
781
+ });
782
+
783
+ const LOCKED_BASE = `##! experimental.givens
784
+
785
+ given:
786
+ ROLE :: string
787
+
788
+ #(authorize) "false"
789
+ source: base_locked is duckdb.table('customers') extend { measure: c is count() }
790
+
791
+ #(authorize) "$ROLE = 'analyst'"
792
+ source: ext_gated is base_locked extend {}
793
+
794
+ source: ext_nogate is base_locked extend {}
795
+
796
+ source: joiner is duckdb.table('customers') extend {
797
+ join_one: base_locked on id = base_locked.id
798
+ measure: c is count()
799
+ }
800
+ `;
801
+
802
+ it('denies a direct query against a base locked with #(authorize) "false"', async () => {
803
+ await writeModel("rt_locked.malloy", LOCKED_BASE);
804
+ await expect(
805
+ runGated("rt_locked.malloy", "run: base_locked -> { aggregate: c }", {
806
+ ROLE: "analyst",
807
+ }),
808
+ ).rejects.toBeInstanceOf(AccessDeniedError);
809
+ });
810
+
811
+ it("allows an extension of a locked base when the extension's own gate passes", async () => {
812
+ await writeModel("rt_locked.malloy", LOCKED_BASE);
813
+ const { result } = await runGated(
814
+ "rt_locked.malloy",
815
+ "run: ext_gated -> { aggregate: c }",
816
+ { ROLE: "analyst" },
817
+ );
818
+ expect(result.data).toBeDefined();
819
+ });
820
+
821
+ it("denies an extension that declares no own gate — it inherits the base lock (safe default)", async () => {
822
+ // Malloy carries the base's #(authorize) onto an extension UNLESS the
823
+ // extension declares its own. So a bare `is base_locked extend {}` with
824
+ // no own gate stays locked by the base's "false". An extension escapes
825
+ // the base gate only by declaring its own #(authorize) (see ext_gated).
826
+ await writeModel("rt_locked.malloy", LOCKED_BASE);
827
+ await expect(
828
+ runGated("rt_locked.malloy", "run: ext_nogate -> { aggregate: c }", {
829
+ ROLE: "analyst",
830
+ }),
831
+ ).rejects.toBeInstanceOf(AccessDeniedError);
832
+ });
833
+
834
+ it("[documented limitation] a query joining a locked base via an ungated source is allowed (top-level-source only)", async () => {
835
+ await writeModel("rt_locked.malloy", LOCKED_BASE);
836
+ const { result } = await runGated(
837
+ "rt_locked.malloy",
838
+ "run: joiner -> { aggregate: c }",
839
+ {},
840
+ );
841
+ expect(result.data).toBeDefined();
842
+ });
843
+ });
844
+
845
+ // The /compile path gates via Model.assertAuthorizedForText (early,
846
+ // surface-syntax) and Model.assertAuthorizedForRunnable (compiled-source
847
+ // backstop). These are the enforcement primitives environment.compileSource
848
+ // calls; exercise them directly here.
849
+ describe("authorize compile-path gate", () => {
850
+ const CP_GATE = `##! experimental.givens
851
+
852
+ given:
853
+ ROLE :: string
854
+
855
+ #(authorize) "$ROLE = 'analyst'"
856
+ source: gated is duckdb.table('customers') extend { measure: c is count() }
857
+
858
+ source: open_src is duckdb.table('customers') extend { measure: c is count() }
859
+ `;
860
+ const CP_FILE_LEVEL = `##! experimental.givens
861
+
862
+ given:
863
+ ROLE :: string
864
+
865
+ ##(authorize) "$ROLE = 'admin'"
866
+
867
+ source: declared is duckdb.table('customers') extend { measure: c is count() }
868
+ `;
869
+
870
+ async function cpModel(file: string, src: string): Promise<Model> {
871
+ await writeModel(file, src);
872
+ return Model.create("test-pkg", TEST_PKG_DIR, file, getConnections());
873
+ }
874
+
875
+ it("assertAuthorizedForText denies/allows a gated named source by its given", async () => {
876
+ const model = await cpModel("cp_gate.malloy", CP_GATE);
877
+ await expect(
878
+ model.assertAuthorizedForText("run: gated -> { aggregate: c }", {}),
879
+ ).rejects.toBeInstanceOf(AccessDeniedError);
880
+ await expect(
881
+ model.assertAuthorizedForText("run: gated -> { aggregate: c }", {
882
+ ROLE: "analyst",
883
+ }),
884
+ ).resolves.toBeUndefined();
885
+ });
886
+
887
+ it("assertAuthorizedForText leaves an ungated source unrestricted", async () => {
888
+ const model = await cpModel("cp_gate.malloy", CP_GATE);
889
+ await expect(
890
+ model.assertAuthorizedForText("run: open_src -> { aggregate: c }", {}),
891
+ ).resolves.toBeUndefined();
892
+ });
893
+
894
+ it("assertAuthorizedForText applies the model-wide file-level gate to inline/unnamed text", async () => {
895
+ const model = await cpModel("cp_file.malloy", CP_FILE_LEVEL);
896
+ // No named source the regex recognizes -> undefined -> file-level gate.
897
+ await expect(
898
+ model.assertAuthorizedForText(
899
+ `run: duckdb.sql("SELECT 1 AS x") -> { aggregate: n is count() }`,
900
+ {},
901
+ ),
902
+ ).rejects.toBeInstanceOf(AccessDeniedError);
903
+ await expect(
904
+ model.assertAuthorizedForText(
905
+ `run: duckdb.sql("SELECT 1 AS x") -> { aggregate: n is count() }`,
906
+ { ROLE: "admin" },
907
+ ),
908
+ ).resolves.toBeUndefined();
909
+ });
910
+
911
+ it("assertAuthorizedForRunnable gates the compiled-source structRef (alias backstop)", async () => {
912
+ const model = await cpModel("cp_gate.malloy", CP_GATE);
913
+ // Stub a runnable whose compiled query reads `gated` (e.g. via an alias
914
+ // the surface-syntax gate would miss).
915
+ const gatedRunnable = {
916
+ getPreparedQuery: async () => ({ _query: { structRef: "gated" } }),
917
+ };
918
+ await expect(
919
+ model.assertAuthorizedForRunnable(gatedRunnable, {}),
920
+ ).rejects.toBeInstanceOf(AccessDeniedError);
921
+ await expect(
922
+ model.assertAuthorizedForRunnable(gatedRunnable, { ROLE: "analyst" }),
923
+ ).resolves.toBeUndefined();
924
+ // Ungated compiled source -> unrestricted.
925
+ const openRunnable = {
926
+ getPreparedQuery: async () => ({ _query: { structRef: "open_src" } }),
927
+ };
928
+ await expect(
929
+ model.assertAuthorizedForRunnable(openRunnable, {}),
930
+ ).resolves.toBeUndefined();
931
+ });
932
+ });