Fix request body limit

This commit is contained in:
Chocobozzz 2021-03-09 09:58:08 +01:00
parent 18b24b2dc5
commit b329abc2f0
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
2 changed files with 16 additions and 5 deletions

View File

@ -205,7 +205,6 @@ async function listAvailablePlugins (req: express.Request, res: express.Response
if (!resultList) { if (!resultList) {
return res.status(HttpStatusCode.SERVICE_UNAVAILABLE_503) return res.status(HttpStatusCode.SERVICE_UNAVAILABLE_503)
.json({ error: 'Plugin index unavailable. Please retry later' }) .json({ error: 'Plugin index unavailable. Please retry later' })
.end()
} }
return res.json(resultList) return res.json(resultList)

View File

@ -30,13 +30,25 @@ const peertubeGot = got.extend({
handlers: [ handlers: [
(options, next) => { (options, next) => {
const promiseOrStream = next(options) as CancelableRequest<any> const promiseOrStream = next(options) as CancelableRequest<any>
const bodyKBLimit = options.context?.bodyKBLimit const bodyKBLimit = options.context?.bodyKBLimit as number
if (!bodyKBLimit) throw new Error('No KB limit for this request') if (!bodyKBLimit) throw new Error('No KB limit for this request')
const bodyLimit = bodyKBLimit * 1000
/* eslint-disable @typescript-eslint/no-floating-promises */ /* eslint-disable @typescript-eslint/no-floating-promises */
promiseOrStream.on('downloadProgress', progress => { promiseOrStream.on('downloadProgress', progress => {
if (progress.transferred * 1000 > bodyKBLimit && progress.percent !== 1) { if (progress.transferred > bodyLimit && progress.percent !== 1) {
promiseOrStream.cancel(`Exceeded the download limit of ${bodyKBLimit} bytes`) const message = `Exceeded the download limit of ${bodyLimit} B`
logger.warn(message)
// CancelableRequest
if (promiseOrStream.cancel) {
promiseOrStream.cancel()
return
}
// Stream
(promiseOrStream as any).destroy()
} }
}) })
@ -177,5 +189,5 @@ function buildRequestError (error: any) {
error.responseBody = error.response.body error.responseBody = error.response.body
} }
return newError return error
} }