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:webdav): treat PUT requests as binary streams to avoid body parsing
johavenedc291ccc634
diff --git a/backend/src/app.bootstrap.ts b/backend/src/app.bootstrap.tsindex 574cc7b6..98ca7145 100644--- a/backend/src/app.bootstrap.ts+++ b/backend/src/app.bootstrap.ts@@ -10,12 +10,11 @@ import multipart from '@fastify/multipart'import { ClassSerializerInterceptor, ValidationPipe } from '@nestjs/common'import { NestFactory, Reflector } from '@nestjs/core'import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify'-import { FastifyRequest } from 'fastify'+import { FastifyInstance, FastifyRequest } from 'fastify'import { Logger, LoggerErrorInterceptor } from 'nestjs-pino'import { CONTENT_SECURITY_POLICY } from './app.constants'import { AppModule } from './app.module'-import { HTTP_WEBDAV_METHOD } from './applications/applications.constants'-import { WEBDAV_NS, WEBDAV_SPACES } from './applications/webdav/constants/routes'+import { bootstrapWebDAV } from './applications/webdav/utils/bootstrap'import { IS_TEST_ENV, STATIC_PATH } from './configuration/config.constants'import { configuration } from './configuration/config.environment'import { WebSocketAdapter } from './infrastructure/websocket/adapters/web-socket.adapter'@@ -39,30 +38,18 @@ export async function appBootstrap(): Promise<NestFastifyApplication> {app.enableShutdownHooks()/* Fastify instance */- const fastifyInstance = fastifyAdapter.getInstance()+ const fastifyInstance: FastifyInstance = fastifyAdapter.getInstance()/* LOGGER */app.useLogger(IS_TEST_ENV ? ['fatal'] : app.get(Logger))+ /* WEBDAV BOOTSTRAP RULES */+ bootstrapWebDAV(app, fastifyInstance)+/* PARSER */- // xml body parser is used for webdav methods- app.useBodyParser(['application/xml', 'text/xml'])- // add webdav methods- for (const method of Object.values(HTTP_WEBDAV_METHOD)) {- fastifyInstance.addHttpMethod(method, { hasBody: true })- }- // '*' body parser allow binary data as stream (unlimited body size)+ // '*' body parser allow binary data as a stream (unlimited body size)fastifyInstance.addContentTypeParser('*', { bodyLimit: 0 }, (_req: FastifyRequest, _payload: FastifyRequest['raw'], done) => done(null))- // Joplin clients send incorrect `Content-Type` headers when syncing over WebDAV (issue: https://github.com/laurent22/joplin/issues/122499)- // This hook intercepts matching requests and sets `application/octet-stream` to ensure compatibility and successful sync.- // todo: remove it when fixed on Joplin side- fastifyInstance.addHook('onRequest', async (req, _reply) => {- if ((req.headers['user-agent'] || '').indexOf('Joplin') !== -1 && req.originalUrl.startsWith(WEBDAV_SPACES[WEBDAV_NS.WEBDAV].route)) {- req.headers['content-type'] = 'application/octet-stream'- }- })-/* INTERCEPTORS */app.useGlobalInterceptors(new LoggerErrorInterceptor(),@@ -70,6 +57,7 @@ export async function appBootstrap(): Promise<NestFastifyApplication> {excludePrefixes: ['_']}))+/* VALIDATION */app.useGlobalPipes(new ValidationPipe({ transform: true, whitelist: true }))diff --git a/backend/src/applications/users/users.e2e-spec.ts b/backend/src/applications/users/users.e2e-spec.tsindex dde16ca2..5c4b8fd4 100644--- a/backend/src/applications/users/users.e2e-spec.ts+++ b/backend/src/applications/users/users.e2e-spec.ts@@ -8,7 +8,7 @@ import { NestFastifyApplication } from '@nestjs/platform-fastify'import { appBootstrap } from '../../app.bootstrap'import { TokenResponseDto } from '../../authentication/dto/token-response.dto'import { AuthManager } from '../../authentication/services/auth-manager.service'-import { dbCheckConnection, dbCloseConnection } from '../../infrastructure/database/utils'+import { dbCheckConnection } from '../../infrastructure/database/utils'import { API_USERS_AVATAR, API_USERS_ME } from './constants/routes'import { USER_ROLE } from './constants/user'import { DeleteUserDto } from './dto/delete-user.dto'@@ -36,7 +36,6 @@ describe('Users (e2e)', () => {await expect(adminUsersManager.deleteUserOrGuest(userTest.id, userTest.login, { deleteSpace: true, isGuest: false } satisfies DeleteUserDto)).resolves.not.toThrow()- await dbCloseConnection(app)await app.close()})diff --git a/backend/src/applications/webdav/constants/webdav.ts b/backend/src/applications/webdav/constants/webdav.tsindex a7fe4cf1..ab22001c 100644--- a/backend/src/applications/webdav/constants/webdav.ts+++ b/backend/src/applications/webdav/constants/webdav.ts@@ -14,6 +14,7 @@ export const NS_PREFIX = 'D'export const LOCK_PREFIX = 'urn:uuid:'export const XML_CONTENT_TYPE = 'application/xml; charset=utf-8'export const HTML_CONTENT_TYPE = 'text/html; charset=utf-8'+export const WEBDAV_CONTENT_TYPES = ['application/xml', 'text/xml']export const WEBDAV_APP_LOCK = 'WebDAV' as constexport const ALLOWED_WEBDAV_METHODS: string = [diff --git a/backend/src/applications/webdav/utils/bootstrap.ts b/backend/src/applications/webdav/utils/bootstrap.tsnew file mode 100644index 00000000..f39eb1d9--- /dev/null+++ b/backend/src/applications/webdav/utils/bootstrap.ts@@ -0,0 +1,50 @@+/*+ * Copyright (C) 2012-2026 Johan Legrand <[email protected]>+ * This file is part of Sync-in | The open source file sync and share solution+ * See the LICENSE file for licensing details+ */++import { NestFastifyApplication } from '@nestjs/platform-fastify'+import { FastifyInstance } from 'fastify'+import { HTTP_METHOD, HTTP_WEBDAV_METHOD } from '../../applications.constants'+import { WEBDAV_NS, WEBDAV_SPACES } from '../constants/routes'+import { WEBDAV_CONTENT_TYPES } from '../constants/webdav'++/**+ * Bootstrap WebDAV-specific Nest/Fastify configuration.+ *+ * - Enables XML body parsing for WebDAV XML-based methods (PROPFIND/PROPPATCH/etc.)+ * - Registers additional WebDAV HTTP methods+ * - Forces binary Content-Type ONLY for WebDAV PUT requests (file uploads)+ *+ * Per RFC 4918, PUT is used to create/replace the content of a resource and the+ * request body must be treated as an opaque stream (even if the file is JSON/XML).+ */+export function bootstrapWebDAV(app: NestFastifyApplication, fastifyInstance: FastifyInstance) {+ // Enable XML body parser for WebDAV XML requests (PROPFIND, PROPPATCH, LOCK, etc.)+ app.useBodyParser(WEBDAV_CONTENT_TYPES)++ // Register WebDAV-specific HTTP methods+ for (const method of Object.values(HTTP_WEBDAV_METHOD)) {+ fastifyInstance.addHttpMethod(method, { hasBody: true })+ }++ /**+ * onRequest hook for WebDAV uploads.+ *+ * WebDAV clients may send JSON/XML files with a Content-Type that triggers+ * Fastify's body parsers (application/json, text/plain, application/xml...).+ * For PUT uploads we must always treat the payload as a raw binary stream.+ *+ * This hook forces `application/octet-stream` for WebDAV PUT requests only,+ * leaving XML-based WebDAV methods unaffected.+ */+ fastifyInstance.addHook('onRequest', (req, _reply, done) => {+ // Only handle WebDAV PUT requests under the WebDAV base path+ if (req.method !== HTTP_METHOD.PUT) return done()+ if (!req.originalUrl.startsWith(WEBDAV_SPACES[WEBDAV_NS.WEBDAV].route)) return done()++ req.headers['content-type'] = 'application/octet-stream'+ return done()+ })+}diff --git a/backend/src/applications/webdav/webdav.controller.ts b/backend/src/applications/webdav/webdav.controller.tsindex 75027345..83d371bd 100644--- a/backend/src/applications/webdav/webdav.controller.ts+++ b/backend/src/applications/webdav/webdav.controller.ts@@ -4,7 +4,7 @@* See the LICENSE file for licensing details*/-import { All, Controller, HttpStatus, Options, Param, Propfind, Req, Res, UseGuards } from '@nestjs/common'+import { All, Controller, HttpStatus, Options, Param, Propfind, Req, Res, StreamableFile, UseGuards } from '@nestjs/common'import { FastifyReply } from 'fastify'import { HTTP_METHOD } from '../applications.constants'import { SPACE_REPOSITORY } from '../spaces/constants/spaces'@@ -20,41 +20,45 @@ export class WebDAVController {constructor(private readonly webdavMethods: WebDAVMethods) {}@Options()- serverOptions() {+ serverOptions(): void {// OPTIONS method is handled in the `WebDAVProtocolGuard`, return empty response with headersreturn}@Propfind()- serverPropFind(@Req() req: FastifyDAVRequest, @Res({ passthrough: true }) res: FastifyReply) {+ serverPropFind(@Req() req: FastifyDAVRequest, @Res({ passthrough: true }) res: FastifyReply): Promise<string> {return this.webdavMethods.propfind(req, res, WEBDAV_NS.SERVER)}@Options(WEBDAV_BASE_PATH)- webdavOptions() {+ webdavOptions(): void {// OPTIONS method is handled in the `WebDAVProtocolGuard`, return empty response with headersreturn}@Propfind(WEBDAV_BASE_PATH)- async webdavPropfind(@Req() req: FastifyDAVRequest, @Res({ passthrough: true }) res: FastifyReply) {+ async webdavPropfind(@Req() req: FastifyDAVRequest, @Res({ passthrough: true }) res: FastifyReply): Promise<string> {return this.webdavMethods.propfind(req, res, WEBDAV_NS.WEBDAV)}@Options(`${WEBDAV_BASE_PATH}/:repository(^(${WEBDAV_NS.SPACES}|${WEBDAV_NS.TRASH})$)`)- repositoriesOptions() {+ repositoriesOptions(): void {// OPTIONS method is handled in the `WebDAVProtocolGuard`return}@Propfind(`${WEBDAV_BASE_PATH}/:repository(^(${WEBDAV_NS.SPACES}|${WEBDAV_NS.TRASH})$)`)- async repositoriesPropfind(@Req() req: FastifyDAVRequest, @Res({ passthrough: true }) res: FastifyReply, @Param('repository') repository: string) {+ async repositoriesPropfind(+ @Req() req: FastifyDAVRequest,+ @Res({ passthrough: true }) res: FastifyReply,+ @Param('repository') repository: string+ ): Promise<string> {return this.webdavMethods.propfind(req, res, repository)}@All(`${WEBDAV_BASE_PATH}/*`)@UseGuards(SpaceGuard)- async files(@Req() req: FastifyDAVRequest, @Res({ passthrough: true }) res: FastifyReply) {+ async files(@Req() req: FastifyDAVRequest, @Res({ passthrough: true }) res: FastifyReply): Promise<string | StreamableFile> {// OPTIONS method is handled in the `WebDAVProtocolGuard`switch (req.method) {case HTTP_METHOD.PROPFIND:diff --git a/backend/src/applications/webdav/webdav.e2e-spec.ts b/backend/src/applications/webdav/webdav.e2e-spec.tsindex 5f0cd721..e169ffcb 100644--- a/backend/src/applications/webdav/webdav.e2e-spec.ts+++ b/backend/src/applications/webdav/webdav.e2e-spec.ts@@ -6,7 +6,6 @@import { NestFastifyApplication } from '@nestjs/platform-fastify'import { appBootstrap } from '../../app.bootstrap'-import { dbCloseConnection } from '../../infrastructure/database/utils'import { XML_CONTENT_TYPE } from './constants/webdav'const XML_VERSION_STR = '<?xml version="1.0" encoding="utf-8" standalone="yes"?>'@@ -21,7 +20,6 @@ describe('WebDAV (e2e)', () => {})afterAll(async () => {- await dbCloseConnection(app)await app.close()})@@ -62,4 +60,126 @@ describe('WebDAV (e2e)', () => {} as any)expect(res.statusCode).toEqual(207)})++ describe('PUT with non-XML Content-Types (stream preservation)', () => {+ const testFilePath = '/webdav/personal/test-content-type.txt'+ const auth = 'Basic am86cGFzc3dvcmQ='++ afterEach(async () => {+ // Cleanup: delete the test file if it exists+ await app.inject({+ method: 'DELETE',+ url: testFilePath,+ headers: { authorization: auth }+ } as any)+ })++ it('PUT with application/json should preserve stream and create file with content', async () => {+ const jsonContent = '{"key":"value","number":42}'++ const putRes = await app.inject({+ method: 'PUT',+ url: testFilePath,+ headers: {+ authorization: auth,+ 'content-type': 'application/json'+ },+ body: jsonContent+ } as any)++ expect([201, 204]).toContain(putRes.statusCode)++ // Verify the file was created with the correct content+ const getRes = await app.inject({+ method: 'GET',+ url: testFilePath,+ headers: { authorization: auth }+ } as any)++ expect(getRes.statusCode).toEqual(200)+ expect(getRes.body).toEqual(jsonContent)+ expect(getRes.headers['content-length']).toEqual(String(jsonContent.length))+ })++ it('PUT with text/plain should preserve stream and create file with content', async () => {+ const textContent = 'This is plain text content with special chars: éàù'++ const putRes = await app.inject({+ method: 'PUT',+ url: testFilePath,+ headers: {+ authorization: auth,+ 'content-type': 'text/plain'+ },+ body: textContent+ } as any)++ expect([201, 204]).toContain(putRes.statusCode)++ // Verify the file was created with the correct content+ const getRes = await app.inject({+ method: 'GET',+ url: testFilePath,+ headers: { authorization: auth }+ } as any)++ expect(getRes.statusCode).toEqual(200)+ expect(getRes.body).toEqual(textContent)+ expect(getRes.headers['content-length']).toEqual(String(Buffer.byteLength(textContent, 'utf8')))+ })++ it('PUT with text/plain; charset=utf-8 should preserve stream and create file with content', async () => {+ const textContent = 'Text with charset and emoji: 🚀 ✅'++ const putRes = await app.inject({+ method: 'PUT',+ url: testFilePath,+ headers: {+ authorization: auth,+ 'content-type': 'text/plain; charset=utf-8'+ },+ body: textContent+ } as any)++ expect([201, 204]).toContain(putRes.statusCode)++ // Verify the file was created with the correct content+ const getRes = await app.inject({+ method: 'GET',+ url: testFilePath,+ headers: { authorization: auth }+ } as any)++ expect(getRes.statusCode).toEqual(200)+ expect(getRes.body).toEqual(textContent)+ expect(getRes.headers['content-length']).toEqual(String(Buffer.byteLength(textContent, 'utf8')))+ })++ it('PUT with application/octet-stream should work as expected', async () => {+ const binaryContent = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])++ const putRes = await app.inject({+ method: 'PUT',+ url: testFilePath,+ headers: {+ authorization: auth,+ 'content-type': 'application/octet-stream'+ },+ body: binaryContent+ } as any)++ expect([201, 204]).toContain(putRes.statusCode)++ // Verify the file was created with the correct content+ const getRes = await app.inject({+ method: 'GET',+ url: testFilePath,+ headers: { authorization: auth }+ } as any)++ expect(getRes.statusCode).toEqual(200)+ expect(Buffer.from(getRes.rawPayload)).toEqual(binaryContent)+ expect(getRes.headers['content-length']).toEqual(String(binaryContent.length))+ })+ })})