Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

fix(backend:sync): limit gzip diff body size

johaven65acac184daf
diff --git a/backend/src/applications/sync/interceptors/sync-diff-gzip-body.interceptor.spec.ts b/backend/src/applications/sync/interceptors/sync-diff-gzip-body.interceptor.spec.ts
index a9a9823c..7c8ec17a 100644
--- a/backend/src/applications/sync/interceptors/sync-diff-gzip-body.interceptor.spec.ts
+++ b/backend/src/applications/sync/interceptors/sync-diff-gzip-body.interceptor.spec.ts
@@ -2,8 +2,15 @@ import { CallHandler, ExecutionContext, HttpException, HttpStatus } from '@nestj
import { Test, TestingModule } from '@nestjs/testing'
import { lastValueFrom, of } from 'rxjs'
import { Readable } from 'stream'
-import zlib from 'zlib'
-import { SyncDiffGzipBodyInterceptor } from './sync-diff-gzip-body.interceptor'
+import zlib from 'node:zlib'
+import {
+ MAX_COMPRESSED_SYNC_DIFF_BODY_SIZE,
+ MAX_DECOMPRESSED_SYNC_DIFF_BODY_SIZE,
+ SyncDiffGzipBodyInterceptor
+} from './sync-diff-gzip-body.interceptor'
+
+const EXPECTED_MAX_COMPRESSED_SYNC_DIFF_BODY_SIZE = 25 * 1024 * 1024
+const EXPECTED_MAX_DECOMPRESSED_SYNC_DIFF_BODY_SIZE = 50 * 1024 * 1024
describe('SyncDiffGzipBodyInterceptor', () => {
let interceptor: SyncDiffGzipBodyInterceptor
@@ -37,6 +44,15 @@ describe('SyncDiffGzipBodyInterceptor', () => {
interceptor = module.get(SyncDiffGzipBodyInterceptor)
})
+ afterEach(() => {
+ vi.restoreAllMocks()
+ })
+
+ it('should keep sync diff gzip body limits aligned with expected sizes', () => {
+ expect(MAX_COMPRESSED_SYNC_DIFF_BODY_SIZE).toBe(EXPECTED_MAX_COMPRESSED_SYNC_DIFF_BODY_SIZE)
+ expect(MAX_DECOMPRESSED_SYNC_DIFF_BODY_SIZE).toBe(EXPECTED_MAX_DECOMPRESSED_SYNC_DIFF_BODY_SIZE)
+ })
+
it('should gunzip and parse JSON body when Content-Encoding is gzip', async () => {
const originalBody = { a: 1, b: 'two' }
const gzipped = zlib.gzipSync(Buffer.from(JSON.stringify(originalBody)))
@@ -86,6 +102,70 @@ describe('SyncDiffGzipBodyInterceptor', () => {
expect(next.handle).not.toHaveBeenCalled()
})
+ it('should reject gzip body when compressed payload is too large', async () => {
+ const req: any = {
+ headers: { 'content-encoding': 'gzip' },
+ raw: createReadableFrom(Buffer.alloc(EXPECTED_MAX_COMPRESSED_SYNC_DIFF_BODY_SIZE + 1))
+ }
+ const ctx = createExecutionContextWithRequest(req)
+ const next = createCallHandler()
+
+ let error: unknown
+ try {
+ await interceptor.intercept(ctx, next)
+ } catch (e) {
+ error = e
+ }
+ expect(error).toBeInstanceOf(HttpException)
+ const ex = error as HttpException
+ expect(ex.getStatus()).toBe(HttpStatus.PAYLOAD_TOO_LARGE)
+ expect(ex.getResponse()).toBe('Gzip body is too large')
+ expect(next.handle).not.toHaveBeenCalled()
+ })
+
+ it('should pass decompressed output limit to gunzip', async () => {
+ const gzipped = zlib.gzipSync(Buffer.from(JSON.stringify({ ok: true })))
+ const gunzip = vi.spyOn(zlib, 'gunzip').mockImplementationOnce(((_body, options, callback) => {
+ expect(options).toMatchObject({ maxOutputLength: MAX_DECOMPRESSED_SYNC_DIFF_BODY_SIZE })
+ callback(null, Buffer.from(JSON.stringify({ ok: true })))
+ }) as typeof zlib.gunzip)
+ const req: any = {
+ headers: { 'content-encoding': 'gzip' },
+ raw: createReadableFrom(gzipped)
+ }
+ const ctx = createExecutionContextWithRequest(req)
+ const next = createCallHandler()
+
+ await interceptor.intercept(ctx, next)
+
+ expect(gunzip).toHaveBeenCalledTimes(1)
+ expect(next.handle).toHaveBeenCalledTimes(1)
+ })
+
+ it('should throw Payload Too Large when decompressed gzip body exceeds the limit', async () => {
+ vi.spyOn(zlib, 'gunzip').mockImplementationOnce(((_body, _options, callback) => {
+ callback(new Error('maxOutputLength exceeded'), null)
+ }) as typeof zlib.gunzip)
+ const req: any = {
+ headers: { 'content-encoding': 'gzip' },
+ raw: createReadableFrom(zlib.gzipSync(Buffer.from(JSON.stringify({ ok: true }))))
+ }
+ const ctx = createExecutionContextWithRequest(req)
+ const next = createCallHandler()
+
+ let error: unknown
+ try {
+ await interceptor.intercept(ctx, next)
+ } catch (e) {
+ error = e
+ }
+ expect(error).toBeInstanceOf(HttpException)
+ const ex = error as HttpException
+ expect(ex.getStatus()).toBe(HttpStatus.PAYLOAD_TOO_LARGE)
+ expect(ex.getResponse()).toBe('Invalid gzip body')
+ expect(next.handle).not.toHaveBeenCalled()
+ })
+
it('should throw BadRequest when decoded JSON is invalid', async () => {
// gzip-compressed invalid JSON (plain text)
const gzippedInvalidJson = zlib.gzipSync(Buffer.from('not-json'))
@@ -96,15 +176,16 @@ describe('SyncDiffGzipBodyInterceptor', () => {
const ctx = createExecutionContextWithRequest(req)
const next = createCallHandler()
+ let error: unknown
try {
await interceptor.intercept(ctx, next)
- fail('Expected interceptor to throw for invalid JSON')
} catch (e) {
- expect(e).toBeInstanceOf(HttpException)
- const ex = e as HttpException
- expect(ex.getStatus()).toBe(HttpStatus.BAD_REQUEST)
- expect(String(ex.getResponse())).toContain('Invalid JSON')
- expect(next.handle).not.toHaveBeenCalled()
+ error = e
}
+ expect(error).toBeInstanceOf(HttpException)
+ const ex = error as HttpException
+ expect(ex.getStatus()).toBe(HttpStatus.BAD_REQUEST)
+ expect(String(ex.getResponse())).toContain('Invalid JSON')
+ expect(next.handle).not.toHaveBeenCalled()
})
})
diff --git a/backend/src/applications/sync/interceptors/sync-diff-gzip-body.interceptor.ts b/backend/src/applications/sync/interceptors/sync-diff-gzip-body.interceptor.ts
index b4f63eae..a34767c5 100644
--- a/backend/src/applications/sync/interceptors/sync-diff-gzip-body.interceptor.ts
+++ b/backend/src/applications/sync/interceptors/sync-diff-gzip-body.interceptor.ts
@@ -1,19 +1,22 @@
import { CallHandler, ExecutionContext, HttpException, HttpStatus, Injectable, NestInterceptor } from '@nestjs/common'
import { FastifyRequest } from 'fastify'
-import { buffer } from 'node:stream/consumers'
+import { IncomingMessage } from 'node:http'
import zlib from 'node:zlib'
import { Observable } from 'rxjs'
+export const MAX_COMPRESSED_SYNC_DIFF_BODY_SIZE = 25 * 1024 * 1024
+export const MAX_DECOMPRESSED_SYNC_DIFF_BODY_SIZE = 50 * 1024 * 1024
+
@Injectable()
export class SyncDiffGzipBodyInterceptor implements NestInterceptor {
async intercept(context: ExecutionContext, next: CallHandler): Promise<Observable<any>> {
const req: FastifyRequest = context.switchToHttp().getRequest()
if (req.headers['content-encoding'] === 'gzip') {
- const body: Buffer<ArrayBufferLike> = await buffer(req.raw)
+ const body: Buffer<ArrayBufferLike> = await this.readBody(req.raw)
await new Promise<void>((resolve, reject) => {
- zlib.gunzip(body, (err: Error, decoded: Buffer<ArrayBufferLike>) => {
+ zlib.gunzip(body, { maxOutputLength: MAX_DECOMPRESSED_SYNC_DIFF_BODY_SIZE }, (err: Error, decoded: Buffer<ArrayBufferLike>) => {
if (err) {
- return reject(new HttpException('Invalid gzip body', HttpStatus.BAD_REQUEST))
+ return reject(new HttpException('Invalid gzip body', this.gzipErrorStatus(err)))
}
try {
req.body = JSON.parse(decoded.toString())
@@ -26,4 +29,40 @@ export class SyncDiffGzipBodyInterceptor implements NestInterceptor {
}
return next.handle()
}
+
+ private readBody(raw: IncomingMessage): Promise<Buffer<ArrayBufferLike>> {
+ return new Promise((resolve, reject) => {
+ const chunks: Buffer[] = []
+ let size = 0
+ let bodyTooLarge = false
+
+ raw.on('data', (chunk: Buffer) => {
+ if (bodyTooLarge) {
+ return
+ }
+ size += chunk.length
+ if (size > MAX_COMPRESSED_SYNC_DIFF_BODY_SIZE) {
+ bodyTooLarge = true
+ reject(new HttpException('Gzip body is too large', HttpStatus.PAYLOAD_TOO_LARGE))
+ raw.destroy()
+ return
+ }
+ chunks.push(chunk)
+ })
+ raw.on('end', () => {
+ if (!bodyTooLarge) {
+ resolve(Buffer.concat(chunks))
+ }
+ })
+ raw.on('error', (e) => {
+ if (!bodyTooLarge) {
+ reject(e)
+ }
+ })
+ })
+ }
+
+ private gzipErrorStatus(err: Error): HttpStatus {
+ return err.message.includes('maxOutputLength') || err.message.includes('larger than') ? HttpStatus.PAYLOAD_TOO_LARGE : HttpStatus.BAD_REQUEST
+ }
}