fix: Server error in document search with single quotes

This commit is contained in:
Tom Moor
2022-12-04 17:10:06 -05:00
parent 908ca36de2
commit 0a68266365

View File

@@ -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
);
}