Skip to content

Commit

Permalink
Fix regex escape in exclusion editor text match (#1577)
Browse files Browse the repository at this point in the history
- Fixes #1566, #1544

- Fixes issue where "match text" was being displayed as regex when
containing special characters.
  • Loading branch information
SuaYoo authored and ikreymer committed Mar 6, 2024
1 parent 2c7229d commit 79a217b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
11 changes: 6 additions & 5 deletions frontend/src/features/crawl-workflows/queue-exclusion-table.ts
Expand Up @@ -6,7 +6,7 @@ import RegexColorize from "regex-colorize";

import type { SeedConfig } from "@/pages/org/types";
import LiteElement, { html } from "@/utils/LiteElement";
import { regexEscape } from "@/utils/string";
import { regexEscape, regexUnescape } from "@/utils/string";
import type { Exclusion } from "./queue-exclusion-form";
import { type PageChangeEvent } from "@/components/ui/pagination";
import { type TemplateResult, type PropertyValues } from "lit";
Expand Down Expand Up @@ -115,11 +115,12 @@ export class QueueExclusionTable extends LiteElement {
this.results = this.exclusions
.slice((this.page - 1) * this.pageSize, this.page * this.pageSize)
.map((str: string) => {
// if escaped version of string, with '\' removed matches string, then consider it
// to be matching text, otherwise, regex
const isText = regexEscape(str.replace(/\\/g, "")) === str;
return {
// if escaped version of string, with '\' removed matches string, then consider it
// to be matching text, otherwise, regex
type: regexEscape(str.replace(/\\/g, "")) === str ? "text" : "regex",
value: str,
type: isText ? "text" : "regex",
value: isText ? regexUnescape(str) : str,
};
});
}
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/pages/org/workflow-editor.ts
Expand Up @@ -27,7 +27,7 @@ import uniq from "lodash/fp/uniq";
import Fuse from "fuse.js";

import LiteElement, { html } from "@/utils/LiteElement";
import { regexEscape } from "@/utils/string";
import { regexEscape, regexUnescape } from "@/utils/string";
import type { AuthState } from "@/utils/AuthService";
import {
getUTCSchedule,
Expand Down Expand Up @@ -495,7 +495,7 @@ export class CrawlConfigEditor extends LiteElement {
if (primarySeedConfig.include?.length) {
formState.customIncludeUrlList = primarySeedConfig.include
// Unescape regex
.map((url) => url.replace(/(\\|\/\.\*)/g, ""))
.map(regexUnescape)
.join("\n");
// if we have additional include URLs, set to "custom" scope here
// to indicate 'Custom Page Prefix' option
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/utils/string.ts
Expand Up @@ -5,3 +5,7 @@
export function regexEscape(s: unknown) {
return String(s).replace(/[\\^$*+?.()|[\]{}]/g, "\\$&");
}

export function regexUnescape(s: unknown) {
return String(s).replace(/(\\|\/\.\*)/g, "");
}

0 comments on commit 79a217b

Please sign in to comment.