@malloy-publisher/server 0.0.197 → 0.0.198

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 (36) hide show
  1. package/README.docker.md +47 -0
  2. package/dist/app/api-doc.yaml +3 -20
  3. package/dist/app/assets/{EnvironmentPage-BVkQH_xQ.js → EnvironmentPage-C7rtH4mC.js} +1 -1
  4. package/dist/app/assets/{HomePage-BgH9UkjK.js → HomePage-DwkH7OrS.js} +1 -1
  5. package/dist/app/assets/{MainPage-DiBxABem.js → MainPage-D38LtZDV.js} +1 -1
  6. package/dist/app/assets/{ModelPage-oS70fj83.js → ModelPage-DOol8Mz7.js} +1 -1
  7. package/dist/app/assets/{PackagePage-F_qLDAdv.js → PackagePage-0tgzA_kO.js} +1 -1
  8. package/dist/app/assets/{RouteError-WqpffppN.js → RouteError-BaMsOSly.js} +1 -1
  9. package/dist/app/assets/{WorkbookPage-_YmC-ebR.js → WorkbookPage-Cx4SePkx.js} +1 -1
  10. package/dist/app/assets/{core-B8L9xCYT.es-BcRLJTnC.js → core-CbsC6R_Y.es-Cwf6asf3.js} +1 -1
  11. package/dist/app/assets/{index-rg8Ok8nl.js → index-DL6BZTuw.js} +1 -1
  12. package/dist/app/assets/{index-C3XPaTaS.js → index-DNofXMxi.js} +1 -1
  13. package/dist/app/assets/{index-BMViiwtJ.js → index-U38AyjJL.js} +3 -3
  14. package/dist/app/assets/{index.umd-CCAfKkxY.js → index.umd-B68wGGkM.js} +1 -1
  15. package/dist/app/index.html +1 -1
  16. package/dist/server.mjs +812 -450
  17. package/package.json +1 -1
  18. package/src/config.spec.ts +81 -0
  19. package/src/config.ts +126 -0
  20. package/src/controller/package.controller.ts +70 -29
  21. package/src/errors.ts +13 -0
  22. package/src/health.ts +0 -26
  23. package/src/mcp/tools/discovery_tools.ts +6 -2
  24. package/src/path_safety.spec.ts +158 -0
  25. package/src/path_safety.ts +140 -0
  26. package/src/server.ts +13 -0
  27. package/src/service/environment.ts +614 -198
  28. package/src/service/environment_admission.spec.ts +180 -0
  29. package/src/service/environment_store.spec.ts +0 -19
  30. package/src/service/environment_store.ts +24 -21
  31. package/src/service/manifest_service.spec.ts +7 -2
  32. package/src/service/manifest_service.ts +8 -2
  33. package/src/service/materialization_service.ts +14 -3
  34. package/src/service/package_memory_governor.spec.ts +173 -0
  35. package/src/service/package_memory_governor.ts +233 -0
  36. package/src/service/package_race.spec.ts +208 -0
@@ -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
@@ -33,6 +33,7 @@ import {
33
33
  } from "./instrumentation";
34
34
  import { logger, loggerMiddleware } from "./logger";
35
35
 
36
+ import { getMemoryGovernorConfig } from "./config";
36
37
  import { ManifestController } from "./controller/manifest.controller";
37
38
  import { MaterializationController } from "./controller/materialization.controller";
38
39
  import { initializeMcpServer } from "./mcp/server";
@@ -40,6 +41,7 @@ import { registerLegacyRoutes } from "./server-old";
40
41
  import { EnvironmentStore } from "./service/environment_store";
41
42
  import { ManifestService } from "./service/manifest_service";
42
43
  import { MaterializationService } from "./service/materialization_service";
44
+ import { PackageMemoryGovernor } from "./service/package_memory_governor";
43
45
 
44
46
  /** Normalize an Express query param into a string[] or undefined. */
45
47
  export function normalizeQueryArray(value: unknown): string[] | undefined {
@@ -158,6 +160,17 @@ const manifestService = new ManifestService(environmentStore);
158
160
  const watchModeController = new WatchModeController(environmentStore);
159
161
  const connectionController = new ConnectionController(environmentStore);
160
162
  const modelController = new ModelController(environmentStore);
163
+ // PackageMemoryGovernor is opt-in via PUBLISHER_MAX_MEMORY_BYTES.
164
+ // When set, it polls process RSS and flips an `isBackpressured` flag
165
+ // that Environment.getPackage / addPackage consult before allocating
166
+ // any new package — the server responds with HTTP 503 instead of
167
+ // OOM-killing the pod.
168
+ const memoryGovernorConfig = getMemoryGovernorConfig();
169
+ const memoryGovernor = memoryGovernorConfig
170
+ ? new PackageMemoryGovernor(memoryGovernorConfig)
171
+ : null;
172
+ memoryGovernor?.start();
173
+ environmentStore.setMemoryGovernor(memoryGovernor);
161
174
  const packageController = new PackageController(
162
175
  environmentStore,
163
176
  manifestService,