@malloy-publisher/server 0.0.203 → 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.
- package/dist/app/api-doc.yaml +17 -0
- package/dist/app/assets/{EnvironmentPage-BVQ7glKP.js → EnvironmentPage-CX06cjOF.js} +1 -1
- package/dist/app/assets/HomePage-CNFt_eUU.js +1 -0
- package/dist/app/assets/{MainPage-bYOWcgDP.js → MainPage-nUJ9YatG.js} +1 -1
- package/dist/app/assets/{PackagePage-N1ZBNJul.js → MaterializationsPage-B5goxVXW.js} +1 -1
- package/dist/app/assets/{ModelPage-DT0gjNy1.js → ModelPage-Ba7Xh4lL.js} +1 -1
- package/dist/app/assets/PackagePage-BaEVdEAG.js +1 -0
- package/dist/app/assets/{RouteError-_J-EBz7W.js → RouteError-BShQjZio.js} +1 -1
- package/dist/app/assets/{WorkbookPage-Bjs9Nm-_.js → WorkbookPage-CBn6ZjJW.js} +1 -1
- package/dist/app/assets/{core-BPLlx5VM.es-C2ARtwWI.js → core-DECXYL4E.es-OaRfXwuQ.js} +1 -1
- package/dist/app/assets/{index-CqUWJELr.js → index-BLfPC1gy.js} +2 -2
- package/dist/app/assets/index-DqiJ0bWp.js +455 -0
- package/dist/app/assets/index-Dy3YhAZQ.js +1812 -0
- package/dist/app/assets/index.umd-DAN9K8yC.js +2469 -0
- package/dist/app/index.html +1 -1
- package/dist/package_load_worker.mjs +392 -67
- package/dist/server.mjs +415 -152
- package/package.json +11 -11
- package/src/ducklake_version.spec.ts +43 -0
- package/src/ducklake_version.ts +26 -0
- package/src/errors.ts +18 -1
- package/src/package_load/package_load_pool.ts +0 -5
- package/src/package_load/package_load_worker.ts +41 -99
- package/src/package_load/protocol.ts +1 -7
- package/src/service/annotations.spec.ts +118 -0
- package/src/service/annotations.ts +91 -0
- package/src/service/authorize.spec.ts +132 -0
- package/src/service/authorize.ts +241 -0
- package/src/service/authorize_integration.spec.ts +838 -0
- package/src/service/connection.ts +1 -1
- package/src/service/environment.ts +4 -4
- package/src/service/filter.spec.ts +14 -3
- package/src/service/filter.ts +5 -1
- package/src/service/filter_bypass.spec.ts +418 -0
- package/src/service/given.ts +37 -12
- package/src/service/givens_integration.spec.ts +34 -7
- package/src/service/materialization_service.ts +25 -20
- package/src/service/materialized_table_gc.spec.ts +6 -5
- package/src/service/materialized_table_gc.ts +2 -50
- package/src/service/model.spec.ts +203 -8
- package/src/service/model.ts +305 -155
- package/src/service/package_worker_path.spec.ts +113 -0
- package/src/service/quoting.ts +0 -20
- package/src/service/restricted_mode.spec.ts +299 -0
- package/src/service/source_extraction.ts +226 -0
- package/src/storage/StorageManager.ts +73 -0
- package/dist/app/assets/HomePage-D9drXoZX.js +0 -1
- package/dist/app/assets/index-BeNwIeYQ.js +0 -454
- package/dist/app/assets/index-Dx7qi2LO.js +0 -1803
- package/dist/app/assets/index.umd-BXm2lnUO.js +0 -1145
|
@@ -1084,7 +1084,7 @@ export function buildEnvironmentMalloyConfig(
|
|
|
1084
1084
|
...azureDuckDBCache.values(),
|
|
1085
1085
|
];
|
|
1086
1086
|
const closeResults = await Promise.allSettled([
|
|
1087
|
-
malloyConfig.
|
|
1087
|
+
malloyConfig.shutdown("close"),
|
|
1088
1088
|
...wrapperPromises.map(async (promise) => {
|
|
1089
1089
|
const connection = await promise;
|
|
1090
1090
|
await connection.close();
|
|
@@ -736,7 +736,7 @@ export class Environment {
|
|
|
736
736
|
);
|
|
737
737
|
if (existingPackage !== undefined && reload) {
|
|
738
738
|
this.retireConnectionGeneration(`package ${packageName}`, () =>
|
|
739
|
-
existingPackage.getMalloyConfig().
|
|
739
|
+
existingPackage.getMalloyConfig().shutdown("close"),
|
|
740
740
|
);
|
|
741
741
|
}
|
|
742
742
|
this.packages.set(packageName, _package);
|
|
@@ -955,7 +955,7 @@ export class Environment {
|
|
|
955
955
|
|
|
956
956
|
if (oldPackage) {
|
|
957
957
|
this.retireConnectionGeneration(`package ${packageName}`, () =>
|
|
958
|
-
oldPackage.getMalloyConfig().
|
|
958
|
+
oldPackage.getMalloyConfig().shutdown("close"),
|
|
959
959
|
);
|
|
960
960
|
}
|
|
961
961
|
|
|
@@ -1139,7 +1139,7 @@ export class Environment {
|
|
|
1139
1139
|
// any in-flight queries that already acquired a connection finish
|
|
1140
1140
|
// before the underlying duckdb handle is released.
|
|
1141
1141
|
this.retireConnectionGeneration(`package ${packageName}`, () =>
|
|
1142
|
-
_package.getMalloyConfig().
|
|
1142
|
+
_package.getMalloyConfig().shutdown("close"),
|
|
1143
1143
|
);
|
|
1144
1144
|
|
|
1145
1145
|
// Atomically rename the canonical tree out of the way so no reader
|
|
@@ -1235,7 +1235,7 @@ export class Environment {
|
|
|
1235
1235
|
// they wrap. Without this, hard unload leaks per-package DuckDB handles.
|
|
1236
1236
|
const packageReleases = await Promise.allSettled(
|
|
1237
1237
|
Array.from(this.packages.values(), (pkg) =>
|
|
1238
|
-
pkg.getMalloyConfig().
|
|
1238
|
+
pkg.getMalloyConfig().shutdown("close"),
|
|
1239
1239
|
),
|
|
1240
1240
|
);
|
|
1241
1241
|
for (const result of packageReleases) {
|
|
@@ -423,7 +423,7 @@ describe("service/filter", () => {
|
|
|
423
423
|
const query = "run: orders -> summary";
|
|
424
424
|
const clause = "`status` = 'active'";
|
|
425
425
|
expect(injectFilterRefinement(query, clause)).toBe(
|
|
426
|
-
"run: orders -> summary
|
|
426
|
+
"run: orders -> summary\n+ {where: `status` = 'active'}",
|
|
427
427
|
);
|
|
428
428
|
});
|
|
429
429
|
|
|
@@ -432,7 +432,7 @@ describe("service/filter", () => {
|
|
|
432
432
|
"run: orders -> { group_by: status; aggregate: order_count }";
|
|
433
433
|
const clause = "`region` = 'US'";
|
|
434
434
|
expect(injectFilterRefinement(query, clause)).toBe(
|
|
435
|
-
"run: orders -> { group_by: status; aggregate: order_count }
|
|
435
|
+
"run: orders -> { group_by: status; aggregate: order_count }\n+ {where: `region` = 'US'}",
|
|
436
436
|
);
|
|
437
437
|
});
|
|
438
438
|
|
|
@@ -440,8 +440,19 @@ describe("service/filter", () => {
|
|
|
440
440
|
const query = "run: orders -> summary \n ";
|
|
441
441
|
const clause = "`status` = 'active'";
|
|
442
442
|
expect(injectFilterRefinement(query, clause)).toBe(
|
|
443
|
-
"run: orders -> summary
|
|
443
|
+
"run: orders -> summary\n+ {where: `status` = 'active'}",
|
|
444
444
|
);
|
|
445
445
|
});
|
|
446
|
+
|
|
447
|
+
it("places refinement on its own line so a trailing comment cannot swallow it", () => {
|
|
448
|
+
const clause = "`org_id` = 'acme'";
|
|
449
|
+
// A trailing line comment must not extend over the injected filter.
|
|
450
|
+
for (const comment of ["//", "-- sneaky"]) {
|
|
451
|
+
const query = `run: orders -> { group_by: status } ${comment}`;
|
|
452
|
+
const refined = injectFilterRefinement(query, clause);
|
|
453
|
+
const lastLine = refined.split("\n").pop() ?? "";
|
|
454
|
+
expect(lastLine).toBe(`+ {where: ${clause}}`);
|
|
455
|
+
}
|
|
456
|
+
});
|
|
446
457
|
});
|
|
447
458
|
});
|
package/src/service/filter.ts
CHANGED
|
@@ -272,6 +272,10 @@ export function buildFilterClause(
|
|
|
272
272
|
* Append a filter refinement to a Malloy query string.
|
|
273
273
|
* Uses Malloy's `+ {where: ...}` refinement syntax.
|
|
274
274
|
*
|
|
275
|
+
* The refinement is placed on its own line so that a trailing line comment
|
|
276
|
+
* (`//` or `--`) on the caller's query cannot extend over it and neutralize
|
|
277
|
+
* the filter.
|
|
278
|
+
*
|
|
275
279
|
* If `filterClause` is empty, returns the original query unchanged.
|
|
276
280
|
*/
|
|
277
281
|
export function injectFilterRefinement(
|
|
@@ -281,7 +285,7 @@ export function injectFilterRefinement(
|
|
|
281
285
|
if (!filterClause) {
|
|
282
286
|
return query;
|
|
283
287
|
}
|
|
284
|
-
return `${query.trimEnd()}
|
|
288
|
+
return `${query.trimEnd()}\n+ {where: ${filterClause}}`;
|
|
285
289
|
}
|
|
286
290
|
|
|
287
291
|
// ---------------------------------------------------------------------------
|
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Filter-param (`#(filter)`) enforcement across source-derivation paths.
|
|
3
|
+
*
|
|
4
|
+
* A `#(filter)` annotation marks a source as protected: a required filter (e.g.
|
|
5
|
+
* the implicit multi-tenant `org_id` boundary) must be supplied before the
|
|
6
|
+
* source can be read. The aim is to enforce that without narrowing the query
|
|
7
|
+
* shapes `execute_query` accepts.
|
|
8
|
+
*
|
|
9
|
+
* Cases guarded here:
|
|
10
|
+
* - Direct read of a protected source: rejected (400) naming the missing
|
|
11
|
+
* required filter; scoped to the supplied values once provided.
|
|
12
|
+
* - Reaching a protected source under an ad-hoc name (alias / extend / chain):
|
|
13
|
+
* enforced identically — the query still runs, scoped — rather than being
|
|
14
|
+
* refused for its shape.
|
|
15
|
+
* - Unprotected sources are unaffected; `bypassFilters` skips enforcement.
|
|
16
|
+
*
|
|
17
|
+
* Language-level escapes (import, raw tables, raw SQL) are out of scope here and
|
|
18
|
+
* covered in `restricted_mode.spec.ts`.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { DuckDBConnection } from "@malloydata/db-duckdb";
|
|
22
|
+
import { Connection } from "@malloydata/malloy";
|
|
23
|
+
import { afterAll, beforeAll, describe, expect, it } from "bun:test";
|
|
24
|
+
import fs from "fs/promises";
|
|
25
|
+
import os from "os";
|
|
26
|
+
import path from "path";
|
|
27
|
+
import { BadRequestError } from "../errors";
|
|
28
|
+
import { Model } from "./model";
|
|
29
|
+
import type { FilterParams } from "./filter";
|
|
30
|
+
|
|
31
|
+
const TEST_DIR = path.join(os.tmpdir(), "filter-bypass-tests");
|
|
32
|
+
const TEST_DB_DIR = path.join(TEST_DIR, "db");
|
|
33
|
+
const TEST_DB_PATH = path.join(TEST_DB_DIR, "test.duckdb");
|
|
34
|
+
const TEST_PKG_DIR = path.join(TEST_DIR, "pkg");
|
|
35
|
+
|
|
36
|
+
let duckdbConnection: DuckDBConnection;
|
|
37
|
+
|
|
38
|
+
// Three tenants. org_id is the partition key, so any cross-org leak shows up
|
|
39
|
+
// directly as extra rows when we `group_by: org_id` (acme has 2 products).
|
|
40
|
+
const SEED_SQL = `
|
|
41
|
+
CREATE TABLE IF NOT EXISTS products (
|
|
42
|
+
org_id VARCHAR,
|
|
43
|
+
product_name VARCHAR
|
|
44
|
+
);
|
|
45
|
+
INSERT INTO products VALUES
|
|
46
|
+
('acme', 'AcmeAnvil'),
|
|
47
|
+
('acme', 'AcmeRocket'),
|
|
48
|
+
('globex', 'GlobexPhone'),
|
|
49
|
+
('initech', 'InitechStapler');
|
|
50
|
+
|
|
51
|
+
CREATE TABLE IF NOT EXISTS orders (
|
|
52
|
+
org_id VARCHAR,
|
|
53
|
+
category VARCHAR,
|
|
54
|
+
region VARCHAR
|
|
55
|
+
);
|
|
56
|
+
INSERT INTO orders VALUES
|
|
57
|
+
('acme', 'widgets', 'US'),
|
|
58
|
+
('acme', 'gadgets', 'EU'),
|
|
59
|
+
('globex', 'widgets', 'US'),
|
|
60
|
+
('initech', 'gadgets', 'APAC');
|
|
61
|
+
|
|
62
|
+
CREATE TABLE IF NOT EXISTS events (
|
|
63
|
+
org_id VARCHAR,
|
|
64
|
+
kind VARCHAR
|
|
65
|
+
);
|
|
66
|
+
INSERT INTO events VALUES
|
|
67
|
+
('acme', 'click'),
|
|
68
|
+
('globex', 'view'),
|
|
69
|
+
('initech', 'scroll');
|
|
70
|
+
`;
|
|
71
|
+
|
|
72
|
+
// products: implicit Organization filter only (the multi-tenant boundary).
|
|
73
|
+
const PRODUCTS_MODEL = `
|
|
74
|
+
#(filter) name=Organization dimension=org_id type=equal implicit required
|
|
75
|
+
source: products is duckdb.table('products') extend {
|
|
76
|
+
measure: n is count()
|
|
77
|
+
view: by_org is {
|
|
78
|
+
group_by: org_id, product_name
|
|
79
|
+
aggregate: n
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
`;
|
|
83
|
+
|
|
84
|
+
// orders: implicit Organization + required-explicit Category + optional Region.
|
|
85
|
+
const ORDERS_MODEL = `
|
|
86
|
+
#(filter) name=Organization dimension=org_id type=equal implicit required
|
|
87
|
+
#(filter) name=Category dimension=category type=equal required
|
|
88
|
+
#(filter) name=Region dimension=region type=in
|
|
89
|
+
source: orders is duckdb.table('orders') extend {
|
|
90
|
+
measure: n is count()
|
|
91
|
+
}
|
|
92
|
+
`;
|
|
93
|
+
|
|
94
|
+
// A model that neither defines nor imports any protected source. Used as the
|
|
95
|
+
// target for the unprotected-source regression.
|
|
96
|
+
const ANALYTICS_MODEL = `
|
|
97
|
+
source: metrics is duckdb.table('events') extend {
|
|
98
|
+
measure: c is count()
|
|
99
|
+
}
|
|
100
|
+
`;
|
|
101
|
+
|
|
102
|
+
beforeAll(async () => {
|
|
103
|
+
await fs.mkdir(TEST_DB_DIR, { recursive: true });
|
|
104
|
+
await fs.mkdir(TEST_PKG_DIR, { recursive: true });
|
|
105
|
+
duckdbConnection = new DuckDBConnection("duckdb", TEST_DB_PATH, TEST_DB_DIR);
|
|
106
|
+
for (const stmt of SEED_SQL.trim().split(";").filter(Boolean)) {
|
|
107
|
+
await duckdbConnection.runSQL(stmt.trim() + ";");
|
|
108
|
+
}
|
|
109
|
+
await fs.writeFile(
|
|
110
|
+
path.join(TEST_PKG_DIR, "products.malloy"),
|
|
111
|
+
PRODUCTS_MODEL,
|
|
112
|
+
"utf-8",
|
|
113
|
+
);
|
|
114
|
+
await fs.writeFile(
|
|
115
|
+
path.join(TEST_PKG_DIR, "orders.malloy"),
|
|
116
|
+
ORDERS_MODEL,
|
|
117
|
+
"utf-8",
|
|
118
|
+
);
|
|
119
|
+
await fs.writeFile(
|
|
120
|
+
path.join(TEST_PKG_DIR, "analytics.malloy"),
|
|
121
|
+
ANALYTICS_MODEL,
|
|
122
|
+
"utf-8",
|
|
123
|
+
);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
afterAll(async () => {
|
|
127
|
+
try {
|
|
128
|
+
await duckdbConnection.close();
|
|
129
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
130
|
+
await fs.rm(TEST_DIR, { recursive: true, force: true });
|
|
131
|
+
} catch {
|
|
132
|
+
// Ignore cleanup errors
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
function getConnections(): Map<string, Connection> {
|
|
137
|
+
const map = new Map<string, Connection>();
|
|
138
|
+
map.set("duckdb", duckdbConnection);
|
|
139
|
+
return map;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
type Row = Record<string, unknown>;
|
|
143
|
+
|
|
144
|
+
function asRows(compactResult: unknown): Row[] {
|
|
145
|
+
return compactResult as Row[];
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
async function makeModel(modelPath: string): Promise<Model> {
|
|
149
|
+
return Model.create("test-pkg", TEST_PKG_DIR, modelPath, getConnections());
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Run an ad-hoc query string and return the result rows. */
|
|
153
|
+
async function runAdHoc(
|
|
154
|
+
model: Model,
|
|
155
|
+
query: string,
|
|
156
|
+
filterParams?: FilterParams,
|
|
157
|
+
): Promise<Row[]> {
|
|
158
|
+
const { compactResult } = await model.getQueryResults(
|
|
159
|
+
undefined,
|
|
160
|
+
undefined,
|
|
161
|
+
query,
|
|
162
|
+
filterParams,
|
|
163
|
+
);
|
|
164
|
+
return asRows(compactResult);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Assert an ad-hoc query is rejected with a 400 whose message names a missing
|
|
169
|
+
* required filter (`expectedSubstring`, e.g. "Organization"). If the query
|
|
170
|
+
* instead succeeds, the assertion fails reporting the row count — a filter
|
|
171
|
+
* bypass / data leak slipped through.
|
|
172
|
+
*/
|
|
173
|
+
async function expectFilterRejected(
|
|
174
|
+
model: Model,
|
|
175
|
+
query: string,
|
|
176
|
+
filterParams: FilterParams | undefined,
|
|
177
|
+
expectedSubstring: string,
|
|
178
|
+
): Promise<void> {
|
|
179
|
+
let leakedRows: number | undefined;
|
|
180
|
+
try {
|
|
181
|
+
const { compactResult } = await model.getQueryResults(
|
|
182
|
+
undefined,
|
|
183
|
+
undefined,
|
|
184
|
+
query,
|
|
185
|
+
filterParams,
|
|
186
|
+
);
|
|
187
|
+
leakedRows = asRows(compactResult).length;
|
|
188
|
+
} catch (error) {
|
|
189
|
+
expect(error).toBeInstanceOf(BadRequestError);
|
|
190
|
+
expect((error as Error).message).toContain(expectedSubstring);
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
throw new Error(
|
|
194
|
+
`Expected a 400 naming "${expectedSubstring}", but the query succeeded ` +
|
|
195
|
+
`and returned ${leakedRows} rows (FILTER BYPASS / LEAK).`,
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** Assert every returned row is scoped to acme and the count matches. */
|
|
200
|
+
function expectAcmeScoped(rows: Row[], expectedRows: number): void {
|
|
201
|
+
expect(rows.length).toBe(expectedRows);
|
|
202
|
+
for (const row of rows) expect(row.org_id).toBe("acme");
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// ===========================================================================
|
|
206
|
+
// Enforced: direct reads of a protected source.
|
|
207
|
+
// ===========================================================================
|
|
208
|
+
|
|
209
|
+
describe("direct read of a protected source is enforced", () => {
|
|
210
|
+
it("rejects a direct query with no filter params", async () => {
|
|
211
|
+
const model = await makeModel("products.malloy");
|
|
212
|
+
await expectFilterRejected(
|
|
213
|
+
model,
|
|
214
|
+
"run: products -> { group_by: org_id, product_name; aggregate: n is count() }",
|
|
215
|
+
undefined,
|
|
216
|
+
"Organization",
|
|
217
|
+
);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it("scopes a direct query when Organization is supplied", async () => {
|
|
221
|
+
const model = await makeModel("products.malloy");
|
|
222
|
+
const rows = await runAdHoc(
|
|
223
|
+
model,
|
|
224
|
+
"run: products -> { group_by: org_id, product_name; aggregate: n is count() }",
|
|
225
|
+
{ Organization: "acme" },
|
|
226
|
+
);
|
|
227
|
+
expectAcmeScoped(rows, 2); // acme has exactly 2 products
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
// A predefined view on the trusted source is still the direct path.
|
|
231
|
+
it("enforces filters on a direct named-view read", async () => {
|
|
232
|
+
const model = await makeModel("products.malloy");
|
|
233
|
+
await expectFilterRejected(
|
|
234
|
+
model,
|
|
235
|
+
"run: products -> by_org",
|
|
236
|
+
undefined,
|
|
237
|
+
"Organization",
|
|
238
|
+
);
|
|
239
|
+
const rows = await runAdHoc(model, "run: products -> by_org", {
|
|
240
|
+
Organization: "acme",
|
|
241
|
+
});
|
|
242
|
+
expect(rows.length).toBe(2);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
// orders — implicit Organization + required-explicit Category. Both required
|
|
246
|
+
// filters are enforced; the error names whichever is still missing.
|
|
247
|
+
it("rejects orders with no filter params (names Organization)", async () => {
|
|
248
|
+
const model = await makeModel("orders.malloy");
|
|
249
|
+
await expectFilterRejected(
|
|
250
|
+
model,
|
|
251
|
+
"run: orders -> { group_by: org_id, category; aggregate: n is count() }",
|
|
252
|
+
undefined,
|
|
253
|
+
"Organization",
|
|
254
|
+
);
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it("rejects orders with only Category supplied (still missing Organization)", async () => {
|
|
258
|
+
const model = await makeModel("orders.malloy");
|
|
259
|
+
await expectFilterRejected(
|
|
260
|
+
model,
|
|
261
|
+
"run: orders -> { group_by: org_id, category; aggregate: n is count() }",
|
|
262
|
+
{ Category: "widgets" },
|
|
263
|
+
"Organization",
|
|
264
|
+
);
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
it("rejects orders with only Organization supplied (still missing Category)", async () => {
|
|
268
|
+
const model = await makeModel("orders.malloy");
|
|
269
|
+
await expectFilterRejected(
|
|
270
|
+
model,
|
|
271
|
+
"run: orders -> { group_by: org_id, category; aggregate: n is count() }",
|
|
272
|
+
{ Organization: "acme" },
|
|
273
|
+
"Category",
|
|
274
|
+
);
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it("scopes orders when both Organization and Category are supplied", async () => {
|
|
278
|
+
const model = await makeModel("orders.malloy");
|
|
279
|
+
const rows = await runAdHoc(
|
|
280
|
+
model,
|
|
281
|
+
"run: orders -> { group_by: org_id, category; aggregate: n is count() }",
|
|
282
|
+
{ Organization: "acme", Category: "widgets" },
|
|
283
|
+
);
|
|
284
|
+
expect(rows.length).toBe(1); // acme + widgets → one group
|
|
285
|
+
expect(rows[0].org_id).toBe("acme");
|
|
286
|
+
expect(rows[0].category).toBe("widgets");
|
|
287
|
+
});
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
// ===========================================================================
|
|
291
|
+
// Enforced: alias / extend / chain of a protected source.
|
|
292
|
+
//
|
|
293
|
+
// Reaching a protected source under an ad-hoc name carries the SAME filter
|
|
294
|
+
// requirement. The query is not rejected for its shape — it runs, scoped — so
|
|
295
|
+
// the `execute_query` surface stays intact.
|
|
296
|
+
// ===========================================================================
|
|
297
|
+
|
|
298
|
+
interface EnforcedVector {
|
|
299
|
+
label: string;
|
|
300
|
+
modelPath: string;
|
|
301
|
+
query: string;
|
|
302
|
+
/** Required filter named in the missing-param rejection. */
|
|
303
|
+
missing: string;
|
|
304
|
+
/** Params satisfying every required filter on the protected source. */
|
|
305
|
+
validParams: FilterParams;
|
|
306
|
+
/** Rows expected once scoped to acme. */
|
|
307
|
+
expectedRows: number;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
const enforcedVectors: EnforcedVector[] = [
|
|
311
|
+
{
|
|
312
|
+
// Plain alias — a pure rename of the protected source.
|
|
313
|
+
label: "alias (source a is products)",
|
|
314
|
+
modelPath: "products.malloy",
|
|
315
|
+
query: "source: a is products\nrun: a -> { group_by: org_id, product_name; aggregate: n is count() }",
|
|
316
|
+
missing: "Organization",
|
|
317
|
+
validParams: { Organization: "acme" },
|
|
318
|
+
expectedRows: 2,
|
|
319
|
+
},
|
|
320
|
+
{
|
|
321
|
+
// Extend with an extra measure. The body adds to the curated surface but
|
|
322
|
+
// does not touch the filter dimension, so enforcement is unaffected.
|
|
323
|
+
label: "extend (source e is products extend { … })",
|
|
324
|
+
modelPath: "products.malloy",
|
|
325
|
+
query: "source: e is products extend { measure: rc is count() }\nrun: e -> { group_by: org_id, product_name; aggregate: rc }",
|
|
326
|
+
missing: "Organization",
|
|
327
|
+
validParams: { Organization: "acme" },
|
|
328
|
+
expectedRows: 2,
|
|
329
|
+
},
|
|
330
|
+
{
|
|
331
|
+
// Chained derivation — protection is inherited link by link and resolved
|
|
332
|
+
// back to products.
|
|
333
|
+
label: "chained (a is products; b is a)",
|
|
334
|
+
modelPath: "products.malloy",
|
|
335
|
+
query: "source: a is products\nsource: b is a\nrun: b -> { group_by: org_id, product_name; aggregate: n is count() }",
|
|
336
|
+
missing: "Organization",
|
|
337
|
+
validParams: { Organization: "acme" },
|
|
338
|
+
expectedRows: 2,
|
|
339
|
+
},
|
|
340
|
+
{
|
|
341
|
+
// Ad-hoc `#(filter)` annotation in the query text declaring a NON-required
|
|
342
|
+
// Organization filter on the alias, attempting to drop the requirement.
|
|
343
|
+
// Annotations written in the query are not honored — only the protected
|
|
344
|
+
// source's own annotation counts — so the requirement still stands.
|
|
345
|
+
label: "ad-hoc #(filter) annotation override is ignored",
|
|
346
|
+
modelPath: "products.malloy",
|
|
347
|
+
query:
|
|
348
|
+
"#(filter) name=Organization dimension=org_id type=equal\n" +
|
|
349
|
+
"source: a is products extend {}\n" +
|
|
350
|
+
"run: a -> { group_by: org_id, product_name; aggregate: n is count() }",
|
|
351
|
+
missing: "Organization",
|
|
352
|
+
validParams: { Organization: "acme" },
|
|
353
|
+
expectedRows: 2,
|
|
354
|
+
},
|
|
355
|
+
{
|
|
356
|
+
// orders alias — confirms enforcement on a multi-required-filter source.
|
|
357
|
+
label: "orders alias (source a is orders)",
|
|
358
|
+
modelPath: "orders.malloy",
|
|
359
|
+
query: "source: a is orders\nrun: a -> { group_by: org_id, category; aggregate: n is count() }",
|
|
360
|
+
missing: "Organization",
|
|
361
|
+
validParams: { Organization: "acme", Category: "widgets" },
|
|
362
|
+
expectedRows: 1,
|
|
363
|
+
},
|
|
364
|
+
{
|
|
365
|
+
// orders extend.
|
|
366
|
+
label: "orders extend",
|
|
367
|
+
modelPath: "orders.malloy",
|
|
368
|
+
query: "source: e is orders extend { measure: rc is count() }\nrun: e -> { group_by: org_id, category; aggregate: rc }",
|
|
369
|
+
missing: "Organization",
|
|
370
|
+
validParams: { Organization: "acme", Category: "widgets" },
|
|
371
|
+
expectedRows: 1,
|
|
372
|
+
},
|
|
373
|
+
];
|
|
374
|
+
|
|
375
|
+
describe("alias/extend/chain of a protected source is enforced (not restricted)", () => {
|
|
376
|
+
for (const v of enforcedVectors) {
|
|
377
|
+
describe(v.label, () => {
|
|
378
|
+
it("rejects when the required filter is missing", async () => {
|
|
379
|
+
const model = await makeModel(v.modelPath);
|
|
380
|
+
await expectFilterRejected(model, v.query, undefined, v.missing);
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
it("runs scoped when valid filter params are supplied", async () => {
|
|
384
|
+
const model = await makeModel(v.modelPath);
|
|
385
|
+
const rows = await runAdHoc(model, v.query, v.validParams);
|
|
386
|
+
expectAcmeScoped(rows, v.expectedRows);
|
|
387
|
+
});
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
// ===========================================================================
|
|
393
|
+
// Regression: unprotected sources stay open; bypassFilters short-circuits
|
|
394
|
+
// filter enforcement.
|
|
395
|
+
// ===========================================================================
|
|
396
|
+
|
|
397
|
+
describe("regressions", () => {
|
|
398
|
+
it("runs an unprotected source with no filter params (no spurious demand)", async () => {
|
|
399
|
+
const model = await makeModel("analytics.malloy");
|
|
400
|
+
const rows = await runAdHoc(
|
|
401
|
+
model,
|
|
402
|
+
"run: metrics -> { group_by: org_id; aggregate: c is count() }",
|
|
403
|
+
);
|
|
404
|
+
expect(rows.length).toBe(3); // all three orgs, unfiltered
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
it("bypassFilters short-circuits filter enforcement on a derivation", async () => {
|
|
408
|
+
const model = await makeModel("products.malloy");
|
|
409
|
+
const { compactResult } = await model.getQueryResults(
|
|
410
|
+
undefined,
|
|
411
|
+
undefined,
|
|
412
|
+
"source: a is products\nrun: a -> { group_by: org_id; aggregate: n is count() }",
|
|
413
|
+
{},
|
|
414
|
+
true,
|
|
415
|
+
);
|
|
416
|
+
expect(asRows(compactResult).length).toBe(3);
|
|
417
|
+
});
|
|
418
|
+
});
|
package/src/service/given.ts
CHANGED
|
@@ -13,14 +13,18 @@
|
|
|
13
13
|
* deps, so it's safe to bundle into the worker entry).
|
|
14
14
|
*/
|
|
15
15
|
|
|
16
|
+
import type { Annotations } from "@malloydata/malloy";
|
|
17
|
+
import { isReservedRoute } from "./annotations";
|
|
18
|
+
|
|
16
19
|
/**
|
|
17
20
|
* Duck-typed shape of a Malloy SDK `Given` instance (the value type
|
|
18
|
-
* of `Model.givens`).
|
|
21
|
+
* of `Model.givens`). `Given` itself isn't re-exported from the
|
|
22
|
+
* package root, but the `Annotations` view it returns is.
|
|
19
23
|
*/
|
|
20
24
|
export interface MalloyGiven {
|
|
21
25
|
readonly name: string;
|
|
22
26
|
readonly type: { type: string; filterType?: string };
|
|
23
|
-
|
|
27
|
+
readonly annotations: Annotations;
|
|
24
28
|
}
|
|
25
29
|
|
|
26
30
|
/**
|
|
@@ -32,6 +36,14 @@ export interface MalloyGivenApi {
|
|
|
32
36
|
name: string;
|
|
33
37
|
type: string;
|
|
34
38
|
annotations?: string[];
|
|
39
|
+
/**
|
|
40
|
+
* The given's default as a Malloy source literal — one literal per declared
|
|
41
|
+
* `type`. Examples across the type range: `'WN'` or `"WN"` (string), `2003`
|
|
42
|
+
* (number), `true` (boolean), `@2024-01-01` (date), `f'WN'` (filter). Omitted
|
|
43
|
+
* when the given has no default. Consumers render/prefill it per `type` (e.g.
|
|
44
|
+
* unquote a string).
|
|
45
|
+
*/
|
|
46
|
+
default?: string;
|
|
35
47
|
}
|
|
36
48
|
|
|
37
49
|
/**
|
|
@@ -46,16 +58,18 @@ export interface MalloyGivenApi {
|
|
|
46
58
|
* location either; matching that floor. A future PR can add a
|
|
47
59
|
* sanitised package-relative path if a client needs it.
|
|
48
60
|
*
|
|
49
|
-
* - `default`
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
61
|
+
* - `default` is surfaced as the rendered source literal
|
|
62
|
+
* (`given._internal.defaultText` — e.g. a string `'WN'`, number
|
|
63
|
+
* `2003`, boolean `true`, date `@2024-01-01`, or filter `f'WN'`).
|
|
64
|
+
* Malloy's public surface still exposes only the parsed `.default`
|
|
65
|
+
* AST; `_internal.defaultText` is the already-rendered string, so we
|
|
66
|
+
* forward it verbatim rather than re-implement the printer. Omitted
|
|
67
|
+
* when the given has no default.
|
|
53
68
|
*
|
|
54
|
-
* `annotations` is restricted to
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
* surface contract.
|
|
69
|
+
* `annotations` is restricted to app-route annotations (bracketed,
|
|
70
|
+
* caller-facing, e.g. `#(doc)`), excluding Malloy's reserved routes
|
|
71
|
+
* (plain `#` tags, `#"` doc strings, `##!` pragmas), which aren't part
|
|
72
|
+
* of the given's surface contract.
|
|
59
73
|
*
|
|
60
74
|
* Type rendering: `GivenTypeDef` is typed as `AtomicTypeDef |
|
|
61
75
|
* FilterExpressionParamTypeDef`, but Malloy's grammar only emits
|
|
@@ -75,6 +89,17 @@ export function malloyGivenToApi(given: MalloyGiven): MalloyGivenApi {
|
|
|
75
89
|
return {
|
|
76
90
|
name: given.name,
|
|
77
91
|
type: renderedType,
|
|
78
|
-
annotations: given.
|
|
92
|
+
annotations: given.annotations
|
|
93
|
+
.forRoute(undefined)
|
|
94
|
+
.filter((note) => !isReservedRoute(note.route))
|
|
95
|
+
.map((note) => note.text),
|
|
96
|
+
// `_internal.defaultText` is the already-rendered source literal of the
|
|
97
|
+
// given's default. It lives on Malloy's private `_internal` (the public
|
|
98
|
+
// surface exposes only the parsed `.default` AST node, not a stringified
|
|
99
|
+
// form), so we reach it through a localized cast rather than widening the
|
|
100
|
+
// duck-typed `MalloyGiven` — which would collide with the SDK `Given`'s
|
|
101
|
+
// own private `_internal` at every `as MalloyGiven` cast site.
|
|
102
|
+
default: (given as { _internal?: { defaultText?: string } })._internal
|
|
103
|
+
?.defaultText,
|
|
79
104
|
};
|
|
80
105
|
}
|
|
@@ -123,8 +123,36 @@ describe("givens introspection", () => {
|
|
|
123
123
|
|
|
124
124
|
expect(region).toBeDefined();
|
|
125
125
|
expect(region?.type).toBe("string");
|
|
126
|
+
expect(region?.default).toBe("'US'");
|
|
126
127
|
expect(cutoff).toBeDefined();
|
|
127
128
|
expect(cutoff?.type).toBe("date");
|
|
129
|
+
expect(cutoff?.default).toBe("@2024-02-01");
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("omits default for a given declared without one", async () => {
|
|
133
|
+
await fs.writeFile(
|
|
134
|
+
path.join(TEST_PKG_DIR, "mixed_defaults.malloy"),
|
|
135
|
+
`##! experimental.givens
|
|
136
|
+
|
|
137
|
+
given: with_default :: string is 'WN'
|
|
138
|
+
given: no_default :: string
|
|
139
|
+
|
|
140
|
+
source: orders is duckdb.table('orders') extend {
|
|
141
|
+
primary_key: order_id
|
|
142
|
+
}
|
|
143
|
+
`,
|
|
144
|
+
);
|
|
145
|
+
const model = await Model.create(
|
|
146
|
+
"test-pkg",
|
|
147
|
+
TEST_PKG_DIR,
|
|
148
|
+
"mixed_defaults.malloy",
|
|
149
|
+
getConnections(),
|
|
150
|
+
);
|
|
151
|
+
const byName = new Map(
|
|
152
|
+
((await model.getModel()).givens ?? []).map((g) => [g.name, g]),
|
|
153
|
+
);
|
|
154
|
+
expect(byName.get("with_default")?.default).toBe("'WN'");
|
|
155
|
+
expect(byName.get("no_default")?.default).toBeUndefined();
|
|
128
156
|
});
|
|
129
157
|
|
|
130
158
|
it("attaches the model-level givens list to every source", async () => {
|
|
@@ -179,14 +207,13 @@ describe("givens introspection", () => {
|
|
|
179
207
|
const region = compiledModel.givens?.[0];
|
|
180
208
|
expect(region?.name).toBe("region_filter");
|
|
181
209
|
|
|
182
|
-
// The
|
|
183
|
-
// Only
|
|
210
|
+
// The given declares two app-route annotations (`#(doc)`, `#(label)`).
|
|
211
|
+
// Only app routes land on the wire; Malloy-reserved routes — the
|
|
212
|
+
// model-level `##!` pragma, plain `#` tags, `#"` doc strings — must
|
|
213
|
+
// not leak onto the given's surface.
|
|
184
214
|
const annotations = region?.annotations ?? [];
|
|
185
215
|
expect(annotations.length).toBeGreaterThanOrEqual(2);
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}
|
|
189
|
-
// Negative assertion: no pragma leakage.
|
|
190
|
-
expect(annotations.some((a) => a.startsWith("##!"))).toBe(false);
|
|
216
|
+
expect(annotations.some((a) => a.startsWith("##"))).toBe(false);
|
|
217
|
+
expect(annotations.some((a) => a.startsWith('#"'))).toBe(false);
|
|
191
218
|
});
|
|
192
219
|
});
|