]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/misc.ts
Don't inject untrusted input
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / misc.ts
CommitLineData
ac81d1a0 1import 'multer'
f6d6e7f8 2import { UploadFilesForCheck } from 'express'
345da516 3import { sep } from 'path'
f6d6e7f8 4import validator from 'validator'
0628157f 5import { isShortUUID, shortToUUID } from '@shared/extra-utils'
fdbda9e3 6
69818c93 7function exists (value: any) {
e4c55619
C
8 return value !== undefined && value !== null
9}
10
345da516
C
11function isSafePath (p: string) {
12 return exists(p) &&
13 (p + '').split(sep).every(part => {
f023a19c 14 return [ '..' ].includes(part) === false
345da516
C
15 })
16}
17
c158a5fa 18function isArray (value: any): value is any[] {
e4c55619
C
19 return Array.isArray(value)
20}
21
2f1548fd
C
22function isNotEmptyIntArray (value: any) {
23 return Array.isArray(value) && value.every(v => validator.isInt('' + v)) && value.length !== 0
cef534ed
C
24}
25
b033851f
C
26function isNotEmptyStringArray (value: any) {
27 return Array.isArray(value) && value.every(v => typeof v === 'string' && v.length !== 0) && value.length !== 0
28}
29
09209296
C
30function isArrayOf (value: any, validator: (value: any) => boolean) {
31 return isArray(value) && value.every(v => validator(v))
32}
33
72c7248b
C
34function isDateValid (value: string) {
35 return exists(value) && validator.isISO8601(value)
36}
37
38function isIdValid (value: string) {
39 return exists(value) && validator.isInt('' + value)
40}
41
42function isUUIDValid (value: string) {
43 return exists(value) && validator.isUUID('' + value, 4)
44}
45
fbd67e7f
C
46function areUUIDsValid (values: string[]) {
47 return isArray(values) && values.every(v => isUUIDValid(v))
48}
49
72c7248b
C
50function isIdOrUUIDValid (value: string) {
51 return isIdValid(value) || isUUIDValid(value)
52}
53
360329cc 54function isBooleanValid (value: any) {
47564bbe
C
55 return typeof value === 'boolean' || (typeof value === 'string' && validator.isBoolean(value))
56}
57
fb719404
C
58function isIntOrNull (value: any) {
59 return value === null || validator.isInt('' + value)
60}
61
d4a8e7a6 62// ---------------------------------------------------------------------------
f0a39880 63
c729caf6
C
64function isFileValid (options: {
65 files: UploadFilesForCheck
f2eb23cd 66
c729caf6
C
67 maxSize: number | null
68 mimeTypeRegex: string | null
f2eb23cd 69
c729caf6 70 field?: string
f2eb23cd 71
c729caf6
C
72 optional?: boolean // Default false
73}) {
74 const { files, mimeTypeRegex, field, maxSize, optional = false } = options
f2eb23cd 75
ac81d1a0
C
76 // Should have files
77 if (!files) return optional
ac81d1a0 78
c729caf6
C
79 const fileArray = isArray(files)
80 ? files
81 : files[field]
82
83 if (!fileArray || !isArray(fileArray) || fileArray.length === 0) {
ac81d1a0
C
84 return optional
85 }
86
c729caf6 87 // The file exists
a1587156 88 const file = fileArray[0]
ac81d1a0
C
89 if (!file || !file.originalname) return false
90
0c237b19 91 // Check size
c1e791ba 92 if ((maxSize !== null) && file.size > maxSize) return false
0c237b19 93
c729caf6
C
94 if (mimeTypeRegex === null) return true
95
96 return checkMimetypeRegex(file.mimetype, mimeTypeRegex)
97}
98
99function checkMimetypeRegex (fileMimeType: string, mimeTypeRegex: string) {
100 return new RegExp(`^${mimeTypeRegex}$`, 'i').test(fileMimeType)
ac81d1a0
C
101}
102
e4c55619
C
103// ---------------------------------------------------------------------------
104
d4a8e7a6
C
105function toCompleteUUID (value: string) {
106 if (isShortUUID(value)) return shortToUUID(value)
107
108 return value
109}
110
fbd67e7f
C
111function toCompleteUUIDs (values: string[]) {
112 return values.map(v => toCompleteUUID(v))
113}
114
d4a8e7a6
C
115function toIntOrNull (value: string) {
116 const v = toValueOrNull(value)
117
118 if (v === null || v === undefined) return v
119 if (typeof v === 'number') return v
120
121 return validator.toInt('' + v)
122}
123
124function toBooleanOrNull (value: any) {
125 const v = toValueOrNull(value)
126
127 if (v === null || v === undefined) return v
128 if (typeof v === 'boolean') return v
129
130 return validator.toBoolean('' + v)
131}
132
133function toValueOrNull (value: string) {
134 if (value === 'null') return null
135
136 return value
137}
138
d4a8e7a6
C
139function toIntArray (value: any) {
140 if (!value) return []
141 if (isArray(value) === false) return [ validator.toInt(value) ]
142
143 return value.map(v => validator.toInt(v))
144}
145
146// ---------------------------------------------------------------------------
147
65fcc311
C
148export {
149 exists,
09209296 150 isArrayOf,
2f1548fd 151 isNotEmptyIntArray,
72c7248b 152 isArray,
fb719404 153 isIntOrNull,
72c7248b 154 isIdValid,
345da516 155 isSafePath,
b033851f 156 isNotEmptyStringArray,
72c7248b 157 isUUIDValid,
fbd67e7f 158 toCompleteUUIDs,
d4a8e7a6 159 toCompleteUUID,
72c7248b 160 isIdOrUUIDValid,
47564bbe 161 isDateValid,
2efd32f6 162 toValueOrNull,
c8861d5d 163 toBooleanOrNull,
ac81d1a0 164 isBooleanValid,
360329cc 165 toIntOrNull,
fbd67e7f 166 areUUIDsValid,
f0a39880 167 toIntArray,
c729caf6
C
168 isFileValid,
169 checkMimetypeRegex
65fcc311 170}