Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(search): write contains LIKE escapes as E'' literals

Yudhi Armyndharis45a0f9e3181f
diff --git a/src/modules/search/providers/builtin-fts.provider.contains.pg.spec.ts b/src/modules/search/providers/builtin-fts.provider.contains.pg.spec.ts
index 17d6536fc..42f92f839 100644
--- a/src/modules/search/providers/builtin-fts.provider.contains.pg.spec.ts
+++ b/src/modules/search/providers/builtin-fts.provider.contains.pg.spec.ts
@@ -244,6 +244,30 @@ const containsOn = new ConfigService({ search: { containsEnabled: true } });
expect((await provider.search({ q: 'price_100', match: 'contains' })).total).toBe(0);
});
+ it('matches the same where standard_conforming_strings is off, as a role or database default can set it', async () => {
+ const legacy = new DataSource({
+ ...connection(SCRATCH_DB),
+ extra: { options: '-c standard_conforming_strings=off' },
+ });
+ await legacy.initialize();
+ try {
+ expect(await legacy.query('SHOW standard_conforming_strings')).toEqual([
+ { standard_conforming_strings: 'off' },
+ ]);
+ await insert('m5', 's1', String.raw`saved to C:\Temp\new_report`, 50);
+ await insert('m6', 's1', String.raw`saved to C:\Temp\newxreport`, 60);
+ const onLegacy = new BuiltInFtsProvider(legacy, containsOn);
+ await onLegacy.buildContainsIndex();
+
+ const ids = async (q: string) => (await onLegacy.search({ q, match: 'contains' })).hits.map(h => h.messageId);
+ expect(await ids('10% discount')).toEqual(['m4']);
+ expect(await ids('invoice _')).toEqual([]);
+ expect(await ids(String.raw`c:\temp\new_rep`)).toEqual(['m5']);
+ } finally {
+ await legacy.destroy();
+ }
+ });
+
it('transliterates letters SQLite leaves alone, which docs/26 lists as a dialect difference', async () => {
await insert('m5', 's1', 'Shipped from Łódź to the Straße', 50);
expect((await provider.search({ q: 'lodz', match: 'contains' })).hits.map(h => h.messageId)).toEqual(['m5']);
diff --git a/src/modules/search/providers/builtin-fts.provider.ts b/src/modules/search/providers/builtin-fts.provider.ts
index b41424759..3f37aba74 100644
--- a/src/modules/search/providers/builtin-fts.provider.ts
+++ b/src/modules/search/providers/builtin-fts.provider.ts
@@ -156,10 +156,12 @@ function likePattern(term: string): string {
/**
* likePattern in Postgres SQL, around the folded term: unaccent maps fullwidth `%` and `_` to `%` and
- * `_`, so escaping before it would let them through as wildcards.
+ * `_`, so escaping before it would let them through as wildcards. The backslashes are written as E''
+ * literals, which read them the same whatever standard_conforming_strings says; with it off, a plain
+ * `'\'` escapes its closing quote.
*/
function postgresLikePattern(placeholder: string): string {
- return String.raw`'%' || replace(replace(replace(openwa_unaccent(${placeholder}), '\', '\\'), '%', '\%'), '_', '\_') || '%'`;
+ return String.raw`'%' || replace(replace(replace(openwa_unaccent(${placeholder}), E'\\', E'\\\\'), '%', E'\\%'), '_', E'\\_') || '%'`;
}
/** Shorter than a trigram once folded, so no trigram index can look it up. */