Skip to content

Commit c876f1f

Browse files
committed
fix(utils): harden removeChars default char set
* Add "(", ")" and "\" to the default removed chars, since they can break the query string structure built around user input * Escape each char before building the RegExp, since "\" was previously unsafe to pass in * Keep other ES/Lucene operators (:, +, -, *) untouched by default Co-Authored-by: Bertrand Zuchuat <bertrand.zuchuat@rero.ch>
1 parent 621e9bd commit c876f1f

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

projects/rero/ng-core/src/lib/core/utils/utils.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,13 @@ describe('Utils', () => {
3939
expect(removeChars('"House"')).toEqual('House');
4040
expect(removeChars('"House Philipp\'s"', ["'"])).toEqual('"House Philipps"');
4141
});
42+
43+
it('should remove parentheses and backslashes by default', () => {
44+
expect(removeChars('foo (bar) baz')).toEqual('foo bar baz');
45+
expect(removeChars('foo\\bar')).toEqual('foobar');
46+
});
47+
48+
it('should keep other Elasticsearch operators untouched by default', () => {
49+
expect(removeChars('title:foo AND +bar -baz*')).toEqual('title:foo AND +bar -baz*');
50+
});
4251
});

projects/rero/ng-core/src/lib/core/utils/utils.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ export function capitalize(value: string): string {
3434
return value.charAt(0).toUpperCase() + value.slice(1);
3535
}
3636

37-
export function removeChars(value: string, chars: string[] = ['"']): string {
38-
const re = new RegExp('[' + chars.join('') + ']', 'gi');
37+
export function removeChars(value: string, chars: string[] = ['"', '(', ')', '\\']): string {
38+
const escaped = chars.map((char) => char.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'));
39+
const re = new RegExp('[' + escaped.join('') + ']', 'gi');
3940
return value.replace(re, '');
4041
}

0 commit comments

Comments
 (0)