From 0a68266365f5988d2defab600d113d946ea812be Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sun, 4 Dec 2022 17:10:06 -0500 Subject: [PATCH] fix: Server error in document search with single quotes --- server/models/helpers/SearchHelper.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/server/models/helpers/SearchHelper.ts b/server/models/helpers/SearchHelper.ts index fc118dd16..1e57bbdbd 100644 --- a/server/models/helpers/SearchHelper.ts +++ b/server/models/helpers/SearchHelper.ts @@ -313,7 +313,7 @@ export default class SearchHelper { */ private static webSearchQuery(query: string): string { // limit length of search queries as we're using regex against untrusted input - const limitedQuery = this.escapeQuery(query.slice(0, this.maxQueryLength)); + let limitedQuery = this.escapeQuery(query.slice(0, this.maxQueryLength)); // if the search term is one unquoted word then allow partial matches automatically const queryWordCount = limitedQuery.split(" ").length; @@ -322,7 +322,23 @@ export default class SearchHelper { !limitedQuery.startsWith('"') && !limitedQuery.endsWith('"'); - return queryParser({ singleQuoteReplacement: "&" })( + // Replace single quote characters with &. + const singleQuotes = limitedQuery.matchAll(/'/g); + + for (const match of singleQuotes) { + if ( + match.index && + match.index > 0 && + match.index < limitedQuery.length - 1 + ) { + limitedQuery = + limitedQuery.substring(0, match.index) + + "&" + + limitedQuery.substring(match.index + 1); + } + } + + return queryParser()( singleUnquotedSearch ? `${limitedQuery}*` : limitedQuery ); }