@malloy-publisher/server 0.0.197 → 0.0.198-dev1

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 (54) hide show
  1. package/README.docker.md +47 -0
  2. package/build.ts +26 -1
  3. package/dist/app/api-doc.yaml +54 -20
  4. package/dist/app/assets/{EnvironmentPage-BVkQH_xQ.js → EnvironmentPage-Dpee_Kn6.js} +1 -1
  5. package/dist/app/assets/{HomePage-BgH9UkjK.js → HomePage-DLRWTNoL.js} +1 -1
  6. package/dist/app/assets/{MainPage-DiBxABem.js → MainPage-DsVt5QGM.js} +1 -1
  7. package/dist/app/assets/{ModelPage-oS70fj83.js → ModelPage-AwAugZ37.js} +1 -1
  8. package/dist/app/assets/{PackagePage-F_qLDAdv.js → PackagePage-XQ-EWGTC.js} +1 -1
  9. package/dist/app/assets/{RouteError-WqpffppN.js → RouteError-3Mv8JQw7.js} +1 -1
  10. package/dist/app/assets/{WorkbookPage-_YmC-ebR.js → WorkbookPage-DHYYpcYc.js} +1 -1
  11. package/dist/app/assets/{core-B8L9xCYT.es-BcRLJTnC.js → core-DfcpQGVP.es-DQggNOdX.js} +1 -1
  12. package/dist/app/assets/{index-C3XPaTaS.js → index-BUp81Qdm.js} +1 -1
  13. package/dist/app/assets/{index-rg8Ok8nl.js → index-D1pdwrUW.js} +1 -1
  14. package/dist/app/assets/{index-BMViiwtJ.js → index-Dv5bF4Ii.js} +4 -4
  15. package/dist/app/assets/{index.umd-CCAfKkxY.js → index.umd-CQH4LZU8.js} +1 -1
  16. package/dist/app/index.html +1 -1
  17. package/dist/compile_worker.mjs +628 -0
  18. package/dist/instrumentation.mjs +36 -36
  19. package/dist/server.mjs +1781 -809
  20. package/package.json +1 -1
  21. package/src/compile/compile_pool.spec.ts +227 -0
  22. package/src/compile/compile_pool.ts +729 -0
  23. package/src/compile/compile_worker.ts +683 -0
  24. package/src/compile/protocol.ts +251 -0
  25. package/src/config.spec.ts +81 -0
  26. package/src/config.ts +126 -0
  27. package/src/controller/compile.controller.ts +3 -1
  28. package/src/controller/model.controller.ts +8 -1
  29. package/src/controller/package.controller.ts +70 -29
  30. package/src/controller/query.controller.ts +3 -0
  31. package/src/errors.ts +13 -0
  32. package/src/health.spec.ts +90 -0
  33. package/src/health.ts +86 -71
  34. package/src/mcp/tools/discovery_tools.ts +6 -2
  35. package/src/mcp/tools/execute_query_tool.ts +12 -0
  36. package/src/path_safety.spec.ts +158 -0
  37. package/src/path_safety.ts +140 -0
  38. package/src/server.ts +29 -0
  39. package/src/service/environment.ts +616 -199
  40. package/src/service/environment_admission.spec.ts +180 -0
  41. package/src/service/environment_store.spec.ts +0 -19
  42. package/src/service/environment_store.ts +24 -21
  43. package/src/service/filter_integration.spec.ts +110 -0
  44. package/src/service/givens_integration.spec.ts +192 -0
  45. package/src/service/manifest_service.spec.ts +7 -2
  46. package/src/service/manifest_service.ts +8 -2
  47. package/src/service/materialization_service.ts +14 -3
  48. package/src/service/model.spec.ts +105 -0
  49. package/src/service/model.ts +317 -10
  50. package/src/service/model_worker_path.spec.ts +125 -0
  51. package/src/service/package_memory_governor.spec.ts +173 -0
  52. package/src/service/package_memory_governor.ts +233 -0
  53. package/src/service/package_race.spec.ts +208 -0
  54. package/tests/integration/concurrent_package/concurrent_package.integration.spec.ts +280 -0
@@ -0,0 +1,158 @@
1
+ import { describe, expect, it } from "bun:test";
2
+ import * as path from "path";
3
+
4
+ import { BadRequestError } from "./errors";
5
+ import {
6
+ assertSafeEnvironmentPath,
7
+ assertSafePackageName,
8
+ assertSafeRelativeModelPath,
9
+ safeJoinUnderRoot,
10
+ } from "./path_safety";
11
+
12
+ describe("assertSafePackageName", () => {
13
+ it.each([
14
+ "pkg",
15
+ "test_package",
16
+ "test-package",
17
+ "TestPackage1",
18
+ "test.package.name",
19
+ "a",
20
+ "x".repeat(255),
21
+ ])("accepts %p", (name) => {
22
+ expect(() => assertSafePackageName(name)).not.toThrow();
23
+ });
24
+
25
+ it.each([
26
+ ["empty", ""],
27
+ ["dot", "."],
28
+ ["dot-dot", ".."],
29
+ ["leading dot", ".staging"],
30
+ ["forward slash", "foo/bar"],
31
+ ["backslash", "foo\\bar"],
32
+ ["null byte", "foo\0bar"],
33
+ ["traversal", "../etc/passwd"],
34
+ ["abs", "/etc/passwd"],
35
+ ["space", "my pkg"],
36
+ ["unicode", "pkg\u202E"],
37
+ ["too long", "x".repeat(256)],
38
+ ])("rejects %s (%p)", (_label, name) => {
39
+ expect(() => assertSafePackageName(name)).toThrow(BadRequestError);
40
+ });
41
+
42
+ it.each([
43
+ ["number", 42],
44
+ ["null", null],
45
+ ["undefined", undefined],
46
+ ["object", { name: "pkg" }],
47
+ ])("rejects non-string %s (%p)", (_label, value) => {
48
+ expect(() => assertSafePackageName(value)).toThrow(BadRequestError);
49
+ });
50
+ });
51
+
52
+ describe("assertSafeRelativeModelPath", () => {
53
+ it.each([
54
+ "model.malloy",
55
+ "models/foo.malloy",
56
+ "a/b/c/d.malloynb",
57
+ "deep/nested/file_name-1.malloy",
58
+ ])("accepts %p", (modelPath) => {
59
+ expect(() => assertSafeRelativeModelPath(modelPath)).not.toThrow();
60
+ });
61
+
62
+ it.each([
63
+ ["empty", ""],
64
+ ["leading slash (absolute)", "/etc/passwd"],
65
+ ["traversal", "../etc/passwd"],
66
+ ["embedded traversal", "models/../../../etc/passwd"],
67
+ ["embedded dot segment", "models/./foo.malloy"],
68
+ ["double slash", "models//foo.malloy"],
69
+ ["trailing slash", "models/foo/"],
70
+ ["backslash", "models\\foo.malloy"],
71
+ ["null byte", "models/foo\0.malloy"],
72
+ ["dotfile segment", ".staging/foo.malloy"],
73
+ ["dotfile leaf", "models/.hidden.malloy"],
74
+ ])("rejects %s (%p)", (_label, modelPath) => {
75
+ expect(() => assertSafeRelativeModelPath(modelPath)).toThrow(
76
+ BadRequestError,
77
+ );
78
+ });
79
+
80
+ it("rejects non-string inputs", () => {
81
+ expect(() => assertSafeRelativeModelPath(undefined)).toThrow(
82
+ BadRequestError,
83
+ );
84
+ expect(() => assertSafeRelativeModelPath(123)).toThrow(BadRequestError);
85
+ });
86
+ });
87
+
88
+ describe("assertSafeEnvironmentPath", () => {
89
+ it.each([
90
+ "/etc/publisher",
91
+ "/var/lib/publisher/env1",
92
+ "/Users/me/data",
93
+ "/a",
94
+ "C:\\Users\\me\\publisher",
95
+ "C:/Users/me/publisher",
96
+ ])("accepts %p", (p) => {
97
+ expect(() => assertSafeEnvironmentPath(p)).not.toThrow();
98
+ });
99
+
100
+ it.each([
101
+ ["empty", ""],
102
+ ["relative", "publisher/data"],
103
+ ["traversal in middle", "/var/lib/../../etc/passwd"],
104
+ ["traversal at end", "/var/lib/publisher/.."],
105
+ ["null byte", "/var/lib/publisher\0"],
106
+ ["bare dot-dot", ".."],
107
+ ["bare dot", "."],
108
+ ["too long", "/" + "a".repeat(5000)],
109
+ ])("rejects %s (%p)", (_label, p) => {
110
+ expect(() => assertSafeEnvironmentPath(p)).toThrow(BadRequestError);
111
+ });
112
+
113
+ it("rejects non-string inputs", () => {
114
+ expect(() => assertSafeEnvironmentPath(undefined)).toThrow(
115
+ BadRequestError,
116
+ );
117
+ expect(() => assertSafeEnvironmentPath(null)).toThrow(BadRequestError);
118
+ expect(() => assertSafeEnvironmentPath(42)).toThrow(BadRequestError);
119
+ });
120
+ });
121
+
122
+ describe("safeJoinUnderRoot", () => {
123
+ const root = "/tmp/test-root";
124
+
125
+ it("returns the resolved root when joined with no segments", () => {
126
+ expect(safeJoinUnderRoot(root)).toBe(path.resolve(root));
127
+ });
128
+
129
+ it("joins safe segments into a path under root", () => {
130
+ expect(safeJoinUnderRoot(root, "pkg", "model.malloy")).toBe(
131
+ path.resolve(root, "pkg", "model.malloy"),
132
+ );
133
+ });
134
+
135
+ it("throws when traversal escapes the root", () => {
136
+ expect(() => safeJoinUnderRoot(root, "..")).toThrow(BadRequestError);
137
+ expect(() => safeJoinUnderRoot(root, "..", "etc", "passwd")).toThrow(
138
+ BadRequestError,
139
+ );
140
+ expect(() => safeJoinUnderRoot(root, "pkg", "..", "..", "etc")).toThrow(
141
+ BadRequestError,
142
+ );
143
+ });
144
+
145
+ it("throws when an absolute segment overrides the root", () => {
146
+ expect(() => safeJoinUnderRoot(root, "/etc/passwd")).toThrow(
147
+ BadRequestError,
148
+ );
149
+ });
150
+
151
+ it("does NOT match a sibling directory with the same prefix", () => {
152
+ // path.resolve("/tmp/test-root", "../test-root-bad") -> "/tmp/test-root-bad"
153
+ // which starts with "/tmp/test-root" textually but is NOT a child.
154
+ expect(() => safeJoinUnderRoot(root, "..", "test-root-bad")).toThrow(
155
+ BadRequestError,
156
+ );
157
+ });
158
+ });
@@ -0,0 +1,140 @@
1
+ import * as path from "path";
2
+
3
+ import { BadRequestError } from "./errors";
4
+
5
+ /**
6
+ * Path-safety helpers used by `Environment` (and any other service that
7
+ * builds an on-disk path from request data) to defend against directory
8
+ * traversal. The intent is two-fold:
9
+ *
10
+ * 1. **Source-side allowlist**: `assertSafePackageName` /
11
+ * `assertSafeRelativeModelPath` reject hostile inputs (`..`, leading
12
+ * `/`, `\`, NUL, dotfiles) at the entry of every public service
13
+ * method before any path-construction happens. These throw
14
+ * `BadRequestError` so the controller layer's error mapper returns
15
+ * HTTP 400.
16
+ *
17
+ * 2. **Sink-side containment**: `safeJoinUnderRoot` joins, resolves,
18
+ * and verifies the result is strictly within the supplied root.
19
+ * Even if a future caller forgets the source-side check, the sink
20
+ * refuses to hand back an escaping path. This is the standard
21
+ * "resolve-and-contain" pattern that CodeQL's `js/path-injection`
22
+ * query recognises as a sanitizer.
23
+ */
24
+
25
+ // Single path segment: ASCII letters, digits, `-`, `_`, `.`. No leading
26
+ // `.` so internal sibling dirs (`.staging`, `.retired`) and editor /
27
+ // VCS dirs can't be addressed by name from outside.
28
+ const SAFE_NAME_RE = /^(?!\.\.?$)(?!\.)[A-Za-z0-9._-]{1,255}$/;
29
+
30
+ const MAX_MODEL_PATH_LEN = 1024;
31
+
32
+ // An environment path is server-controlled (config / disk-derived), but
33
+ // CodeQL conservatively treats it as tainted because Express handlers on
34
+ // the same class touch user input. The combined regex test +
35
+ // `..` / NUL / length check at the constructor gate is the sanitizer
36
+ // barrier the `js/path-injection` query recognises. Printable ASCII
37
+ // only; absolute POSIX-or-Windows path; no `..`, no NUL.
38
+ const SAFE_ENVIRONMENT_PATH_RE = /^(?:\/|[A-Za-z]:[\\/])[\x20-\x7E]*$/;
39
+ const MAX_ENVIRONMENT_PATH_LEN = 4096;
40
+
41
+ /**
42
+ * Reject anything that isn't a plausible single-segment package name.
43
+ * The allowlist is deliberately conservative — every existing test and
44
+ * production package name we've seen fits within it, and tightening
45
+ * here costs nothing.
46
+ */
47
+ export function assertSafePackageName(packageName: unknown): void {
48
+ if (typeof packageName !== "string" || !SAFE_NAME_RE.test(packageName)) {
49
+ throw new BadRequestError(
50
+ `Invalid package name: must be 1-255 characters of letters, digits, "-", "_", or "." and must not start with "."`,
51
+ );
52
+ }
53
+ }
54
+
55
+ /**
56
+ * Reject anything that isn't a plausible *relative* path to a model
57
+ * file inside a package directory. Forward slashes are allowed (models
58
+ * live in subdirectories like `models/foo.malloy`); backslashes,
59
+ * absolute paths, NUL bytes, and `..` / `.` segments are not.
60
+ */
61
+ export function assertSafeRelativeModelPath(modelPath: unknown): void {
62
+ if (
63
+ typeof modelPath !== "string" ||
64
+ modelPath.length === 0 ||
65
+ modelPath.length > MAX_MODEL_PATH_LEN ||
66
+ modelPath.includes("\0") ||
67
+ modelPath.includes("\\") ||
68
+ path.isAbsolute(modelPath) ||
69
+ modelPath.startsWith("/")
70
+ ) {
71
+ throw new BadRequestError(`Invalid model path`);
72
+ }
73
+
74
+ const segments = modelPath.split("/");
75
+ for (const segment of segments) {
76
+ if (segment === "" || segment === "." || segment === "..") {
77
+ throw new BadRequestError(`Invalid model path`);
78
+ }
79
+ if (segment.startsWith(".")) {
80
+ throw new BadRequestError(`Invalid model path`);
81
+ }
82
+ }
83
+ }
84
+
85
+ /**
86
+ * Reject anything that doesn't look like a server-controlled absolute
87
+ * filesystem path. Applied to `environmentPath` at the constructor
88
+ * gate so all downstream `path.join(this.environmentPath, …)` sites
89
+ * see a value that has cleared an allowlist check — the canonical
90
+ * sanitizer-barrier pattern CodeQL's `js/path-injection` query
91
+ * recognises.
92
+ */
93
+ export function assertSafeEnvironmentPath(environmentPath: unknown): void {
94
+ if (typeof environmentPath !== "string") {
95
+ throw new BadRequestError(`Invalid environment path: must be a string`);
96
+ }
97
+ if (
98
+ environmentPath.length === 0 ||
99
+ environmentPath.length > MAX_ENVIRONMENT_PATH_LEN
100
+ ) {
101
+ throw new BadRequestError(`Invalid environment path: bad length`);
102
+ }
103
+ if (environmentPath.indexOf("\0") !== -1) {
104
+ throw new BadRequestError(`Invalid environment path: contains NUL byte`);
105
+ }
106
+ // Sanitizer barrier in the shape `x.indexOf("..") !== -1` that the
107
+ // CodeQL `js/path-injection` query recognises as a traversal guard.
108
+ if (environmentPath.indexOf("..") !== -1) {
109
+ throw new BadRequestError(
110
+ `Invalid environment path: contains ".." traversal segment`,
111
+ );
112
+ }
113
+ if (!SAFE_ENVIRONMENT_PATH_RE.test(environmentPath)) {
114
+ throw new BadRequestError(
115
+ `Invalid environment path: must be an absolute path of printable ASCII characters`,
116
+ );
117
+ }
118
+ }
119
+
120
+ /**
121
+ * Resolve `path.join(root, ...segments)` and verify the result lives
122
+ * strictly inside `root` (or is `root` itself). Throws
123
+ * `BadRequestError` if the resolved path escapes the root via `..`,
124
+ * absolute segments, or symlink-style trickery in the input.
125
+ *
126
+ * Callers should still run `assertSafePackageName` / similar on
127
+ * user-controlled segments first — this helper is the second line of
128
+ * defense, not the first.
129
+ */
130
+ export function safeJoinUnderRoot(root: string, ...segments: string[]): string {
131
+ const resolvedRoot = path.resolve(root);
132
+ const joined = path.resolve(resolvedRoot, ...segments);
133
+ const rootWithSep = resolvedRoot.endsWith(path.sep)
134
+ ? resolvedRoot
135
+ : resolvedRoot + path.sep;
136
+ if (joined !== resolvedRoot && !joined.startsWith(rootWithSep)) {
137
+ throw new BadRequestError(`Resolved path is outside of root`);
138
+ }
139
+ return joined;
140
+ }
package/src/server.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  // Pre-load the instrumentation module; the instrumentation module must be loaded before the other imports.
2
+ import type { GivenValue } from "@malloydata/malloy";
2
3
  import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
3
4
  import bodyParser from "body-parser";
4
5
  import cors from "cors";
@@ -33,6 +34,7 @@ import {
33
34
  } from "./instrumentation";
34
35
  import { logger, loggerMiddleware } from "./logger";
35
36
 
37
+ import { getMemoryGovernorConfig } from "./config";
36
38
  import { ManifestController } from "./controller/manifest.controller";
37
39
  import { MaterializationController } from "./controller/materialization.controller";
38
40
  import { initializeMcpServer } from "./mcp/server";
@@ -40,6 +42,7 @@ import { registerLegacyRoutes } from "./server-old";
40
42
  import { EnvironmentStore } from "./service/environment_store";
41
43
  import { ManifestService } from "./service/manifest_service";
42
44
  import { MaterializationService } from "./service/materialization_service";
45
+ import { PackageMemoryGovernor } from "./service/package_memory_governor";
43
46
 
44
47
  /** Normalize an Express query param into a string[] or undefined. */
45
48
  export function normalizeQueryArray(value: unknown): string[] | undefined {
@@ -158,6 +161,17 @@ const manifestService = new ManifestService(environmentStore);
158
161
  const watchModeController = new WatchModeController(environmentStore);
159
162
  const connectionController = new ConnectionController(environmentStore);
160
163
  const modelController = new ModelController(environmentStore);
164
+ // PackageMemoryGovernor is opt-in via PUBLISHER_MAX_MEMORY_BYTES.
165
+ // When set, it polls process RSS and flips an `isBackpressured` flag
166
+ // that Environment.getPackage / addPackage consult before allocating
167
+ // any new package — the server responds with HTTP 503 instead of
168
+ // OOM-killing the pod.
169
+ const memoryGovernorConfig = getMemoryGovernorConfig();
170
+ const memoryGovernor = memoryGovernorConfig
171
+ ? new PackageMemoryGovernor(memoryGovernorConfig)
172
+ : null;
173
+ memoryGovernor?.start();
174
+ environmentStore.setMemoryGovernor(memoryGovernor);
161
175
  const packageController = new PackageController(
162
176
  environmentStore,
163
177
  manifestService,
@@ -1097,6 +1111,18 @@ app.get(
1097
1111
  const bypassFilters =
1098
1112
  req.query.bypass_filters === "true" ? true : undefined;
1099
1113
 
1114
+ let givens: Record<string, GivenValue> | undefined;
1115
+ if (typeof req.query.givens === "string") {
1116
+ try {
1117
+ givens = JSON.parse(req.query.givens);
1118
+ } catch {
1119
+ res.status(400).json({
1120
+ error: "Invalid givens: must be valid JSON",
1121
+ });
1122
+ return;
1123
+ }
1124
+ }
1125
+
1100
1126
  res.status(200).json(
1101
1127
  await modelController.executeNotebookCell(
1102
1128
  req.params.environmentName,
@@ -1105,6 +1131,7 @@ app.get(
1105
1131
  cellIndex,
1106
1132
  filterParams,
1107
1133
  bypassFilters,
1134
+ givens,
1108
1135
  ),
1109
1136
  );
1110
1137
  } catch (error) {
@@ -1165,6 +1192,7 @@ app.post(
1165
1192
  | Record<string, string | string[]>
1166
1193
  | undefined,
1167
1194
  req.body.bypassFilters === true ? true : undefined,
1195
+ req.body.givens as Record<string, GivenValue> | undefined,
1168
1196
  ),
1169
1197
  );
1170
1198
  } catch (error) {
@@ -1208,6 +1236,7 @@ app.post(
1208
1236
  req.params.modelName,
1209
1237
  req.body.source,
1210
1238
  req.body.includeSql === true,
1239
+ req.body.givens as Record<string, GivenValue> | undefined,
1211
1240
  );
1212
1241
  res.status(200).json(result);
1213
1242
  } catch (error) {