diff options
author | Chocobozzz <me@florianbigard.com> | 2021-05-27 16:12:41 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-05-27 16:12:41 +0200 |
commit | 8f608a4cb22ab232cfab20665050764b38bac9c7 (patch) | |
tree | 6a6785aae79bf5939ad7b7a50a1bd8031268d2b4 /server/helpers/custom-validators | |
parent | 030ccfce59a8cb8f2fee6ea8dd363ba635c5c5c2 (diff) | |
parent | c215e627b575d2c4085ccb222f4ca8d0237b7552 (diff) | |
download | PeerTube-8f608a4cb22ab232cfab20665050764b38bac9c7.tar.gz PeerTube-8f608a4cb22ab232cfab20665050764b38bac9c7.tar.zst PeerTube-8f608a4cb22ab232cfab20665050764b38bac9c7.zip |
Merge branch 'develop' into shorter-URLs-channels-accounts
Diffstat (limited to 'server/helpers/custom-validators')
-rw-r--r-- | server/helpers/custom-validators/activitypub/actor.ts | 39 | ||||
-rw-r--r-- | server/helpers/custom-validators/misc.ts | 7 | ||||
-rw-r--r-- | server/helpers/custom-validators/videos.ts | 9 |
3 files changed, 27 insertions, 28 deletions
diff --git a/server/helpers/custom-validators/activitypub/actor.ts b/server/helpers/custom-validators/activitypub/actor.ts index 877345157..675a7b663 100644 --- a/server/helpers/custom-validators/activitypub/actor.ts +++ b/server/helpers/custom-validators/activitypub/actor.ts | |||
@@ -1,6 +1,6 @@ | |||
1 | import validator from 'validator' | 1 | import validator from 'validator' |
2 | import { CONSTRAINTS_FIELDS } from '../../../initializers/constants' | 2 | import { CONSTRAINTS_FIELDS } from '../../../initializers/constants' |
3 | import { exists, isArray } from '../misc' | 3 | import { exists, isArray, isDateValid } from '../misc' |
4 | import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc' | 4 | import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc' |
5 | import { isHostValid } from '../servers' | 5 | import { isHostValid } from '../servers' |
6 | import { peertubeTruncate } from '@server/helpers/core-utils' | 6 | import { peertubeTruncate } from '@server/helpers/core-utils' |
@@ -47,7 +47,21 @@ function isActorPrivateKeyValid (privateKey: string) { | |||
47 | validator.isLength(privateKey, CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY) | 47 | validator.isLength(privateKey, CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY) |
48 | } | 48 | } |
49 | 49 | ||
50 | function isActorObjectValid (actor: any) { | 50 | function isActorFollowingCountValid (value: string) { |
51 | return exists(value) && validator.isInt('' + value, { min: 0 }) | ||
52 | } | ||
53 | |||
54 | function isActorFollowersCountValid (value: string) { | ||
55 | return exists(value) && validator.isInt('' + value, { min: 0 }) | ||
56 | } | ||
57 | |||
58 | function isActorDeleteActivityValid (activity: any) { | ||
59 | return isBaseActivityValid(activity, 'Delete') | ||
60 | } | ||
61 | |||
62 | function sanitizeAndCheckActorObject (actor: any) { | ||
63 | normalizeActor(actor) | ||
64 | |||
51 | return exists(actor) && | 65 | return exists(actor) && |
52 | isActivityPubUrlValid(actor.id) && | 66 | isActivityPubUrlValid(actor.id) && |
53 | isActorTypeValid(actor.type) && | 67 | isActorTypeValid(actor.type) && |
@@ -68,24 +82,6 @@ function isActorObjectValid (actor: any) { | |||
68 | (actor.type !== 'Group' || actor.attributedTo.length !== 0) | 82 | (actor.type !== 'Group' || actor.attributedTo.length !== 0) |
69 | } | 83 | } |
70 | 84 | ||
71 | function isActorFollowingCountValid (value: string) { | ||
72 | return exists(value) && validator.isInt('' + value, { min: 0 }) | ||
73 | } | ||
74 | |||
75 | function isActorFollowersCountValid (value: string) { | ||
76 | return exists(value) && validator.isInt('' + value, { min: 0 }) | ||
77 | } | ||
78 | |||
79 | function isActorDeleteActivityValid (activity: any) { | ||
80 | return isBaseActivityValid(activity, 'Delete') | ||
81 | } | ||
82 | |||
83 | function sanitizeAndCheckActorObject (object: any) { | ||
84 | normalizeActor(object) | ||
85 | |||
86 | return isActorObjectValid(object) | ||
87 | } | ||
88 | |||
89 | function normalizeActor (actor: any) { | 85 | function normalizeActor (actor: any) { |
90 | if (!actor) return | 86 | if (!actor) return |
91 | 87 | ||
@@ -95,6 +91,8 @@ function normalizeActor (actor: any) { | |||
95 | actor.url = actor.url.href || actor.url.url | 91 | actor.url = actor.url.href || actor.url.url |
96 | } | 92 | } |
97 | 93 | ||
94 | if (!isDateValid(actor.published)) actor.published = undefined | ||
95 | |||
98 | if (actor.summary && typeof actor.summary === 'string') { | 96 | if (actor.summary && typeof actor.summary === 'string') { |
99 | actor.summary = peertubeTruncate(actor.summary, { length: CONSTRAINTS_FIELDS.USERS.DESCRIPTION.max }) | 97 | actor.summary = peertubeTruncate(actor.summary, { length: CONSTRAINTS_FIELDS.USERS.DESCRIPTION.max }) |
100 | 98 | ||
@@ -135,7 +133,6 @@ export { | |||
135 | isActorPublicKeyValid, | 133 | isActorPublicKeyValid, |
136 | isActorPreferredUsernameValid, | 134 | isActorPreferredUsernameValid, |
137 | isActorPrivateKeyValid, | 135 | isActorPrivateKeyValid, |
138 | isActorObjectValid, | ||
139 | isActorFollowingCountValid, | 136 | isActorFollowingCountValid, |
140 | isActorFollowersCountValid, | 137 | isActorFollowersCountValid, |
141 | isActorDeleteActivityValid, | 138 | isActorDeleteActivityValid, |
diff --git a/server/helpers/custom-validators/misc.ts b/server/helpers/custom-validators/misc.ts index effdd98cb..229e9f03c 100644 --- a/server/helpers/custom-validators/misc.ts +++ b/server/helpers/custom-validators/misc.ts | |||
@@ -1,6 +1,7 @@ | |||
1 | import 'multer' | 1 | import 'multer' |
2 | import validator from 'validator' | 2 | import { UploadFilesForCheck } from 'express' |
3 | import { sep } from 'path' | 3 | import { sep } from 'path' |
4 | import validator from 'validator' | ||
4 | 5 | ||
5 | function exists (value: any) { | 6 | function exists (value: any) { |
6 | return value !== undefined && value !== null | 7 | return value !== undefined && value !== null |
@@ -13,7 +14,7 @@ function isSafePath (p: string) { | |||
13 | }) | 14 | }) |
14 | } | 15 | } |
15 | 16 | ||
16 | function isArray (value: any) { | 17 | function isArray (value: any): value is any[] { |
17 | return Array.isArray(value) | 18 | return Array.isArray(value) |
18 | } | 19 | } |
19 | 20 | ||
@@ -108,7 +109,7 @@ function isFileFieldValid ( | |||
108 | } | 109 | } |
109 | 110 | ||
110 | function isFileMimeTypeValid ( | 111 | function isFileMimeTypeValid ( |
111 | files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[], | 112 | files: UploadFilesForCheck, |
112 | mimeTypeRegex: string, | 113 | mimeTypeRegex: string, |
113 | field: string, | 114 | field: string, |
114 | optional = false | 115 | optional = false |
diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts index 87966798f..b33e088eb 100644 --- a/server/helpers/custom-validators/videos.ts +++ b/server/helpers/custom-validators/videos.ts | |||
@@ -1,4 +1,6 @@ | |||
1 | import { UploadFilesForCheck } from 'express' | ||
1 | import { values } from 'lodash' | 2 | import { values } from 'lodash' |
3 | import * as magnetUtil from 'magnet-uri' | ||
2 | import validator from 'validator' | 4 | import validator from 'validator' |
3 | import { VideoFilter, VideoPrivacy, VideoRateType } from '../../../shared' | 5 | import { VideoFilter, VideoPrivacy, VideoRateType } from '../../../shared' |
4 | import { | 6 | import { |
@@ -6,13 +8,12 @@ import { | |||
6 | MIMETYPES, | 8 | MIMETYPES, |
7 | VIDEO_CATEGORIES, | 9 | VIDEO_CATEGORIES, |
8 | VIDEO_LICENCES, | 10 | VIDEO_LICENCES, |
11 | VIDEO_LIVE, | ||
9 | VIDEO_PRIVACIES, | 12 | VIDEO_PRIVACIES, |
10 | VIDEO_RATE_TYPES, | 13 | VIDEO_RATE_TYPES, |
11 | VIDEO_STATES, | 14 | VIDEO_STATES |
12 | VIDEO_LIVE | ||
13 | } from '../../initializers/constants' | 15 | } from '../../initializers/constants' |
14 | import { exists, isArray, isDateValid, isFileMimeTypeValid, isFileValid } from './misc' | 16 | import { exists, isArray, isDateValid, isFileMimeTypeValid, isFileValid } from './misc' |
15 | import * as magnetUtil from 'magnet-uri' | ||
16 | 17 | ||
17 | const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS | 18 | const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS |
18 | 19 | ||
@@ -81,7 +82,7 @@ function isVideoFileExtnameValid (value: string) { | |||
81 | return exists(value) && (value === VIDEO_LIVE.EXTENSION || MIMETYPES.VIDEO.EXT_MIMETYPE[value] !== undefined) | 82 | return exists(value) && (value === VIDEO_LIVE.EXTENSION || MIMETYPES.VIDEO.EXT_MIMETYPE[value] !== undefined) |
82 | } | 83 | } |
83 | 84 | ||
84 | function isVideoFileMimeTypeValid (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) { | 85 | function isVideoFileMimeTypeValid (files: UploadFilesForCheck) { |
85 | return isFileMimeTypeValid(files, MIMETYPES.VIDEO.MIMETYPES_REGEX, 'videofile') | 86 | return isFileMimeTypeValid(files, MIMETYPES.VIDEO.MIMETYPES_REGEX, 'videofile') |
86 | } | 87 | } |
87 | 88 | ||