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>
This commit is contained in:
Tom Moorandgoogle-labs-jules[bot] authored and GitHub committed 2025-05-24 11:30:00 -04:00
1 parent 73fea094a8
commit 5b7a5d751c
2 files changed
+16 -4

No files matched your search

+12 -3
View File
@@ -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 () => {
+4 -1
View File
@@ -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]