Skip to content

Instantly share code, notes, and snippets.

@samuelkarani
Last active December 20, 2022 23:35
Show Gist options
  • Save samuelkarani/92fe78389e9cf0b46a1334097fa55366 to your computer and use it in GitHub Desktop.
Save samuelkarani/92fe78389e9cf0b46a1334097fa55366 to your computer and use it in GitHub Desktop.
const formidable = require("formidable");
const path = require("path");
const smallest = 100;
const largest = 10 * 1000 * 1000;
const bannedExtensions = [".js", ".html", ".txt", ".exe", ".rtf", ".vbs", ".zip"];
const bannedMimeTypes = [
"text/plain",
"text/html",
"text/css",
"text/javascript",
"text/markdown",
"application/x-javascript",
"application/javascript",
"application/octet-stream",
];
app.post("/upload", (req, res) => {
const form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
if (err) {
res.status(500).end();
} else {
const { mimetype, size, originalFilename } = files.upload;
const ext = path.extname(originalFilename);
if (
bannedMimeTypes.includes(mimetype) ||
bannedExtensions.includes(ext) ||
size < smallest ||
size > largest
) {
res.status(400).end();
} else {
res.status(200).end();
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment