Skip to content

Commit

Permalink
Properly escape regex when performing search query
Browse files Browse the repository at this point in the history
  • Loading branch information
big213 committed Oct 17, 2021
1 parent beb8fb8 commit 15970a8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 4 additions & 0 deletions backend/functions/src/schema/core/helpers/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ export function lowercaseString(str: string): string {
return str.charAt(0).toLowerCase() + str.slice(1);
}

export function escapeRegExp(str: string): string {
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // escape regexp
}

export function objectOnlyHasFields(
obj: StringKeyObject,
fields: string[],
Expand Down
6 changes: 3 additions & 3 deletions backend/functions/src/schema/core/services/normal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {

import { ServiceFunctionInputs } from "../../../types";

import { btoa, isObject } from "../helpers/shared";
import { btoa, escapeRegExp, isObject } from "../helpers/shared";

export type FieldObject = {
field?: string;
Expand Down Expand Up @@ -351,7 +351,7 @@ export class NormalService extends BaseService {
for (const prop in this.searchFieldsMap) {
whereSubObject.fields.push({
field: this.searchFieldsMap[prop].field ?? prop,
value: new RegExp(validatedArgs.search, "i"),
value: new RegExp(escapeRegExp(validatedArgs.search), "i"),
operator: "regex",
});
}
Expand Down Expand Up @@ -426,7 +426,7 @@ export class NormalService extends BaseService {
for (const prop in this.searchFieldsMap) {
whereSubObject.fields.push({
field: this.searchFieldsMap[prop].field ?? prop,
value: new RegExp(validatedArgs.search, "i"),
value: new RegExp(escapeRegExp(validatedArgs.search), "i"),
operator: "regex",
});
}
Expand Down

0 comments on commit 15970a8

Please sign in to comment.