Enhance file name validation by allowing objects with toString method

4efed978f048cced3217f18523abdcc9f6e07743

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +10 -1Showing whitespace changes
src/middleware/validateFileName.js+10 -1
@@ -1,6 +1,15 @@
11import path from 'node:path';
22
33/**
4+ * Checks if an object has a toString method.
5+ * @param {object} o Object to check
6+ * @returns {boolean} True if the object has a toString method, false otherwise
7+ */
8+function hasToString(o) {
9+ return o != null && typeof o.toString === 'function';
10+}
11+
12+/**
413 * Gets a middleware function that validates the field in the request body.
514 * @param {string} fieldName Field name
615 * @returns {import('express').RequestHandler} Middleware function
@@ -13,7 +22,7 @@ export function getFileNameValidationFunction(fieldName) {
1322 * @param {import('express').NextFunction} next Next middleware
1423 */
1524 return function validateAvatarUrlMiddleware(req, res, next) {
1625 if (req.body && fieldName in req.body && (typeof req.body[fieldName] === 'string' || hasToString(req.body[fieldName]))) {
1726 const forbiddenRegExp = path.sep === '/' ? /[/\x00]/ : /[/\x00\\]/;
1827 if (forbiddenRegExp.test(req.body[fieldName])) {
1928 console.error('An error occurred while validating the request body', {