A local-first personal finance app
Fix/5840 escape search wildcards (#7270)
Eduardo Pio092b85e0753b
diff --git a/packages/desktop-client/src/queries/index.ts b/packages/desktop-client/src/queries/index.tsindex dc2c791069..9b5ac7ec42 100644--- a/packages/desktop-client/src/queries/index.ts+++ b/packages/desktop-client/src/queries/index.ts@@ -84,6 +84,7 @@ export function transactionsSearch(dateFormat: SyncedPrefs['dateFormat'],) {const amount = currencyToAmount(search);+ const escapedSearch = search.replace(/[\\%?]/g, '\\$&');// Support various date formatslet parsedDate;@@ -97,11 +98,11 @@ export function transactionsSearch(return currentQuery.filter({$or: {- 'payee.name': { $like: `%${search}%` },- 'payee.transfer_acct.name': { $like: `%${search}%` },- notes: { $like: `%${search}%` },- 'category.name': { $like: `%${search}%` },- 'account.name': { $like: `%${search}%` },+ 'payee.name': { $like: `%${escapedSearch}%` },+ 'payee.transfer_acct.name': { $like: `%${escapedSearch}%` },+ notes: { $like: `%${escapedSearch}%` },+ 'category.name': { $like: `%${escapedSearch}%` },+ 'account.name': { $like: `%${escapedSearch}%` },$or: [isDateValid(parsedDate) && { date: dayFromDate(parsedDate) },amount != null && {diff --git a/packages/loot-core/src/platform/server/sqlite/unicodeLike.test.ts b/packages/loot-core/src/platform/server/sqlite/unicodeLike.test.tsindex 0d8809ad74..bd953a193f 100644--- a/packages/loot-core/src/platform/server/sqlite/unicodeLike.test.ts+++ b/packages/loot-core/src/platform/server/sqlite/unicodeLike.test.ts@@ -55,4 +55,18 @@ describe('unicode LIKE functionality', () => {expect(result).toBe(0);});++ it('should treat \\% as a literal % character instead of a wildcard', () => {+ expect(unicodeLike('100\\%', '100%')).toBe(1);+ expect(unicodeLike('100\\%', '1000')).toBe(0);+ });++ it('should treat \\? as a literal ? character instead of a wildcard', () => {+ expect(unicodeLike('what\\?', 'what?')).toBe(1);+ expect(unicodeLike('what\\?', 'what!')).toBe(0);+ });++ it('should treat \\\\ as a literal \\ character', () => {+ expect(unicodeLike('c:\\\\test', 'c:\\test')).toBe(1);+ });});diff --git a/packages/loot-core/src/platform/server/sqlite/unicodeLike.ts b/packages/loot-core/src/platform/server/sqlite/unicodeLike.tsindex 7e60269304..1e7051d96b 100644--- a/packages/loot-core/src/platform/server/sqlite/unicodeLike.ts+++ b/packages/loot-core/src/platform/server/sqlite/unicodeLike.ts@@ -2,28 +2,44 @@ import { LRUCache } from 'lru-cache';const likePatternCache = new LRUCache<string, RegExp>({ max: 500 });+const REGEX_SPECIAL = /[.*+^${}()|[\]\\?]/g;++function likePatternToRegex(pattern: string): RegExp {+ let regexStr = '';+ let i = 0;+ while (i < pattern.length) {+ const ch = pattern[i];+ if (ch === '\\' && i + 1 < pattern.length) {+ const next = pattern[i + 1];+ if (next === '%' || next === '?' || next === '\\') {+ // escaped wildcard — treat as literal character+ regexStr += next.replace(REGEX_SPECIAL, '\\$&'); // ? added here+ i += 2;+ continue;+ }+ }+ if (ch === '%') {+ regexStr += '.*';+ } else if (ch === '?') {+ regexStr += '.';+ } else {+ regexStr += ch.replace(REGEX_SPECIAL, '\\$&');+ }+ i++;+ }+ return new RegExp(regexStr, 'i');+}+export function unicodeLike(pattern: string | null,value: string | null,): number {- if (!pattern) {- return 0;- }-- if (!value) {- value = '';- }+ if (!pattern) return 0;+ if (!value) value = '';let cachedRegExp = likePatternCache.get(pattern);if (!cachedRegExp) {- // we don't escape ? and % because we don't know- // whether they originate from the user input or from our query compiler.- // Maybe improve the query compiler to correctly process these characters?- const processedPattern = pattern- .replace(/[.*+^${}()|[\]\\]/g, '\\$&')- .replaceAll('?', '.')- .replaceAll('%', '.*');- cachedRegExp = new RegExp(processedPattern, 'i');+ cachedRegExp = likePatternToRegex(pattern);likePatternCache.set(pattern, cachedRegExp);}diff --git a/upcoming-release-notes/7270.md b/upcoming-release-notes/7270.mdnew file mode 100644index 0000000000..2d7a7b5c1c--- /dev/null+++ b/upcoming-release-notes/7270.md@@ -0,0 +1,6 @@+---+category: Bugfixes+authors: [eduardopio03]+---++Fix transaction quick search incorrectly treating "?" and "%" as wildcards, causing all transactions to be returned instead of only those matching the literal character