@malloy-publisher/server 0.0.202 → 0.0.204

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 (51) hide show
  1. package/dist/app/api-doc.yaml +25 -3
  2. package/dist/app/assets/{EnvironmentPage-CNQYDaxR.js → EnvironmentPage-CX06cjOF.js} +1 -1
  3. package/dist/app/assets/HomePage-CNFt_eUU.js +1 -0
  4. package/dist/app/assets/{MainPage-B0kNpkxT.js → MainPage-nUJ9YatG.js} +1 -1
  5. package/dist/app/assets/{PackagePage-yAh0TrOV.js → MaterializationsPage-B5goxVXW.js} +1 -1
  6. package/dist/app/assets/{ModelPage-DcVElc9L.js → ModelPage-Ba7Xh4lL.js} +1 -1
  7. package/dist/app/assets/PackagePage-BaEVdEAG.js +1 -0
  8. package/dist/app/assets/{RouteError-DknUbx_s.js → RouteError-BShQjZio.js} +1 -1
  9. package/dist/app/assets/{WorkbookPage-CCqc8otA.js → WorkbookPage-CBn6ZjJW.js} +1 -1
  10. package/dist/app/assets/{core-B3A61KGJ.es-iOUZ6RJL.js → core-DECXYL4E.es-OaRfXwuQ.js} +1 -1
  11. package/dist/app/assets/{index-W0bOLKGl.js → index-BLfPC1gy.js} +2 -2
  12. package/dist/app/assets/index-DqiJ0bWp.js +455 -0
  13. package/dist/app/assets/index-Dy3YhAZQ.js +1812 -0
  14. package/dist/app/assets/index.umd-DAN9K8yC.js +2469 -0
  15. package/dist/app/index.html +1 -1
  16. package/dist/package_load_worker.mjs +392 -67
  17. package/dist/server.mjs +418 -153
  18. package/package.json +11 -11
  19. package/src/ducklake_version.spec.ts +43 -0
  20. package/src/ducklake_version.ts +26 -0
  21. package/src/errors.ts +18 -1
  22. package/src/package_load/package_load_pool.ts +0 -5
  23. package/src/package_load/package_load_worker.ts +41 -99
  24. package/src/package_load/protocol.ts +1 -7
  25. package/src/service/annotations.spec.ts +118 -0
  26. package/src/service/annotations.ts +91 -0
  27. package/src/service/authorize.spec.ts +132 -0
  28. package/src/service/authorize.ts +241 -0
  29. package/src/service/authorize_integration.spec.ts +838 -0
  30. package/src/service/connection.ts +1 -1
  31. package/src/service/environment.ts +4 -4
  32. package/src/service/environment_store.ts +14 -2
  33. package/src/service/filter.spec.ts +14 -3
  34. package/src/service/filter.ts +5 -1
  35. package/src/service/filter_bypass.spec.ts +418 -0
  36. package/src/service/given.ts +37 -12
  37. package/src/service/givens_integration.spec.ts +34 -7
  38. package/src/service/materialization_service.ts +25 -20
  39. package/src/service/materialized_table_gc.spec.ts +6 -5
  40. package/src/service/materialized_table_gc.ts +2 -50
  41. package/src/service/model.spec.ts +203 -8
  42. package/src/service/model.ts +305 -155
  43. package/src/service/package_worker_path.spec.ts +113 -0
  44. package/src/service/quoting.ts +0 -20
  45. package/src/service/restricted_mode.spec.ts +299 -0
  46. package/src/service/source_extraction.ts +226 -0
  47. package/src/storage/StorageManager.ts +73 -0
  48. package/dist/app/assets/HomePage-DBFTIoD8.js +0 -1
  49. package/dist/app/assets/index-F_o127LC.js +0 -454
  50. package/dist/app/assets/index-QeX_e740.js +0 -1803
  51. package/dist/app/assets/index.umd-CEDRw4TK.js +0 -1145
@@ -0,0 +1,132 @@
1
+ import { describe, expect, it } from "bun:test";
2
+ import {
3
+ collectAuthorizeExprs,
4
+ isProbeTrue,
5
+ parseAuthorizeAnnotation,
6
+ } from "./authorize";
7
+
8
+ describe("isProbeTrue", () => {
9
+ it("grants only on a genuine true / 1 / 'true'", () => {
10
+ expect(isProbeTrue(true)).toBe(true);
11
+ expect(isProbeTrue(1)).toBe(true);
12
+ expect(isProbeTrue("true")).toBe(true);
13
+ });
14
+
15
+ it("denies on anything else (fail closed)", () => {
16
+ for (const v of [false, 0, "false", "", null, undefined, {}, "TRUE", 2]) {
17
+ expect(isProbeTrue(v)).toBe(false);
18
+ }
19
+ });
20
+ });
21
+
22
+ describe("parseAuthorizeAnnotation", () => {
23
+ it("parses a source-level #(authorize) expression", () => {
24
+ expect(parseAuthorizeAnnotation(`#(authorize) "$ROLE = 'analyst'"`)).toBe(
25
+ "$ROLE = 'analyst'",
26
+ );
27
+ });
28
+
29
+ it("parses a file-level ##(authorize) expression", () => {
30
+ expect(parseAuthorizeAnnotation(`##(authorize) "$ROLE = 'admin'"`)).toBe(
31
+ "$ROLE = 'admin'",
32
+ );
33
+ });
34
+
35
+ it("tolerates the trailing newline Malloy keeps on note text", () => {
36
+ expect(
37
+ parseAuthorizeAnnotation(`#(authorize) "$REGION = 'us-west'"\n`),
38
+ ).toBe("$REGION = 'us-west'");
39
+ });
40
+
41
+ it("preserves inner single quotes (Malloy string literals)", () => {
42
+ expect(
43
+ parseAuthorizeAnnotation(`#(authorize) "$TENANT in ['a', 'b']"`),
44
+ ).toBe("$TENANT in ['a', 'b']");
45
+ });
46
+
47
+ it("unescapes escaped inner double quotes", () => {
48
+ expect(parseAuthorizeAnnotation(`#(authorize) "$NAME = \\"foo\\""`)).toBe(
49
+ `$NAME = "foo"`,
50
+ );
51
+ });
52
+
53
+ it("handles a constant false gate", () => {
54
+ expect(parseAuthorizeAnnotation(`#(authorize) "false"`)).toBe("false");
55
+ });
56
+
57
+ it("returns null for non-authorize annotations", () => {
58
+ expect(
59
+ parseAuthorizeAnnotation(`#(filter) dimension=x type=equal`),
60
+ ).toBeNull();
61
+ expect(parseAuthorizeAnnotation(`##! experimental.givens`)).toBeNull();
62
+ expect(parseAuthorizeAnnotation(`## just a doc comment`)).toBeNull();
63
+ expect(parseAuthorizeAnnotation(`# plain`)).toBeNull();
64
+ expect(parseAuthorizeAnnotation(``)).toBeNull();
65
+ });
66
+
67
+ it("throws when the body is not quoted", () => {
68
+ expect(() =>
69
+ parseAuthorizeAnnotation(`#(authorize) $ROLE = 'analyst'`),
70
+ ).toThrow(/double-quoted/);
71
+ });
72
+
73
+ it("throws on mismatched / unterminated quotes", () => {
74
+ expect(() =>
75
+ parseAuthorizeAnnotation(`#(authorize) "$ROLE = 'analyst'`),
76
+ ).toThrow(/mismatched quotes/);
77
+ });
78
+
79
+ it("throws on an empty expression body", () => {
80
+ expect(() => parseAuthorizeAnnotation(`#(authorize) ""`)).toThrow(
81
+ /empty expression/,
82
+ );
83
+ });
84
+
85
+ it("throws on content after the closing quote", () => {
86
+ expect(() =>
87
+ parseAuthorizeAnnotation(`#(authorize) "$ROLE = 'a'" extra`),
88
+ ).toThrow(/unexpected content/);
89
+ });
90
+
91
+ it("throws when the prefix has no body", () => {
92
+ expect(() => parseAuthorizeAnnotation(`#(authorize)`)).toThrow(
93
+ /double-quoted/,
94
+ );
95
+ });
96
+ });
97
+
98
+ describe("collectAuthorizeExprs", () => {
99
+ it("collects authorize expressions in declaration order", () => {
100
+ expect(
101
+ collectAuthorizeExprs([
102
+ `##(authorize) "$ROLE = 'admin'"`,
103
+ `#(filter) dimension=x type=equal`,
104
+ `#(authorize) "$REGION = 'us-west'"`,
105
+ ]),
106
+ ).toEqual(["$ROLE = 'admin'", "$REGION = 'us-west'"]);
107
+ });
108
+
109
+ it("returns [] when there are no authorize annotations", () => {
110
+ expect(
111
+ collectAuthorizeExprs([`#(filter) dimension=x type=equal`, `## doc`]),
112
+ ).toEqual([]);
113
+ });
114
+
115
+ it("keeps duplicate gates (no dedup — OR semantics)", () => {
116
+ expect(
117
+ collectAuthorizeExprs([
118
+ `#(authorize) "$ROLE = 'admin'"`,
119
+ `#(authorize) "$ROLE = 'admin'"`,
120
+ ]),
121
+ ).toEqual(["$ROLE = 'admin'", "$ROLE = 'admin'"]);
122
+ });
123
+
124
+ it("propagates the throw from a malformed authorize annotation", () => {
125
+ expect(() =>
126
+ collectAuthorizeExprs([
127
+ `#(authorize) "$ROLE = 'admin'"`,
128
+ `#(authorize) "unterminated`,
129
+ ]),
130
+ ).toThrow(/mismatched quotes/);
131
+ });
132
+ });
@@ -0,0 +1,241 @@
1
+ /**
2
+ * `#(authorize)` / `##(authorize)` annotation parsing.
3
+ *
4
+ * Annotation format:
5
+ * #(authorize) "<malloy-bool-expr>" — source-level, on a single source
6
+ * ##(authorize) "<malloy-bool-expr>" — file/model-level, applies to the file
7
+ *
8
+ * The body is a single double-quoted Malloy boolean expression that references
9
+ * declared givens (`$NAME`), e.g. `#(authorize) "$ROLE = 'analyst'"`. The
10
+ * expression itself routinely contains single quotes (Malloy string literals),
11
+ * so we cannot reuse filter.ts's whitespace tokenizer — we unwrap exactly one
12
+ * layer of double quotes and hand the inner expression back untouched.
13
+ *
14
+ * This module parses, collects, and (at compile time) validates authorize
15
+ * annotations. Evaluating the expression — the actual access gate — lands in a
16
+ * later PR and reuses `buildAuthorizeProbe`. Kept light so it bundles cleanly
17
+ * into the package-load worker (the only non-type import is the shared
18
+ * `ModelCompilationError`).
19
+ */
20
+
21
+ import { type GivenValue } from "@malloydata/malloy";
22
+ import { ModelCompilationError } from "../errors";
23
+
24
+ const SOURCE_PREFIX = "#(authorize)";
25
+ const FILE_PREFIX = "##(authorize)";
26
+
27
+ /** source name → effective authorize expressions (file-level then source-level). */
28
+ export type AuthorizeMap = Map<string, string[]>;
29
+
30
+ /**
31
+ * Build the synthetic probe query that evaluates a source's authorize
32
+ * expressions. Each expression becomes a boolean `select` column over a
33
+ * one-row, warehouse-independent DuckDB source (the `"duckdb"` sandbox is
34
+ * registered for every package, so this never touches the model's real
35
+ * warehouse). Compiling this probe validates the expressions against the
36
+ * model's `given:` block (unknown givens and source-field references surface as
37
+ * compile errors); running it evaluates the gate. The reserved dummy column
38
+ * name is deliberately obscure so a real authorize expression is unlikely to
39
+ * collide with it — a bare field reference in an expression is meant to fail.
40
+ */
41
+ export function buildAuthorizeProbe(exprs: string[]): string {
42
+ const selects = exprs
43
+ .map((expr, i) => `__auth_${i} is (${expr})`)
44
+ .join("\n ");
45
+ return `run: duckdb.sql("SELECT 1 AS __authorize_probe_row") -> {
46
+ select:
47
+ ${selects}
48
+ limit: 1
49
+ }`;
50
+ }
51
+
52
+ /**
53
+ * Strict, fail-closed truthiness for a probe result cell. DuckDB (the probe's
54
+ * connection) returns a native boolean, but normalize defensively: only a real
55
+ * `true` / SQL `1` / `"true"` grants access. null, undefined, `0`, `false`, a
56
+ * missing cell — anything else — denies.
57
+ */
58
+ export function isProbeTrue(cell: unknown): boolean {
59
+ return cell === true || cell === 1 || cell === "true";
60
+ }
61
+
62
+ /** Minimal materializer surface needed to compile (not run) the probe. */
63
+ interface AuthorizeProbeCompiler {
64
+ loadQuery(query: string): { getPreparedQuery(): Promise<unknown> };
65
+ }
66
+
67
+ /** Minimal materializer surface needed to run the probe (the runtime gate). */
68
+ interface AuthorizeProbeExecutor {
69
+ loadQuery(query: string): {
70
+ run(opts: {
71
+ rowLimit?: number;
72
+ givens?: Record<string, GivenValue>;
73
+ }): Promise<{ data: { value: ReadonlyArray<Record<string, unknown>> } }>;
74
+ };
75
+ }
76
+
77
+ /**
78
+ * Evaluate a source's authorize disjunction against the supplied givens.
79
+ * Returns true if ANY expression evaluates true (OR semantics).
80
+ *
81
+ * Each expression is probed INDEPENDENTLY rather than batched into one query.
82
+ * That is what preserves OR semantics: an expression that can't be evaluated —
83
+ * e.g. it references a given the request didn't supply, so Malloy throws "no
84
+ * value" — counts as "does not grant" (false), and the next disjunct is still
85
+ * tried. Batching them would let a missing given in one unused branch throw and
86
+ * sink the whole request (denying an admin who matched a different branch).
87
+ *
88
+ * Per-branch failures are swallowed to false (fail closed at the branch level);
89
+ * access is granted only if some branch genuinely returns true. Short-circuits
90
+ * on the first true. Returns false (→ caller denies) if none grant.
91
+ */
92
+ export async function evaluateAuthorize(
93
+ executor: AuthorizeProbeExecutor,
94
+ exprs: string[],
95
+ givens: Record<string, GivenValue>,
96
+ ): Promise<boolean> {
97
+ for (const expr of exprs) {
98
+ try {
99
+ const result = await executor
100
+ .loadQuery(buildAuthorizeProbe([expr]))
101
+ .run({ rowLimit: 1, givens });
102
+ const row = result?.data?.value?.[0];
103
+ if (row && isProbeTrue(row.__auth_0)) {
104
+ return true;
105
+ }
106
+ } catch {
107
+ // This disjunct can't be evaluated (e.g. a referenced given has no
108
+ // value) — treat as not-granting and try the next. It does not fail
109
+ // the whole request, which is what keeps OR semantics intact.
110
+ continue;
111
+ }
112
+ }
113
+ return false;
114
+ }
115
+
116
+ /** A source plus its effective authorize expressions. */
117
+ interface SourceWithAuthorize {
118
+ name?: string;
119
+ authorize?: string[];
120
+ }
121
+
122
+ /**
123
+ * Translation-time validation. For each source carrying authorize expressions,
124
+ * compile the probe (no givens, no DB execution) so unknown givens and
125
+ * source-field references surface as compile errors at model load instead of
126
+ * first request. Type mismatches such as `$ROLE = 5` are NOT Malloy compile
127
+ * errors, so they are not caught here — they fail closed at the runtime gate.
128
+ *
129
+ * Throws `ModelCompilationError` naming the source on the first invalid
130
+ * annotation. Shared by `Model.create` and the package-load worker so both
131
+ * compile paths validate identically.
132
+ */
133
+ export async function validateAuthorizeProbes(
134
+ compiler: AuthorizeProbeCompiler,
135
+ sources: readonly SourceWithAuthorize[],
136
+ ): Promise<void> {
137
+ for (const source of sources) {
138
+ const exprs = source.authorize;
139
+ if (!exprs || exprs.length === 0) continue;
140
+ try {
141
+ await compiler
142
+ .loadQuery(buildAuthorizeProbe(exprs))
143
+ .getPreparedQuery();
144
+ } catch (err) {
145
+ const detail = err instanceof Error ? err.message : String(err);
146
+ throw new ModelCompilationError({
147
+ message: `Invalid #(authorize) annotation on source "${source.name ?? "(unnamed)"}" [${exprs.join(" | ")}]: ${detail}`,
148
+ });
149
+ }
150
+ }
151
+ }
152
+
153
+ /**
154
+ * Parse a single annotation string into its authorize expression.
155
+ *
156
+ * Returns the inner expression for a well-formed `#(authorize)` / `##(authorize)`
157
+ * annotation, `null` if the string is not an authorize annotation at all, and
158
+ * throws if it looks like one but is malformed (missing quotes, mismatched
159
+ * quotes, or an empty body). The throw is what later compile-time validation
160
+ * turns into a model-load error.
161
+ */
162
+ export function parseAuthorizeAnnotation(annotation: string): string | null {
163
+ const trimmed = annotation.trim();
164
+
165
+ let body: string;
166
+ // Check the file-level prefix first — `##(authorize)` also starts with `#`.
167
+ if (trimmed.startsWith(FILE_PREFIX)) {
168
+ body = trimmed.slice(FILE_PREFIX.length).trim();
169
+ } else if (trimmed.startsWith(SOURCE_PREFIX)) {
170
+ body = trimmed.slice(SOURCE_PREFIX.length).trim();
171
+ } else {
172
+ return null;
173
+ }
174
+
175
+ return unwrapQuotedExpression(body);
176
+ }
177
+
178
+ /**
179
+ * Extract authorize expressions from a list of annotation strings, preserving
180
+ * declaration order. Non-authorize annotations are ignored; there is no dedup —
181
+ * every authorize annotation is an independent gate (OR semantics), so repeats
182
+ * are kept. Propagates the throw from a malformed authorize annotation.
183
+ */
184
+ export function collectAuthorizeExprs(annotations: string[]): string[] {
185
+ const exprs: string[] = [];
186
+ for (const annotation of annotations) {
187
+ const expr = parseAuthorizeAnnotation(annotation);
188
+ if (expr !== null) {
189
+ exprs.push(expr);
190
+ }
191
+ }
192
+ return exprs;
193
+ }
194
+
195
+ /**
196
+ * Strip exactly one layer of wrapping double quotes off the annotation body and
197
+ * return the inner expression. Inner single quotes are part of the expression
198
+ * and pass through untouched; `\"` and `\\` inside the string are unescaped.
199
+ */
200
+ function unwrapQuotedExpression(body: string): string {
201
+ if (body.length < 2 || body[0] !== '"') {
202
+ throw new Error(
203
+ `authorize annotation expression must be a double-quoted string, got: ${body || "(empty)"}`,
204
+ );
205
+ }
206
+
207
+ let expr = "";
208
+ let i = 1;
209
+ let closed = false;
210
+ for (; i < body.length; i++) {
211
+ const ch = body[i];
212
+ if (ch === "\\" && i + 1 < body.length) {
213
+ const next = body[i + 1];
214
+ if (next === '"' || next === "\\") {
215
+ expr += next;
216
+ i++;
217
+ continue;
218
+ }
219
+ }
220
+ if (ch === '"') {
221
+ closed = true;
222
+ i++;
223
+ break;
224
+ }
225
+ expr += ch;
226
+ }
227
+
228
+ if (!closed) {
229
+ throw new Error(`authorize annotation has mismatched quotes: ${body}`);
230
+ }
231
+ const rest = body.slice(i).trim();
232
+ if (rest.length > 0) {
233
+ throw new Error(
234
+ `authorize annotation has unexpected content after the expression: ${rest}`,
235
+ );
236
+ }
237
+ if (expr.trim().length === 0) {
238
+ throw new Error("authorize annotation has an empty expression body");
239
+ }
240
+ return expr;
241
+ }