mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-07-09 15:17:49 +02:00
fix: escape regex characters when invalid
This commit is contained in:
@@ -1233,10 +1233,7 @@ angular
|
|||||||
function anonymize() {
|
function anonymize() {
|
||||||
$scope.anonymize.terms.$setValidity("regex", true);
|
$scope.anonymize.terms.$setValidity("regex", true);
|
||||||
// check if string has regex characters
|
// check if string has regex characters
|
||||||
if (
|
if ($scope.terms && $scope.terms.match(/[-[\]{}()*+?.,\\^$|#\s]/g)) {
|
||||||
$scope.terms &&
|
|
||||||
$scope.terms.match(/[-[\]{}()*+?.,\\^$|#\s]/g)
|
|
||||||
) {
|
|
||||||
$scope.anonymize.terms.$setValidity("regex", false);
|
$scope.anonymize.terms.$setValidity("regex", false);
|
||||||
}
|
}
|
||||||
const urlRegex =
|
const urlRegex =
|
||||||
@@ -1279,7 +1276,13 @@ angular
|
|||||||
);
|
);
|
||||||
const terms = $scope.terms.split("\n");
|
const terms = $scope.terms.split("\n");
|
||||||
for (let i = 0; i < terms.length; i++) {
|
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() == "") {
|
if (term.trim() == "") {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-2
@@ -133,10 +133,16 @@ export function anonymizeContent(
|
|||||||
|
|
||||||
const terms = repository.options.terms || [];
|
const terms = repository.options.terms || [];
|
||||||
for (let i = 0; i < terms.length; i++) {
|
for (let i = 0; i < terms.length; i++) {
|
||||||
const term = terms[i];
|
let term = terms[i];
|
||||||
if (term.trim() == "") {
|
if (term.trim() == "") {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
new RegExp(term, "gi");
|
||||||
|
} catch {
|
||||||
|
// escape regex characters
|
||||||
|
term = term.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
|
||||||
|
}
|
||||||
// remove whole url if it contains the term
|
// remove whole url if it contains the term
|
||||||
content = content.replace(urlRegex, (match) => {
|
content = content.replace(urlRegex, (match) => {
|
||||||
if (new RegExp(`\\b${term}\\b`, "gi").test(match))
|
if (new RegExp(`\\b${term}\\b`, "gi").test(match))
|
||||||
@@ -155,10 +161,16 @@ export function anonymizeContent(
|
|||||||
|
|
||||||
export function anonymizePath(path: string, terms: string[]) {
|
export function anonymizePath(path: string, terms: string[]) {
|
||||||
for (let i = 0; i < terms.length; i++) {
|
for (let i = 0; i < terms.length; i++) {
|
||||||
const term = terms[i];
|
let term = terms[i];
|
||||||
if (term.trim() == "") {
|
if (term.trim() == "") {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
new RegExp(term, "gi");
|
||||||
|
} catch {
|
||||||
|
// escape regex characters
|
||||||
|
term = term.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
|
||||||
|
}
|
||||||
path = path.replace(
|
path = path.replace(
|
||||||
new RegExp(term, "gi"),
|
new RegExp(term, "gi"),
|
||||||
config.ANONYMIZATION_MASK + "-" + (i + 1)
|
config.ANONYMIZATION_MASK + "-" + (i + 1)
|
||||||
|
|||||||
Reference in New Issue
Block a user