Skip to content

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

fix(sync): validate and ignore empty path filters

johaven4890b398efcb
diff --git a/backend/src/applications/sync/utils/path-filters/README.md b/backend/src/applications/sync/utils/path-filters/README.md
index a12ae5fb..6811b157 100644
--- a/backend/src/applications/sync/utils/path-filters/README.md
+++ b/backend/src/applications/sync/utils/path-filters/README.md
@@ -7,9 +7,9 @@ complexity estimation, types, and tests synchronized. Only import extensions and
## Contract
-- `buildPathFilterSource()` validates the filter list and combined length, preserves regex syntax, and returns the historical `string | null` wire format.
+- `buildPathFilterSource()` validates the filter list and combined length, ignores empty entries, preserves non-empty 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.
+- `validatePathFilterList()` marks empty entries as invalid and 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/path-filter-compiler.spec.ts b/backend/src/applications/sync/utils/path-filters/path-filter-compiler.spec.ts
index 36767fa8..02fa5abb 100644
--- 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
@@ -9,10 +9,11 @@ afterEach(() => vi.restoreAllMocks())
describe(buildPathFilterSource.name, () => {
it.each([
{ filters: [], source: null },
- { filters: [''], source: '(?:)' },
- { filters: ['', 'a'], source: '|a' },
+ { filters: [''], source: null },
+ { filters: [' '], source: null },
+ { filters: ['', 'a'], source: 'a' },
{ filters: [' documents/.+\\.pdf$ ', '^資料/報告$', 'a|b'], source: ' documents/.+\\.pdf$ |^資料/報告$|a|b' }
- ])('preserves the filter source for $filters', ({ filters, source }) => {
+ ])('builds the filter source for $filters', ({ filters, source }) => {
expect(buildPathFilterSource(filters)).toBe(source)
})
@@ -43,6 +44,10 @@ describe(validatePathFilterList.name, () => {
it('marks every filter when the combined syntax cannot be parsed', () => {
expect(validatePathFilterList(['^documents/', '(', '^photos/'])).toEqual([0, 1, 2])
})
+
+ it('marks empty filters as invalid', () => {
+ expect(validatePathFilterList(['', ' ', '^documents/'])).toEqual([0, 1])
+ })
})
describe(compilePathFilters.name, () => {
@@ -56,10 +61,10 @@ describe(compilePathFilters.name, () => {
expect(adversarial?.test(`${'a'.repeat(28)}/`)).toBe(true)
})
- it('distinguishes absent filters from universal empty alternatives', () => {
+ it('treats empty filter entries as absent while preserving direct regex sources', () => {
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(buildPathFilterSource(['']))).toEqual({ matcher: null, rejected: [] })
expect(compilePathFilters('|^documents/').matcher?.test('archive/report.txt')).toBe(true)
})
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
index 83815d68..e8e5388a 100644
--- a/backend/src/applications/sync/utils/path-filters/path-filter-compiler.ts
+++ b/backend/src/applications/sync/utils/path-filters/path-filter-compiler.ts
@@ -17,23 +17,28 @@ export function buildPathFilterSource(filters: readonly string[]): string | null
throw new PathFilterError('Invalid path filters')
}
- let sourceLength = Math.max(0, filters.length - 1)
+ if (filters.length > SYNC_MAX_PATH_FILTERS) {
+ throw new PathFilterError('Too many path filters')
+ }
+
+ const nonEmptyFilters: string[] = []
+ let sourceLength = 0
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 (!filter.trim()) {
+ continue
+ }
+ sourceLength += filter.length + (nonEmptyFilters.length ? 1 : 0)
+ nonEmptyFilters.push(filter)
}
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
+ return nonEmptyFilters.length ? nonEmptyFilters.join('|') : null
}
// Compile only after validation and budget checks; unexpected engine failures must propagate.
@@ -150,12 +155,21 @@ export function validatePathFilterList(filters: readonly string[]): 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) {
+ if (index >= SYNC_MAX_PATH_FILTERS || typeof filter !== 'string') {
invalidIndexes.add(index)
- } else {
- candidates.push(filter)
- candidateIndexes.push(index)
+ continue
+ }
+ if (!filter.trim()) {
+ invalidIndexes.add(index)
+ continue
}
+ if (filter.length > SYNC_MAX_PATH_FILTER_LENGTH) {
+ invalidIndexes.add(index)
+ continue
+ }
+
+ candidates.push(filter)
+ candidateIndexes.push(index)
}
if (!candidates.length) {
diff --git a/frontend/src/app/applications/sync/components/dialogs/sync-path-settings.dialog.component.html b/frontend/src/app/applications/sync/components/dialogs/sync-path-settings.dialog.component.html
index 54862f70..0453901d 100644
--- a/frontend/src/app/applications/sync/components/dialogs/sync-path-settings.dialog.component.html
+++ b/frontend/src/app/applications/sync/components/dialogs/sync-path-settings.dialog.component.html
@@ -121,7 +121,7 @@
<button (click)="onCancel()" class="btn btn-secondary ms-auto" data-dismiss="modal" type="button" l10nTranslate>Cancel</button>
<button
(click)="onSubmit()"
- [disabled]="!syncPath.settings.name"
+ [disabled]="!syncPath.settings.name || (!confirmDeletion && hasInvalidFilters())"
class="btn {{ confirmDeletion ? 'btn-danger' : 'btn-primary' }}"
type="submit"
l10nTranslate
diff --git a/frontend/src/app/applications/sync/components/dialogs/sync-path-settings.dialog.component.ts b/frontend/src/app/applications/sync/components/dialogs/sync-path-settings.dialog.component.ts
index ed5f4fcf..9fde66d5 100755
--- a/frontend/src/app/applications/sync/components/dialogs/sync-path-settings.dialog.component.ts
+++ b/frontend/src/app/applications/sync/components/dialogs/sync-path-settings.dialog.component.ts
@@ -49,7 +49,7 @@ export class SyncPathSettingsDialogComponent implements OnInit {
[this.SYNC_PATH_FILTER_TYPE.START]: 'with a name or pattern',
[this.SYNC_PATH_FILTER_TYPE.IN]: 'with a name or pattern',
[this.SYNC_PATH_FILTER_TYPE.END]: "with the extension ('.mp3', '.avi', '.mov' ...)",
- [this.SYNC_PATH_FILTER_TYPE.EXPERT]: '[-+]?[0-9]*\\.?[0-9]*'
+ [this.SYNC_PATH_FILTER_TYPE.EXPERT]: '^(temp|cache)/.*\\.(log|tmp)$'
}
protected readonly icons = { LucideX, LucideRefreshCw, LucideSettings, LucideFunnel }
protected confirmDeletion = false
@@ -68,6 +68,8 @@ export class SyncPathSettingsDialogComponent implements OnInit {
this.onRemove()
return
}
+ if (this.hasInvalidFilters()) return
+
if (this.store.isElectronApp()) {
this.syncService.updatePath(this.syncPath.export(true)).then(() => {
this.syncService.refreshPaths().catch(console.error)