Return an error on invalid count pagination

pull/2396/head
Chocobozzz 2020-01-09 09:36:31 +01:00
parent 240458d0c9
commit e0b56b7495
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
5 changed files with 35 additions and 12 deletions

View File

@ -22,9 +22,16 @@ const API_VERSION = 'v1'
const PEERTUBE_VERSION = require(join(root(), 'package.json')).version const PEERTUBE_VERSION = require(join(root(), 'package.json')).version
const PAGINATION = { const PAGINATION = {
COUNT: { GLOBAL: {
DEFAULT: 15, COUNT: {
MAX: 100 DEFAULT: 15,
MAX: 100
}
},
OUTBOX: {
COUNT: {
MAX: 50
}
} }
} }

View File

@ -5,11 +5,9 @@ function setDefaultPagination (req: express.Request, res: express.Response, next
if (!req.query.start) req.query.start = 0 if (!req.query.start) req.query.start = 0
else req.query.start = parseInt(req.query.start, 10) else req.query.start = parseInt(req.query.start, 10)
if (!req.query.count) req.query.count = PAGINATION.COUNT.DEFAULT if (!req.query.count) req.query.count = PAGINATION.GLOBAL.COUNT.DEFAULT
else req.query.count = parseInt(req.query.count, 10) else req.query.count = parseInt(req.query.count, 10)
if (req.query.count > PAGINATION.COUNT.MAX) req.query.count = PAGINATION.COUNT.MAX
return next() return next()
} }

View File

@ -2,10 +2,15 @@ import * as express from 'express'
import { query } from 'express-validator' import { query } from 'express-validator'
import { logger } from '../../../helpers/logger' import { logger } from '../../../helpers/logger'
import { areValidationErrors } from '../utils' import { areValidationErrors } from '../utils'
import { PAGINATION } from '@server/initializers/constants'
const apPaginationValidator = [ const apPaginationValidator = [
query('page').optional().isInt({ min: 1 }).withMessage('Should have a valid page number'), query('page')
query('size').optional().isInt({ max: 50 }).withMessage('Should have a valid page size (max: 50)'), .optional()
.isInt({ min: 1 }).withMessage('Should have a valid page number'),
query('size')
.optional()
.isInt({ min: 0, max: PAGINATION.OUTBOX.COUNT.MAX }).withMessage(`Should have a valid page size (max: ${PAGINATION.OUTBOX.COUNT.MAX})`),
(req: express.Request, res: express.Response, next: express.NextFunction) => { (req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking pagination parameters', { parameters: req.query }) logger.debug('Checking pagination parameters', { parameters: req.query })

View File

@ -2,10 +2,15 @@ import * as express from 'express'
import { query } from 'express-validator' import { query } from 'express-validator'
import { logger } from '../../helpers/logger' import { logger } from '../../helpers/logger'
import { areValidationErrors } from './utils' import { areValidationErrors } from './utils'
import { PAGINATION } from '@server/initializers/constants'
const paginationValidator = [ const paginationValidator = [
query('start').optional().isInt({ min: 0 }).withMessage('Should have a number start'), query('start')
query('count').optional().isInt({ min: 0 }).withMessage('Should have a number count'), .optional()
.isInt({ min: 0 }).withMessage('Should have a number start'),
query('count')
.optional()
.isInt({ min: 0, max: PAGINATION.GLOBAL.COUNT.MAX }).withMessage(`Should have a number count (max: ${PAGINATION.GLOBAL.COUNT.MAX})`),
(req: express.Request, res: express.Response, next: express.NextFunction) => { (req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking pagination parameters', { parameters: req.query }) logger.debug('Checking pagination parameters', { parameters: req.query })

View File

@ -11,14 +11,22 @@ function checkBadStartPagination (url: string, path: string, token?: string, que
}) })
} }
function checkBadCountPagination (url: string, path: string, token?: string, query = {}) { async function checkBadCountPagination (url: string, path: string, token?: string, query = {}) {
return makeGetRequest({ await makeGetRequest({
url, url,
path, path,
token, token,
query: immutableAssign(query, { count: 'hello' }), query: immutableAssign(query, { count: 'hello' }),
statusCodeExpected: 400 statusCodeExpected: 400
}) })
await makeGetRequest({
url,
path,
token,
query: immutableAssign(query, { count: 2000 }),
statusCodeExpected: 400
})
} }
function checkBadSortPagination (url: string, path: string, token?: string, query = {}) { function checkBadSortPagination (url: string, path: string, token?: string, query = {}) {