Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.
TypeScript cloud-alternativecollaborationcollaborative-editingdropboxfile-serverfile-sharingfile-storagefile-syncfull-text-searchhomelabnextcloudon-premiseopen-sourceprivacy-firstprivate-cloudrealtimesecureself-hostedstorage-serverwebdav
fix(backend:files): escape search terms for Unicode regex
johaven6b5a0c526c50
diff --git a/backend/src/applications/files/adapters/files-content-store-mysql.service.spec.ts b/backend/src/applications/files/adapters/files-content-store-mysql.service.spec.tsindex 13df0ff4..dece6008 100644--- a/backend/src/applications/files/adapters/files-content-store-mysql.service.spec.ts+++ b/backend/src/applications/files/adapters/files-content-store-mysql.service.spec.ts@@ -404,6 +404,29 @@ describe(FilesContentStoreMySQL.name, () => {expect(res[0].content).toBeUndefined()expect(res[0].matches).toEqual(['<mark>日本語</mark>の文書です。'])})++ it('should highlight hyphenated full-text matches', async () => {+ db.execute.mockResolvedValueOnce([[{ id: 4, sourceIndex: 'files_content_u_1', score: 1 }]])+ db.execute.mockResolvedValueOnce([+ [+ {+ id: 4,+ sourceIndex: 'files_content_u_1',+ path: '/docs',+ name: 'euro-office.txt',+ mime: 'text/plain',+ mtime: 1730000000003,+ content: 'Le dossier euro-office est prêt.'+ }+ ]+ ])++ const res = await filesIndexerMySQL.searchRecords(['files_content_u_1'], 'euro-office', 10)++ expect(db.execute).toHaveBeenCalledTimes(2)+ expect(res[0].content).toBeUndefined()+ expect(res[0].matches).toEqual(['Le dossier <mark>euro-office</mark> est prêt.'])+ })})describe('cleanIndexes', () => {diff --git a/backend/src/applications/files/utils/files-search.spec.ts b/backend/src/applications/files/utils/files-search.spec.tsindex 9b1391ad..98de927e 100644--- a/backend/src/applications/files/utils/files-search.spec.ts+++ b/backend/src/applications/files/utils/files-search.spec.ts@@ -63,6 +63,10 @@ describe('files search utilities', () => {expect(analyzeTerms('file.txt')).toEqual(['file\\.txt'])expect(analyzeTerms('file.txt', false, false)).toEqual(['file.txt'])})++ it('should not escape punctuation that is invalid in Unicode regular expressions', () => {+ expect(analyzeTerms('euro-office')).toEqual(['euro-office'])+ })})describe(genTermsPattern.name, () => {@@ -73,6 +77,13 @@ describe('files search utilities', () => {expect(regexp.test('cañyon')).toBe(true)expect(regexp.test('other')).toBe(false)})++ it('should generate a Unicode regular expression compatible pattern with hyphenated terms', () => {+ const regexp = new RegExp(`^(${genTermsPattern(['euro-office'])})$`, 'iu')++ expect(regexp.test('eurô-office')).toBe(true)+ expect(regexp.test('euro office')).toBe(false)+ })})describe(genRegexPositiveAndNegativeTerms.name, () => {@@ -89,6 +100,13 @@ describe('files search utilities', () => {expect(regexp.test('суперфайл.txt')).toBe(false)})+ it('should match hyphenated terms with Unicode-aware boundaries', () => {+ const regexp = genRegexPositiveAndNegativeTerms('euro-office')++ expect(regexp.test('archive/euro-office.pdf')).toBe(true)+ expect(regexp.test('archive/euro office.pdf')).toBe(false)+ })+it('should exclude negative CJK terms', () => {const regexp = genRegexPositiveAndNegativeTerms('文書 -秘密')diff --git a/backend/src/applications/files/utils/files-search.ts b/backend/src/applications/files/utils/files-search.tsindex 77da931e..8145aeab 100644--- a/backend/src/applications/files/utils/files-search.ts+++ b/backend/src/applications/files/utils/files-search.ts@@ -1,4 +1,4 @@-import { escapeString } from '../../../common/functions'+import { regexpEscape } from '../../../common/functions'import { MIN_CHARS_TO_SEARCH } from '../constants/indexing'const regexMatchSearchBoolean = new RegExp(`([+-]?)(?:"([^"]+)"|(\\S+))`)@@ -80,7 +80,7 @@ export function analyzeTerms(search: string, onlyAllowNegative = false, escapeFoterm = term.substring(0, term.length - 1)}- return escapeForRegexp ? escapeString(term) : term+ return escapeForRegexp ? escapeSearchTermRegexp(term) : term}).filter(Boolean)}@@ -149,3 +149,7 @@ function genAccentInsensitiveRegexpPattern(input: string): string {.map((char: string) => accentToBaseMap.get(char) || char).join('')}++function escapeSearchTermRegexp(input: string): string {+ return input.replace(regexpEscape, '\\$&')+}