Skip to content

A local-first personal finance app

[AI] Match nothing for an invalid regex instead of failing the query (#9238)

Emdadul Haque Apu253823d390dc
diff --git a/packages/loot-core/src/platform/server/sqlite/index.electron.ts b/packages/loot-core/src/platform/server/sqlite/index.electron.ts
index d0993a2ce9..53e755513a 100644
--- a/packages/loot-core/src/platform/server/sqlite/index.electron.ts
+++ b/packages/loot-core/src/platform/server/sqlite/index.electron.ts
@@ -6,6 +6,7 @@ import { getDataDir, readFile, removeFile } from '#platform/server/fs';
import { logger } from '#platform/server/log';
import { normalise } from './normalise';
+import { regexp } from './regexp';
import type { SqlParam } from './types';
import { unicodeLike } from './unicodeLike';
@@ -121,10 +122,6 @@ export async function asyncTransaction(
}
}
-function regexp(regex: string, text: string | null) {
- return new RegExp(regex).test(text || '') ? 1 : 0;
-}
-
export function openDatabase(pathOrBuffer: string | Buffer): SQL.Database {
const db = new SQL(pathOrBuffer);
// Define Unicode-aware LOWER, UPPER, and LIKE implementation.
diff --git a/packages/loot-core/src/platform/server/sqlite/index.test.ts b/packages/loot-core/src/platform/server/sqlite/index.test.ts
index cf27e12536..5623bcadb8 100644
--- a/packages/loot-core/src/platform/server/sqlite/index.test.ts
+++ b/packages/loot-core/src/platform/server/sqlite/index.test.ts
@@ -194,6 +194,26 @@ describe('Web sqlite', () => {
expect(rows[0].id).toBe('id1');
});
+ it('should not match anything with an invalid regex', async () => {
+ const db = await openDatabase();
+ execQuery(db, initSQL);
+
+ runQuery(
+ db,
+ "INSERT INTO textstrings (id, string) VALUES ('id1', 'asdf\\')",
+ );
+
+ for (const pattern of ['\\', 'asdf\\', '[']) {
+ const rows = runQuery(
+ db,
+ 'SELECT id FROM textstrings where REGEXP(?, string)',
+ [pattern],
+ true,
+ );
+ expect(rows).toEqual([]);
+ }
+ });
+
it('should free statements it prepares', async () => {
const db = await openDatabase();
execQuery(db, initSQL);
diff --git a/packages/loot-core/src/platform/server/sqlite/index.ts b/packages/loot-core/src/platform/server/sqlite/index.ts
index c40813cc68..ce2a7d23b6 100644
--- a/packages/loot-core/src/platform/server/sqlite/index.ts
+++ b/packages/loot-core/src/platform/server/sqlite/index.ts
@@ -5,6 +5,7 @@ import type { Database, SqlJsStatic, Statement } from '@jlongster/sql.js';
import { logger } from '#platform/server/log';
import { normalise } from './normalise';
+import { regexp } from './regexp';
import type { SqlParam } from './types';
import { unicodeLike } from './unicodeLike';
@@ -217,10 +218,6 @@ export async function asyncTransaction(db: Database, fn: () => Promise<void>) {
}
}
-function regexp(regex: string, text: string) {
- return new RegExp(regex).test(text || '') ? 1 : 0;
-}
-
export async function openDatabase(pathOrBuffer?: string | Uint8Array) {
let db = null;
if (pathOrBuffer) {
diff --git a/packages/loot-core/src/platform/server/sqlite/regexp.test.ts b/packages/loot-core/src/platform/server/sqlite/regexp.test.ts
new file mode 100644
index 0000000000..0f66a584e0
--- /dev/null
+++ b/packages/loot-core/src/platform/server/sqlite/regexp.test.ts
@@ -0,0 +1,24 @@
+import { regexp } from './regexp';
+
+describe('REGEXP functionality', () => {
+ it('should match a valid pattern', () => {
+ expect(regexp('n.', 'not empty string')).toBe(1);
+ expect(regexp('^#\\d+$', '#123')).toBe(1);
+ });
+
+ it('should not match a valid pattern that does not apply', () => {
+ expect(regexp('^#\\d+$', 'no number')).toBe(0);
+ });
+
+ it('should treat null text as an empty string', () => {
+ expect(regexp('^$', null)).toBe(1);
+ expect(regexp('a', null)).toBe(0);
+ });
+
+ it('should not match anything with an invalid pattern', () => {
+ expect(regexp('\\', '\\')).toBe(0);
+ expect(regexp('asdf\\', 'asdf\\')).toBe(0);
+ expect(regexp('[', '[')).toBe(0);
+ expect(regexp('(', null)).toBe(0);
+ });
+});
diff --git a/packages/loot-core/src/platform/server/sqlite/regexp.ts b/packages/loot-core/src/platform/server/sqlite/regexp.ts
new file mode 100644
index 0000000000..b6582443e1
--- /dev/null
+++ b/packages/loot-core/src/platform/server/sqlite/regexp.ts
@@ -0,0 +1,12 @@
+export function regexp(regex: string, text: string | null) {
+ let pattern: RegExp;
+ try {
+ pattern = new RegExp(regex);
+ } catch {
+ // An invalid pattern, such as one still being typed into a rule
+ // condition, matches nothing instead of failing the whole query. This
+ // is how `matches` conditions already behave when evaluated in memory.
+ return 0;
+ }
+ return pattern.test(text || '') ? 1 : 0;
+}
diff --git a/packages/loot-core/src/server/transactions/transaction-rules.test.ts b/packages/loot-core/src/server/transactions/transaction-rules.test.ts
index 24140248e0..4701f944aa 100644
--- a/packages/loot-core/src/server/transactions/transaction-rules.test.ts
+++ b/packages/loot-core/src/server/transactions/transaction-rules.test.ts
@@ -850,6 +850,32 @@ describe('Transaction rules', () => {
]);
});
+ test('transactions can be queried by a notes regex that is still being typed', async () => {
+ await loadRules();
+ const account = await db.insertAccount({ name: 'bank' });
+
+ await db.insertTransaction({
+ id: '1',
+ date: '2020-10-01',
+ account,
+ notes: 'Invoice #123',
+ amount: 123,
+ });
+
+ // An incomplete pattern matches nothing instead of failing the query
+ for (const value of ['#\\', '#[', '#\\d+(']) {
+ const transactions = await getMatchingTransactions([
+ { field: 'notes', op: 'matches', value },
+ ]);
+ expect(transactions).toEqual([]);
+ }
+
+ const transactions = await getMatchingTransactions([
+ { field: 'notes', op: 'matches', value: '#\\d+' },
+ ]);
+ expect(transactions.map(t => t.id)).toEqual(['1']);
+ });
+
test('transactions can be queried by hasTags if no "#" is included', async () => {
await loadRules();
const account = await db.insertAccount({ name: 'bank' });
diff --git a/upcoming-release-notes/rules-invalid-regex-preview.md b/upcoming-release-notes/rules-invalid-regex-preview.md
new file mode 100644
index 0000000000..e3e4f6846a
--- /dev/null
+++ b/upcoming-release-notes/rules-invalid-regex-preview.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [Apu-Emdad]
+---
+
+Stop the rule editor from showing an error while a "matches" regex is still being typed