diff --git a/apps/t3/src/cli.test.ts b/apps/t3/src/cli.test.ts index 5420fed132..b0aed0f6f8 100644 --- a/apps/t3/src/cli.test.ts +++ b/apps/t3/src/cli.test.ts @@ -3,7 +3,12 @@ import os from "node:os"; import path from "node:path"; import { describe, expect, it } from "vitest"; -import { formatStartupError, parseCliOptions, readCliVersion } from "./cli"; +import { + formatStartupError, + parseCliOptions, + readCliVersion, + resolveStaticAssetPath, +} from "./cli"; describe("parseCliOptions", () => { it("reads defaults from environment variables", () => { @@ -144,6 +149,11 @@ describe("parseCliOptions", () => { expect(options.webPortLocked).toBe(false); }); + it("normalizes the parser cwd for default launch path", () => { + const options = parseCliOptions([], {}, "apps/t3"); + expect(options.launchCwd).toBe(path.resolve("apps/t3")); + }); + it("supports help flag", () => { const options = parseCliOptions(["--help"], {}, "/workspace"); expect(options.showHelp).toBe(true); @@ -283,3 +293,44 @@ describe("formatStartupError", () => { expect(message).toBe("Failed to start t3 runtime."); }); }); + +describe("resolveStaticAssetPath", () => { + const distRoot = "/workspace/apps/renderer/dist"; + + it("maps root path to index.html", () => { + const result = resolveStaticAssetPath("/", distRoot); + expect(result).toEqual({ + kind: "file", + filePath: path.join(distRoot, "index.html"), + }); + }); + + it("maps request paths without query strings", () => { + const result = resolveStaticAssetPath("/assets/main.js?v=123", distRoot); + expect(result).toEqual({ + kind: "file", + filePath: path.join(distRoot, "assets", "main.js"), + }); + }); + + it("rejects traversal attempts with decoded dot-dot segments", () => { + const result = resolveStaticAssetPath("/../package.json", distRoot); + expect(result).toEqual({ + kind: "forbidden", + }); + }); + + it("rejects traversal attempts with encoded dot-dot segments", () => { + const result = resolveStaticAssetPath("/%2e%2e/%2e%2e/package.json", distRoot); + expect(result).toEqual({ + kind: "forbidden", + }); + }); + + it("rejects malformed encoded paths", () => { + const result = resolveStaticAssetPath("/%E0%A4%A", distRoot); + expect(result).toEqual({ + kind: "bad_request", + }); + }); +}); diff --git a/apps/t3/src/cli.ts b/apps/t3/src/cli.ts index 9afb52bb8d..df50a85a2c 100644 --- a/apps/t3/src/cli.ts +++ b/apps/t3/src/cli.ts @@ -104,6 +104,7 @@ export function parseCliOptions( env: NodeJS.ProcessEnv, cwd: string, ): CliOptions { + const parserCwd = path.resolve(cwd); const backendPortFromEnv = parseEnvPort( env.T3_BACKEND_PORT, "T3_BACKEND_PORT", @@ -115,7 +116,7 @@ export function parseCliOptions( let webPort = webPortFromEnv.port; let backendPortLocked = backendPortFromEnv.locked; let webPortLocked = webPortFromEnv.locked; - let launchCwd = cwd; + let launchCwd = parserCwd; let usedPositionalCwd = false; let noOpen = parseBooleanEnvFlag(env.T3_NO_OPEN); let showHelp = false; @@ -170,12 +171,12 @@ export function parseCliOptions( } if (arg.startsWith("--cwd=")) { - launchCwd = parseExplicitPath(arg.split("=")[1] ?? "", "--cwd", cwd); + launchCwd = parseExplicitPath(arg.split("=")[1] ?? "", "--cwd", parserCwd); continue; } if (arg === "--cwd") { - launchCwd = parseExplicitPath(readArgValue(argv, index, "--cwd"), "--cwd", cwd); + launchCwd = parseExplicitPath(readArgValue(argv, index, "--cwd"), "--cwd", parserCwd); index += 1; continue; } @@ -184,7 +185,7 @@ export function parseCliOptions( if (usedPositionalCwd) { throw new Error(`Unexpected positional argument: ${arg}`); } - launchCwd = parseExplicitPath(arg, "[path]", cwd); + launchCwd = parseExplicitPath(arg, "[path]", parserCwd); usedPositionalCwd = true; continue; } @@ -354,22 +355,60 @@ function contentTypeFor(filePath: string): string { return "application/octet-stream"; } +function isPathInside(parent: string, child: string): boolean { + const relative = path.relative(parent, child); + return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative)); +} + +export function resolveStaticAssetPath( + requestUrl: string | undefined, + distRoot: string, +): { kind: "file"; filePath: string } | { kind: "forbidden" } | { kind: "bad_request" } { + const pathWithoutQuery = requestUrl ? requestUrl.split("?")[0] : "/"; + const rawPath = pathWithoutQuery || "/"; + let decodedPath: string; + try { + decodedPath = decodeURIComponent(rawPath); + } catch { + return { kind: "bad_request" }; + } + + const normalizedPath = + decodedPath === "/" ? "index.html" : decodedPath.replace(/^\/+/, ""); + const candidateFilePath = path.resolve(distRoot, normalizedPath); + if (!isPathInside(distRoot, candidateFilePath)) { + return { kind: "forbidden" }; + } + + return { kind: "file", filePath: candidateFilePath }; +} + +function isExistingFile(filePath: string): boolean { + try { + return fs.statSync(filePath).isFile(); + } catch { + return false; + } +} + function startStaticWebServer(distRoot: string, port: number) { const server = createServer((request, response) => { - const requestPath = request.url ? request.url.split("?")[0] : "/"; - const normalized = - requestPath === "/" ? "index.html" : (requestPath ?? "/").replace(/^\/+/, ""); - const safePath = path.normalize(normalized).replace(/^(\.\.[/\\])+/, ""); - const filePath = path.join(distRoot, safePath); + const resolvedPath = resolveStaticAssetPath(request.url, distRoot); + if (resolvedPath.kind === "bad_request") { + response.statusCode = 400; + response.end("Invalid request path"); + return; + } - if (!filePath.startsWith(distRoot)) { + if (resolvedPath.kind === "forbidden") { response.statusCode = 403; response.end("Forbidden"); return; } - const exists = fs.existsSync(filePath); - const targetPath = exists ? filePath : path.join(distRoot, "index.html"); + const targetPath = isExistingFile(resolvedPath.filePath) + ? resolvedPath.filePath + : path.join(distRoot, "index.html"); fs.readFile(targetPath, (error, content) => { if (error) { response.statusCode = 404;