ActivityPub-federated video streaming platform using P2P directly in your web browser
Fix setting many query params
Chocobozzzff5aa283adbc
diff --git a/packages/tests/src/api/check-params/search.ts b/packages/tests/src/api/check-params/search.tsindex 75af2d4cf1..2ceb572124 100644--- a/packages/tests/src/api/check-params/search.ts+++ b/packages/tests/src/api/check-params/search.ts@@ -149,8 +149,13 @@ describe('Test videos API validator', function () {await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })})+ it('Should fail with too many uuids', async function () {+ const customQuery = { ...query, uuids: new Array(101).fill('dfd70b83-639f-4980-94af-304a56ab4b35') }+ await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })+ })+it('Should succeed with valid uuids', async function () {- const customQuery = { ...query, uuids: [ 'dfd70b83-639f-4980-94af-304a56ab4b35' ] }+ const customQuery = { ...query, uuids: new Array(90).fill('dfd70b83-639f-4980-94af-304a56ab4b35') }await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.OK_200 })})})@@ -184,8 +189,18 @@ describe('Test videos API validator', function () {await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })})+ it('Should fail with too many uuids', async function () {+ const customQuery = { ...query, uuids: new Array(101).fill('dfd70b83-639f-4980-94af-304a56ab4b35') }+ await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })+ })+it('Should succeed with the correct parameters', async function () {- await makeGetRequest({ url: server.url, path, query, expectedStatus: HttpStatusCode.OK_200 })+ await makeGetRequest({+ url: server.url,+ path,+ query: { ...query, uuids: new Array(90).fill('dfd70b83-639f-4980-94af-304a56ab4b35') },+ expectedStatus: HttpStatusCode.OK_200+ })})})@@ -217,8 +232,18 @@ describe('Test videos API validator', function () {await makeGetRequest({ url: server.url, path, query: { ...query, handles: [ '' ] }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })})+ it('Should fail with too many handles', async function () {+ const customQuery = { ...query, handles: new Array(101).fill('handle') }+ await makeGetRequest({ url: server.url, path, query: customQuery, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })+ })+it('Should succeed with the correct parameters', async function () {- await makeGetRequest({ url: server.url, path, query, expectedStatus: HttpStatusCode.OK_200 })+ await makeGetRequest({+ url: server.url,+ path,+ query: { ...query, handles: new Array(90).fill('handle') },+ expectedStatus: HttpStatusCode.OK_200+ })})})diff --git a/packages/tests/src/api/check-params/video-playlists.ts b/packages/tests/src/api/check-params/video-playlists.tsindex f3b82d3d17..5b310f0746 100644--- a/packages/tests/src/api/check-params/video-playlists.ts+++ b/packages/tests/src/api/check-params/video-playlists.ts@@ -808,6 +808,30 @@ describe('Test video playlists API validator', function () {})})+ it('Should succeed with many handles', async function () {+ const handles = Array.from({ length: 90 }, (_, i) => i + 1)++ await makeGetRequest({+ url: server.url,+ token: server.accessToken,+ path,+ query: { videoIds: handles },+ expectedStatus: HttpStatusCode.OK_200+ })+ })++ it('Should fail with too many handles', async function () {+ const handles = Array.from({ length: 101 }, (_, i) => i + 1)++ await makeGetRequest({+ url: server.url,+ token: server.accessToken,+ path,+ query: { videoIds: handles },+ expectedStatus: HttpStatusCode.BAD_REQUEST_400+ })+ })+it('Should succeed with the correct params', async function () {await makeGetRequest({url: server.url,diff --git a/server/core/helpers/custom-validators/misc.ts b/server/core/helpers/custom-validators/misc.tsindex 726e2b1ae4..ba34dc7ff2 100644--- a/server/core/helpers/custom-validators/misc.ts+++ b/server/core/helpers/custom-validators/misc.ts@@ -3,6 +3,7 @@ import { UploadFilesForCheck } from 'express'import 'multer'import { sep } from 'path'import validator from 'validator'+import { logger } from '../logger.js'export function exists (value: any) {return value !== undefined && value !== null@@ -165,11 +166,24 @@ export function toValueOrNull (value: string) {return value}+export function toArray (value: any) {+ if (!value) return []+ if (isArray(value)) return value++ if (value?.[0]) return Object.values(value)++ return [ value ]+}+export function toIntArray (value: any) {if (!value) return []- if (isArray(value) === false) return [ validator.default.toInt(value) ]+ if (isArray(value)) return value.map(v => validator.default.toInt(v))++ if (value?.[0]) return Object.values(value).map(v => validator.default.toInt(v + ''))++ if (typeof value === 'string' || typeof value === 'number') return [ validator.default.toInt(value + '') ]- return value.map(v => validator.default.toInt(v))+ return []}// ---------------------------------------------------------------------------diff --git a/server/core/middlewares/validators/follows.ts b/server/core/middlewares/validators/follows.tsindex 76571e594a..ed3efc58df 100644--- a/server/core/middlewares/validators/follows.ts+++ b/server/core/middlewares/validators/follows.ts@@ -1,12 +1,13 @@-import express from 'express'-import { body, param, query } from 'express-validator'import { HttpStatusCode, ServerFollowCreate } from '@peertube/peertube-models'import { isProdInstance } from '@peertube/peertube-node-utils'import { isEachUniqueHandleValid, isFollowStateValid, isRemoteHandleValid } from '@server/helpers/custom-validators/follows.js'+import { hasArrayLength, toArray } from '@server/helpers/custom-validators/misc.js'import { loadActorUrlOrGetFromWebfinger } from '@server/lib/activitypub/actors/index.js'import { getRemoteNameAndHost } from '@server/lib/activitypub/follow.js'import { getServerActor } from '@server/models/application/application.js'import { MActorFollowActorsDefault } from '@server/types/models/index.js'+import express from 'express'+import { body, param, query } from 'express-validator'import { isActorTypeValid, isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor.js'import { isEachUniqueHostValid, isHostValid } from '../../helpers/custom-validators/servers.js'import { logger } from '../../helpers/logger.js'@@ -32,11 +33,13 @@ const listFollowsValidator = [const followValidator = [body('hosts')- .toArray()+ .customSanitizer(toArray)+ .custom(v => hasArrayLength(v, { max: 100 })).custom(isEachUniqueHostValid).withMessage('Should have an array of unique hosts'),body('handles')- .toArray()+ .customSanitizer(toArray)+ .custom(v => hasArrayLength(v, { max: 100 })).custom(isEachUniqueHandleValid).withMessage('Should have an array of handles'),(req: express.Request, res: express.Response, next: express.NextFunction) => {@@ -147,10 +150,10 @@ const rejectFollowerValidator = [// ---------------------------------------------------------------------------export {+ acceptFollowerValidator,followValidator,- removeFollowingValidator,getFollowerValidator,- acceptFollowerValidator,+ listFollowsValidator,rejectFollowerValidator,- listFollowsValidator+ removeFollowingValidator}diff --git a/server/core/middlewares/validators/search.ts b/server/core/middlewares/validators/search.tsindex c4c184c11e..a8fc496dd4 100644--- a/server/core/middlewares/validators/search.ts+++ b/server/core/middlewares/validators/search.ts@@ -1,8 +1,15 @@-import express from 'express'-import { query } from 'express-validator'import { isSearchTargetValid } from '@server/helpers/custom-validators/search.js'import { isHostValid } from '@server/helpers/custom-validators/servers.js'-import { areUUIDsValid, isDateValid, isNotEmptyStringArray, toCompleteUUIDs } from '../../helpers/custom-validators/misc.js'+import express from 'express'+import { query } from 'express-validator'+import {+ areUUIDsValid,+ hasArrayLength,+ isDateValid,+ isNotEmptyStringArray,+ toArray,+ toCompleteUUIDs+} from '../../helpers/custom-validators/misc.js'import { areValidationErrors } from './shared/index.js'const videosSearchValidator = [@@ -33,7 +40,8 @@ const videosSearchValidator = [query('uuids').optional()- .toArray()+ .customSanitizer(toArray)+ .custom(v => hasArrayLength(v, { max: 100 })).customSanitizer(toCompleteUUIDs).custom(areUUIDsValid).withMessage('Should have valid array of uuid'),@@ -63,7 +71,8 @@ const videoChannelsListSearchValidator = [query('handles').optional()- .toArray()+ .customSanitizer(toArray)+ .custom(v => hasArrayLength(v, { max: 100 })).custom(isNotEmptyStringArray).withMessage('Should have valid array of handles'),(req: express.Request, res: express.Response, next: express.NextFunction) => {@@ -88,7 +97,8 @@ const videoPlaylistsListSearchValidator = [query('uuids').optional()- .toArray()+ .customSanitizer(toArray)+ .custom(v => hasArrayLength(v, { max: 100 })).customSanitizer(toCompleteUUIDs).custom(areUUIDsValid).withMessage('Should have valid array of uuid'),@@ -102,7 +112,7 @@ const videoPlaylistsListSearchValidator = [// ---------------------------------------------------------------------------export {- videosSearchValidator,videoChannelsListSearchValidator,- videoPlaylistsListSearchValidator+ videoPlaylistsListSearchValidator,+ videosSearchValidator}diff --git a/server/core/middlewares/validators/videos/video-playlists.ts b/server/core/middlewares/validators/videos/video-playlists.tsindex 1208016d1c..bf333c08db 100644--- a/server/core/middlewares/validators/videos/video-playlists.ts+++ b/server/core/middlewares/validators/videos/video-playlists.ts@@ -16,6 +16,7 @@ import { MUserAccountId } from '@server/types/models/index.js'import express from 'express'import { body, param, query, ValidationChain } from 'express-validator'import {+ hasArrayLength,isArrayOf,isIdOrUUIDValid,isIdValid,@@ -465,6 +466,7 @@ export const commonVideoPlaylistFiltersValidator = [export const doVideosInPlaylistExistValidator = [query('videoIds').customSanitizer(toIntArray)+ .custom(v => hasArrayLength(v, { max: 100 })).custom(v => isArrayOf(v, isIdValid)).withMessage('Should have a valid video ids array'),(req: express.Request, res: express.Response, next: express.NextFunction) => {