Skip to content

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

fix(backend:security): prevent stored XSS by serving files with `Content-Disposition: attachment` to avoid arbitrary JavaScript execution in the browser

johavena6276d067725
diff --git a/backend/src/applications/files/services/files-manager.service.ts b/backend/src/applications/files/services/files-manager.service.ts
index 98c93673..28aa1e72 100644
--- a/backend/src/applications/files/services/files-manager.service.ts
+++ b/backend/src/applications/files/services/files-manager.service.ts
@@ -80,8 +80,8 @@ export class FilesManager {
public readonly filesLockManager: FilesLockManager
) {}
- sendFileFromSpace(space: SpaceEnv, asAttachment = false, downloadName = ''): SendFile {
- return new SendFile(space.realPath, asAttachment, downloadName)
+ sendFileFromSpace(space: SpaceEnv, downloadName = ''): SendFile {
+ return new SendFile(space.realPath, downloadName)
}
async saveStream(
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 395e8c52..3d93a0fc 100644
--- a/backend/src/applications/files/services/files-tasks-manager.service.ts
+++ b/backend/src/applications/files/services/files-tasks-manager.service.ts
@@ -104,7 +104,7 @@ export class FilesTasksManager {
throw new HttpException('Not applicable', HttpStatus.BAD_REQUEST)
}
const rPath = path.join(user.tasksPath, task.name)
- const sendFile = new SendFile(rPath, true)
+ const sendFile = new SendFile(rPath)
try {
await sendFile.checks()
} catch (e) {
diff --git a/backend/src/applications/files/utils/send-file.ts b/backend/src/applications/files/utils/send-file.ts
index d8a15f01..3e4a095f 100644
--- a/backend/src/applications/files/utils/send-file.ts
+++ b/backend/src/applications/files/utils/send-file.ts
@@ -11,12 +11,16 @@ import { DEFAULT_HIGH_WATER_MARK } from '../constants/files'
import { FileError } from '../models/file-error'
import { fileName, isPathExists, isPathIsDir, isPathIsReadable } from './files'
+export function makeContentDispositionAttachment(fileName: string) {
+ const downloadName = fileName.normalize('NFD').replace(/[\u0300-\u036f]/g, '')
+ return `attachment; filename="${downloadName}";filename*=UTF-8''${fileName}`
+}
+
export class SendFile {
private fileName: string
constructor(
private readonly filePath: string,
- private readonly asAttachment = true,
private readonly downloadName = '',
private readonly sendOptions: SendOptions = {
acceptRanges: true,
@@ -53,10 +57,8 @@ export class SendFile {
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''${this.fileName}`
- }
+ // Force file to be downloaded as an attachment
+ sendResult.headers['content-disposition'] = makeContentDispositionAttachment(this.fileName)
res.headers(sendResult.headers)
res.status(sendResult.statusCode)
// sendStream.once('stream', () => console.log(`Sending: ${this.fileName}`))
diff --git a/backend/src/applications/links/services/links-manager.service.ts b/backend/src/applications/links/services/links-manager.service.ts
index 120f1723..62ec8def 100644
--- a/backend/src/applications/links/services/links-manager.service.ts
+++ b/backend/src/applications/links/services/links-manager.service.ts
@@ -62,7 +62,7 @@ export class LinksManager {
this.logger.log(`${this.linkAccess.name} - *${user.login}* (${user.id}) downloading ${spaceLink.share.name}`)
this.incrementLinkNbAccess(link)
const spaceEnv: SpaceEnv = await this.spaceEnvFromLink(user, spaceLink)
- const sendFile: SendFile = this.filesManager.sendFileFromSpace(spaceEnv, true, spaceLink.share.name)
+ const sendFile: SendFile = this.filesManager.sendFileFromSpace(spaceEnv, spaceLink.share.name)
try {
await sendFile.checks()
return await sendFile.stream(req, res)
diff --git a/backend/src/applications/users/users.controller.ts b/backend/src/applications/users/users.controller.ts
index fc87367d..50e9c0fb 100755
--- a/backend/src/applications/users/users.controller.ts
+++ b/backend/src/applications/users/users.controller.ts
@@ -9,6 +9,7 @@ import { createReadStream } from 'fs'
import { LoginResponseDto } from '../../authentication/dto/login-response.dto'
import { AuthTwoFaGuardWithoutPassword } from '../../authentication/guards/auth-two-fa-guard'
import { FastifyAuthenticatedRequest } from '../../authentication/interfaces/auth-request.interface'
+import { makeContentDispositionAttachment } from '../files/utils/send-file'
import { USERS_ROUTE } from './constants/routes'
import { USER_PERMISSION, USER_ROLE } from './constants/user'
import { UserHavePermission } from './decorators/permissions.decorator'
@@ -27,6 +28,7 @@ import { Member } from './interfaces/member.interface'
import { UserAppPassword } from './interfaces/user-secrets.interface'
import { UserModel } from './models/user.model'
import { UsersManager } from './services/users-manager.service'
+import { USER_AVATAR_FILE_NAME } from './utils/avatar'
@Controller(USERS_ROUTE.BASE)
@UseGuards(UserRolesGuard)
@@ -90,7 +92,7 @@ export class UsersController {
async avatar(@GetUser() user: UserModel, @Param('login') login: 'me' | string): Promise<StreamableFile> {
const isMe: boolean = login === 'me'
const [path, mime] = await this.usersManager.getAvatar(isMe ? user.login : login, false, isMe && user.role <= USER_ROLE.USER)
- return new StreamableFile(createReadStream(path), { type: mime })
+ return new StreamableFile(createReadStream(path), { type: mime, disposition: makeContentDispositionAttachment(USER_AVATAR_FILE_NAME) })
}
@Put(`${USERS_ROUTE.ME}/${USERS_ROUTE.AVATAR}`)