Skip to content

Commit 0bc21df

Browse files
committed
add missing cases
1 parent f8a5a07 commit 0bc21df

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

src/main/java/eu/righettod/SecurityUtils.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,6 +1605,8 @@ public static boolean isGZIPCompressedDataSafe(byte[] compressedBytes, long maxC
16051605
* The following information are removed:
16061606
* <ul>
16071607
* <li>Characters: Carriage Return (CR), Linefeed (LF) and Tabulation (TAB).</li>
1608+
* <li>Characters: Unicode LINE SEPARATOR and Unicode PARAGRAPH SEPARATOR.</li>
1609+
* <li>Characters: CSI sequences and bare ESC.</li>
16081610
* <li>Leading and trailing spaces.</li>
16091611
* <li>Any HTML tags.</li>
16101612
* </ul><br>
@@ -1633,10 +1635,14 @@ public static String sanitizeLogMessage(String message, int maxMessageLength) {
16331635
}
16341636
//Step 1: Remove any CR/LR/TAB characters as well as leading and trailing spaces
16351637
sanitized = sanitized.replaceAll("[\\n\\r\\t]", "").trim();
1636-
//Step 2: Remove any HTML tags
1638+
//Step 2: Remove any Unicode LINE SEPARATOR or Unicode PARAGRAPH SEPARATOR as well as leading and trailing spaces
1639+
sanitized = sanitized.replace("\u2028", "").replace("\u2029", "").trim();
1640+
//Step 3: Remove ANSI escape sequences as well as leading and trailing spaces
1641+
sanitized = sanitized.replaceAll("\u001B\\[[\\d;]*[a-zA-Z]", "").replace("\u001B", "").trim();
1642+
//Step 4: Remove any HTML tags
16371643
PolicyFactory htmlSanitizerPolicy = new HtmlPolicyBuilder().toFactory();
16381644
sanitized = htmlSanitizerPolicy.sanitize(sanitized);
1639-
//Step 3: Truncate the string in case of need
1645+
//Step 5: Truncate the string in case of need
16401646
if (sanitized.length() > maxSanitizedMessageLength) {
16411647
sanitized = sanitized.substring(0, maxSanitizedMessageLength);
16421648
}

src/test/java/eu/righettod/TestSecurityUtils.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,8 @@ public void sanitizeLogMessage() {
798798
cases.add(new String[]{"1000", " test<xss>msg</xss>\n1\r2\t3\t4\n5\r6\t7\t\r\n ", "testmsg1234567"});
799799
cases.add(new String[]{"0", "<b>test msg</b><script>alert(1)</script>", "test msg"});
800800
cases.add(new String[]{"10", "AAAAAAAAAACCC<script src='https://evil.com/a.js'></script>BBBBBBBBBB", "AAAAAAAAAA"});
801+
cases.add(new String[]{"100", "hello\u2028world\u2029end", "helloworldend"});
802+
cases.add(new String[]{"100", "hello\u001B[31mworld\u001Bend", "helloworldend"});
801803
cases.forEach(caseData -> {
802804
int maxMessageLength = Integer.parseInt(caseData[0].trim());
803805
String originalMessage = caseData[1];

0 commit comments

Comments
 (0)