X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fcustom-validators%2Fmisc.ts;h=3ef38fce1d0b135625647e24dfbbd3671d86595a;hb=7cd4d2ba10106c10602c86f74f55743ded588896;hp=76647fea2be8264c424d887dd6252afc2ebda3ba;hpb=88108880bbdba473cfe36ecbebc1c3c4f972e102;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/custom-validators/misc.ts b/server/helpers/custom-validators/misc.ts index 76647fea2..3ef38fce1 100644 --- a/server/helpers/custom-validators/misc.ts +++ b/server/helpers/custom-validators/misc.ts @@ -1,10 +1,18 @@ import 'multer' import * as validator from 'validator' +import { sep } from 'path' function exists (value: any) { return value !== undefined && value !== null } +function isSafePath (p: string) { + return exists(p) && + (p + '').split(sep).every(part => { + return [ '..' ].includes(part) === false + }) +} + function isArray (value: any) { return Array.isArray(value) } @@ -49,12 +57,19 @@ function toValueOrNull (value: string) { return value } -function toArray (value: string) { +function toArray (value: any) { if (value && isArray(value) === false) return [ value ] return value } +function toIntArray (value: any) { + if (!value) return [] + if (isArray(value) === false) return [ validator.toInt(value) ] + + return value.map(v => validator.toInt(v)) +} + function isFileValid ( files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], mimeTypeRegex: string, @@ -90,6 +105,7 @@ export { isNotEmptyIntArray, isArray, isIdValid, + isSafePath, isUUIDValid, isIdOrUUIDValid, isDateValid, @@ -97,5 +113,6 @@ export { isBooleanValid, toIntOrNull, toArray, + toIntArray, isFileValid }