Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.
TypeScript cloud-alternativecollaborationcollaborative-editingdropboxfile-serverfile-sharingfile-storagefile-syncfull-text-searchhomelabnextcloudon-premiseopen-sourceprivacy-firstprivate-cloudrealtimesecureself-hostedstorage-serverwebdav
fix(backend:files): ensure HTTP/1 uploads close on quota or size errors
johaven795146538eb6
diff --git a/backend/src/app.bootstrap.ts b/backend/src/app.bootstrap.tsindex 204e9ec2..39c52c2a 100644--- a/backend/src/app.bootstrap.ts+++ b/backend/src/app.bootstrap.ts@@ -42,7 +42,8 @@ export async function appBootstrap(): Promise<NestFastifyApplication> {/* PARSER */// Keep unknown binary payloads available through req.raw.- // Fastify's bodyLimit still protects buffered parsers; upload consumers meter this raw stream.+ // This parser does not consume or meter the payload: bodyLimit only protects buffered parsers,+ // while raw and multipart consumers meter their own streams.fastifyInstance.addContentTypeParser('*', (_req: FastifyRequest, _payload: FastifyRequest['raw'], done) => done(null))/* INTERCEPTORS */diff --git a/backend/src/applications/files/files.controller.spec.ts b/backend/src/applications/files/files.controller.spec.tsindex 665d5a95..d09740da 100644--- a/backend/src/applications/files/files.controller.spec.ts+++ b/backend/src/applications/files/files.controller.spec.ts@@ -103,10 +103,10 @@ describe(FilesController.name, () => {expect(filesMethodsMock.make).toHaveBeenCalledWith(fakeUser, fakeSpace, dto)})- it('upload() should call filesMethods.upload(req)', async () => {- await filesController.uploadCreate(fakeReq)+ it('upload() should call filesMethods.upload(req, res)', async () => {+ await filesController.uploadCreate(fakeReq, fakeRes)- expect(filesMethodsMock.upload).toHaveBeenCalledWith(fakeReq)+ expect(filesMethodsMock.upload).toHaveBeenCalledWith(fakeReq, fakeRes)})it('copy() should call filesMethods.copy(user, space, dto) and return its result', async () => {diff --git a/backend/src/applications/files/files.controller.ts b/backend/src/applications/files/files.controller.tsindex 8764d7fd..b52e3930 100644--- a/backend/src/applications/files/files.controller.ts+++ b/backend/src/applications/files/files.controller.ts@@ -82,18 +82,18 @@ export class FilesController {}@Post(`${FILES_ROUTE.OPERATION}/${FILE_OPERATION.UPLOAD}/*`)- async uploadCreate(@Req() req: FastifySpaceRequest): Promise<void> {- return this.filesMethods.upload(req)+ async uploadCreate(@Req() req: FastifySpaceRequest, @Res({ passthrough: true }) res: FastifyReply): Promise<void> {+ return this.filesMethods.upload(req, res)}@Put(`${FILES_ROUTE.OPERATION}/${FILE_OPERATION.UPLOAD}/*`)- async uploadOverwrite(@Req() req: FastifySpaceRequest): Promise<void> {- return this.filesMethods.upload(req)+ async uploadOverwrite(@Req() req: FastifySpaceRequest, @Res({ passthrough: true }) res: FastifyReply): Promise<void> {+ return this.filesMethods.upload(req, res)}@Patch(`${FILES_ROUTE.OPERATION}/${FILE_OPERATION.UPLOAD}/*`)- async uploadContent(@Req() req: FastifySpaceRequest): Promise<void> {- return this.filesMethods.upload(req)+ async uploadContent(@Req() req: FastifySpaceRequest, @Res({ passthrough: true }) res: FastifyReply): Promise<void> {+ return this.filesMethods.upload(req, res)}@Copy(`${FILES_ROUTE.OPERATION}/*`)diff --git a/backend/src/applications/files/services/files-methods.service.ts b/backend/src/applications/files/services/files-methods.service.tsindex ffd3d8fe..168d8388 100644--- a/backend/src/applications/files/services/files-methods.service.ts+++ b/backend/src/applications/files/services/files-methods.service.ts@@ -35,11 +35,11 @@ export class FilesMethods {}}- async upload(req: FastifySpaceRequest): Promise<void> {+ async upload(req: FastifySpaceRequest, res: FastifyReply): Promise<void> {try {await this.filesManager.saveMultipart(req.user, req.space, req)} catch (e) {- this.handleError(req.space, FILE_OPERATION.UPLOAD, e)+ this.handleError(req.space, FILE_OPERATION.UPLOAD, e, undefined, undefined, res)}}@@ -183,17 +183,37 @@ export class FilesMethods {}}- private handleError(space: SpaceEnv, action: string, e: any, dstSpace?: SpaceEnv, signal?: AbortSignal): never {- if (signal && this.isCancellationError(e, signal)) {+ private handleError(+ space: SpaceEnv,+ action: FILE_OPERATION | string,+ error: unknown,+ dstSpace?: SpaceEnv,+ signal?: AbortSignal,+ res?: FastifyReply+ ): void {+ if (signal && this.isCancellationError(error, signal)) {throw signal.reason}- this.logger.error({ tag: this.handleError.name, msg: `unable to ${action} ${space.url}${dstSpace?.url ? ` -> ${dstSpace.url}` : ''} : ${e}` })+ this.logger.error({+ tag: this.handleError.name,+ msg: `unable to ${action} ${space.url}${dstSpace?.url ? ` -> ${dstSpace.url}` : ''} : ${error}`+ })+ const normalizedError = error instanceof Error ? error : new Error(String(error))// Remove the last part to avoid exposing the path- const errorMsg = e.message.split(',')[0]- if (e instanceof LockConflict) {+ const errorMsg = normalizedError.message.split(',')[0]+ if (normalizedError instanceof LockConflict) {throw new HttpException('The file is locked', HttpStatus.LOCKED)- } else if (e instanceof FileError) {- throw new HttpException(errorMsg, e.httpCode)+ } else if (normalizedError instanceof FileError) {+ // A quota or size limit may reject a multipart upload before its body is fully consumed.+ // Mark the HTTP/1 connection as non-reusable instead of draining the remaining payload.+ if (+ res &&+ res.request.raw.httpVersionMajor === 1 &&+ (normalizedError.httpCode === HttpStatus.INSUFFICIENT_STORAGE || normalizedError.httpCode === HttpStatus.PAYLOAD_TOO_LARGE)+ ) {+ void res.header('Connection', 'close')+ }+ throw new HttpException(errorMsg, normalizedError.httpCode)}throw new HttpException(errorMsg, HttpStatus.INTERNAL_SERVER_ERROR)}diff --git a/backend/src/applications/webdav/services/webdav-methods.service.spec.ts b/backend/src/applications/webdav/services/webdav-methods.service.spec.tsindex 20a5d4af..f45d02ba 100644--- a/backend/src/applications/webdav/services/webdav-methods.service.spec.ts+++ b/backend/src/applications/webdav/services/webdav-methods.service.spec.ts@@ -79,6 +79,7 @@ describe('WebDAVMethods', () => {({method: 'GET',headers: {},+ raw: { httpVersionMajor: 1 },user: { id: 1, login: 'test-user', fullName: 'Test User', email: '[email protected]', tmpPath: '/tmp/test-user' },dav: {url: '/webdav/test/file.txt',diff --git a/backend/src/applications/webdav/services/webdav-methods.service.ts b/backend/src/applications/webdav/services/webdav-methods.service.tsindex 35982d1c..eea3d7a8 100644--- a/backend/src/applications/webdav/services/webdav-methods.service.ts+++ b/backend/src/applications/webdav/services/webdav-methods.service.ts@@ -1,7 +1,7 @@import { HttpException, HttpStatus, Injectable, Logger, StreamableFile } from '@nestjs/common'import { FastifyReply } from 'fastify'import { currentTimeStamp, encodeUrl } from '../../../common/shared'-import { HTTP_VERSION } from '../../applications.constants'+import { HTTP_METHOD, HTTP_VERSION } from '../../applications.constants'import { FileLock, FileLockOptions } from '../../files/interfaces/file-lock.interface'import { FileError } from '../../files/models/file-error'import { LockConflict } from '../../files/models/file-lock-error'@@ -519,7 +519,15 @@ export class WebDAVMethods {if (e instanceof LockConflict) {return DAV_ERROR_RES(HttpStatus.LOCKED, PRECONDITION.LOCK_CONFLICT, res, e.lock.options?.lockRoot || e.lock.dbFilePath)} else if (e instanceof FileError) {- return res.status(e.httpCode).send(errorMsg)+ // A quota or size limit may reject a PUT before its body is fully consumed.+ // Mark the HTTP/1 connection as non-reusable instead of draining the remaining payload.+ const reply =+ req.method === HTTP_METHOD.PUT &&+ req.raw.httpVersionMajor === 1 &&+ (e.httpCode === HttpStatus.INSUFFICIENT_STORAGE || e.httpCode === HttpStatus.PAYLOAD_TOO_LARGE)+ ? res.header('Connection', 'close')+ : res+ return reply.status(e.httpCode).send(errorMsg)}throw new HttpException(errorMsg, HttpStatus.INTERNAL_SERVER_ERROR)}diff --git a/frontend/src/app/auth/auth.interceptor.ts b/frontend/src/app/auth/auth.interceptor.tsindex 7f9c8132..39a42088 100644--- a/frontend/src/app/auth/auth.interceptor.ts+++ b/frontend/src/app/auth/auth.interceptor.ts@@ -30,6 +30,11 @@ export class AuthInterceptor implements HttpInterceptor {if (e.status === 401) {return this.handleAuthorizationError(request, next, e)} else if (e.status === 0) {+ // Do not retry multipart uploads on a transport error: the connection may have+ // been closed after a quota or size rejection, and replaying would resend all files.+ if (request.body instanceof FormData) {+ return throwError(() => e)+ }return this.handleRetries(request, next, e)}return throwError(() => e)