Skip to content

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

fix(backend:files): centralize path containment checks

johavene96c3f1273e0
diff --git a/backend/src/applications/files/services/files-manager.service.ts b/backend/src/applications/files/services/files-manager.service.ts
index b1ee9b5e..c8dfd0a5 100644
--- a/backend/src/applications/files/services/files-manager.service.ts
+++ b/backend/src/applications/files/services/files-manager.service.ts
@@ -43,6 +43,7 @@ import {
fileSize,
getMimeType,
isPathExists,
+ isPathInside,
isPathIsDir,
makeDir,
moveFiles,
@@ -214,16 +215,13 @@ export class FilesManager {
}
}
- const basePath = realParentPath + path.sep
-
try {
for await (const part of req.files({ throwFileSizeLimit: false })) {
// If the request uses the PATCH method, the file name corresponds to the space
const partFileName = patchMethod ? fileName(space.realPath) : part.filename
// `part.filename` may contain a path like foo/bar.txt
- const dstFile = path.resolve(basePath, partFileName)
- // Prevent path traversal
- if (!dstFile.startsWith(basePath)) {
+ const dstFile = path.resolve(realParentPath, partFileName)
+ if (!isPathInside(realParentPath, dstFile)) {
throw new FileError(HttpStatus.FORBIDDEN, 'Location is not allowed')
}
const dstExists = await isPathExists(dstFile)
diff --git a/backend/src/applications/files/services/files-methods.service.ts b/backend/src/applications/files/services/files-methods.service.ts
index 3bff53fc..876ef781 100644
--- a/backend/src/applications/files/services/files-methods.service.ts
+++ b/backend/src/applications/files/services/files-methods.service.ts
@@ -11,7 +11,7 @@ import { CompressFileDto, CopyMoveFileDto, DownloadFileDto, MakeFileDto } from '
import { FileLockProps } from '../interfaces/file-props.interface'
import { FileError } from '../models/file-error'
import { LockConflict } from '../models/file-lock-error'
-import { checkFileName, dirName, fileName, getMimeType, isPathExists, isPathIsDir, sanitizeName } from '../utils/files'
+import { checkFileName, dirName, fileName, getMimeType, isPathExists, isPathInside, isPathIsDir, sanitizeName } from '../utils/files'
import { SendFile } from '../utils/send-file'
import { FilesManager } from './files-manager.service'
import type { CopyMoveFileResponse } from '../interfaces/copy-move-file.interface'
@@ -100,8 +100,7 @@ export class FilesMethods {
f.path = path.resolve(dirName(space.realPath), f.name)
}
}
- // prevent path traversal
- if (!f.path.startsWith(baseSpace.realBasePath)) {
+ if (!isPathInside(baseSpace.realBasePath, f.path, true)) {
return this.handleError(space, FILE_OPERATION.COMPRESS, new FileError(HttpStatus.FORBIDDEN, `${f.name} not allowed`))
}
if (!(await isPathExists(f.path))) {
diff --git a/backend/src/applications/files/services/files-trash-retention.service.ts b/backend/src/applications/files/services/files-trash-retention.service.ts
index 6cc7c5c1..41937729 100644
--- a/backend/src/applications/files/services/files-trash-retention.service.ts
+++ b/backend/src/applications/files/services/files-trash-retention.service.ts
@@ -6,7 +6,7 @@ import { users } from '../../users/schemas/users.schema'
import { and, eq, inArray, lte, sql } from 'drizzle-orm'
import { USER_ROLE } from '../../users/constants/user'
import { UserModel } from '../../users/models/user.model'
-import { isPathExists, removeFiles } from '../utils/files'
+import { isPathExists, isPathInside, removeFiles } from '../utils/files'
import { spaces } from '../../spaces/schemas/spaces.schema'
import { SpaceModel } from '../../spaces/models/space.model'
import { configuration } from '../../../configuration/config.environment'
@@ -401,7 +401,7 @@ export class FilesTrashRetention {
const realPath = path.resolve(basePath, ft.path)
// DB paths are resolved against the trash root and must stay inside it before any filesystem deletion.
- if (realPath !== basePath && realPath.startsWith(`${basePath}${path.sep}`)) {
+ if (isPathInside(basePath, realPath)) {
return realPath
}
diff --git a/backend/src/applications/files/utils/files.spec.ts b/backend/src/applications/files/utils/files.spec.ts
index cd5d2227..adf131e9 100644
--- a/backend/src/applications/files/utils/files.spec.ts
+++ b/backend/src/applications/files/utils/files.spec.ts
@@ -5,7 +5,28 @@ import path from 'node:path'
import { Readable } from 'node:stream'
import { FileError } from '../models/file-error'
import { FILE_ERROR_MESSAGES } from './errors'
-import { writeFromStream } from './files'
+import { isPathInside, writeFromStream } from './files'
+
+describe(isPathInside.name, () => {
+ const basePath = path.join(path.sep, 'tmp', 'output')
+
+ it('accepts paths inside the base path', () => {
+ expect(isPathInside(basePath, path.join(basePath, 'safe', 'file.txt'))).toBe(true)
+ })
+
+ it('accepts the base path only when explicitly allowed', () => {
+ expect(isPathInside(basePath, basePath)).toBe(false)
+ expect(isPathInside(basePath, basePath, true)).toBe(true)
+ expect(isPathInside(path.parse(basePath).root, path.parse(basePath).root)).toBe(false)
+ })
+
+ it.each([path.join(basePath, '..', 'zip-slip-proof.txt'), path.join(path.sep, 'tmp', 'output-evil', 'file.txt')])(
+ 'rejects path "%s"',
+ (candidatePath) => {
+ expect(isPathInside(basePath, candidatePath)).toBe(false)
+ }
+ )
+})
describe(writeFromStream.name, () => {
let tmpDir: string
diff --git a/backend/src/applications/files/utils/files.ts b/backend/src/applications/files/utils/files.ts
index f443b784..ac3c2c15 100644
--- a/backend/src/applications/files/utils/files.ts
+++ b/backend/src/applications/files/utils/files.ts
@@ -20,6 +20,17 @@ export function sanitizePath(fPath: string): string {
return path.normalize(fPath).replace(regExpPreventPathTraversal, '')
}
+export function isPathInside(basePath: string, candidatePath: string, allowBasePath = false): boolean {
+ // Prevent lexical path traversal and prefix collisions by checking the resolved candidate against the base directory boundary.
+ const resolvedBasePath = path.resolve(basePath)
+ const resolvedCandidatePath = path.resolve(candidatePath)
+ if (resolvedCandidatePath === resolvedBasePath) {
+ return allowBasePath
+ }
+ const basePathPrefix = resolvedBasePath.endsWith(path.sep) ? resolvedBasePath : `${resolvedBasePath}${path.sep}`
+ return resolvedCandidatePath.startsWith(basePathPrefix)
+}
+
export function sanitizeName(name: string): string {
return name
.replace(/^\s+|[. ]+$/g, '') // trimStart + trimEnd + strip trailing dots
diff --git a/backend/src/applications/files/utils/unzip-file.spec.ts b/backend/src/applications/files/utils/unzip-file.spec.ts
new file mode 100644
index 00000000..0df914e7
--- /dev/null
+++ b/backend/src/applications/files/utils/unzip-file.spec.ts
@@ -0,0 +1,78 @@
+import { access, mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'
+import os from 'node:os'
+import path from 'node:path'
+import * as filesUtils from './files'
+import { extractZip } from './unzip-file'
+
+function createEmptyZip(entryName: string): Buffer {
+ const encodedEntryName = Buffer.from(entryName)
+ const localFileHeader = Buffer.alloc(30)
+ localFileHeader.writeUInt32LE(0x04034b50, 0)
+ localFileHeader.writeUInt16LE(20, 4)
+ localFileHeader.writeUInt16LE(encodedEntryName.length, 26)
+
+ const centralDirectoryHeader = Buffer.alloc(46)
+ centralDirectoryHeader.writeUInt32LE(0x02014b50, 0)
+ centralDirectoryHeader.writeUInt16LE(20, 4)
+ centralDirectoryHeader.writeUInt16LE(20, 6)
+ centralDirectoryHeader.writeUInt16LE(encodedEntryName.length, 28)
+
+ const endOfCentralDirectory = Buffer.alloc(22)
+ endOfCentralDirectory.writeUInt32LE(0x06054b50, 0)
+ endOfCentralDirectory.writeUInt16LE(1, 8)
+ endOfCentralDirectory.writeUInt16LE(1, 10)
+ endOfCentralDirectory.writeUInt32LE(centralDirectoryHeader.length + encodedEntryName.length, 12)
+ endOfCentralDirectory.writeUInt32LE(localFileHeader.length + encodedEntryName.length, 16)
+
+ return Buffer.concat([localFileHeader, encodedEntryName, centralDirectoryHeader, encodedEntryName, endOfCentralDirectory])
+}
+
+describe(extractZip.name, () => {
+ let tmpDir: string
+ let archivePath: string
+ let outputDir: string
+
+ beforeEach(async () => {
+ tmpDir = await mkdtemp(path.join(os.tmpdir(), 'extract-zip-'))
+ archivePath = path.join(tmpDir, 'archive.zip')
+ outputDir = path.join(tmpDir, 'output')
+ await mkdir(outputDir)
+ })
+
+ afterEach(async () => {
+ jest.restoreAllMocks()
+ await rm(tmpDir, { recursive: true, force: true })
+ })
+
+ it('rejects entries escaping the output directory', async () => {
+ const escapedPath = path.join(tmpDir, 'zip-slip-proof.txt')
+ await writeFile(archivePath, createEmptyZip('../zip-slip-proof.txt'))
+
+ await expect(extractZip(archivePath, outputDir)).rejects.toThrow('invalid relative path: ../zip-slip-proof.txt')
+ await expect(access(escapedPath)).rejects.toMatchObject({ code: 'ENOENT' })
+ })
+
+ it('extracts entries inside the output directory', async () => {
+ const extractedPath = path.join(outputDir, 'safe.txt')
+ await writeFile(archivePath, createEmptyZip('safe.txt'))
+
+ await expect(extractZip(archivePath, outputDir)).resolves.toBeUndefined()
+ await expect(access(extractedPath)).resolves.toBeUndefined()
+ })
+
+ it('rejects entries outside the output directory', async () => {
+ await writeFile(archivePath, createEmptyZip('safe.txt'))
+ const isPathInsideSpy = jest.spyOn(filesUtils, 'isPathInside').mockReturnValueOnce(false)
+
+ await expect(extractZip(archivePath, outputDir)).rejects.toThrow('Zip entry "safe.txt" would escape the output directory')
+ expect(isPathInsideSpy).toHaveBeenCalledWith(outputDir, path.join(outputDir, 'safe.txt'), false)
+ })
+
+ it('accepts a root directory entry', async () => {
+ await writeFile(archivePath, createEmptyZip('./'))
+ const isPathInsideSpy = jest.spyOn(filesUtils, 'isPathInside')
+
+ await expect(extractZip(archivePath, outputDir)).resolves.toBeUndefined()
+ expect(isPathInsideSpy).toHaveBeenCalledWith(outputDir, outputDir, true)
+ })
+})
diff --git a/backend/src/applications/files/utils/unzip-file.ts b/backend/src/applications/files/utils/unzip-file.ts
index 6fe65e63..2911bb8c 100644
--- a/backend/src/applications/files/utils/unzip-file.ts
+++ b/backend/src/applications/files/utils/unzip-file.ts
@@ -1,41 +1,44 @@
import fs from 'node:fs'
import path from 'node:path'
+import { pipeline } from 'node:stream/promises'
import { promisify } from 'node:util'
import { Entry, open as openZip, Options, ZipFile } from 'yauzl'
import { DEFAULT_HIGH_WATER_MARK } from '../constants/files'
-import { makeDir } from './files'
+import { isPathInside, makeDir } from './files'
const openZipAsync: (path: string, options: Options) => Promise<ZipFile> = promisify(openZip)
export async function extractZip(filePath: string, outputDir: string): Promise<void> {
const zipFile = await openZipAsync(filePath, { lazyEntries: true })
+ const openReadStream = promisify(zipFile.openReadStream.bind(zipFile))
+ const resolvedOutputDir = path.resolve(outputDir)
return new Promise((resolve, reject) => {
- zipFile.readEntry()
-
zipFile.on('entry', async (entry: Entry) => {
try {
- // make directory destination
- const fullPath = path.join(outputDir, entry.fileName)
- if (entry.fileName.endsWith('/')) {
+ const isDir = entry.fileName.endsWith('/')
+ const fullPath = path.resolve(resolvedOutputDir, entry.fileName)
+ if (!isPathInside(resolvedOutputDir, fullPath, isDir)) {
+ throw new Error(`Zip entry "${entry.fileName}" would escape the output directory`)
+ }
+ if (isDir) {
await makeDir(fullPath, true)
zipFile.readEntry()
} else {
// make sure parent exists
await makeDir(path.dirname(fullPath), true)
- const openReadStream = promisify(zipFile.openReadStream.bind(zipFile))
const readStream = await openReadStream(entry)
- const writeStream = fs.createWriteStream(fullPath, { highWaterMark: DEFAULT_HIGH_WATER_MARK })
-
- readStream.on('end', () => zipFile.readEntry())
- readStream.pipe(writeStream)
+ await pipeline(readStream, fs.createWriteStream(fullPath, { highWaterMark: DEFAULT_HIGH_WATER_MARK }))
+ zipFile.readEntry()
}
} catch (err) {
+ zipFile.close()
reject(err)
}
})
zipFile.on('end', resolve)
zipFile.on('error', reject)
+ zipFile.readEntry()
})
}
diff --git a/backend/src/applications/spaces/utils/paths.ts b/backend/src/applications/spaces/utils/paths.ts
index e437bd83..1c46ef0e 100644
--- a/backend/src/applications/spaces/utils/paths.ts
+++ b/backend/src/applications/spaces/utils/paths.ts
@@ -4,6 +4,7 @@ import path from 'node:path'
import { FileDBProps } from '../../files/interfaces/file-db-props.interface'
import { FileProps } from '../../files/interfaces/file-props.interface'
import { FileError } from '../../files/models/file-error'
+import { isPathInside } from '../../files/utils/files'
import { UserModel } from '../../users/models/user.model'
import { SPACE_REPOSITORY } from '../constants/spaces'
import { SpaceEnv } from '../models/space-env.model'
@@ -66,8 +67,7 @@ export function realPathFromSpace(user: UserModel, space: SpaceEnv, withBasePath
throw new FileError(HttpStatus.NOT_FOUND, 'Space root not found')
}
const rPath = path.resolve(bPath, ...fPath)
- // prevent path traversal
- if (!rPath.startsWith(bPath)) {
+ if (!isPathInside(bPath, rPath, true)) {
throw new FileError(HttpStatus.FORBIDDEN, 'Location is not allowed')
}
return withBasePath ? [bPath, rPath] : rPath