|
import path from 'node:path'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getFileNameValidationFunction(fieldName) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
return function validateAvatarUrlMiddleware(req, res, next) { |
|
if (req.body && fieldName in req.body && typeof req.body[fieldName] === 'string') { |
|
const forbiddenRegExp = path.sep === '/' ? /[/\x00]/ : /[/\x00\\]/; |
|
if (forbiddenRegExp.test(req.body[fieldName])) { |
|
console.error('An error occurred while validating the request body', { |
|
handle: req.user.profile.handle, |
|
path: req.originalUrl, |
|
field: fieldName, |
|
value: req.body[fieldName], |
|
}); |
|
return res.sendStatus(400); |
|
} |
|
} |
|
|
|
next(); |
|
}; |
|
} |
|
|
|
const avatarUrlValidationFunction = getFileNameValidationFunction('avatar_url'); |
|
export default avatarUrlValidationFunction; |
|
|