Added security policies to all endpoints and implemented file upload feature on posts

This commit is contained in:
Sven laptop
2026-07-24 23:25:31 +02:00
parent 9519a01ca0
commit 9e56f00c1e
19 changed files with 718 additions and 134 deletions
+27
View File
@@ -0,0 +1,27 @@
const fs = require('fs');
function strictInput({ body = [], query = [], cleanupUploadedFile = false } = {}) {
const allowedBody = new Set(body);
const allowedQuery = new Set(query);
return function validateInputShape(req, res, next) {
const bodyKeys = Object.keys(req.body || {});
const queryKeys = Object.keys(req.query || {});
const unexpectedBody = bodyKeys.filter((key) => !allowedBody.has(key));
const unexpectedQuery = queryKeys.filter((key) => !allowedQuery.has(key));
if (unexpectedBody.length > 0 || unexpectedQuery.length > 0) {
if (cleanupUploadedFile && req.file?.path) {
fs.promises.unlink(req.file.path).catch(() => {});
}
return res.status(400).json({
error: 'Unexpected request field(s)',
fields: [...unexpectedBody, ...unexpectedQuery],
});
}
next();
};
}
module.exports = { strictInput };