Block static server symlink escape paths
Co-authored-by: Theo Browne <[email protected]>
This commit is contained in:
1 parent
e0d34cd373
commit
be9c7df998
2 files changed
+115
-8
No files matched your search
+42
-1
@@ -1,4 +1,4 @@
|
||||
import { mkdtempSync, writeFileSync } from "node:fs";
|
||||
import { mkdtempSync, mkdirSync, symlinkSync, writeFileSync } from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { describe, expect, it } from "vitest";
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
formatStartupError,
|
||||
parseCliOptions,
|
||||
readCliVersion,
|
||||
resolveStaticAssetReadTarget,
|
||||
resolveStaticAssetPath,
|
||||
} from "./cli";
|
||||
|
||||
@@ -346,3 +347,43 @@ describe("resolveStaticAssetPath", () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveStaticAssetReadTarget", () => {
|
||||
it("falls back to index for unknown routes", () => {
|
||||
const tempDir = mkdtempSync(path.join(os.tmpdir(), "t3-static-route-"));
|
||||
writeFileSync(path.join(tempDir, "index.html"), "<html>ok</html>", "utf8");
|
||||
|
||||
const result = resolveStaticAssetReadTarget("/unknown/route", tempDir);
|
||||
expect(result).toEqual({
|
||||
kind: "file",
|
||||
filePath: path.join(tempDir, "index.html"),
|
||||
});
|
||||
});
|
||||
|
||||
it("returns concrete file paths for existing assets", () => {
|
||||
const tempDir = mkdtempSync(path.join(os.tmpdir(), "t3-static-asset-"));
|
||||
const assetsDir = path.join(tempDir, "assets");
|
||||
mkdirSync(assetsDir, { recursive: true });
|
||||
writeFileSync(path.join(tempDir, "index.html"), "<html>ok</html>", "utf8");
|
||||
writeFileSync(path.join(assetsDir, "main.js"), "console.log('ok')", "utf8");
|
||||
|
||||
const result = resolveStaticAssetReadTarget("/assets/main.js", tempDir);
|
||||
expect(result).toEqual({
|
||||
kind: "file",
|
||||
filePath: path.join(assetsDir, "main.js"),
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects symlinked files that escape the dist directory", () => {
|
||||
const tempDir = mkdtempSync(path.join(os.tmpdir(), "t3-static-symlink-"));
|
||||
writeFileSync(path.join(tempDir, "index.html"), "<html>ok</html>", "utf8");
|
||||
const outsideFile = path.join(os.tmpdir(), `t3-outside-${Date.now()}.txt`);
|
||||
writeFileSync(outsideFile, "outside", "utf8");
|
||||
symlinkSync(outsideFile, path.join(tempDir, "outside.txt"));
|
||||
|
||||
const result = resolveStaticAssetReadTarget("/outside.txt", tempDir);
|
||||
expect(result).toEqual({
|
||||
kind: "forbidden",
|
||||
});
|
||||
});
|
||||
});
|
||||
+73
-7
@@ -383,17 +383,85 @@ export function resolveStaticAssetPath(
|
||||
return { kind: "file", filePath: candidateFilePath };
|
||||
}
|
||||
|
||||
function isExistingFile(filePath: string): boolean {
|
||||
function resolveSafeFilePathInDist(
|
||||
filePath: string,
|
||||
realDistRoot: string,
|
||||
): { kind: "file"; filePath: string } | { kind: "missing" } | { kind: "forbidden" } {
|
||||
let stats: fs.Stats;
|
||||
try {
|
||||
return fs.statSync(filePath).isFile();
|
||||
stats = fs.lstatSync(filePath);
|
||||
} catch {
|
||||
return false;
|
||||
return { kind: "missing" };
|
||||
}
|
||||
|
||||
if (stats.isDirectory()) {
|
||||
return { kind: "missing" };
|
||||
}
|
||||
|
||||
if (!stats.isFile() && !stats.isSymbolicLink()) {
|
||||
return { kind: "missing" };
|
||||
}
|
||||
|
||||
let realFilePath: string;
|
||||
try {
|
||||
realFilePath = fs.realpathSync(filePath);
|
||||
} catch {
|
||||
return { kind: "missing" };
|
||||
}
|
||||
|
||||
if (!isPathInside(realDistRoot, realFilePath)) {
|
||||
return { kind: "forbidden" };
|
||||
}
|
||||
|
||||
try {
|
||||
if (!fs.statSync(realFilePath).isFile()) {
|
||||
return { kind: "missing" };
|
||||
}
|
||||
} catch {
|
||||
return { kind: "missing" };
|
||||
}
|
||||
|
||||
return { kind: "file", filePath: realFilePath };
|
||||
}
|
||||
|
||||
export function resolveStaticAssetReadTarget(
|
||||
requestUrl: string | undefined,
|
||||
distRoot: string,
|
||||
): { kind: "file"; filePath: string } | { kind: "forbidden" } | { kind: "bad_request" } {
|
||||
const normalizedDistRoot = path.resolve(distRoot);
|
||||
const realDistRoot = (() => {
|
||||
try {
|
||||
return fs.realpathSync(normalizedDistRoot);
|
||||
} catch {
|
||||
return normalizedDistRoot;
|
||||
}
|
||||
})();
|
||||
|
||||
const requestedPath = resolveStaticAssetPath(requestUrl, normalizedDistRoot);
|
||||
if (requestedPath.kind === "bad_request" || requestedPath.kind === "forbidden") {
|
||||
return requestedPath;
|
||||
}
|
||||
|
||||
const requestedFile = resolveSafeFilePathInDist(requestedPath.filePath, realDistRoot);
|
||||
if (requestedFile.kind === "forbidden") {
|
||||
return { kind: "forbidden" };
|
||||
}
|
||||
if (requestedFile.kind === "file") {
|
||||
return requestedFile;
|
||||
}
|
||||
|
||||
const indexPath = path.join(normalizedDistRoot, "index.html");
|
||||
const indexFile = resolveSafeFilePathInDist(indexPath, realDistRoot);
|
||||
if (indexFile.kind !== "file") {
|
||||
return { kind: "bad_request" };
|
||||
}
|
||||
|
||||
return indexFile;
|
||||
}
|
||||
|
||||
function startStaticWebServer(distRoot: string, port: number) {
|
||||
const server = createServer((request, response) => {
|
||||
const resolvedPath = resolveStaticAssetPath(request.url, distRoot);
|
||||
const resolvedPath = resolveStaticAssetReadTarget(request.url, distRoot);
|
||||
if (resolvedPath.kind === "bad_request") {
|
||||
response.statusCode = 400;
|
||||
response.end("Invalid request path");
|
||||
@@ -406,9 +474,7 @@ function startStaticWebServer(distRoot: string, port: number) {
|
||||
return;
|
||||
}
|
||||
|
||||
const targetPath = isExistingFile(resolvedPath.filePath)
|
||||
? resolvedPath.filePath
|
||||
: path.join(distRoot, "index.html");
|
||||
const targetPath = resolvedPath.filePath;
|
||||
fs.readFile(targetPath, (error, content) => {
|
||||
if (error) {
|
||||
response.statusCode = 404;
|
||||
|
||||
Reference in new issue
Block a user