Skip to content

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

fix(frontend:sync): harden search and synchronized path matching

johaven874de6f2a0a9
diff --git a/frontend/src/app/applications/sync/components/wizard/sync-wizard-client.component.ts b/frontend/src/app/applications/sync/components/wizard/sync-wizard-client.component.ts
index 74c581b2..fd3b8ab8 100644
--- a/frontend/src/app/applications/sync/components/wizard/sync-wizard-client.component.ts
+++ b/frontend/src/app/applications/sync/components/wizard/sync-wizard-client.component.ts
@@ -10,6 +10,7 @@ import type { FileModel } from '../../../files/models/file.model'
import { SyncPathModel } from '../../models/sync-path.model'
import { SyncService } from '../../services/sync.service'
import { SYNC_ICON, SYNC_PATH, SYNC_TITLE } from '../../sync.constants'
+import { isSameOrDescendantPath } from '../../sync.utils'
@Component({
selector: 'app-sync-wizard-client',
@@ -114,7 +115,7 @@ export class SyncWizardClientComponent {
private checkSelection() {
const root = this.store
.clientSyncPaths()
- .find((sp: SyncPathModel) => new RegExp(`^${sp.settings.localPath}((\\/.*)+|\\/?)$`).test(this.syncService.wizard.localPath.path))
+ .find((sp: SyncPathModel) => isSameOrDescendantPath(sp.settings.localPath, this.syncService.wizard.localPath.path))
if (root) {
this.pathIsValid = false
this.syncService.wizard.localPath.mimeUrl = getAssetsMimeUrl(`${mimeDirectory}_sync`)
diff --git a/frontend/src/app/applications/sync/components/wizard/sync-wizard-server.component.ts b/frontend/src/app/applications/sync/components/wizard/sync-wizard-server.component.ts
index 4f4190c2..6b4404ef 100644
--- a/frontend/src/app/applications/sync/components/wizard/sync-wizard-server.component.ts
+++ b/frontend/src/app/applications/sync/components/wizard/sync-wizard-server.component.ts
@@ -16,7 +16,7 @@ import { UserService } from '../../../users/user.service'
import { SyncWizardPath } from '../../models/sync-wizard-path.model'
import { SyncService } from '../../services/sync.service'
import { SYNC_ICON, SYNC_PATH, SYNC_TITLE } from '../../sync.constants'
-import { isSynchronizable } from '../../sync.utils'
+import { isSameOrDescendantPath, isSynchronizable } from '../../sync.utils'
@Component({
selector: 'app-sync-wizard-server',
@@ -179,9 +179,8 @@ export class SyncWizardServerComponent {
private setCurrentPaths(paths?: SyncWizardPath[]) {
for (const syncPath of this.store.clientSyncPaths()) {
- const searchRegexp = new RegExp(`^${syncPath.settings.remotePath}((\\/.*)+|\\/?)$`)
for (const p of paths) {
- if (searchRegexp.test(p.serverPath)) {
+ if (isSameOrDescendantPath(syncPath.settings.remotePath, p.serverPath)) {
p.setAlreadySynced()
}
}
diff --git a/frontend/src/app/applications/sync/sync.utils.ts b/frontend/src/app/applications/sync/sync.utils.ts
index a7893612..c62dd2b9 100644
--- a/frontend/src/app/applications/sync/sync.utils.ts
+++ b/frontend/src/app/applications/sync/sync.utils.ts
@@ -32,3 +32,18 @@ export function getServerPath(path: string): string {
}
return segments.join('/')
}
+
+function normalizeSyncPath(path: string): string {
+ const normalizedPath = path.replace(/\\/g, '/').replace(/\/+$/, '')
+ return normalizedPath || '/'
+}
+
+export function isSameOrDescendantPath(basePath: string, candidatePath: string): boolean {
+ if (!basePath || !candidatePath) return false
+
+ const normalizedBasePath = normalizeSyncPath(basePath)
+ const normalizedCandidatePath = normalizeSyncPath(candidatePath)
+ const descendantPrefix = normalizedBasePath === '/' ? '/' : `${normalizedBasePath}/`
+
+ return normalizedCandidatePath === normalizedBasePath || normalizedCandidatePath.startsWith(descendantPrefix)
+}
diff --git a/frontend/src/app/common/utils/functions.ts b/frontend/src/app/common/utils/functions.ts
index d8a2aa49..203b65f6 100644
--- a/frontend/src/app/common/utils/functions.ts
+++ b/frontend/src/app/common/utils/functions.ts
@@ -1,6 +1,7 @@
import { UrlSegment } from '@angular/router'
import { encodeUrl } from '@sync-in-server/backend/src/common/shared'
import { themeDark, themeLight } from '../../layout/layout.interfaces'
+import { escapeRegexp } from './regexp'
export const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
export const reservedUrlChars = new Map([
@@ -150,7 +151,7 @@ export function titleCase(input: string) {
export const originalOrderKeyValue = () => 0
export function filterArray<T>(search: string, collection: T[], field?: string): T[] {
- const searchRegexp = new RegExp(search, 'i')
+ const searchRegexp = new RegExp(escapeRegexp(search), 'i')
if (field) {
return collection.filter((obj) => searchRegexp.test(obj[field].normalize()))
} else {