Skip to content

Commit cb58631

Browse files
authored
Merge commit from fork
fix: path traversal vuln
2 parents 4b7dc39 + 2acfd21 commit cb58631

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

server.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ const uploads = new Map();
234234
// Routes
235235
app.post('/upload/init', async (req, res) => {
236236
const { filename, fileSize } = req.body;
237+
238+
const safeFilename = path.normalize(filename).replace(/^(\.\.(\/|\\|$))+/, '')
237239

238240
// Check file size limit
239241
if (fileSize > maxFileSize) {
@@ -246,20 +248,20 @@ app.post('/upload/init', async (req, res) => {
246248
}
247249

248250
const uploadId = Date.now().toString();
249-
const filePath = path.join(uploadDir, filename);
251+
const filePath = path.join(uploadDir, safeFilename);
250252

251253
try {
252254
await ensureDirectoryExists(filePath);
253255

254256
uploads.set(uploadId, {
255-
filename,
257+
safeFilename,
256258
filePath,
257259
fileSize,
258260
bytesReceived: 0,
259261
writeStream: fs.createWriteStream(filePath)
260262
});
261263

262-
log.info(`Initialized upload for ${filename} (${fileSize} bytes)`);
264+
log.info(`Initialized upload for ${safeFilename} (${fileSize} bytes)`);
263265
res.json({ uploadId });
264266
} catch (err) {
265267
log.error(`Failed to initialize upload: ${err.message}`);

0 commit comments

Comments
 (0)