@tschk/moonshine-server 0.3.1 → 0.3.2
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 +3 -3
- package/src/static.ts +16 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tschk/moonshine-server",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
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.
|
|
19
|
-
"@tschk/moonshine-router": "^0.3.
|
|
18
|
+
"@tschk/moonshine-framework": "^0.3.2",
|
|
19
|
+
"@tschk/moonshine-router": "^0.3.2"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
22
|
"@types/bun": "1.3.14",
|
package/src/static.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { realpathSync } from "node:fs";
|
|
1
2
|
import { join, normalize, resolve, sep } from "node:path";
|
|
2
3
|
|
|
3
4
|
function decodeSegment(part: string): string | null {
|
|
@@ -59,6 +60,16 @@ export function resolveStaticPath(
|
|
|
59
60
|
return candidate;
|
|
60
61
|
}
|
|
61
62
|
|
|
63
|
+
/**
|
|
64
|
+
* Lexical containment cannot see through symlinks, so compare real paths too:
|
|
65
|
+
* a link inside the root pointing outside it would otherwise be served.
|
|
66
|
+
*/
|
|
67
|
+
export function isContained(root: string, filePath: string): boolean {
|
|
68
|
+
const realRoot = realpathSync.native(resolve(root));
|
|
69
|
+
const real = realpathSync.native(filePath);
|
|
70
|
+
return real === realRoot || real.startsWith(realRoot + sep);
|
|
71
|
+
}
|
|
72
|
+
|
|
62
73
|
export async function tryServeStatic(
|
|
63
74
|
staticDir: string,
|
|
64
75
|
pathname: string,
|
|
@@ -68,6 +79,11 @@ export async function tryServeStatic(
|
|
|
68
79
|
if (!filePath) return null;
|
|
69
80
|
const file = Bun.file(filePath);
|
|
70
81
|
if (!(await file.exists())) return null;
|
|
82
|
+
try {
|
|
83
|
+
if (!isContained(staticDir, filePath)) return null;
|
|
84
|
+
} catch {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
71
87
|
const type =
|
|
72
88
|
MIME[extOf(filePath)] ?? (file.type || "application/octet-stream");
|
|
73
89
|
return new Response(file, {
|