Merge pull request #14827 from overleaf/bg-size-limit-for-all-files

add size limit for all streamed files in web

GitOrigin-RevId: 65ab73163bc94a643977f8a0a1fd7418bbf6e365
This commit is contained in:
Brian Gough 2023-09-20 08:47:06 +01:00 committed by Copybot
parent c8c2f661b3
commit 67ec78f7c6
3 changed files with 27 additions and 2 deletions

View file

@ -34,7 +34,10 @@ const {
RemoteServiceError,
FileCannotRefreshError,
} = require('./LinkedFilesErrors')
const { OutputFileFetchFailedError } = require('../Errors/Errors')
const {
OutputFileFetchFailedError,
FileTooLargeError,
} = require('../Errors/Errors')
const Modules = require('../../infrastructure/Modules')
const { plainTextResponse } = require('../../infrastructure/Response')
@ -209,6 +212,9 @@ module.exports = LinkedFilesController = {
} else if (/\bECONNREFUSED\b/.test(error.message)) {
res.status(500)
plainTextResponse(res, 'Importing references is not currently available')
} else if (error instanceof FileTooLargeError) {
res.status(422)
plainTextResponse(res, 'File too large')
} else {
next(error)
}

View file

@ -27,7 +27,7 @@ class SizeLimitedStream extends Transform {
super(options)
this.bytes = 0
this.maxSizeBytes = options.maxSizeBytes
this.maxSizeBytes = options.maxSizeBytes || Settings.maxUploadSize
this.drain = false
this.on('error', () => {
this.drain = true

View file

@ -16,6 +16,8 @@ LinkedUrlProxy.get('/', (req, res, next) => {
return plainTextResponse(res, 'foo foo foo')
} else if (req.query.url === 'http://example.com/bar') {
return plainTextResponse(res, 'bar bar bar')
} else if (req.query.url === 'http://example.com/large') {
return plainTextResponse(res, 'x'.repeat(Settings.maxUploadSize + 1))
} else {
return res.sendStatus(404)
}
@ -319,6 +321,23 @@ describe('LinkedFiles', function () {
expect(body).to.equal('bar bar bar')
})
it('should return an error if the file exceeds the maximum size', async function () {
// download does not succeed
const { response, body } = await owner.doRequest('post', {
url: `/project/${projectOneId}/linked_file`,
json: {
provider: 'url',
data: {
url: 'http://example.com/large',
},
parent_folder_id: projectOneRootFolderId,
name: 'url-large-file-1',
},
})
expect(response.statusCode).to.equal(422)
expect(body).to.equal('File too large')
})
it("should return an error if the file can't be downloaded", async function () {
// download does not succeed
let { response, body } = await owner.doRequest('post', {