Browse Source

[frontend] Escape user search input before building regular expressions (#1350)

pull/1354/head
github-actions[bot] 1 month ago
committed by GitHub
parent
commit
f828dd717e
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 4
      frontend/src/app/features/settings/pages/languages/language-add-form.component.ts
  2. 6
      frontend/src/app/features/teams/state/team-contributors.state.ts
  3. 4
      frontend/src/app/framework/angular/forms/editors/dropdown.component.ts
  4. 4
      frontend/src/app/framework/angular/pipes/highlight.pipe.ts
  5. 11
      frontend/src/app/framework/utils/string-helper.spec.ts
  6. 8
      frontend/src/app/framework/utils/string-helper.ts
  7. 13
      frontend/src/app/shared/state/contributors.state.spec.ts
  8. 6
      frontend/src/app/shared/state/contributors.state.ts
  9. 4
      frontend/src/app/shared/state/schemas.state.ts

4
frontend/src/app/features/settings/pages/languages/language-add-form.component.ts

@ -8,7 +8,7 @@
import { ChangeDetectionStrategy, Component, Input } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { Observable, of } from 'rxjs';
import { AddLanguageForm, AutocompleteComponent, AutocompleteSource, FormHintComponent, LanguageDto, LanguagesState, TranslatePipe } from '@app/shared';
import { AddLanguageForm, AutocompleteComponent, AutocompleteSource, FormHintComponent, LanguageDto, LanguagesState, StringHelper, TranslatePipe } from '@app/shared';
class LanguageSource implements AutocompleteSource {
constructor(
@ -21,7 +21,7 @@ class LanguageSource implements AutocompleteSource {
return of(this.languages);
}
const regex = new RegExp(query, 'i');
const regex = StringHelper.searchRegex(query);
const results: LanguageDto[] = [];
const result = this.languages.find(x => x.iso2Code === query);

6
frontend/src/app/features/teams/state/team-contributors.state.ts

@ -8,7 +8,7 @@
import { Injectable } from '@angular/core';
import { EMPTY, Observable, throwError } from 'rxjs';
import { catchError, finalize, tap } from 'rxjs/operators';
import { AssignContributorDto, ContributorDto, ContributorsDto, debug, DialogService, ErrorDto, getPagingInfo, ListState, shareMapSubscribed, shareSubscribed, State, TeamsState, Types, VersionTag } from '@app/shared';
import { AssignContributorDto, ContributorDto, ContributorsDto, debug, DialogService, ErrorDto, getPagingInfo, ListState, shareMapSubscribed, shareSubscribed, State, StringHelper, TeamsState, Types, VersionTag } from '@app/shared';
import { TeamContributorsService } from '../internal';
interface Snapshot extends ListState<string> {
@ -159,7 +159,7 @@ export class TeamContributorsState extends State<Snapshot> {
}
function getRegex(query?: string): RegExp | undefined {
return query ? new RegExp(query, 'i') : undefined;
return query ? StringHelper.searchRegex(query) : undefined;
}
function getFilteredContributors(snapshot: Snapshot) {
@ -168,7 +168,7 @@ function getFilteredContributors(snapshot: Snapshot) {
let filtered = contributors;
if (query) {
const regex = new RegExp(query, 'i');
const regex = StringHelper.searchRegex(query);
filtered = filtered.filter(x => regex.test(x.contributorName));
}

4
frontend/src/app/framework/angular/forms/editors/dropdown.component.ts

@ -9,7 +9,7 @@
import { AfterContentInit, booleanAttribute, ChangeDetectionStrategy, Component, ContentChildren, EventEmitter, forwardRef, Input, OnInit, Output, QueryList, TemplateRef } from '@angular/core';
import { FormsModule, NG_VALUE_ACCESSOR, ReactiveFormsModule, UntypedFormControl } from '@angular/forms';
import { map } from 'rxjs/operators';
import { FloatingPlacement, Keys, ModalModel, StatefulControlComponent, Subscriptions, TypedSimpleChanges, Types } from '@app/framework/internal';
import { FloatingPlacement, Keys, ModalModel, StatefulControlComponent, StringHelper, Subscriptions, TypedSimpleChanges, Types } from '@app/framework/internal';
import { DropdownMenuComponent } from '../../dropdown-menu.component';
import { LoaderComponent } from '../../loader.component';
import { ModalPlacementDirective } from '../../modals/modal-placement.directive';
@ -136,7 +136,7 @@ export class DropdownComponent extends StatefulControlComponent<State, ReadonlyA
if (!this.items || !queryText) {
return { query: undefined, items: this.items };
} else {
const query = new RegExp(queryText, 'i');
const query = StringHelper.searchRegex(queryText);
const items = this.items.filter(x => {
if (Types.isString(x)) {

4
frontend/src/app/framework/angular/pipes/highlight.pipe.ts

@ -6,7 +6,7 @@
*/
import { Pipe, PipeTransform } from '@angular/core';
import { escapeHTML, Types } from '@app/framework/internal';
import { escapeHTML, StringHelper, Types } from '@app/framework/internal';
@Pipe({
name: 'sqxHighlight',
@ -21,7 +21,7 @@ export class HighlightPipe implements PipeTransform {
}
if (Types.isString(highlight)) {
highlight = new RegExp(highlight, 'i');
highlight = StringHelper.searchRegex(highlight);
}
const result = text.replace(highlight, s => `<b>${s}</b>`);

11
frontend/src/app/framework/utils/string-helper.spec.ts

@ -58,6 +58,17 @@ describe('StringHelper', () => {
expect(StringHelper.hashCode('ABC')).not.toBe(StringHelper.hashCode('XYZ'));
});
it('should escape regex special characters', () => {
expect(StringHelper.escapeRegex('a(b)[c]*.+?^$|{}\\/-')).toBe('a\\(b\\)\\[c\\]\\*\\.\\+\\?\\^\\$\\|\\{\\}\\\\\\/\\-');
});
it('should create search regex that matches special characters literally', () => {
const regex = StringHelper.searchRegex('(A.');
expect(regex.test('x(a.y')).toBeTruthy();
expect(regex.test('xaby')).toBeFalsy();
});
it('should build query for empty object', () => {
const url = StringHelper.buildQuery({});

8
frontend/src/app/framework/utils/string-helper.ts

@ -54,6 +54,14 @@ export module StringHelper {
}
}
export function escapeRegex(value: string) {
return value.replace(/[.*+?^${}()|[\]\\/-]/g, '\\$&');
}
export function searchRegex(query: string) {
return new RegExp(escapeRegex(query), 'i');
}
export function hashCode(value: string) {
let hash = 0;

13
frontend/src/app/shared/state/contributors.state.spec.ts

@ -112,6 +112,19 @@ describe('ContributorsState', () => {
expect(contributorsState.snapshot.pageSize).toEqual(10);
});
it('should treat search query as literal text', () => {
contributorsState.load().subscribe();
contributorsState.search('(4');
let contributors: ReadonlyArray<ContributorDto>;
contributorsState.contributorsFiltered.subscribe(result => {
contributors = result;
});
expect(contributors!).toEqual([]);
});
it('should show notification on load if reload is true', () => {
contributorsState.load(true).subscribe();

6
frontend/src/app/shared/state/contributors.state.ts

@ -8,7 +8,7 @@
import { Injectable } from '@angular/core';
import { EMPTY, Observable, throwError } from 'rxjs';
import { catchError, finalize, tap } from 'rxjs/operators';
import { debug, DialogService, ErrorDto, getPagingInfo, ListState, shareMapSubscribed, shareSubscribed, State, Types, VersionTag } from '@app/framework';
import { debug, DialogService, ErrorDto, getPagingInfo, ListState, shareMapSubscribed, shareSubscribed, State, StringHelper, Types, VersionTag } from '@app/framework';
import { AssignContributorDto, ContributorDto, ContributorsDto } from '../model';
import { ContributorsService } from '../services/contributors.service';
import { AppsState } from './apps.state';
@ -165,7 +165,7 @@ export class ContributorsState extends State<Snapshot> {
}
function getRegex(query?: string): RegExp | undefined {
return query ? new RegExp(query, 'i') : undefined;
return query ? StringHelper.searchRegex(query) : undefined;
}
function getFilteredContributors(snapshot: Snapshot) {
@ -174,7 +174,7 @@ function getFilteredContributors(snapshot: Snapshot) {
let filtered = contributors;
if (query) {
const regex = new RegExp(query, 'i');
const regex = StringHelper.searchRegex(query);
filtered = filtered.filter(x => regex.test(x.contributorName));
}

4
frontend/src/app/shared/state/schemas.state.ts

@ -8,7 +8,7 @@
import { Injectable } from '@angular/core';
import { EMPTY, forkJoin, Observable, of } from 'rxjs';
import { catchError, finalize, tap } from 'rxjs/operators';
import { debug, DialogService, LoadingState, shareMapSubscribed, shareSubscribed, State, Version } from '@app/framework';
import { debug, DialogService, LoadingState, shareMapSubscribed, shareSubscribed, State, StringHelper, Version } from '@app/framework';
import { AddFieldDto, ChangeCategoryDto, ConfigureFieldRulesDto, ConfigureUIFieldsDto, CreateSchemaDto, ExportContentsDto, FieldDto, MigrateContentsDto, NestedFieldDto, SchemaDto, SynchronizeSchemaDto, UpdateFieldDto, UpdateSchemaDto } from '../model';
import { SchemasService } from '../services/schemas.service';
import { AppsState } from './apps.state';
@ -454,7 +454,7 @@ export function getCategoryTree(allSchemas: ReadonlyArray<SchemaDto>, categories
let match = (_: SchemaDto) => true;
if (filter) {
const terms = filter.split(' ').map(filter => new RegExp(filter.trim(), 'i'));
const terms = filter.split(' ').map(filter => StringHelper.searchRegex(filter.trim()));
const matches = (value: string | undefined | null) => {
return !!value && terms.every(term => value.search(term) >= 0);

Loading…
Cancel
Save