Skip to content

Commit c5c4bf5

Browse files
committed
added some middleware to reject bad things in the body
1 parent 588cc8d commit c5c4bf5

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/controller/org.controller/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,9 +1423,11 @@ router.post('/org/:shortname/user',
14231423
.bail()
14241424
.customSanitizer(toUpperCaseArray)
14251425
.custom(isUserRole),
1426+
mw.rejectUnexpectedKeys(['username', 'org_uuid', 'uuid', 'name', 'authority']),
14261427
parseError,
14271428
parsePostParams,
14281429
controller.USER_CREATE_SINGLE)
1430+
14291431
router.get('/org/:shortname/user/:username',
14301432
/*
14311433
#swagger.tags = ['Users']

src/middleware/middleware.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,27 @@ function containsNoInvalidCharacters (val) {
547547
return true
548548
}
549549

550+
/**
551+
* Middleware factory that rejects any keys in the request body
552+
* that are not listed in the allowedKeys array.
553+
*
554+
* @param {Array<string>} allowedKeys - List of permitted keys in req.body
555+
* @returns {function} Express middleware
556+
*/
557+
function rejectUnexpectedKeys (allowedKeys) {
558+
return (req, res, next) => {
559+
const bodyKeys = Object.keys(req.body || {})
560+
const unexpected = bodyKeys.filter(k => !allowedKeys.includes(k))
561+
if (unexpected.length > 0) {
562+
return res.status(400).json({
563+
error: 'Unexpected keys in request body',
564+
unexpected
565+
})
566+
}
567+
next()
568+
}
569+
}
570+
550571
module.exports = {
551572
setCacheControl,
552573
optionallyValidateUser,
@@ -572,5 +593,6 @@ module.exports = {
572593
toUpperCaseArray,
573594
toLowerCaseArray,
574595
containsNoInvalidCharacters,
575-
trimJSONWhitespace
596+
trimJSONWhitespace,
597+
rejectUnexpectedKeys
576598
}

0 commit comments

Comments
 (0)