fix: Server error when search term contains double single quotes

This commit is contained in:
Tom Moor
2023-12-13 21:17:16 -05:00
parent d6c357d909
commit a53f304a9e
3 changed files with 450 additions and 428 deletions

View File

@@ -427,7 +427,7 @@ export default class SearchHelper {
* @param query The user search query
* @returns The query formatted for Postgres ts_query
*/
private static webSearchQuery(query: string): string {
public static webSearchQuery(query: string): string {
// limit length of search queries as we're using regex against untrusted input
let limitedQuery = this.escapeQuery(query.slice(0, this.maxQueryLength));
@@ -439,7 +439,7 @@ export default class SearchHelper {
!limitedQuery.endsWith('"');
// Replace single quote characters with &.
const singleQuotes = limitedQuery.matchAll(/'/g);
const singleQuotes = limitedQuery.matchAll(/'+/g);
for (const match of singleQuotes) {
if (
@@ -454,8 +454,10 @@ export default class SearchHelper {
}
}
return queryParser()(
singleUnquotedSearch ? `${limitedQuery}*` : limitedQuery
return (
queryParser()(singleUnquotedSearch ? `${limitedQuery}*` : limitedQuery)
// Remove any trailing join characters
.replace(/&$/, "")
);
}