@tschk/moonshine-server 0.3.2 → 0.3.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tschk/moonshine-server",
3
- "version": "0.3.2",
3
+ "version": "0.3.4",
4
4
  "license": "ISC",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
@@ -15,8 +15,8 @@
15
15
  "test": "bun test"
16
16
  },
17
17
  "dependencies": {
18
- "@tschk/moonshine-framework": "^0.3.2",
19
- "@tschk/moonshine-router": "^0.3.2"
18
+ "@tschk/moonshine-framework": "^0.3.4",
19
+ "@tschk/moonshine-router": "^0.3.4"
20
20
  },
21
21
  "devDependencies": {
22
22
  "@types/bun": "1.3.14",
package/src/errors.ts CHANGED
@@ -26,9 +26,14 @@ export function errorResponse(
26
26
  error: unknown,
27
27
  mode?: "development" | "production",
28
28
  ): Response {
29
- const isDev = mode === "development";
29
+ // Anything but an explicit development mode is treated as production: the
30
+ // thrown value can carry file paths, queries, or secrets, so only the
31
+ // generic status text crosses the boundary.
32
+ if (mode !== "development") {
33
+ return json({ error: "Internal Server Error" }, { status: 500 });
34
+ }
30
35
  const message = error instanceof Error ? error.message : String(error);
31
36
  const stack = error instanceof Error ? error.stack : undefined;
32
- const body = isDev && stack ? { error: message, stack } : { error: message };
37
+ const body = stack ? { error: message, stack } : { error: message };
33
38
  return json(body, { status: 500 });
34
39
  }
package/src/index.ts CHANGED
@@ -2,7 +2,7 @@ export { createRequestHandler } from "./pipeline.js";
2
2
  export { callAction, callLoader, createRouteContext } from "./data.js";
3
3
  export { redirect, json, errorResponse, Redirect } from "./errors.js";
4
4
  export { serializeData } from "./serialize.js";
5
- export { resolveStaticPath, tryServeStatic } from "./static.js";
5
+ export { isContained, resolveStaticPath, tryServeStatic } from "./static.js";
6
6
  export type {
7
7
  Action,
8
8
  ErrorBoundary,
package/src/pipeline.ts CHANGED
@@ -236,6 +236,10 @@ function runBefores(
236
236
  export function createRequestHandler(
237
237
  options: RequestHandlerOptions,
238
238
  ): (request: Request) => Promise<Response> {
239
+ // Compiling the route graph is linear in the route count, so build it once
240
+ // per handler instead of once per request.
241
+ let cachedGraph = options.graph;
242
+
239
243
  return async (request) => {
240
244
  const url = new URL(request.url);
241
245
  const pathname = url.pathname.replace(/\/+$/, "") || "/";
@@ -250,13 +254,11 @@ export function createRequestHandler(
250
254
  if (staticRes) return staticRes;
251
255
  }
252
256
 
253
- const graph =
254
- options.graph ??
255
- (options.routes
256
- ? createRouteGraph(options.routes)
257
- : options.manifest
258
- ? createRouteGraph(options.manifest.routes)
259
- : undefined);
257
+ const graph = (cachedGraph ??= options.routes
258
+ ? createRouteGraph(options.routes)
259
+ : options.manifest
260
+ ? createRouteGraph(options.manifest.routes)
261
+ : undefined);
260
262
  if (!graph) {
261
263
  return new Response("No route graph configured", { status: 500 });
262
264
  }
package/src/static.ts CHANGED
@@ -49,7 +49,8 @@ export function resolveStaticPath(
49
49
  d === ".." ||
50
50
  d.includes("\0") ||
51
51
  d.includes(sep) ||
52
- d.includes("/")
52
+ d.includes("/") ||
53
+ (d.startsWith(".") && d !== ".well-known")
53
54
  )
54
55
  return null;
55
56
  decoded.push(d);
@@ -64,8 +65,18 @@ export function resolveStaticPath(
64
65
  * Lexical containment cannot see through symlinks, so compare real paths too:
65
66
  * a link inside the root pointing outside it would otherwise be served.
66
67
  */
68
+ const realRoots = new Map<string, string>();
69
+
67
70
  export function isContained(root: string, filePath: string): boolean {
68
- const realRoot = realpathSync.native(resolve(root));
71
+ const absRoot = resolve(root);
72
+ // The served root is fixed for the life of the process, so resolve it once;
73
+ // the file itself is still resolved on every request, which is what keeps a
74
+ // symlink escaping the root from being served.
75
+ let realRoot = realRoots.get(absRoot);
76
+ if (realRoot === undefined) {
77
+ realRoot = realpathSync.native(absRoot);
78
+ realRoots.set(absRoot, realRoot);
79
+ }
69
80
  const real = realpathSync.native(filePath);
70
81
  return real === realRoot || real.startsWith(realRoot + sep);
71
82
  }
@@ -87,6 +98,6 @@ export async function tryServeStatic(
87
98
  const type =
88
99
  MIME[extOf(filePath)] ?? (file.type || "application/octet-stream");
89
100
  return new Response(file, {
90
- headers: { "content-type": type },
101
+ headers: { "content-type": type, "x-content-type-options": "nosniff" },
91
102
  });
92
103
  }