From c8861d5dc0436ef4342ce517241e3591fa256a13 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 25 Jul 2019 16:23:44 +0200 Subject: [PATCH] Fix express validator --- .../video/infinite-scroller.directive.ts | 2 +- .../video/video-miniature.component.html | 2 +- .../+video-watch/video-watch.component.ts | 4 +-- server/helpers/custom-validators/accounts.ts | 1 - server/helpers/custom-validators/misc.ts | 18 +++++++++++-- server/helpers/custom-validators/search.ts | 2 -- server/helpers/custom-validators/servers.ts | 4 +-- server/helpers/custom-validators/users.ts | 1 - .../custom-validators/video-channels.ts | 2 -- .../custom-validators/video-comments.ts | 1 - .../custom-validators/video-imports.ts | 1 - server/helpers/custom-validators/videos.ts | 7 +---- server/middlewares/oauth.ts | 1 - server/middlewares/pagination.ts | 2 -- server/middlewares/servers.ts | 1 - server/middlewares/sort.ts | 1 - server/middlewares/user-right.ts | 1 - server/middlewares/validators/account.ts | 2 +- .../validators/activitypub/signature.ts | 2 +- server/middlewares/validators/avatar.ts | 2 +- server/middlewares/validators/blocklist.ts | 2 +- server/middlewares/validators/config.ts | 2 +- server/middlewares/validators/feeds.ts | 2 +- server/middlewares/validators/follows.ts | 2 +- server/middlewares/validators/jobs.ts | 2 +- server/middlewares/validators/logs.ts | 2 +- server/middlewares/validators/oembed.ts | 2 +- server/middlewares/validators/pagination.ts | 2 +- server/middlewares/validators/plugins.ts | 6 ++--- server/middlewares/validators/redundancy.ts | 7 +++-- server/middlewares/validators/search.ts | 2 +- server/middlewares/validators/server.ts | 2 +- server/middlewares/validators/themes.ts | 2 +- server/middlewares/validators/user-history.ts | 3 +-- .../validators/user-notifications.ts | 7 +++-- .../validators/user-subscriptions.ts | 3 +-- server/middlewares/validators/users.ts | 7 +++-- server/middlewares/validators/utils.ts | 2 +- .../validators/videos/video-abuses.ts | 3 +-- .../validators/videos/video-blacklist.ts | 6 ++--- .../validators/videos/video-captions.ts | 2 +- .../validators/videos/video-channels.ts | 2 +- .../validators/videos/video-comments.ts | 2 +- .../validators/videos/video-imports.ts | 6 ++--- .../validators/videos/video-playlists.ts | 17 ++++++++---- .../validators/videos/video-rates.ts | 3 +-- .../validators/videos/video-shares.ts | 3 +-- .../validators/videos/video-watch.ts | 6 ++--- .../middlewares/validators/videos/videos.ts | 26 +++++++++---------- server/middlewares/validators/webfinger.ts | 2 +- .../tests/api/check-params/video-imports.ts | 2 +- 51 files changed, 93 insertions(+), 101 deletions(-) diff --git a/client/src/app/shared/video/infinite-scroller.directive.ts b/client/src/app/shared/video/infinite-scroller.directive.ts index 5f8a1dd6e..b1e88882c 100644 --- a/client/src/app/shared/video/infinite-scroller.directive.ts +++ b/client/src/app/shared/video/infinite-scroller.directive.ts @@ -39,7 +39,7 @@ export class InfiniteScrollerDirective implements OnInit, OnDestroy { const scrollObservable = fromEvent(this.container || window, 'scroll') .pipe( - startWith(null), + startWith(null as string), // FIXME: typings throttleTime(200, undefined, throttleOptions), map(() => this.getScrollInfo()), distinctUntilChanged((o1, o2) => o1.current === o2.current), diff --git a/client/src/app/shared/video/video-miniature.component.html b/client/src/app/shared/video/video-miniature.component.html index 7af0f1113..51ca1393d 100644 --- a/client/src/app/shared/video/video-miniature.component.html +++ b/client/src/app/shared/video/video-miniature.component.html @@ -31,7 +31,7 @@
{{ video.privacy.label }} - - + - {{ getStateLabel(video) }}
diff --git a/client/src/app/videos/+video-watch/video-watch.component.ts b/client/src/app/videos/+video-watch/video-watch.component.ts index 027c2b026..0d499d47f 100644 --- a/client/src/app/videos/+video-watch/video-watch.component.ts +++ b/client/src/app/videos/+video-watch/video-watch.component.ts @@ -255,10 +255,10 @@ export class VideoWatchComponent implements OnInit, OnDestroy { ) // Video did change - forkJoin( + forkJoin([ videoObs, this.videoCaptionService.listCaptions(videoId) - ) + ]) .pipe( // If 401, the video is private or blacklisted so redirect to 404 catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 401, 403, 404 ])) diff --git a/server/helpers/custom-validators/accounts.ts b/server/helpers/custom-validators/accounts.ts index be196d2a4..f676669ea 100644 --- a/server/helpers/custom-validators/accounts.ts +++ b/server/helpers/custom-validators/accounts.ts @@ -1,4 +1,3 @@ -import 'express-validator' import { isUserDescriptionValid, isUserUsernameValid } from './users' import { exists } from './misc' diff --git a/server/helpers/custom-validators/misc.ts b/server/helpers/custom-validators/misc.ts index 3ef38fce1..1b7e00431 100644 --- a/server/helpers/custom-validators/misc.ts +++ b/server/helpers/custom-validators/misc.ts @@ -1,6 +1,7 @@ import 'multer' import * as validator from 'validator' import { sep } from 'path' +import toBoolean = require('validator/lib/toBoolean') function exists (value: any) { return value !== undefined && value !== null @@ -46,9 +47,21 @@ function isBooleanValid (value: any) { } function toIntOrNull (value: string) { - if (value === 'null') return null + const v = toValueOrNull(value) - return validator.toInt(value) + if (v === null || v === undefined) return v + if (typeof v === 'number') return v + + return validator.toInt(v) +} + +function toBooleanOrNull (value: any) { + const v = toValueOrNull(value) + + if (v === null || v === undefined) return v + if (typeof v === 'boolean') return v + + return toBoolean(v) } function toValueOrNull (value: string) { @@ -110,6 +123,7 @@ export { isIdOrUUIDValid, isDateValid, toValueOrNull, + toBooleanOrNull, isBooleanValid, toIntOrNull, toArray, diff --git a/server/helpers/custom-validators/search.ts b/server/helpers/custom-validators/search.ts index 15b389a58..ee732b15a 100644 --- a/server/helpers/custom-validators/search.ts +++ b/server/helpers/custom-validators/search.ts @@ -1,6 +1,4 @@ import * as validator from 'validator' -import 'express-validator' - import { isArray } from './misc' function isNumberArray (value: any) { diff --git a/server/helpers/custom-validators/servers.ts b/server/helpers/custom-validators/servers.ts index 5c8bf0d2d..7ced36fd3 100644 --- a/server/helpers/custom-validators/servers.ts +++ b/server/helpers/custom-validators/servers.ts @@ -1,7 +1,5 @@ import * as validator from 'validator' -import 'express-validator' - -import { isArray, exists } from './misc' +import { exists, isArray } from './misc' import { isTestInstance } from '../core-utils' import { CONSTRAINTS_FIELDS } from '../../initializers/constants' diff --git a/server/helpers/custom-validators/users.ts b/server/helpers/custom-validators/users.ts index 738d5cbbf..c56ae14ef 100644 --- a/server/helpers/custom-validators/users.ts +++ b/server/helpers/custom-validators/users.ts @@ -1,4 +1,3 @@ -import 'express-validator' import * as validator from 'validator' import { UserRole } from '../../../shared' import { CONSTRAINTS_FIELDS, NSFW_POLICY_TYPES } from '../../initializers/constants' diff --git a/server/helpers/custom-validators/video-channels.ts b/server/helpers/custom-validators/video-channels.ts index f55f0c8ef..6c52dc093 100644 --- a/server/helpers/custom-validators/video-channels.ts +++ b/server/helpers/custom-validators/video-channels.ts @@ -1,5 +1,3 @@ -import 'express-validator' -import 'multer' import * as validator from 'validator' import { CONSTRAINTS_FIELDS } from '../../initializers/constants' import { exists } from './misc' diff --git a/server/helpers/custom-validators/video-comments.ts b/server/helpers/custom-validators/video-comments.ts index 0707e2af2..8a7cd7105 100644 --- a/server/helpers/custom-validators/video-comments.ts +++ b/server/helpers/custom-validators/video-comments.ts @@ -1,4 +1,3 @@ -import 'express-validator' import 'multer' import * as validator from 'validator' import { CONSTRAINTS_FIELDS } from '../../initializers/constants' diff --git a/server/helpers/custom-validators/video-imports.ts b/server/helpers/custom-validators/video-imports.ts index f4235e2fa..8820c4c0a 100644 --- a/server/helpers/custom-validators/video-imports.ts +++ b/server/helpers/custom-validators/video-imports.ts @@ -1,4 +1,3 @@ -import 'express-validator' import 'multer' import * as validator from 'validator' import { CONSTRAINTS_FIELDS, MIMETYPES, VIDEO_IMPORT_STATES } from '../../initializers/constants' diff --git a/server/helpers/custom-validators/videos.ts b/server/helpers/custom-validators/videos.ts index 157e1a8e3..9ab1ef234 100644 --- a/server/helpers/custom-validators/videos.ts +++ b/server/helpers/custom-validators/videos.ts @@ -1,9 +1,6 @@ -import { Response } from 'express' -import 'express-validator' import { values } from 'lodash' -import 'multer' import * as validator from 'validator' -import { UserRight, VideoFilter, VideoPrivacy, VideoRateType } from '../../../shared' +import { VideoFilter, VideoPrivacy, VideoRateType } from '../../../shared' import { CONSTRAINTS_FIELDS, MIMETYPES, @@ -13,9 +10,7 @@ import { VIDEO_RATE_TYPES, VIDEO_STATES } from '../../initializers/constants' -import { VideoModel } from '../../models/video/video' import { exists, isArray, isDateValid, isFileValid } from './misc' -import { UserModel } from '../../models/account/user' import * as magnetUtil from 'magnet-uri' const VIDEOS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEOS diff --git a/server/middlewares/oauth.ts b/server/middlewares/oauth.ts index 2b4e300e4..77fb305dd 100644 --- a/server/middlewares/oauth.ts +++ b/server/middlewares/oauth.ts @@ -1,6 +1,5 @@ import * as express from 'express' import * as OAuthServer from 'express-oauth-server' -import 'express-validator' import { OAUTH_LIFETIME } from '../initializers/constants' import { logger } from '../helpers/logger' import { Socket } from 'socket.io' diff --git a/server/middlewares/pagination.ts b/server/middlewares/pagination.ts index 83304940f..043869303 100644 --- a/server/middlewares/pagination.ts +++ b/server/middlewares/pagination.ts @@ -1,6 +1,4 @@ -import 'express-validator' import * as express from 'express' - import { PAGINATION } from '../initializers/constants' function setDefaultPagination (req: express.Request, res: express.Response, next: express.NextFunction) { diff --git a/server/middlewares/servers.ts b/server/middlewares/servers.ts index c52f4685b..9c0af443a 100644 --- a/server/middlewares/servers.ts +++ b/server/middlewares/servers.ts @@ -1,5 +1,4 @@ import * as express from 'express' -import 'express-validator' import { getHostWithPort } from '../helpers/express-utils' function setBodyHostsPort (req: express.Request, res: express.Response, next: express.NextFunction) { diff --git a/server/middlewares/sort.ts b/server/middlewares/sort.ts index 6507aa5b8..8c27e8237 100644 --- a/server/middlewares/sort.ts +++ b/server/middlewares/sort.ts @@ -1,5 +1,4 @@ import * as express from 'express' -import 'express-validator' import { SortType } from '../models/utils' function setDefaultSort (req: express.Request, res: express.Response, next: express.NextFunction) { diff --git a/server/middlewares/user-right.ts b/server/middlewares/user-right.ts index 498e3d677..4da7b9802 100644 --- a/server/middlewares/user-right.ts +++ b/server/middlewares/user-right.ts @@ -1,5 +1,4 @@ import * as express from 'express' -import 'express-validator' import { UserRight } from '../../shared' import { logger } from '../helpers/logger' diff --git a/server/middlewares/validators/account.ts b/server/middlewares/validators/account.ts index 67e4bf8cc..cbdcef2fd 100644 --- a/server/middlewares/validators/account.ts +++ b/server/middlewares/validators/account.ts @@ -1,5 +1,5 @@ import * as express from 'express' -import { param } from 'express-validator/check' +import { param } from 'express-validator' import { isAccountNameValid } from '../../helpers/custom-validators/accounts' import { logger } from '../../helpers/logger' import { areValidationErrors } from './utils' diff --git a/server/middlewares/validators/activitypub/signature.ts b/server/middlewares/validators/activitypub/signature.ts index be14e92ea..02b191480 100644 --- a/server/middlewares/validators/activitypub/signature.ts +++ b/server/middlewares/validators/activitypub/signature.ts @@ -1,5 +1,5 @@ import * as express from 'express' -import { body } from 'express-validator/check' +import { body } from 'express-validator' import { isSignatureCreatorValid, isSignatureTypeValid, isSignatureValueValid diff --git a/server/middlewares/validators/avatar.ts b/server/middlewares/validators/avatar.ts index bab3ed118..8623d07e8 100644 --- a/server/middlewares/validators/avatar.ts +++ b/server/middlewares/validators/avatar.ts @@ -1,5 +1,5 @@ import * as express from 'express' -import { body } from 'express-validator/check' +import { body } from 'express-validator' import { isAvatarFile } from '../../helpers/custom-validators/users' import { areValidationErrors } from './utils' import { CONSTRAINTS_FIELDS } from '../../initializers/constants' diff --git a/server/middlewares/validators/blocklist.ts b/server/middlewares/validators/blocklist.ts index 63d95e9e0..47a0b1a1c 100644 --- a/server/middlewares/validators/blocklist.ts +++ b/server/middlewares/validators/blocklist.ts @@ -1,4 +1,4 @@ -import { body, param } from 'express-validator/check' +import { body, param } from 'express-validator' import * as express from 'express' import { logger } from '../../helpers/logger' import { areValidationErrors } from './utils' diff --git a/server/middlewares/validators/config.ts b/server/middlewares/validators/config.ts index 9c43da165..5059ed0f2 100644 --- a/server/middlewares/validators/config.ts +++ b/server/middlewares/validators/config.ts @@ -1,5 +1,5 @@ import * as express from 'express' -import { body } from 'express-validator/check' +import { body } from 'express-validator' import { isUserNSFWPolicyValid, isUserVideoQuotaDailyValid, isUserVideoQuotaValid } from '../../helpers/custom-validators/users' import { logger } from '../../helpers/logger' import { CustomConfig } from '../../../shared/models/server/custom-config.model' diff --git a/server/middlewares/validators/feeds.ts b/server/middlewares/validators/feeds.ts index fa130121f..1bef9891b 100644 --- a/server/middlewares/validators/feeds.ts +++ b/server/middlewares/validators/feeds.ts @@ -1,5 +1,5 @@ import * as express from 'express' -import { param, query } from 'express-validator/check' +import { param, query } from 'express-validator' import { isIdOrUUIDValid, isIdValid } from '../../helpers/custom-validators/misc' import { logger } from '../../helpers/logger' import { areValidationErrors } from './utils' diff --git a/server/middlewares/validators/follows.ts b/server/middlewares/validators/follows.ts index 2e5a02307..c3d772297 100644 --- a/server/middlewares/validators/follows.ts +++ b/server/middlewares/validators/follows.ts @@ -1,5 +1,5 @@ import * as express from 'express' -import { body, param } from 'express-validator/check' +import { body, param } from 'express-validator' import { isTestInstance } from '../../helpers/core-utils' import { isEachUniqueHostValid, isHostValid } from '../../helpers/custom-validators/servers' import { logger } from '../../helpers/logger' diff --git a/server/middlewares/validators/jobs.ts b/server/middlewares/validators/jobs.ts index 2f8b1738c..41a8d6899 100644 --- a/server/middlewares/validators/jobs.ts +++ b/server/middlewares/validators/jobs.ts @@ -1,5 +1,5 @@ import * as express from 'express' -import { param } from 'express-validator/check' +import { param } from 'express-validator' import { isValidJobState } from '../../helpers/custom-validators/jobs' import { logger } from '../../helpers/logger' import { areValidationErrors } from './utils' diff --git a/server/middlewares/validators/logs.ts b/server/middlewares/validators/logs.ts index 7380c6edd..07f3f552f 100644 --- a/server/middlewares/validators/logs.ts +++ b/server/middlewares/validators/logs.ts @@ -2,7 +2,7 @@ import * as express from 'express' import { logger } from '../../helpers/logger' import { areValidationErrors } from './utils' import { isDateValid } from '../../helpers/custom-validators/misc' -import { query } from 'express-validator/check' +import { query } from 'express-validator' import { isValidLogLevel } from '../../helpers/custom-validators/logs' const getLogsValidator = [ diff --git a/server/middlewares/validators/oembed.ts b/server/middlewares/validators/oembed.ts index 505319980..24ba5569d 100644 --- a/server/middlewares/validators/oembed.ts +++ b/server/middlewares/validators/oembed.ts @@ -1,5 +1,5 @@ import * as express from 'express' -import { query } from 'express-validator/check' +import { query } from 'express-validator' import { join } from 'path' import { isTestInstance } from '../../helpers/core-utils' import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' diff --git a/server/middlewares/validators/pagination.ts b/server/middlewares/validators/pagination.ts index e1ed8cd65..80ae57c0b 100644 --- a/server/middlewares/validators/pagination.ts +++ b/server/middlewares/validators/pagination.ts @@ -1,5 +1,5 @@ import * as express from 'express' -import { query } from 'express-validator/check' +import { query } from 'express-validator' import { logger } from '../../helpers/logger' import { areValidationErrors } from './utils' diff --git a/server/middlewares/validators/plugins.ts b/server/middlewares/validators/plugins.ts index dc3f1454a..910d03c29 100644 --- a/server/middlewares/validators/plugins.ts +++ b/server/middlewares/validators/plugins.ts @@ -1,10 +1,10 @@ import * as express from 'express' -import { body, param, query } from 'express-validator/check' +import { body, param, query } from 'express-validator' import { logger } from '../../helpers/logger' import { areValidationErrors } from './utils' import { isNpmPluginNameValid, isPluginNameValid, isPluginTypeValid, isPluginVersionValid } from '../../helpers/custom-validators/plugins' import { PluginManager } from '../../lib/plugins/plugin-manager' -import { isBooleanValid, isSafePath } from '../../helpers/custom-validators/misc' +import { isBooleanValid, isSafePath, toBooleanOrNull } from '../../helpers/custom-validators/misc' import { PluginModel } from '../../models/server/plugin' import { InstallOrUpdatePlugin } from '../../../shared/models/plugins/install-plugin.model' import { PluginType } from '../../../shared/models/plugins/plugin.type' @@ -39,7 +39,7 @@ const listPluginsValidator = [ .custom(isPluginTypeValid).withMessage('Should have a valid plugin type'), query('uninstalled') .optional() - .toBoolean() + .customSanitizer(toBooleanOrNull) .custom(isBooleanValid).withMessage('Should have a valid uninstalled attribute'), (req: express.Request, res: express.Response, next: express.NextFunction) => { diff --git a/server/middlewares/validators/redundancy.ts b/server/middlewares/validators/redundancy.ts index edc53a6b2..1fdac0e4e 100644 --- a/server/middlewares/validators/redundancy.ts +++ b/server/middlewares/validators/redundancy.ts @@ -1,7 +1,6 @@ import * as express from 'express' -import 'express-validator' -import { body, param } from 'express-validator/check' -import { exists, isBooleanValid, isIdOrUUIDValid, toIntOrNull } from '../../helpers/custom-validators/misc' +import { body, param } from 'express-validator' +import { exists, isBooleanValid, isIdOrUUIDValid, toBooleanOrNull, toIntOrNull } from '../../helpers/custom-validators/misc' import { logger } from '../../helpers/logger' import { areValidationErrors } from './utils' import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy' @@ -68,7 +67,7 @@ const videoPlaylistRedundancyGetValidator = [ const updateServerRedundancyValidator = [ param('host').custom(isHostValid).withMessage('Should have a valid host'), body('redundancyAllowed') - .toBoolean() + .customSanitizer(toBooleanOrNull) .custom(isBooleanValid).withMessage('Should have a valid redundancyAllowed attribute'), async (req: express.Request, res: express.Response, next: express.NextFunction) => { diff --git a/server/middlewares/validators/search.ts b/server/middlewares/validators/search.ts index 7816d229c..5a3c83f2c 100644 --- a/server/middlewares/validators/search.ts +++ b/server/middlewares/validators/search.ts @@ -1,7 +1,7 @@ import * as express from 'express' import { areValidationErrors } from './utils' import { logger } from '../../helpers/logger' -import { query } from 'express-validator/check' +import { query } from 'express-validator' import { isDateValid } from '../../helpers/custom-validators/misc' const videosSearchValidator = [ diff --git a/server/middlewares/validators/server.ts b/server/middlewares/validators/server.ts index 6eff8e9ee..f6812647b 100644 --- a/server/middlewares/validators/server.ts +++ b/server/middlewares/validators/server.ts @@ -3,7 +3,7 @@ import { logger } from '../../helpers/logger' import { areValidationErrors } from './utils' import { isHostValid, isValidContactBody } from '../../helpers/custom-validators/servers' import { ServerModel } from '../../models/server/server' -import { body } from 'express-validator/check' +import { body } from 'express-validator' import { isUserDisplayNameValid } from '../../helpers/custom-validators/users' import { Emailer } from '../../lib/emailer' import { Redis } from '../../lib/redis' diff --git a/server/middlewares/validators/themes.ts b/server/middlewares/validators/themes.ts index 642f2df78..24a9673f7 100644 --- a/server/middlewares/validators/themes.ts +++ b/server/middlewares/validators/themes.ts @@ -1,5 +1,5 @@ import * as express from 'express' -import { param } from 'express-validator/check' +import { param } from 'express-validator' import { logger } from '../../helpers/logger' import { areValidationErrors } from './utils' import { isPluginNameValid, isPluginVersionValid } from '../../helpers/custom-validators/plugins' diff --git a/server/middlewares/validators/user-history.ts b/server/middlewares/validators/user-history.ts index 418313d09..2f1d3cc41 100644 --- a/server/middlewares/validators/user-history.ts +++ b/server/middlewares/validators/user-history.ts @@ -1,6 +1,5 @@ import * as express from 'express' -import 'express-validator' -import { body } from 'express-validator/check' +import { body } from 'express-validator' import { logger } from '../../helpers/logger' import { areValidationErrors } from './utils' import { isDateValid } from '../../helpers/custom-validators/misc' diff --git a/server/middlewares/validators/user-notifications.ts b/server/middlewares/validators/user-notifications.ts index 3ded8d8cf..308b32655 100644 --- a/server/middlewares/validators/user-notifications.ts +++ b/server/middlewares/validators/user-notifications.ts @@ -1,15 +1,14 @@ import * as express from 'express' -import 'express-validator' -import { body, query } from 'express-validator/check' +import { body, query } from 'express-validator' import { logger } from '../../helpers/logger' import { areValidationErrors } from './utils' import { isUserNotificationSettingValid } from '../../helpers/custom-validators/user-notifications' -import { isNotEmptyIntArray } from '../../helpers/custom-validators/misc' +import { isNotEmptyIntArray, toBooleanOrNull } from '../../helpers/custom-validators/misc' const listUserNotificationsValidator = [ query('unread') .optional() - .toBoolean() + .customSanitizer(toBooleanOrNull) .isBoolean().withMessage('Should have a valid unread boolean'), (req: express.Request, res: express.Response, next: express.NextFunction) => { diff --git a/server/middlewares/validators/user-subscriptions.ts b/server/middlewares/validators/user-subscriptions.ts index 2356745d7..9bc8c87e7 100644 --- a/server/middlewares/validators/user-subscriptions.ts +++ b/server/middlewares/validators/user-subscriptions.ts @@ -1,6 +1,5 @@ import * as express from 'express' -import 'express-validator' -import { body, param, query } from 'express-validator/check' +import { body, param, query } from 'express-validator' import { logger } from '../../helpers/logger' import { areValidationErrors } from './utils' import { ActorFollowModel } from '../../models/activitypub/actor-follow' diff --git a/server/middlewares/validators/users.ts b/server/middlewares/validators/users.ts index 7002de20d..db03dc231 100644 --- a/server/middlewares/validators/users.ts +++ b/server/middlewares/validators/users.ts @@ -1,9 +1,8 @@ import * as Bluebird from 'bluebird' import * as express from 'express' -import 'express-validator' -import { body, param } from 'express-validator/check' +import { body, param } from 'express-validator' import { omit } from 'lodash' -import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' +import { isIdOrUUIDValid, toIntOrNull } from '../../helpers/custom-validators/misc' import { isUserAdminFlagsValid, isUserAutoPlayVideoValid, @@ -358,7 +357,7 @@ const usersVerifyEmailValidator = [ .not().isEmpty().withMessage('Should have a valid verification string'), body('isPendingEmail') .optional() - .toBoolean(), + .customSanitizer(toIntOrNull), async (req: express.Request, res: express.Response, next: express.NextFunction) => { logger.debug('Checking usersVerifyEmail parameters', { parameters: req.params }) diff --git a/server/middlewares/validators/utils.ts b/server/middlewares/validators/utils.ts index 8f77c9fbd..43e5652fa 100644 --- a/server/middlewares/validators/utils.ts +++ b/server/middlewares/validators/utils.ts @@ -1,5 +1,5 @@ import * as express from 'express' -import { query, validationResult } from 'express-validator/check' +import { query, validationResult } from 'express-validator' import { logger } from '../../helpers/logger' function areValidationErrors (req: express.Request, res: express.Response) { diff --git a/server/middlewares/validators/videos/video-abuses.ts b/server/middlewares/validators/videos/video-abuses.ts index e176e01af..e27d91bb1 100644 --- a/server/middlewares/validators/videos/video-abuses.ts +++ b/server/middlewares/validators/videos/video-abuses.ts @@ -1,6 +1,5 @@ import * as express from 'express' -import 'express-validator' -import { body, param } from 'express-validator/check' +import { body, param } from 'express-validator' import { isIdOrUUIDValid, isIdValid } from '../../../helpers/custom-validators/misc' import { logger } from '../../../helpers/logger' import { areValidationErrors } from '../utils' diff --git a/server/middlewares/validators/videos/video-blacklist.ts b/server/middlewares/validators/videos/video-blacklist.ts index db59427c7..3e8c5b30c 100644 --- a/server/middlewares/validators/videos/video-blacklist.ts +++ b/server/middlewares/validators/videos/video-blacklist.ts @@ -1,6 +1,6 @@ import * as express from 'express' -import { body, param, query } from 'express-validator/check' -import { isBooleanValid, isIdOrUUIDValid } from '../../../helpers/custom-validators/misc' +import { body, param, query } from 'express-validator' +import { isBooleanValid, isIdOrUUIDValid, toBooleanOrNull } from '../../../helpers/custom-validators/misc' import { logger } from '../../../helpers/logger' import { areValidationErrors } from '../utils' import { isVideoBlacklistReasonValid, isVideoBlacklistTypeValid } from '../../../helpers/custom-validators/video-blacklist' @@ -24,7 +24,7 @@ const videosBlacklistAddValidator = [ param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), body('unfederate') .optional() - .toBoolean() + .customSanitizer(toBooleanOrNull) .custom(isBooleanValid).withMessage('Should have a valid unfederate boolean'), body('reason') .optional() diff --git a/server/middlewares/validators/videos/video-captions.ts b/server/middlewares/validators/videos/video-captions.ts index f8739e27f..f5610222a 100644 --- a/server/middlewares/validators/videos/video-captions.ts +++ b/server/middlewares/validators/videos/video-captions.ts @@ -1,7 +1,7 @@ import * as express from 'express' import { areValidationErrors } from '../utils' import { isIdOrUUIDValid } from '../../../helpers/custom-validators/misc' -import { body, param } from 'express-validator/check' +import { body, param } from 'express-validator' import { CONSTRAINTS_FIELDS } from '../../../initializers/constants' import { UserRight } from '../../../../shared' import { logger } from '../../../helpers/logger' diff --git a/server/middlewares/validators/videos/video-channels.ts b/server/middlewares/validators/videos/video-channels.ts index c1065b898..3ee5064fc 100644 --- a/server/middlewares/validators/videos/video-channels.ts +++ b/server/middlewares/validators/videos/video-channels.ts @@ -1,5 +1,5 @@ import * as express from 'express' -import { body, param } from 'express-validator/check' +import { body, param } from 'express-validator' import { UserRight } from '../../../../shared' import { isVideoChannelDescriptionValid, diff --git a/server/middlewares/validators/videos/video-comments.ts b/server/middlewares/validators/videos/video-comments.ts index 1e3e42833..83a0c24b0 100644 --- a/server/middlewares/validators/videos/video-comments.ts +++ b/server/middlewares/validators/videos/video-comments.ts @@ -1,5 +1,5 @@ import * as express from 'express' -import { body, param } from 'express-validator/check' +import { body, param } from 'express-validator' import { UserRight } from '../../../../shared' import { isIdOrUUIDValid, isIdValid } from '../../../helpers/custom-validators/misc' import { isValidVideoCommentText } from '../../../helpers/custom-validators/video-comments' diff --git a/server/middlewares/validators/videos/video-imports.ts b/server/middlewares/validators/videos/video-imports.ts index 8b0dd8960..318dad100 100644 --- a/server/middlewares/validators/videos/video-imports.ts +++ b/server/middlewares/validators/videos/video-imports.ts @@ -1,6 +1,6 @@ import * as express from 'express' -import { body } from 'express-validator/check' -import { isIdValid } from '../../../helpers/custom-validators/misc' +import { body } from 'express-validator' +import { isIdValid, toIntOrNull } from '../../../helpers/custom-validators/misc' import { logger } from '../../../helpers/logger' import { areValidationErrors } from '../utils' import { getCommonVideoEditAttributes } from './videos' @@ -13,7 +13,7 @@ import { doesVideoChannelOfAccountExist } from '../../../helpers/middlewares' const videoImportAddValidator = getCommonVideoEditAttributes().concat([ body('channelId') - .toInt() + .customSanitizer(toIntOrNull) .custom(isIdValid).withMessage('Should have correct video channel id'), body('targetUrl') .optional() diff --git a/server/middlewares/validators/videos/video-playlists.ts b/server/middlewares/validators/videos/video-playlists.ts index 638122a2e..2e9c8aa33 100644 --- a/server/middlewares/validators/videos/video-playlists.ts +++ b/server/middlewares/validators/videos/video-playlists.ts @@ -1,12 +1,20 @@ import * as express from 'express' -import { body, param, query, ValidationChain } from 'express-validator/check' +import { body, param, query, ValidationChain } from 'express-validator' import { UserRight, VideoPlaylistCreate, VideoPlaylistUpdate } from '../../../../shared' import { logger } from '../../../helpers/logger' import { UserModel } from '../../../models/account/user' import { areValidationErrors } from '../utils' import { isVideoImage } from '../../../helpers/custom-validators/videos' import { CONSTRAINTS_FIELDS } from '../../../initializers/constants' -import { isArrayOf, isIdOrUUIDValid, isIdValid, isUUIDValid, toIntArray, toValueOrNull } from '../../../helpers/custom-validators/misc' +import { + isArrayOf, + isIdOrUUIDValid, + isIdValid, + isUUIDValid, + toIntArray, + toIntOrNull, + toValueOrNull +} from '../../../helpers/custom-validators/misc' import { isVideoPlaylistDescriptionValid, isVideoPlaylistNameValid, @@ -374,12 +382,11 @@ function getCommonPlaylistEditAttributes () { .custom(isVideoPlaylistDescriptionValid).withMessage('Should have a valid description'), body('privacy') .optional() - .toInt() + .customSanitizer(toIntOrNull) .custom(isVideoPlaylistPrivacyValid).withMessage('Should have correct playlist privacy'), body('videoChannelId') .optional() - .customSanitizer(toValueOrNull) - .toInt() + .customSanitizer(toIntOrNull) ] as (ValidationChain | express.Handler)[] } diff --git a/server/middlewares/validators/videos/video-rates.ts b/server/middlewares/validators/videos/video-rates.ts index 5bb3f4a51..4021cfecc 100644 --- a/server/middlewares/validators/videos/video-rates.ts +++ b/server/middlewares/validators/videos/video-rates.ts @@ -1,6 +1,5 @@ import * as express from 'express' -import 'express-validator' -import { body, param, query } from 'express-validator/check' +import { body, param, query } from 'express-validator' import { isIdOrUUIDValid } from '../../../helpers/custom-validators/misc' import { isRatingValid } from '../../../helpers/custom-validators/video-rates' import { isVideoRatingTypeValid } from '../../../helpers/custom-validators/videos' diff --git a/server/middlewares/validators/videos/video-shares.ts b/server/middlewares/validators/videos/video-shares.ts index 6f4a1f3e0..ace62be5c 100644 --- a/server/middlewares/validators/videos/video-shares.ts +++ b/server/middlewares/validators/videos/video-shares.ts @@ -1,6 +1,5 @@ import * as express from 'express' -import 'express-validator' -import { param } from 'express-validator/check' +import { param } from 'express-validator' import { isIdOrUUIDValid, isIdValid } from '../../../helpers/custom-validators/misc' import { logger } from '../../../helpers/logger' import { VideoShareModel } from '../../../models/video/video-share' diff --git a/server/middlewares/validators/videos/video-watch.ts b/server/middlewares/validators/videos/video-watch.ts index a0b530c75..d6ca1d341 100644 --- a/server/middlewares/validators/videos/video-watch.ts +++ b/server/middlewares/validators/videos/video-watch.ts @@ -1,6 +1,6 @@ -import { body, param } from 'express-validator/check' +import { body, param } from 'express-validator' import * as express from 'express' -import { isIdOrUUIDValid } from '../../../helpers/custom-validators/misc' +import { isIdOrUUIDValid, toIntOrNull } from '../../../helpers/custom-validators/misc' import { areValidationErrors } from '../utils' import { logger } from '../../../helpers/logger' import { doesVideoExist } from '../../../helpers/middlewares' @@ -8,7 +8,7 @@ import { doesVideoExist } from '../../../helpers/middlewares' const videoWatchingValidator = [ param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), body('currentTime') - .toInt() + .customSanitizer(toIntOrNull) .isInt().withMessage('Should have correct current time'), async (req: express.Request, res: express.Response, next: express.NextFunction) => { diff --git a/server/middlewares/validators/videos/videos.ts b/server/middlewares/validators/videos/videos.ts index 8f5e5c95c..27dfe91ca 100644 --- a/server/middlewares/validators/videos/videos.ts +++ b/server/middlewares/validators/videos/videos.ts @@ -1,6 +1,5 @@ import * as express from 'express' -import 'express-validator' -import { body, param, query, ValidationChain } from 'express-validator/check' +import { body, param, query, ValidationChain } from 'express-validator' import { UserRight, VideoChangeOwnershipStatus, VideoPrivacy } from '../../../../shared' import { isBooleanValid, @@ -9,6 +8,7 @@ import { isIdValid, isUUIDValid, toArray, + toBooleanOrNull, toIntOrNull, toValueOrNull } from '../../../helpers/custom-validators/misc' @@ -53,7 +53,7 @@ const videosAddValidator = getCommonVideoEditAttributes().concat([ ), body('name').custom(isVideoNameValid).withMessage('Should have a valid name'), body('channelId') - .toInt() + .customSanitizer(toIntOrNull) .custom(isIdValid).withMessage('Should have correct video channel id'), async (req: express.Request, res: express.Response, next: express.NextFunction) => { @@ -101,7 +101,7 @@ const videosUpdateValidator = getCommonVideoEditAttributes().concat([ .custom(isVideoNameValid).withMessage('Should have a valid name'), body('channelId') .optional() - .toInt() + .customSanitizer(toIntOrNull) .custom(isIdValid).withMessage('Should have correct video channel id'), async (req: express.Request, res: express.Response, next: express.NextFunction) => { @@ -307,15 +307,15 @@ function getCommonVideoEditAttributes () { .custom(isVideoLanguageValid).withMessage('Should have a valid language'), body('nsfw') .optional() - .toBoolean() + .customSanitizer(toBooleanOrNull) .custom(isBooleanValid).withMessage('Should have a valid NSFW attribute'), body('waitTranscoding') .optional() - .toBoolean() + .customSanitizer(toBooleanOrNull) .custom(isBooleanValid).withMessage('Should have a valid wait transcoding attribute'), body('privacy') .optional() - .toInt() + .customSanitizer(toValueOrNull) .custom(isVideoPrivacyValid).withMessage('Should have correct video privacy'), body('description') .optional() @@ -331,16 +331,16 @@ function getCommonVideoEditAttributes () { .custom(isVideoTagsValid).withMessage('Should have correct tags'), body('commentsEnabled') .optional() - .toBoolean() + .customSanitizer(toBooleanOrNull) .custom(isBooleanValid).withMessage('Should have comments enabled boolean'), body('downloadEnabled') .optional() - .toBoolean() + .customSanitizer(toBooleanOrNull) .custom(isBooleanValid).withMessage('Should have downloading enabled boolean'), body('originallyPublishedAt') - .optional() - .customSanitizer(toValueOrNull) - .custom(isVideoOriginallyPublishedAtValid).withMessage('Should have a valid original publication date'), + .optional() + .customSanitizer(toValueOrNull) + .custom(isVideoOriginallyPublishedAtValid).withMessage('Should have a valid original publication date'), body('scheduleUpdate') .optional() .customSanitizer(toValueOrNull), @@ -349,7 +349,7 @@ function getCommonVideoEditAttributes () { .custom(isDateValid).withMessage('Should have a valid schedule update date'), body('scheduleUpdate.privacy') .optional() - .toInt() + .customSanitizer(toValueOrNull) .custom(isScheduleVideoUpdatePrivacyValid).withMessage('Should have correct schedule update privacy') ] as (ValidationChain | express.Handler)[] } diff --git a/server/middlewares/validators/webfinger.ts b/server/middlewares/validators/webfinger.ts index 63a1678ec..d7cfe17f0 100644 --- a/server/middlewares/validators/webfinger.ts +++ b/server/middlewares/validators/webfinger.ts @@ -1,5 +1,5 @@ import * as express from 'express' -import { query } from 'express-validator/check' +import { query } from 'express-validator' import { isWebfingerLocalResourceValid } from '../../helpers/custom-validators/webfinger' import { logger } from '../../helpers/logger' import { ActorModel } from '../../models/activitypub/actor' diff --git a/server/tests/api/check-params/video-imports.ts b/server/tests/api/check-params/video-imports.ts index 8ff115e7b..231d5cc85 100644 --- a/server/tests/api/check-params/video-imports.ts +++ b/server/tests/api/check-params/video-imports.ts @@ -91,7 +91,7 @@ describe('Test video imports API validator', function () { support: 'my super support text', tags: [ 'tag1', 'tag2' ], privacy: VideoPrivacy.PUBLIC, - channelId: channelId + channelId } })