From 5e4c1b197faaa3c8e7877607eeb5918b90711455 Mon Sep 17 00:00:00 2001 From: Thomas Lynch Date: Tue, 18 May 2021 23:15:15 +0000 Subject: [PATCH] fixes to paramconverter, null 0 length trimmed fields, tweak custompage error --- controllers/forms/addcustompage.js | 2 +- helpers/paramconverter.js | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/controllers/forms/addcustompage.js b/controllers/forms/addcustompage.js index a534f233..25afc5bd 100644 --- a/controllers/forms/addcustompage.js +++ b/controllers/forms/addcustompage.js @@ -29,7 +29,7 @@ module.exports = { } return false; } , expected: true, error: '.html name must contain a-z 0-9 _ - only' }, - { result: numberBody(res.locals.messageLength, 0, globalLimits.customPages.maxLength), expected: true, error: `Message must be ${globalLimits.customPages.maxLength} characters or less` }, + { result: !existsBody(req.body.message) || numberBody(res.locals.messageLength, 0, globalLimits.customPages.maxLength), expected: true, error: `Message must be ${globalLimits.customPages.maxLength} characters or less` }, { result: lengthBody(req.body.title, 0, 50), expected: false, error: 'Title must be 50 characters or less' }, { result: lengthBody(req.body.page, 0, 50), expected: false, error: '.html name must be 50 characters or less' }, { result: async () => { diff --git a/helpers/paramconverter.js b/helpers/paramconverter.js index 093c4663..7b102663 100644 --- a/helpers/paramconverter.js +++ b/helpers/paramconverter.js @@ -29,6 +29,7 @@ module.exports = (options) => { const { timeFields, trimFields, allowedArrays, processThreadIdParam, processDateParam, processMessageLength, numberFields, numberArrays, objectIdFields, objectIdArrays } = options; + /* check all body fields, body-parser prevents this array being too big, so no worry. whitelist for fields that can be arrays, and convert singular of those fields to 1 length array */ const bodyFields = Object.keys(req.body); @@ -50,7 +51,7 @@ module.exports = (options) => { const field = trimFields[i]; if (req.body[field]) { //trimEnd() because trailing whitespace doesnt affect how a post appear and if it is all whitespace, trimEnd will get it all anyway - req.body[field] = req.body[field].trimEnd(); + req.body[field] = req.body[field].trimEnd() || null; } } @@ -143,7 +144,7 @@ module.exports = (options) => { } /* normalise message length check for CRLF vs just LF, because String.length depending on browser wont count CRLF as - 2 characters, so user gets "message too long" at the right length. */ + 2 characters, so user gets "message too long" at the right length. Maybe will add another array for these in future */ if (processMessageLength && req.body.message) { res.locals.messageLength = req.body.message.replace(/\r\n/igm, '\n').length; }