Skip to content

Commit

Permalink
Merge pull request #477 from Dohbedoh/JENKINS-71529
Browse files Browse the repository at this point in the history
[JENKINS-71529] Fix sensitive word boundaries matching
  • Loading branch information
Dohbedoh committed Jun 26, 2023
2 parents ba6a7bc + 32fd393 commit b4e66d4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public synchronized void reload() {
trie.add(lowerCaseOriginal);
}
}));
this.mappingsPattern.set(Pattern.compile("\\b" + trie.getRegex() + "\\b", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE));
this.mappingsPattern.set(Pattern.compile("(?<!\\w)" + trie.getRegex() + "(?!\\w)", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE));
this.replacementsMap.set(replacementsMap);
LOGGER.log(Level.FINE, "Took " + (System.currentTimeMillis()-startTime) + "ms to reload");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import hudson.Functions;
import org.junit.Ignore;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.recipes.WithTimeout;

import java.util.ArrayList;
Expand Down Expand Up @@ -326,6 +327,22 @@ public void replacementByShorterWordTest() {
assertEquals(result, WordReplacer.replaceWords(input, triePattern(words), replacementsMap(words, replaces)));
}

@Test
@Issue("JENKINS-71529")
public void testBoundaries() {
String specialChars = "~`!@#$%^&*()_+-={}[]|\\:\";'<>?,./";
String[] words = new String[specialChars.length()];
String[] replaces = new String[specialChars.length()];
for(int i=0; i< specialChars.length(); i++) {
words[i] = specialChars.charAt(i) + "word" + specialChars.charAt(i);
replaces[i] = "**" + words[i] + "**";
}
String result = String.join(" ", replaces);

assertEquals(result, WordReplacer.replaceWords(String.join(" ", words), words, replaces));
assertEquals(result, WordReplacer.replaceWords(String.join(" ", words), triePattern(words), replacementsMap(words, replaces)));
}

private List<String> generateFakeListString(int lines) {
assertTrue(lines < 1001);
return Stream.generate(() -> FilteredOutputStreamTest.FAKE_TEXT).limit(lines).collect(Collectors.toList());
Expand Down Expand Up @@ -382,7 +399,7 @@ private Pattern triePattern(String [] originals, boolean lowercase) {
for (String search : originals) {
trie.add(lowercase ? search.toLowerCase(Locale.ENGLISH): search);
}
return Pattern.compile("\\b" + trie.getRegex() + "\\b", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
return Pattern.compile("(?<!\\w)" + trie.getRegex() + "(?!\\w)", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
}


Expand Down Expand Up @@ -434,6 +451,6 @@ private Pattern unionPattern(String [] originals) {
buf.deleteCharAt(buf.length() - 1);
buf.append(')');
}
return Pattern.compile("\\b" + buf.toString() + "\\b", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
return Pattern.compile("(?<!\\w)" + buf + "(?!\\w)", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
}
}

0 comments on commit b4e66d4

Please sign in to comment.