Handle absolute-form static request targets safely

Co-authored-by: Theo Browne <[email protected]>
This commit is contained in:
Cursor AgentandTheo Browne committed 2026-02-10 02:17:34 +00:00
1 parent 1345ec325a
commit 2d7754c19f
2 files changed
+33 -1

No files matched your search

+15
View File
@@ -695,6 +695,21 @@ describe("resolveStaticAssetPath", () => {
});
});
it("supports absolute-form request targets by using URL pathname", () => {
const result = resolveStaticAssetPath("http://127.0.0.1/assets/main.js?x=1", distRoot);
expect(result).toEqual({
kind: "file",
filePath: path.join(distRoot, "assets", "main.js"),
});
});
it("rejects malformed absolute-form request targets", () => {
const result = resolveStaticAssetPath("http://%zz", distRoot);
expect(result).toEqual({
kind: "bad_request",
});
});
it("rejects traversal attempts with decoded dot-dot segments", () => {
const result = resolveStaticAssetPath("/../package.json", distRoot);
expect(result).toEqual({
+18 -1
View File
@@ -515,7 +515,24 @@ export function resolveStaticAssetPath(
requestUrl: string | undefined,
distRoot: string,
): { kind: "file"; filePath: string } | { kind: "forbidden" } | { kind: "bad_request" } {
const rawPath = requestUrl ? (requestUrl.split(/[?#]/, 1)[0] ?? "/") : "/";
const rawPath = (() => {
if (!requestUrl) {
return "/";
}
if (requestUrl.startsWith("http://") || requestUrl.startsWith("https://")) {
try {
return new URL(requestUrl).pathname;
} catch {
return null;
}
}
return requestUrl.split(/[?#]/, 1)[0] ?? "/";
})();
if (!rawPath) {
return { kind: "bad_request" };
}
let decodedPath: string;
try {
decodedPath = decodeURIComponent(rawPath);