From 5b7a5d751ce32a4d2acb3ced5b7d2fef3ebb7410 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sat, 24 May 2025 11:30:00 -0400 Subject: [PATCH] Fix: Properly escape backslashes in search queries (#9297) * Fix: Properly escape backslashes in search queries This commit fixes a bug where backslashes in search queries were not properly escaped, leading to database errors. The `escapeQuery` method is now applied to quoted queries and URLs to ensure that all parts of the search query are correctly escaped. A test case has been added to verify that searching with backslashes works as expected. * Fix: Properly escape backslashes in search query URLs This commit fixes a bug where backslashes in URLs within search queries were not properly escaped, leading to database errors. The `escapeQuery` method is now applied to `likelyUrls` before they are used in `iLike` conditions. Quoted queries were found to be already escaped and are no longer double-escaped. The existing test case for searching with backslashes remains relevant for verifying URL escaping. * lint --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> --- server/models/helpers/SearchHelper.test.ts | 15 ++++++++++++--- server/models/helpers/SearchHelper.ts | 5 ++++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/server/models/helpers/SearchHelper.test.ts b/server/models/helpers/SearchHelper.test.ts index e5f21f1441..3a44b196ba 100644 --- a/server/models/helpers/SearchHelper.test.ts +++ b/server/models/helpers/SearchHelper.test.ts @@ -133,10 +133,19 @@ describe("SearchHelper", () => { test("should handle backslashes in search term", async () => { const team = await buildTeam(); - const { results } = await SearchHelper.searchForTeam(team, { - query: "\\\\", + const collection = await buildCollection({ + teamId: team.id, }); - expect(results.length).toBe(0); + const document = await buildDocument({ + teamId: team.id, + collectionId: collection.id, + title: "test with backslash \\", + }); + const { results } = await SearchHelper.searchForTeam(team, { + query: "test with backslash \\", + }); + expect(results.length).toBe(1); + expect(results[0].document?.id).toBe(document.id); }); test("should return the total count of search results", async () => { diff --git a/server/models/helpers/SearchHelper.ts b/server/models/helpers/SearchHelper.ts index c63fd4bdc9..fb2a2aba5c 100644 --- a/server/models/helpers/SearchHelper.ts +++ b/server/models/helpers/SearchHelper.ts @@ -476,7 +476,7 @@ export default class SearchHelper { if (options.query) { // find words that look like urls, these should be treated separately as the postgres full-text // index will generally not match them. - const likelyUrls = getUrls(options.query); + let likelyUrls = getUrls(options.query); // remove likely urls, and escape the rest of the query. let limitedQuery = this.escapeQuery( @@ -486,6 +486,9 @@ export default class SearchHelper { .trim() ); + // Escape the URLs + likelyUrls = likelyUrls.map((url) => this.escapeQuery(url)); + // Extract quoted queries and add them to the where clause, up to a maximum of 3 total. const quotedQueries = Array.from(limitedQuery.matchAll(/"([^"]*)"/g)).map( (match) => match[1]