Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

fix: Search falls back to full JSON scan and does not escape LIKE metacharacters

Blue Mousea6012d0650e1
diff --git a/backend/apps/cloud/src/ai/ai-chat.service.ts b/backend/apps/cloud/src/ai/ai-chat.service.ts
index 71d21c932..eec0ca528 100644
--- a/backend/apps/cloud/src/ai/ai-chat.service.ts
+++ b/backend/apps/cloud/src/ai/ai-chat.service.ts
@@ -95,10 +95,17 @@ export class AiChatService {
}
if (search && search.trim().length > 0) {
- const term = `%${search.trim()}%`
+ // Escape backslash first, then LIKE metacharacters, so a user typing
+ // '%' / '_' matches the literal characters instead of acting as wildcards.
+ const escaped = search
+ .trim()
+ .replace(/\\/g, '\\\\')
+ .replace(/%/g, '\\%')
+ .replace(/_/g, '\\_')
+ const term = `%${escaped}%`
const nameQb = applyTagAndPinned(baseQuery()).andWhere(
- 'chat.name LIKE :term',
+ "chat.name LIKE :term ESCAPE '\\\\'",
{ term },
)
@@ -111,7 +118,7 @@ export class AiChatService {
// Fallback to content search across messages JSON
const contentQb = applyTagAndPinned(baseQuery()).andWhere(
- 'CAST(chat.messages AS CHAR) LIKE :term',
+ "CAST(chat.messages AS CHAR) LIKE :term ESCAPE '\\\\'",
{ term },
)