diff --git a/public/script/app.js b/public/script/app.js index 3e3406e..fff072b 100644 --- a/public/script/app.js +++ b/public/script/app.js @@ -1233,10 +1233,7 @@ angular function anonymize() { $scope.anonymize.terms.$setValidity("regex", true); // check if string has regex characters - if ( - $scope.terms && - $scope.terms.match(/[-[\]{}()*+?.,\\^$|#\s]/g) - ) { + if ($scope.terms && $scope.terms.match(/[-[\]{}()*+?.,\\^$|#\s]/g)) { $scope.anonymize.terms.$setValidity("regex", false); } const urlRegex = @@ -1279,7 +1276,13 @@ angular ); const terms = $scope.terms.split("\n"); for (let i = 0; i < terms.length; i++) { - const term = terms[i]; + let term = terms[i]; + try { + new RegExp(term, "gi"); + } catch { + // escape regex characters + term = term.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); + } if (term.trim() == "") { continue; } diff --git a/src/anonymize-utils.ts b/src/anonymize-utils.ts index fc70e5d..ddd627e 100644 --- a/src/anonymize-utils.ts +++ b/src/anonymize-utils.ts @@ -133,10 +133,16 @@ export function anonymizeContent( const terms = repository.options.terms || []; for (let i = 0; i < terms.length; i++) { - const term = terms[i]; + let term = terms[i]; if (term.trim() == "") { continue; } + try { + new RegExp(term, "gi"); + } catch { + // escape regex characters + term = term.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); + } // remove whole url if it contains the term content = content.replace(urlRegex, (match) => { if (new RegExp(`\\b${term}\\b`, "gi").test(match)) @@ -155,10 +161,16 @@ export function anonymizeContent( export function anonymizePath(path: string, terms: string[]) { for (let i = 0; i < terms.length; i++) { - const term = terms[i]; + let term = terms[i]; if (term.trim() == "") { continue; } + try { + new RegExp(term, "gi"); + } catch { + // escape regex characters + term = term.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); + } path = path.replace( new RegExp(term, "gi"), config.ANONYMIZATION_MASK + "-" + (i + 1)