Skip to content

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

fix(backend:sync): validate path filter regex before diff

johavenb1dcaa1d1c1b
diff --git a/backend/package.json b/backend/package.json
index 132c4c0e..9f7fbe87 100755
--- a/backend/package.json
+++ b/backend/package.json
@@ -77,6 +77,7 @@
"pino-pretty": "^13.1.3",
"qrcode-generator": "^2.0.4",
"redis": "^5.12.1",
+ "safe-regex2": "^5.1.1",
"sax": "^1.6.0",
"sharp": "^0.34.5",
"socket.io": "^4.8.1",
diff --git a/backend/src/applications/sync/dtos/sync-operations.dto.ts b/backend/src/applications/sync/dtos/sync-operations.dto.ts
index d30448e7..7cde66ca 100644
--- a/backend/src/applications/sync/dtos/sync-operations.dto.ts
+++ b/backend/src/applications/sync/dtos/sync-operations.dto.ts
@@ -2,6 +2,7 @@ import { Transform } from 'class-transformer'
import { IsBoolean, IsDefined, IsInt, IsNotEmpty, IsObject, IsOptional, IsString } from 'class-validator'
import { MakeFileDto } from '../../files/dto/file-operations.dto'
import { SyncFileStats } from '../interfaces/sync-diff.interface'
+import { transformPathFilters } from '../utils/functions'
import { NormalizedMap } from '../utils/normalizedMap'
export class SyncDiffDto {
@@ -19,7 +20,7 @@ export class SyncDiffDto {
defaultFilters: Set<string>
@IsOptional()
- @Transform(({ value }) => (typeof value === 'string' && value.length > 0 ? new RegExp(value, 'i') : null))
+ @Transform(({ value }) => transformPathFilters(value))
pathFilters?: RegExp = null
@IsOptional()
diff --git a/backend/src/applications/sync/utils/functions.spec.ts b/backend/src/applications/sync/utils/functions.spec.ts
new file mode 100644
index 00000000..f2932d7b
--- /dev/null
+++ b/backend/src/applications/sync/utils/functions.spec.ts
@@ -0,0 +1,38 @@
+import { BadRequestException } from '@nestjs/common'
+import { transformPathFilters } from './functions'
+
+describe(transformPathFilters.name, () => {
+ it('returns null for empty or non-string values', () => {
+ expect(transformPathFilters(null)).toBeNull()
+ expect(transformPathFilters('')).toBeNull()
+ })
+
+ it('returns a case-insensitive regular expression for safe patterns', () => {
+ const pathFilter = transformPathFilters('documents/.+\\.pdf$')
+
+ expect(pathFilter).toBeInstanceOf(RegExp)
+ expect(pathFilter?.flags).toContain('i')
+ expect(pathFilter?.test('DOCUMENTS/report.PDF')).toBe(true)
+ })
+
+ it('rejects path filter patterns that are too long', () => {
+ const transform = () => transformPathFilters('a'.repeat(201))
+
+ expect(transform).toThrow(BadRequestException)
+ expect(transform).toThrow('Path filter pattern is too long')
+ })
+
+ it('rejects invalid regular expressions with an explicit invalid-pattern error', () => {
+ const transform = () => transformPathFilters('(')
+
+ expect(transform).toThrow(BadRequestException)
+ expect(transform).toThrow('Invalid path filter pattern')
+ })
+
+ it('rejects unsafe regular expressions with an explicit unsafe-pattern error', () => {
+ const transform = () => transformPathFilters('^(a+)+b')
+
+ expect(transform).toThrow(BadRequestException)
+ expect(transform).toThrow('Unsafe path filter pattern')
+ })
+})
diff --git a/backend/src/applications/sync/utils/functions.ts b/backend/src/applications/sync/utils/functions.ts
index 5f1ecc0d..9e8c4a51 100644
--- a/backend/src/applications/sync/utils/functions.ts
+++ b/backend/src/applications/sync/utils/functions.ts
@@ -1,6 +1,34 @@
+import { BadRequestException } from '@nestjs/common'
import path from 'node:path'
+import safeRegex from 'safe-regex2'
import { SYNC_FILE_NAME_PREFIX } from '../constants/sync'
+const MAX_PATH_FILTER_LENGTH = 200
+const MAX_PATH_FILTER_REPETITIONS = 25
+
export function getTmpFilePath(rPath: string): string {
return `${path.dirname(rPath)}/${SYNC_FILE_NAME_PREFIX}${path.basename(rPath)}`
}
+
+export function transformPathFilters(value: unknown): RegExp | null {
+ if (typeof value !== 'string' || value.length === 0) {
+ return null
+ }
+
+ if (value.length > MAX_PATH_FILTER_LENGTH) {
+ throw new BadRequestException('Path filter pattern is too long')
+ }
+
+ let pathFilter: RegExp
+ try {
+ pathFilter = new RegExp(value, 'i')
+ } catch {
+ throw new BadRequestException('Invalid path filter pattern')
+ }
+
+ if (!safeRegex(pathFilter, { limit: MAX_PATH_FILTER_REPETITIONS })) {
+ throw new BadRequestException('Unsafe path filter pattern')
+ }
+
+ return pathFilter
+}
diff --git a/package-lock.json b/package-lock.json
index c7931d3c..89d0748c 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -86,6 +86,7 @@
"pino-pretty": "^13.1.3",
"qrcode-generator": "^2.0.4",
"redis": "^5.12.1",
+ "safe-regex2": "^5.1.1",
"sax": "^1.6.0",
"sharp": "^0.34.5",
"socket.io": "^4.8.1",