Skip to content

Commit 22fed9d

Browse files
fix: prevent path traversal vulnerability in dashboard server
Amp-Thread-ID: https://ampcode.com/threads/T-019c0918-5b9d-753a-ac3e-317b748ce4e7 Co-authored-by: Amp <amp@ampcode.com>
1 parent 86e1691 commit 22fed9d

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

scripts/dashboard-web/server.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,20 @@ const httpServer = http.createServer((req, res) => {
364364
}
365365

366366
// Serve static files
367-
let filePath = req.url === "/" ? "/index.html" : req.url;
368-
filePath = path.join(PUBLIC_DIR, filePath);
367+
let requestedPath = req.url === "/" ? "/index.html" : req.url;
368+
369+
// Sanitize: remove query strings and decode
370+
requestedPath = decodeURIComponent(requestedPath.split("?")[0]);
371+
372+
// Resolve to absolute path and verify it's within PUBLIC_DIR (prevent path traversal)
373+
const filePath = path.resolve(PUBLIC_DIR, "." + requestedPath);
374+
const normalizedPublicDir = path.resolve(PUBLIC_DIR);
375+
376+
if (!filePath.startsWith(normalizedPublicDir + path.sep) && filePath !== normalizedPublicDir) {
377+
res.writeHead(403);
378+
res.end("Forbidden");
379+
return;
380+
}
369381

370382
const ext = path.extname(filePath);
371383
const contentType = MIME_TYPES[ext] || "application/octet-stream";

0 commit comments

Comments
 (0)