θΏ™ζ˜―indexlocζδΎ›ηš„ζœεŠ‘οΌŒδΈθ¦θΎ“ε…₯任何密码
Skip to content

Normalize paths on files uploaded to prevent arbitrary file writes #2905

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions server/utils/files/multer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const multer = require("multer");
const path = require("path");
const fs = require("fs");
const { v4 } = require("uuid");
const { normalizePath } = require(".");

/**
* Handle File uploads for auto-uploading.
Expand All @@ -16,8 +17,8 @@ const fileUploadStorage = multer.diskStorage({
cb(null, uploadOutput);
},
filename: function (_, file, cb) {
file.originalname = Buffer.from(file.originalname, "latin1").toString(
"utf8"
file.originalname = normalizePath(
Buffer.from(file.originalname, "latin1").toString("utf8")
);
cb(null, file.originalname);
},
Expand All @@ -36,6 +37,7 @@ const fileAPIUploadStorage = multer.diskStorage({
cb(null, uploadOutput);
},
filename: function (_, file, cb) {
file.originalname = normalizePath(file.originalname);
cb(null, file.originalname);
},
});
Expand All @@ -51,8 +53,8 @@ const assetUploadStorage = multer.diskStorage({
return cb(null, uploadOutput);
},
filename: function (_, file, cb) {
file.originalname = Buffer.from(file.originalname, "latin1").toString(
"utf8"
file.originalname = normalizePath(
Buffer.from(file.originalname, "latin1").toString("utf8")
);
cb(null, file.originalname);
},
Expand All @@ -71,7 +73,9 @@ const pfpUploadStorage = multer.diskStorage({
return cb(null, uploadOutput);
},
filename: function (req, file, cb) {
const randomFileName = `${v4()}${path.extname(file.originalname)}`;
const randomFileName = `${v4()}${path.extname(
normalizePath(file.originalname)
)}`;
req.randomFileName = randomFileName;
cb(null, randomFileName);
},
Expand Down