Skip to content

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

fix(backend:sendfile): properly encode file paths with special characters and await call to catch errors

johaven2bf2284bb273
diff --git a/backend/src/applications/files/services/files-methods.service.ts b/backend/src/applications/files/services/files-methods.service.ts
index 87dedc4f..e5619734 100644
--- a/backend/src/applications/files/services/files-methods.service.ts
+++ b/backend/src/applications/files/services/files-methods.service.ts
@@ -33,7 +33,7 @@ export class FilesMethods {
const sendFile: SendFile = this.filesManager.sendFileFromSpace(req.space)
try {
await sendFile.checks()
- return sendFile.stream(req, res)
+ return await sendFile.stream(req, res)
} catch (e) {
this.handleError(req.space, req.method, e)
}
diff --git a/backend/src/applications/files/services/files-tasks-manager.service.ts b/backend/src/applications/files/services/files-tasks-manager.service.ts
index cdd28842..395e8c52 100644
--- a/backend/src/applications/files/services/files-tasks-manager.service.ts
+++ b/backend/src/applications/files/services/files-tasks-manager.service.ts
@@ -110,7 +110,7 @@ export class FilesTasksManager {
} catch (e) {
throw new HttpException(e.message, e.httpCode)
}
- return sendFile.stream(req, res)
+ return await sendFile.stream(req, res)
}
private async storeTask(cacheKey: string, task: FileTask) {
diff --git a/backend/src/applications/files/utils/send-file.ts b/backend/src/applications/files/utils/send-file.ts
index df8f115e..88845da3 100644
--- a/backend/src/applications/files/utils/send-file.ts
+++ b/backend/src/applications/files/utils/send-file.ts
@@ -44,11 +44,17 @@ export class SendFile {
async stream(req: FastifyRequest, res: FastifyReply): Promise<StreamableFile> {
// SendStream manages HEAD (no including body in response) & GET request (with body)
// Ranges, LastModified, Etag are also handled
- this.fileName = this.downloadName || fileName(this.filePath)
- const sendResult: SendResult = await send(req.raw, this.filePath, this.sendOptions)
+ // Send function uses decodeURIComponent, but filePath is already decoded : we need to encode it again before passing it.
+ const encodedFilePath = encodeURIComponent(this.filePath)
+ this.fileName = this.downloadName ? encodeURIComponent(this.downloadName) : fileName(encodedFilePath)
+ const sendResult: SendResult = await send(req.raw, encodedFilePath, this.sendOptions)
+ // Check if the path was correctly validated
+ if (sendResult.metadata['path'] === undefined) {
+ throw new FileError(HttpStatus.BAD_REQUEST, 'Location not found')
+ }
if (this.asAttachment) {
const downloadName = this.fileName.normalize('NFD').replace(/[\u0300-\u036f]/g, '')
- sendResult.headers['content-disposition'] = `attachment; filename="${downloadName}";filename*=UTF-8''${encodeURIComponent(this.fileName)}`
+ sendResult.headers['content-disposition'] = `attachment; filename="${downloadName}";filename*=UTF-8''${this.fileName}`
}
res.headers(sendResult.headers)
res.status(sendResult.statusCode)
diff --git a/backend/src/applications/links/services/links-manager.service.ts b/backend/src/applications/links/services/links-manager.service.ts
index 888d229e..d901fae1 100644
--- a/backend/src/applications/links/services/links-manager.service.ts
+++ b/backend/src/applications/links/services/links-manager.service.ts
@@ -64,7 +64,7 @@ export class LinksManager {
const sendFile: SendFile = this.filesManager.sendFileFromSpace(spaceEnv, true, spaceLink.share.name)
try {
await sendFile.checks()
- return sendFile.stream(req, res)
+ return await sendFile.stream(req, res)
} catch (e) {
this.logger.error(`${this.linkAccess.name} - unable to send file : ${e}`)
throw new HttpException('Unable to download file', HttpStatus.INTERNAL_SERVER_ERROR)
diff --git a/backend/src/applications/sync/services/sync-manager.service.ts b/backend/src/applications/sync/services/sync-manager.service.ts
index ed8f23d9..5e83c726 100644
--- a/backend/src/applications/sync/services/sync-manager.service.ts
+++ b/backend/src/applications/sync/services/sync-manager.service.ts
@@ -44,7 +44,7 @@ export class SyncManager {
const sendFile: SendFile = this.filesManager.sendFileFromSpace(req.space)
try {
await sendFile.checks()
- return sendFile.stream(req, res)
+ return await sendFile.stream(req, res)
} catch (e) {
this.handleError(req.space, req.method, e)
}
diff --git a/backend/src/applications/webdav/services/webdav-methods.service.ts b/backend/src/applications/webdav/services/webdav-methods.service.ts
index f797d7a6..f189b38c 100644
--- a/backend/src/applications/webdav/services/webdav-methods.service.ts
+++ b/backend/src/applications/webdav/services/webdav-methods.service.ts
@@ -52,7 +52,7 @@ export class WebDAVMethods {
const sendFile = this.filesManager.sendFileFromSpace(req.space)
try {
await sendFile.checks()
- return sendFile.stream(req, res)
+ return await sendFile.stream(req, res)
} catch (e) {
return this.handleError(req, res, e)
}