This repository has been archived on 2026-06-29. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
spellsaurus/api/functions.js
2020-12-28 12:25:29 +01:00

35 lines
810 B
JavaScript

const regexInt = RegExp(/^[1-9]\d*$/)
const regexXSS = RegExp(/<[^>]*script/)
// Check if int for param validation
const paramIntCheck = (req, res, next, input) => {
try {
if (regexInt.test(input)) {
next()
} else {
throw new Error
}
} catch (err) {
res.status(err.code).send(JSON.stringify({
"message": "Le paramètre doit être un entier non-nul.",
"code": 403,
})
)
}
}
// Check if script injection attempt
const isXSSAttempt = (string) => {
return regexXSS.test(string);
}
// Check if object is null
const isEmptyObject = (obj) => {
return (Object.keys(obj).length === 0 && obj.constructor === Object);
}
module.exports = {
paramIntCheck,
isXSSAttempt,
isEmptyObject
}