Skip to content

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

fix(backend:sync): prevent path filter ReDoS

johavend6914d0acd37
diff --git a/backend/package.json b/backend/package.json
index db687695..2fb56ec3 100755
--- a/backend/package.json
+++ b/backend/package.json
@@ -33,6 +33,7 @@
"test:watch": "NODE_ENV=test vitest"
},
"dependencies": {
+ "@eslint-community/regexpp": "^4.12.2",
"@fastify/cookie": "^11.0.2",
"@fastify/helmet": "^13.0.2",
"@fastify/multipart": "^10.0.0",
@@ -77,8 +78,8 @@
"pino-http": "^11.0.0",
"pino-pretty": "^13.2.0",
"qrcode-generator": "^2.0.4",
+ "re2js": "^2.9.0",
"redis": "^6.3.0",
- "safe-regex2": "^5.1.1",
"sax": "^1.6.0",
"sharp": "^0.35.2",
"socket.io": "4.8.4",
diff --git a/backend/src/applications/sync/constants/sync.ts b/backend/src/applications/sync/constants/sync.ts
index 01409a15..a2a8f367 100644
--- a/backend/src/applications/sync/constants/sync.ts
+++ b/backend/src/applications/sync/constants/sync.ts
@@ -5,10 +5,6 @@ export const CHECK_SERVER_RESP = { server: SYNC_IN_SERVER_AGENT } as const
export const SYNC_CHECKSUM_ALG = 'sha512-256'
export const SYNC_DIFF_DONE = 'done'
export const SYNC_TEMPORARY_FILE_PREFIX = '.sync-in.'
-export const SYNC_MAX_PATH_FILTERS = 100
-export const SYNC_MAX_PATH_FILTER_LENGTH = 1024
-export const SYNC_MAX_PATH_FILTER_PATTERN_LENGTH = SYNC_MAX_PATH_FILTERS * SYNC_MAX_PATH_FILTER_LENGTH + (SYNC_MAX_PATH_FILTERS - 1)
-export const SYNC_MAX_PATH_FILTER_REPETITIONS = 25
export enum SYNC_REPOSITORY {
PERSONAL = SPACE_ALIAS.PERSONAL,
diff --git a/backend/src/applications/sync/dtos/sync-operations.dto.spec.ts b/backend/src/applications/sync/dtos/sync-operations.dto.spec.ts
new file mode 100644
index 00000000..a842a028
--- /dev/null
+++ b/backend/src/applications/sync/dtos/sync-operations.dto.spec.ts
@@ -0,0 +1,32 @@
+import { Logger } from '@nestjs/common'
+import { syncDiffSchema } from './sync-operations.dto.js'
+
+const diff = { secureDiff: false, firstSync: true, defaultFilters: [] }
+
+describe('Sync diff path filters', () => {
+ beforeEach(() => {
+ vi.spyOn(Logger.prototype, 'warn').mockImplementation(() => undefined)
+ })
+ afterEach(() => vi.restoreAllMocks())
+
+ it('accepts the historical string source and keeps supported alternatives', () => {
+ const parsed = syncDiffSchema.parse({ ...diff, pathFilters: '(?:cache|tmp)/|(?=documents)' })
+
+ expect(parsed.pathFilters?.test('cache/file.txt')).toBe(true)
+ expect(parsed.pathFilters?.test('tmp/file.txt')).toBe(true)
+ expect(parsed.pathFilters?.test('documents/file.txt')).toBe(false)
+ expect(Logger.prototype.warn).toHaveBeenCalledOnce()
+ })
+
+ it.each([{}, { pathFilters: null }, { pathFilters: '' }])('accepts missing or empty custom exclusions: %j', (filters) => {
+ expect(syncDiffSchema.parse({ ...diff, ...filters }).pathFilters).toBeNull()
+ expect(Logger.prototype.warn).not.toHaveBeenCalled()
+ })
+
+ it.each([{ pathFilters: [] }, { pathFilters: ['\\.tmp$'] }, { pathFilters: [1] }, { pathFilters: 42 }, { pathFilters: {} }])(
+ 'rejects a non-string HTTP path filter source: $pathFilters',
+ ({ pathFilters }) => {
+ expect(syncDiffSchema.safeParse({ ...diff, pathFilters }).success).toBe(false)
+ }
+ )
+})
diff --git a/backend/src/applications/sync/dtos/sync-operations.dto.ts b/backend/src/applications/sync/dtos/sync-operations.dto.ts
index b3cb642a..72fdfecf 100644
--- a/backend/src/applications/sync/dtos/sync-operations.dto.ts
+++ b/backend/src/applications/sync/dtos/sync-operations.dto.ts
@@ -9,8 +9,8 @@ export const syncDiffSchema = z.object({
firstSync: z.boolean(),
defaultFilters: z.array(z.string()).transform((value) => new Set(value)),
pathFilters: z
- .unknown()
- .optional()
+ .string()
+ .nullish()
.transform((value) => transformPathFilters(value)),
snapshot: z
.record(z.string(), z.unknown())
diff --git a/backend/src/applications/sync/dtos/sync-path.dto.ts b/backend/src/applications/sync/dtos/sync-path.dto.ts
index ade2d6ec..bba123a4 100644
--- a/backend/src/applications/sync/dtos/sync-path.dto.ts
+++ b/backend/src/applications/sync/dtos/sync-path.dto.ts
@@ -1,13 +1,7 @@
import { z } from 'zod'
import { integerNumberSchema } from '../../../configuration/config.schema.js'
-import {
- SYNC_MAX_PATH_FILTER_LENGTH,
- SYNC_MAX_PATH_FILTERS,
- SYNC_PATH_CONFLICT_MODE,
- SYNC_PATH_DIFF_MODE,
- SYNC_PATH_MODE,
- SYNC_PATH_SCHEDULER_UNIT
-} from '../constants/sync.js'
+import { SYNC_PATH_CONFLICT_MODE, SYNC_PATH_DIFF_MODE, SYNC_PATH_MODE, SYNC_PATH_SCHEDULER_UNIT } from '../constants/sync.js'
+import { SYNC_MAX_PATH_FILTER_LENGTH, SYNC_MAX_PATH_FILTERS } from '../utils/path-filters/constants.js'
const syncPathSchedulerSchema = z.object({
value: z.number().int(),
diff --git a/backend/src/applications/sync/utils/functions.spec.ts b/backend/src/applications/sync/utils/functions.spec.ts
index 3fb49595..96ce1521 100644
--- a/backend/src/applications/sync/utils/functions.spec.ts
+++ b/backend/src/applications/sync/utils/functions.spec.ts
@@ -1,39 +1,74 @@
-import { BadRequestException } from '@nestjs/common'
-import { SYNC_MAX_PATH_FILTER_PATTERN_LENGTH } from '../constants/sync.js'
+import { BadRequestException, Logger } from '@nestjs/common'
+import { RE2JS } from 're2js'
import { transformPathFilters } from './functions.js'
+import { SYNC_MAX_PATH_FILTER_PATTERN_LENGTH, SYNC_MAX_PATH_FILTER_WARNINGS } from './path-filters/constants.js'
describe(transformPathFilters.name, () => {
+ beforeEach(() => {
+ vi.spyOn(Logger.prototype, 'warn').mockImplementation(() => undefined)
+ })
+ afterEach(() => vi.restoreAllMocks())
+
it('returns null for empty or non-string values', () => {
- expect(transformPathFilters(null)).toBeNull()
- expect(transformPathFilters('')).toBeNull()
+ for (const value of [null, undefined, '', [], {}, 0]) {
+ expect(transformPathFilters(value)).toBeNull()
+ }
+ expect(Logger.prototype.warn).not.toHaveBeenCalled()
})
- it('returns a case-insensitive regular expression for safe patterns', () => {
+ it('returns a case-insensitive RE2JS matcher with unanchored search semantics', () => {
const pathFilter = transformPathFilters('documents/.+\\.pdf$')
- expect(pathFilter).toBeInstanceOf(RegExp)
- expect(pathFilter?.flags).toContain('i')
- expect(pathFilter?.test('DOCUMENTS/report.PDF')).toBe(true)
+ expect(pathFilter).toBeInstanceOf(RE2JS)
+ expect(pathFilter?.test('archive/DOCUMENTS/report.PDF')).toBe(true)
})
- it('rejects path filter patterns that are too long', () => {
+ it('keeps supported alternatives and logs rejected ones', () => {
+ const pathFilter = transformPathFilters('\\.tmp$|(?=documents)')
+
+ expect(pathFilter?.test('documents/report.TMP')).toBe(true)
+ expect(pathFilter?.test('documents/report.txt')).toBe(false)
+ expect(Logger.prototype.warn).toHaveBeenCalledOnce()
+ expect(vi.mocked(Logger.prototype.warn).mock.calls[0][0]).toMatchObject({
+ index: 1,
+ pattern: '(?=documents)',
+ reason: 'Invalid or unsupported path filter pattern'
+ })
+ })
+
+ it('maps an oversized source to a bad request', () => {
const transform = () => transformPathFilters('a'.repeat(SYNC_MAX_PATH_FILTER_PATTERN_LENGTH + 1))
expect(transform).toThrow(BadRequestException)
- expect(transform).toThrow('Path filter pattern is too long')
+ expect(Logger.prototype.warn).not.toHaveBeenCalled()
})
- it('rejects invalid regular expressions with an explicit invalid-pattern error', () => {
- const transform = () => transformPathFilters('(')
+ it('caps rejection details and summarizes the remaining ignored alternatives', () => {
+ const source = Array(SYNC_MAX_PATH_FILTER_WARNINGS + 2)
+ .fill('(?=x)')
+ .join('|')
- expect(transform).toThrow(BadRequestException)
- expect(transform).toThrow('Invalid path filter pattern')
+ expect(transformPathFilters(source)).toBeNull()
+ expect(Logger.prototype.warn).toHaveBeenCalledTimes(SYNC_MAX_PATH_FILTER_WARNINGS + 1)
+ expect(vi.mocked(Logger.prototype.warn).mock.calls[SYNC_MAX_PATH_FILTER_WARNINGS][0]).toMatchObject({
+ msg: 'Additional path filter alternatives ignored',
+ count: 2
+ })
})
- it('rejects unsafe regular expressions with an explicit unsafe-pattern error', () => {
- const transform = () => transformPathFilters('^(a+)+b')
+ it('propagates unexpected compilation errors', () => {
+ const failure = new Error('Unexpected compilation failure')
+ vi.spyOn(RE2JS, 'compile').mockImplementation(() => {
+ throw failure
+ })
- expect(transform).toThrow(BadRequestException)
- expect(transform).toThrow('Unsafe path filter pattern')
+ let thrown: unknown
+ try {
+ transformPathFilters('\\.tmp$')
+ } catch (error) {
+ thrown = error
+ }
+ expect(thrown).toBe(failure)
+ expect(Logger.prototype.warn).not.toHaveBeenCalled()
})
})
diff --git a/backend/src/applications/sync/utils/functions.ts b/backend/src/applications/sync/utils/functions.ts
index 427c2ad1..878d09bb 100644
--- a/backend/src/applications/sync/utils/functions.ts
+++ b/backend/src/applications/sync/utils/functions.ts
@@ -1,31 +1,43 @@
-import { BadRequestException } from '@nestjs/common'
+import { BadRequestException, Logger } from '@nestjs/common'
import path from 'node:path'
-import safeRegex from 'safe-regex2'
-import { SYNC_MAX_PATH_FILTER_PATTERN_LENGTH, SYNC_MAX_PATH_FILTER_REPETITIONS, SYNC_TEMPORARY_FILE_PREFIX } from '../constants/sync.js'
+import { SYNC_TEMPORARY_FILE_PREFIX } from '../constants/sync.js'
+import { PathFilterError } from './path-filters/errors.js'
+import { compilePathFilters } from './path-filters/path-filter-compiler.js'
+import { SYNC_MAX_PATH_FILTER_LENGTH, SYNC_MAX_PATH_FILTER_WARNINGS } from './path-filters/constants.js'
+import type { SyncPathFilterMatcher } from './path-filters/path-filter-compiler.interface.js'
+
+const pathFilterLogger = new Logger('SyncPathFilters')
export function getSyncTmpFilePath(rPath: string): string {
return `${path.dirname(rPath)}/${SYNC_TEMPORARY_FILE_PREFIX}${path.basename(rPath)}`
}
-export function transformPathFilters(value: unknown): RegExp | null {
- if (typeof value !== 'string' || value.length === 0) {
+export function transformPathFilters(value: unknown): SyncPathFilterMatcher | null {
+ if (typeof value !== 'string' || value === '') {
return null
}
- if (value.length > SYNC_MAX_PATH_FILTER_PATTERN_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')
+ const result = compilePathFilters(value)
+ for (const rejection of result.rejected.slice(0, SYNC_MAX_PATH_FILTER_WARNINGS)) {
+ pathFilterLogger.warn({
+ msg: 'Ignoring path filter alternative',
+ index: rejection.index,
+ pattern: rejection.pattern.slice(0, SYNC_MAX_PATH_FILTER_LENGTH),
+ reason: rejection.reason
+ })
+ }
+ if (result.rejected.length > SYNC_MAX_PATH_FILTER_WARNINGS) {
+ pathFilterLogger.warn({
+ msg: 'Additional path filter alternatives ignored',
+ count: result.rejected.length - SYNC_MAX_PATH_FILTER_WARNINGS
+ })
+ }
+ return result.matcher
+ } catch (error) {
+ if (error instanceof PathFilterError) {
+ throw new BadRequestException(error.message)
+ }
+ throw error
}
-
- if (!safeRegex(pathFilter, { limit: SYNC_MAX_PATH_FILTER_REPETITIONS })) {
- throw new BadRequestException('Unsafe path filter pattern')
- }
-
- return pathFilter
}
diff --git a/backend/src/applications/sync/utils/path-filters/README.md b/backend/src/applications/sync/utils/path-filters/README.md
new file mode 100644
index 00000000..a12ae5fb
--- /dev/null
+++ b/backend/src/applications/sync/utils/path-filters/README.md
@@ -0,0 +1,15 @@
+# Path filters
+
+This module mirrors `core/components/utils/path-filters/` in `sync-in-desktop`. Keep validation, limits, RE2JS flags, dependency versions,
+complexity estimation, types, and tests synchronized. Only import extensions and test setup may differ.
+
+`path-filter-compiler.interface.ts` is intentionally colocated with the compiler as an exception to the component-wide `interfaces/` convention.
+
+## Contract
+
+- `buildPathFilterSource()` validates the filter list and combined length, preserves regex syntax, and returns the historical `string | null` wire format.
+- `compilePathFilters()` parses the source once and evaluates only root alternatives. A globally invalid source is rejected as a whole.
+- `validatePathFilterList()` maps rejected alternatives back to their original filter indexes for user interfaces.
+- Oversized, unsupported, or over-budget alternatives are ignored independently; later alternatives are still evaluated. Budgets reserved before RE2JS compilation are never refunded.
+- Accepted alternatives use a case-insensitive RE2JS matcher. If none remain, synchronization continues without custom exclusions.
+- The desktop sends the original combined source so both sides apply the same deterministic selection.
diff --git a/backend/src/applications/sync/utils/path-filters/constants.ts b/backend/src/applications/sync/utils/path-filters/constants.ts
new file mode 100644
index 00000000..27c054d8
--- /dev/null
+++ b/backend/src/applications/sync/utils/path-filters/constants.ts
@@ -0,0 +1,11 @@
+import { RE2JS } from 're2js'
+
+export const SYNC_MAX_PATH_FILTERS = 100
+export const SYNC_MAX_PATH_FILTER_WARNINGS = 100
+export const SYNC_MAX_PATH_FILTER_LENGTH = 1024
+export const SYNC_MAX_PATH_FILTER_PATTERN_LENGTH = SYNC_MAX_PATH_FILTERS * SYNC_MAX_PATH_FILTER_LENGTH + (SYNC_MAX_PATH_FILTERS - 1)
+// Unicode property escapes are disabled because RE2JS expands their tables synchronously while parsing.
+export const SYNC_PATH_FILTER_RE2_FLAGS = RE2JS.CASE_INSENSITIVE | RE2JS.DISABLE_UNICODE_GROUPS
+// Bound the combined filter's estimated program size independently of source length.
+export const SYNC_MAX_PATH_FILTER_PROGRAM_SIZE = 16_384
+export const SYNC_MAX_PATH_FILTER_PARSE_COST = 100_000
diff --git a/backend/src/applications/sync/utils/path-filters/errors.ts b/backend/src/applications/sync/utils/path-filters/errors.ts
new file mode 100644
index 00000000..1c32498d
--- /dev/null
+++ b/backend/src/applications/sync/utils/path-filters/errors.ts
@@ -0,0 +1,6 @@
+export class PathFilterError extends Error {
+ constructor(message: string) {
+ super(message)
+ this.name = 'PathFilterError'
+ }
+}
diff --git a/backend/src/applications/sync/utils/path-filters/path-filter-compiler.interface.ts b/backend/src/applications/sync/utils/path-filters/path-filter-compiler.interface.ts
new file mode 100644
index 00000000..d5561187
--- /dev/null
+++ b/backend/src/applications/sync/utils/path-filters/path-filter-compiler.interface.ts
@@ -0,0 +1,24 @@
+export interface SyncPathFilterMatcher {
+ test(path: string): boolean
+}
+
+export interface SyncPathFilterParseBudget {
+ remaining: number
+}
+
+export interface SyncPathFilterComplexity {
+ programSize: number
+ parseCost: number
+ captureNames: string[]
+}
+
+export interface SyncPathFilterRejection {
+ index: number
+ pattern: string
+ reason: string
+}
+
+export interface SyncPathFiltersResult {
+ matcher: SyncPathFilterMatcher | null
+ rejected: SyncPathFilterRejection[]
+}
diff --git a/backend/src/applications/sync/utils/path-filters/path-filter-compiler.spec.ts b/backend/src/applications/sync/utils/path-filters/path-filter-compiler.spec.ts
new file mode 100644
index 00000000..36767fa8
--- /dev/null
+++ b/backend/src/applications/sync/utils/path-filters/path-filter-compiler.spec.ts
@@ -0,0 +1,196 @@
+import { RegExpParser } from '@eslint-community/regexpp'
+import { RE2JS } from 're2js'
+import { SYNC_MAX_PATH_FILTER_LENGTH, SYNC_MAX_PATH_FILTER_PATTERN_LENGTH, SYNC_MAX_PATH_FILTERS } from './constants.js'
+import { PathFilterError } from './errors.js'
+import { buildPathFilterSource, compilePathFilters, validatePathFilterList } from './path-filter-compiler.js'
+
+afterEach(() => vi.restoreAllMocks())
+
+describe(buildPathFilterSource.name, () => {
+ it.each([
+ { filters: [], source: null },
+ { filters: [''], source: '(?:)' },
+ { filters: ['', 'a'], source: '|a' },
+ { filters: [' documents/.+\\.pdf$ ', '^資料/報告$', 'a|b'], source: ' documents/.+\\.pdf$ |^資料/報告$|a|b' }
+ ])('preserves the filter source for $filters', ({ filters, source }) => {
+ expect(buildPathFilterSource(filters)).toBe(source)
+ })
+
+ it('validates the list shape and entry count', () => {
+ for (const filters of [null, 'a', [null], Array(1)]) {
+ expect(() => buildPathFilterSource(filters as unknown as readonly string[])).toThrow('Invalid path filters')
+ }
+ expect(() => buildPathFilterSource(Array(SYNC_MAX_PATH_FILTERS + 1).fill('a'))).toThrow('Too many path filters')
+ })
+
+ it('enforces only the combined length so oversized entries can be ignored later', () => {
+ const maxFilters = Array(SYNC_MAX_PATH_FILTERS).fill('a'.repeat(SYNC_MAX_PATH_FILTER_LENGTH))
+ const oversizedEntry = 'a'.repeat(SYNC_MAX_PATH_FILTER_LENGTH + 1)
+
+ expect(buildPathFilterSource(maxFilters)).toHaveLength(SYNC_MAX_PATH_FILTER_PATTERN_LENGTH)
+ expect(buildPathFilterSource([oversizedEntry])).toBe(oversizedEntry)
+ expect(() => buildPathFilterSource(['a'.repeat(SYNC_MAX_PATH_FILTER_PATTERN_LENGTH), 'b'])).toThrow(
+ new PathFilterError('Path filter pattern is too long')
+ )
+ })
+})
+
+describe(validatePathFilterList.name, () => {
+ it('maps rejected alternatives to their original filters', () => {
+ expect(validatePathFilterList(['^documents/', 'a{00}|^photos/', '\\Qfile\\E'])).toEqual([1, 2])
+ })
+
+ it('marks every filter when the combined syntax cannot be parsed', () => {
+ expect(validatePathFilterList(['^documents/', '(', '^photos/'])).toEqual([0, 1, 2])
+ })
+})
+
+describe(compilePathFilters.name, () => {
+ it('uses a case-insensitive RE2JS matcher with unanchored search semantics', () => {
+ const result = compilePathFilters('documents/.+\\.pdf$')
+ const adversarial = compilePathFilters('(a|a)*$').matcher
+
+ expect(result).toMatchObject({ rejected: [], matcher: expect.any(RE2JS) })
+ expect(result.matcher?.test('archive/DOCUMENTS/report.PDF')).toBe(true)
+ expect(adversarial).toBeInstanceOf(RE2JS)
+ expect(adversarial?.test(`${'a'.repeat(28)}/`)).toBe(true)
+ })
+
+ it('distinguishes absent filters from universal empty alternatives', () => {
+ expect(compilePathFilters(null)).toEqual({ matcher: null, rejected: [] })
+ expect(compilePathFilters('')).toEqual({ matcher: null, rejected: [] })
+ expect(compilePathFilters(buildPathFilterSource([''])).matcher?.test('documents/report.txt')).toBe(true)
+ expect(compilePathFilters('|^documents/').matcher?.test('archive/report.txt')).toBe(true)
+ })
+
+ it('parses once, preserves structural pipes and keeps supported alternatives in order', () => {
+ const parse = vi.spyOn(RegExpParser.prototype, 'parsePattern')
+ const compileRE2 = vi.spyOn(RE2JS, 'compile')
+ const source = '^(?:foo|bar)$|^literal\\|pipe$|^[a|b]$|(?=ignored)|^資料/'
+ const accepted = '^(?:foo|bar)$|^literal\\|pipe$|^[a|b]$|^資料/'
+ const result = compilePathFilters(source)
+
+ expect(parse).toHaveBeenCalledOnce()
+ expect(result.rejected).toEqual([{ index: 3, pattern: '(?=ignored)', reason: 'Invalid or unsupported path filter pattern' }])
+ expect(compileRE2).toHaveBeenLastCalledWith(accepted, expect.any(Number))
+ for (const path of ['foo', 'bar', 'literal|pipe', '|', '資料/report.pdf']) {
+ expect(result.matcher?.test(path)).toBe(true)
+ }
+ expect(result.matcher?.test('literal')).toBe(false)
+ })
+
+ it.each(['a{00}', '\\Qfile\\E', '[]', '[[:alpha:]]', '[\\a-z]', '[B-😀]'])(
+ 'rejects regexpp/RE2JS dialect divergences before compilation: %s',
+ (pattern) => {
+ const compileRE2 = vi.spyOn(RE2JS, 'compile')
+
+ expect(compilePathFilters(pattern)).toEqual({
+ matcher: null,
+ rejected: [{ index: 0, pattern, reason: 'Invalid or unsupported path filter pattern' }]
+ })
+ expect(compileRE2).not.toHaveBeenCalled()
+ }
+ )
+
+ it('ignores an oversized alternative while accepting the inclusive entry limit', () => {
+ const atLimit = 'a'.repeat(SYNC_MAX_PATH_FILTER_LENGTH)
+ const tooLong = `${atLimit}a`
+ const result = compilePathFilters(`${atLimit}|${tooLong}|^documents/`)
+
+ expect(result.rejected).toEqual([{ index: 1, pattern: tooLong, reason: 'Path filter pattern is too long' }])
+ expect(result.matcher?.test('documents/report.txt')).toBe(true)
+ })
+
+ it('rejects an oversized source before parsing or compiling', () => {
+ const parse = vi.spyOn(RegExpParser.prototype, 'parsePattern')
+ const compileRE2 = vi.spyOn(RE2JS, 'compile')
+ const compile = () => compilePathFilters('a'.repeat(SYNC_MAX_PATH_FILTER_PATTERN_LENGTH + 1))
+
+ expect(compile).toThrow('Path filter pattern is too long')
+ expect(parse).not.toHaveBeenCalled()
+ expect(compileRE2).not.toHaveBeenCalled()
+ })
+
+ it('allows more root alternatives than original filter entries', () => {
+ const source = Array<string>(SYNC_MAX_PATH_FILTERS + 1)
+ .fill('a')
+ .join('|')
+
+ expect(compilePathFilters(source)).toMatchObject({ rejected: [], matcher: expect.any(RE2JS) })
+ })
+
+ it('rejects a globally malformed source without recovering individual alternatives', () => {
+ const source = '^documents/|('
+ const compileRE2 = vi.spyOn(RE2JS, 'compile')
+
+ expect(compilePathFilters(source)).toEqual({
+ matcher: null,
+ rejected: [{ index: 0, pattern: source, reason: 'Invalid or unsupported path filter pattern' }]
+ })
+ expect(compileRE2).not.toHaveBeenCalled()
+ })
+
+ it('turns parser stack exhaustion into a complexity rejection', () => {
+ const source = '^documents/|(?:nested)'
+ vi.spyOn(RegExpParser.prototype, 'parsePattern').mockImplementation(() => {
+ throw new RangeError('Maximum call stack size exceeded')
+ })
+
+ expect(compilePathFilters(source)).toEqual({
+ matcher: null,
+ rejected: [{ index: 0, pattern: source, reason: 'Path filter pattern is too complex' }]
+ })
+ })
+
+ it.each([
+ {
+ budget: 'program',
+ unsupported: `${'a{1000}'.repeat(16)}(?=x)`,
+ tooExpensive: 'a{380}',
+ fallback: 'c'
+ },
+ {
+ budget: 'parsing',
+ unsupported: `${'[B-翿]'.repeat(3)}(?=x)`,
+ tooExpensive: '[A-ࠀ]',
+ fallback: '[a-z]'
+ }
+ ])('does not refund the $budget budget after a failed compilation', ({ unsupported, tooExpensive, fallback }) => {
+ const compileRE2 = vi.spyOn(RE2JS, 'compile')
+ const result = compilePathFilters(`${unsupported}|${tooExpensive}|${fallback}`)
+
+ expect(result.rejected).toEqual([
+ { index: 0, pattern: unsupported, reason: 'Invalid or unsupported path filter pattern' },
+ { index: 1, pattern: tooExpensive, reason: 'Path filter pattern is too complex' }
+ ])
+ expect(compileRE2.mock.calls.map(([source]) => source)).toEqual([unsupported, fallback])
+ })
+
+ it('reserves capture names inside zero-repeat groups', () => {
+ const result = compilePathFilters('(?<item>a){0}|(?<item>b)|(?<other>c)')
+
+ expect(result.rejected).toEqual([{ index: 1, pattern: '(?<item>b)', reason: 'Invalid or unsupported path filter pattern' }])
+ expect(result.matcher?.test('c')).toBe(true)
+ })
+
+ it.each(['parser', 'RE2JS'] as const)('propagates unexpected %s errors unchanged', (stage) => {
+ const failure = stage === 'parser' ? new Error('Unexpected parser failure') : new RangeError('Unexpected RE2JS stack failure')
+ if (stage === 'parser') {
+ vi.spyOn(RegExpParser.prototype, 'parsePattern').mockImplementation(() => {
+ throw failure
+ })
+ } else {
+ vi.spyOn(RE2JS, 'compile').mockImplementation(() => {
+ throw failure
+ })
+ }
+
+ let thrown: unknown
+ try {
+ compilePathFilters('a')
+ } catch (error) {
+ thrown = error
+ }
+ expect(thrown).toBe(failure)
+ })
+})
diff --git a/backend/src/applications/sync/utils/path-filters/path-filter-compiler.ts b/backend/src/applications/sync/utils/path-filters/path-filter-compiler.ts
new file mode 100644
index 00000000..83815d68
--- /dev/null
+++ b/backend/src/applications/sync/utils/path-filters/path-filter-compiler.ts
@@ -0,0 +1,212 @@
+import type { AST } from '@eslint-community/regexpp'
+import { RE2JS, RE2JSCompileException, RE2JSSyntaxException } from 're2js'
+import {
+ SYNC_MAX_PATH_FILTER_LENGTH,
+ SYNC_MAX_PATH_FILTER_PARSE_COST,
+ SYNC_MAX_PATH_FILTER_PATTERN_LENGTH,
+ SYNC_MAX_PATH_FILTER_PROGRAM_SIZE,
+ SYNC_MAX_PATH_FILTERS,
+ SYNC_PATH_FILTER_RE2_FLAGS
+} from './constants.js'
+import { PathFilterError } from './errors.js'
+import type { SyncPathFilterComplexity, SyncPathFilterRejection, SyncPathFiltersResult } from './path-filter-compiler.interface.js'
+import { getPathFilterAlternativeComplexity, parsePathFilterAlternatives } from './path-filter-complexity.js'
+
+export function buildPathFilterSource(filters: readonly string[]): string | null {
+ if (!Array.isArray(filters)) {
+ throw new PathFilterError('Invalid path filters')
+ }
+
+ let sourceLength = Math.max(0, filters.length - 1)
+ for (const filter of filters) {
+ // Array iteration exposes sparse entries as undefined, unlike some().
+ if (typeof filter !== 'string') {
+ throw new PathFilterError('Invalid path filters')
+ }
+ sourceLength += filter.length
+ }
+ if (filters.length > SYNC_MAX_PATH_FILTERS) {
+ throw new PathFilterError('Too many path filters')
+ }
+ if (sourceLength > SYNC_MAX_PATH_FILTER_PATTERN_LENGTH) {
+ throw new PathFilterError('Path filter pattern is too long')
+ }
+
+ // Preserve the original alternatives, including empty filters and whitespace.
+ return filters.length ? filters.join('|') || '(?:)' : null
+}
+
+// Compile only after validation and budget checks; unexpected engine failures must propagate.
+function compileRE2PathFilter(source: string): RE2JS {
+ try {
+ return RE2JS.compile(source, SYNC_PATH_FILTER_RE2_FLAGS)
+ } catch (error) {
+ if (error instanceof RE2JSSyntaxException || error instanceof RE2JSCompileException) {
+ throw new PathFilterError('Invalid or unsupported path filter pattern')
+ }
+ throw error
+ }
+}
+
+// Both public entry points reuse this step so list validation keeps the parser's
+// source offsets without parsing the same expression twice.
+function compilePathFilterAlternatives(alternatives: AST.Alternative[]): SyncPathFiltersResult {
+ const accepted: string[] = []
+ const rejected: SyncPathFilterRejection[] = []
+ const namedGroups = new Set<string>()
+ let remainingCompileSize = SYNC_MAX_PATH_FILTER_PROGRAM_SIZE
+ let remainingParseCost = SYNC_MAX_PATH_FILTER_PARSE_COST
+ let matcher: RE2JS | null = null
+
+ for (const [index, alternative] of alternatives.entries()) {
+ const pattern = alternative.raw
+ if (pattern.length > SYNC_MAX_PATH_FILTER_LENGTH) {
+ rejected.push({ index, pattern, reason: 'Path filter pattern is too long' })
+ continue
+ }
+
+ let complexity: SyncPathFilterComplexity
+ try {
+ complexity = getPathFilterAlternativeComplexity(alternative, SYNC_MAX_PATH_FILTER_PROGRAM_SIZE, SYNC_MAX_PATH_FILTER_PARSE_COST)
+ } catch (error) {
+ if (error instanceof SyntaxError) {
+ rejected.push({ index, pattern, reason: 'Invalid or unsupported path filter pattern' })
+ continue
+ }
+ throw error
+ }
+
+ if (complexity.captureNames.some((name) => namedGroups.has(name))) {
+ rejected.push({ index, pattern, reason: 'Invalid or unsupported path filter pattern' })
+ continue
+ }
+
+ if (complexity.programSize > remainingCompileSize || complexity.parseCost > remainingParseCost) {
+ rejected.push({ index, pattern, reason: 'Path filter pattern is too complex' })
+ continue
+ }
+
+ // Standalone estimates include shared overhead, so their sum also bounds the final combined program.
+ // Failed RE2 syntax checks consume work too; never refund either reservation.
+ remainingCompileSize -= complexity.programSize
+ remainingParseCost -= complexity.parseCost
+
+ try {
+ matcher = compileRE2PathFilter(pattern || '(?:)')
+ } catch (error) {
+ if (error instanceof PathFilterError) {
+ rejected.push({ index, pattern, reason: error.message })
+ continue
+ }
+ throw error
+ }
+
+ for (const name of complexity.captureNames) {
+ namedGroups.add(name)
+ }
+ accepted.push(pattern)
+ }
+
+ if (accepted.length > 1) {
+ matcher = compileRE2PathFilter(accepted.join('|'))
+ }
+ return { matcher, rejected }
+}
+
+export function compilePathFilters(source: string | null): SyncPathFiltersResult {
+ if (source === null || source === '') {
+ return { matcher: null, rejected: [] }
+ }
+ if (typeof source !== 'string') {
+ throw new PathFilterError('Invalid path filters')
+ }
+ if (source.length > SYNC_MAX_PATH_FILTER_PATTERN_LENGTH) {
+ throw new PathFilterError('Path filter pattern is too long')
+ }
+
+ let alternatives: AST.Alternative[]
+ try {
+ alternatives = parsePathFilterAlternatives(source)
+ } catch (error) {
+ if (error instanceof SyntaxError || error instanceof RangeError) {
+ const reason = error instanceof RangeError ? 'Path filter pattern is too complex' : 'Invalid or unsupported path filter pattern'
+ return { matcher: null, rejected: [{ index: 0, pattern: source, reason }] }
+ }
+ throw error
+ }
+
+ return compilePathFilterAlternatives(alternatives)
+}
+
+export function validatePathFilterList(filters: readonly string[]): number[] {
+ if (!Array.isArray(filters)) {
+ throw new PathFilterError('Invalid path filters')
+ }
+
+ const invalidIndexes = new Set<number>()
+ const candidates: string[] = []
+ const candidateIndexes: number[] = []
+
+ // Discard entries that cannot safely participate in the combined parse while
+ // preserving their original indexes for per-field feedback.
+ for (const [index, filter] of filters.entries()) {
+ if (index >= SYNC_MAX_PATH_FILTERS || typeof filter !== 'string' || filter.length > SYNC_MAX_PATH_FILTER_LENGTH) {
+ invalidIndexes.add(index)
+ } else {
+ candidates.push(filter)
+ candidateIndexes.push(index)
+ }
+ }
+
+ if (!candidates.length) {
+ return [...invalidIndexes]
+ }
+
+ let alternatives: AST.Alternative[]
+ try {
+ const source = buildPathFilterSource(candidates)!
+ alternatives = parsePathFilterAlternatives(source)
+ } catch (error) {
+ if (error instanceof PathFilterError || error instanceof SyntaxError || error instanceof RangeError) {
+ return [...filters.keys()]
+ }
+ throw error
+ }
+
+ let rejected: SyncPathFilterRejection[]
+ try {
+ rejected = compilePathFilterAlternatives(alternatives).rejected
+ } catch (error) {
+ // Only controlled compilation errors invalidate candidates; engine failures
+ // must still surface instead of being mistaken for user input errors.
+ if (error instanceof PathFilterError) {
+ return [...filters.keys()]
+ }
+ throw error
+ }
+
+ if (!rejected.length) {
+ return [...invalidIndexes]
+ }
+
+ let offset = 0
+ const ranges = candidates.map((filter, index) => {
+ const range = { index: candidateIndexes[index], start: offset, end: offset + filter.length }
+ offset = range.end + 1
+ return range
+ })
+
+ // Both collections follow source order; inclusive bounds retain empty alternatives.
+ let firstRange = 0
+ for (const rejection of rejected) {
+ const alternative = alternatives[rejection.index]
+ while (firstRange < ranges.length && ranges[firstRange].end < alternative.start) {
+ firstRange++
+ }
+ for (let index = firstRange; index < ranges.length && ranges[index].start <= alternative.end; index++) {
+ invalidIndexes.add(ranges[index].index)
+ }
+ }
+
+ return [...invalidIndexes].sort((a, b) => a - b)
+}
diff --git a/backend/src/applications/sync/utils/path-filters/path-filter-complexity.spec.ts b/backend/src/applications/sync/utils/path-filters/path-filter-complexity.spec.ts
new file mode 100644
index 00000000..9a29bfd4
--- /dev/null
+++ b/backend/src/applications/sync/utils/path-filters/path-filter-complexity.spec.ts
@@ -0,0 +1,40 @@
+import { RegExpParser } from '@eslint-community/regexpp'
+import { getPathFilterAlternativeComplexity, parsePathFilterAlternatives } from './path-filter-complexity.js'
+
+afterEach(() => vi.restoreAllMocks())
+
+describe(getPathFilterAlternativeComplexity.name, () => {
+ it('reuses the parsed alternatives to collect costs and capture names', () => {
+ const parse = vi.spyOn(RegExpParser.prototype, 'parsePattern')
+ const alternatives = parsePathFilterAlternatives('[A-Z]{0}|(?<entry>[a-z]){0}')
+
+ expect(alternatives.map((alternative) => alternative.raw)).toEqual(['[A-Z]{0}', '(?<entry>[a-z]){0}'])
+ expect(alternatives.map((alternative) => getPathFilterAlternativeComplexity(alternative, 3, 26))).toEqual([
+ { programSize: 3, parseCost: 26, captureNames: [] },
+ { programSize: 3, parseCost: 26, captureNames: ['entry'] }
+ ])
+ expect(parse).toHaveBeenCalledOnce()
+ })
+
+ it.each([
+ { pattern: '[A-Z]', programLimit: 3, parseLimit: 26, programSize: 3 },
+ { pattern: '[A-Z]', programLimit: 2, parseLimit: 26, programSize: 3 },
+ { pattern: '[A-Z]', programLimit: 3, parseLimit: 25, programSize: 4 },
+ { pattern: '(?:a{1000}[A-Z]){0}', programLimit: 3, parseLimit: 26, programSize: 3 }
+ ])('bounds program and parsing costs for $pattern', ({ pattern, programLimit, parseLimit, programSize }) => {
+ const [alternative] = parsePathFilterAlternatives(pattern)
+
+ expect(getPathFilterAlternativeComplexity(alternative, programLimit, parseLimit)).toEqual({
+ programSize,
+ parseCost: 26,
+ captureNames: []
+ })
+ })
+
+ it.each(['\\w', '[\\w]'])('charges shorthand classes inside and outside brackets: %s', (pattern) => {
+ const [alternative] = parsePathFilterAlternatives(pattern)
+
+ expect(getPathFilterAlternativeComplexity(alternative, 3, 128)).toEqual({ programSize: 3, parseCost: 128, captureNames: [] })
+ expect(getPathFilterAlternativeComplexity(alternative, 3, 127).programSize).toBe(4)
+ })
+})
diff --git a/backend/src/applications/sync/utils/path-filters/path-filter-complexity.ts b/backend/src/applications/sync/utils/path-filters/path-filter-complexity.ts
new file mode 100644
index 00000000..d2885e10
--- /dev/null
+++ b/backend/src/applications/sync/utils/path-filters/path-filter-complexity.ts
@@ -0,0 +1,126 @@
+import { type AST, RegExpParser } from '@eslint-community/regexpp'
+import type { SyncPathFilterComplexity, SyncPathFilterParseBudget } from './path-filter-compiler.interface.js'
+
+const pathFilterParser = new RegExpParser({ ecmaVersion: 2025 })
+const RE2_PROGRAM_OVERHEAD = 2
+// RE2JS expands predefined classes into rune ranges while parsing them.
+const RE2_ASCII_CLASS_PARSE_COST = 128
+const SUPPORTED_CLASS_ESCAPE_PATTERN = /^\\(?:[0fnrtv]|x[0-9a-fA-F]{2}|[^a-zA-Z0-9\u0080-\uffff])$/
+const LEADING_ZERO_REPEAT_PATTERN = /^\{(?:0\d|[0-9]+,0\d)/
+
+function getClassCharacterValue(character: AST.Character): number {
+ // Only accept escapes whose values agree between legacy JavaScript and RE2JS.
+ if (character.raw.startsWith('\\') && !SUPPORTED_CLASS_ESCAPE_PATTERN.test(character.raw)) {
+ throw new SyntaxError('Unsupported path filter character class escape')
+ }
+
+ return character.value
+}
+
+function consumeCharacterClassParseBudget(characterClass: AST.CharacterClass, parseBudget: SyncPathFilterParseBudget): void {
+ if (characterClass.unicodeSets || characterClass.elements.length === 0) {
+ throw new SyntaxError('Unsupported path filter character class')
+ }
+
+ for (const element of characterClass.elements) {
+ if (element.type === 'CharacterClassRange') {
+ const min = getClassCharacterValue(element.min)
+ const max = getClassCharacterValue(element.max)
+ if ((min >= 0xd800 && min <= 0xdfff) || (max >= 0xd800 && max <= 0xdfff)) {
+ throw new SyntaxError('Astral path filter character class ranges are not supported')
+ }
+ parseBudget.remaining -= max - min + 1
+ } else if (element.type === 'Character') {
+ getClassCharacterValue(element)
+ if (element.raw === '[' && characterClass.raw[element.end - characterClass.start] === ':') {
+ throw new SyntaxError('POSIX path filter character classes are not supported')
+ }
+ parseBudget.remaining -= 1
+ } else {
+ parseBudget.remaining -= RE2_ASCII_CLASS_PARSE_COST
+ }
+
+ if (parseBudget.remaining < 0) {
+ return
+ }
+ }
+}
+
+function estimateProgramSize(
+ alternatives: AST.Alternative[],
+ overflow: number,
+ parseBudget: SyncPathFilterParseBudget,
+ captureNames: string[]
+): number {
+ let size = Math.max(0, alternatives.length - 1)
+
+ // Clamp every addition so large bounded repetitions cannot grow the estimate itself.
+ for (const alternative of alternatives) {
+ for (const element of alternative.elements) {
+ size = Math.min(overflow, size + estimateElementProgramSize(element, overflow, parseBudget, captureNames))
+ if (parseBudget.remaining < 0) {
+ return overflow
+ }
+ }
+ }
+
+ return Math.max(1, size)
+}
+
+function estimateElementProgramSize(element: AST.Element, overflow: number, parseBudget: SyncPathFilterParseBudget, captureNames: string[]): number {
+ if (element.type === 'CapturingGroup' && element.name !== null) {
+ captureNames.push(element.name)
+ }
+
+ // RE2 quoting would turn regexpp's quantifiers into literals during compilation.
+ if (element.type === 'Character' && (element.raw === '\\Q' || element.raw === '\\E')) {
+ throw new SyntaxError('RE2 quoted literals are not supported in path filters')
+ }
+
+ if (element.type === 'Quantifier') {
+ const quantifierStart = element.element.end - element.start
+ // RE2JS treats repeat counts with leading zeros as literals, unlike regexpp.
+ if (element.raw[quantifierStart] === '{' && LEADING_ZERO_REPEAT_PATTERN.test(element.raw.slice(quantifierStart))) {
+ throw new SyntaxError('Path filter quantifiers must not contain leading zeros')
+ }
+
+ const elementSize = estimateElementProgramSize(element.element, overflow, parseBudget, captureNames)
+
+ if (!Number.isFinite(element.max)) {
+ return Math.min(overflow, element.min === 0 ? elementSize + 2 : elementSize * element.min + 1)
+ }
+
+ // RE2 expands bounded repetitions rather than evaluating them as one instruction.
+ return Math.max(1, Math.min(overflow, elementSize * element.max + (element.max - element.min)))
+ }
+
+ if ('alternatives' in element) {
+ const size = estimateProgramSize(element.alternatives, overflow, parseBudget, captureNames)
+ return element.type === 'Group' ? size : Math.min(overflow, size + 2)
+ }
+
+ if (element.type === 'CharacterClass') {
+ consumeCharacterClassParseBudget(element, parseBudget)
+ } else if (element.type === 'CharacterSet' && element.kind !== 'any') {
+ parseBudget.remaining -= RE2_ASCII_CLASS_PARSE_COST
+ }
+
+ return 1
+}
+
+export function parsePathFilterAlternatives(source: string): AST.Alternative[] {
+ return pathFilterParser.parsePattern(source).alternatives
+}
+
+export function getPathFilterAlternativeComplexity(
+ alternative: AST.Alternative,
+ programSizeLimit: number,
+ parseCostLimit: number
+): SyncPathFilterComplexity {
+ const overflow = programSizeLimit + 1
+ const parseBudget: SyncPathFilterParseBudget = { remaining: parseCostLimit }
+ const captureNames: string[] = []
+ const programSize = Math.min(overflow, estimateProgramSize([alternative], overflow, parseBudget, captureNames) + RE2_PROGRAM_OVERHEAD)
+
+ return { programSize, parseCost: parseCostLimit - parseBudget.remaining, captureNames }
+}
diff --git a/package-lock.json b/package-lock.json
index 43d11d01..5907e023 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -43,6 +43,7 @@
"name": "@sync-in-server/backend",
"license": "AGPL-3.0-or-later",
"dependencies": {
+ "@eslint-community/regexpp": "^4.12.2",
"@fastify/cookie": "^11.0.2",
"@fastify/helmet": "^13.0.2",
"@fastify/multipart": "^10.0.0",
@@ -87,8 +88,8 @@
"pino-http": "^11.0.0",
"pino-pretty": "^13.2.0",
"qrcode-generator": "^2.0.4",
+ "re2js": "^2.9.0",
"redis": "^6.3.0",
- "safe-regex2": "^5.1.1",
"sax": "^1.6.0",
"sharp": "^0.35.2",
"socket.io": "4.8.4",
@@ -3543,7 +3544,6 @@
"version": "4.12.2",
"resolved": "https://registry.npmjs.org/@eslint-community/regexpp/-/regexpp-4.12.2.tgz",
"integrity": "sha512-EriSTlt5OC9/7SXkRSCAhfSxxoSUgBm33OH+IkwbdpgoqsSsUg7y3uh+IICI/Qg4BBWr3U2i39RpmycbxMq4ew==",
- "dev": true,
"license": "MIT",
"engines": {
"node": "^12.0.0 || ^14.0.0 || >=16.0.0"
@@ -18880,6 +18880,15 @@
"node": ">= 0.10"
}
},
+ "node_modules/re2js": {
+ "version": "2.9.0",
+ "resolved": "https://registry.npmjs.org/re2js/-/re2js-2.9.0.tgz",
+ "integrity": "sha512-rZpqkd4ETP3GrQ5mDLzADr14OleeKdb/40lG84WE8absRfsVLJpWt2ifjtYYeoPqeouIkcxhTlurxIXfR5kR5Q==",
+ "license": "MIT",
+ "engines": {
+ "node": ">=20.0.0"
+ }
+ },
"node_modules/readdirp": {
"version": "5.1.1",
"resolved": "https://registry.npmjs.org/readdirp/-/readdirp-5.1.1.tgz",