Skip to content

Commit

Permalink
fix(auth): fix regex header string for long strim (#1402)
Browse files Browse the repository at this point in the history
Co-authored-by: alozano3 <[email protected]>
  • Loading branch information
2 people authored and tchiotludo committed Apr 4, 2023
1 parent 486e8d8 commit 23ff2bb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/main/java/org/akhq/utils/ContentUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@ public class ContentUtils {
*/
private static boolean isValidUTF8(byte[] value) throws UnsupportedEncodingException
{
Pattern p = Pattern.compile("\\A(\n" +
" [\\x09\\x0A\\x0D\\x20-\\x7E] # ASCII\\n" +
"| [\\xC2-\\xDF][\\x80-\\xBF] # non-overlong 2-byte\n" +
"| \\xE0[\\xA0-\\xBF][\\x80-\\xBF] # excluding overlongs\n" +
"| [\\xE1-\\xEC\\xEE\\xEF][\\x80-\\xBF]{2} # straight 3-byte\n" +
"| \\xED[\\x80-\\x9F][\\x80-\\xBF] # excluding surrogates\n" +
"| \\xF0[\\x90-\\xBF][\\x80-\\xBF]{2} # planes 1-3\n" +
"| [\\xF1-\\xF3][\\x80-\\xBF]{3} # planes 4-15\n" +
"| \\xF4[\\x80-\\x8F][\\x80-\\xBF]{2} # plane 16\n" +
")*\\z", Pattern.COMMENTS);
//If the array is too long, it throws a StackOverflowError due to the regex, so we assume it is a String.
if (value.length <= 1000) {
Pattern p = Pattern.compile("\\A(\n" +
" [\\x09\\x0A\\x0D\\x20-\\x7E] # ASCII\\n" +
"| [\\xC2-\\xDF][\\x80-\\xBF] # non-overlong 2-byte\n" +
"| \\xE0[\\xA0-\\xBF][\\x80-\\xBF] # excluding overlongs\n" +
"| [\\xE1-\\xEC\\xEE\\xEF][\\x80-\\xBF]{2} # straight 3-byte\n" +
"| \\xED[\\x80-\\x9F][\\x80-\\xBF] # excluding surrogates\n" +
"| \\xF0[\\x90-\\xBF][\\x80-\\xBF]{2} # planes 1-3\n" +
"| [\\xF1-\\xF3][\\x80-\\xBF]{3} # planes 4-15\n" +
"| \\xF4[\\x80-\\x8F][\\x80-\\xBF]{2} # plane 16\n" +
")*\\z", Pattern.COMMENTS);

String phonyString = new String(value, "ISO-8859-1");
return p.matcher(phonyString).matches();
String phonyString = new String(value, "ISO-8859-1");
return p.matcher(phonyString).matches();
}
return true;
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/org/akhq/utils/ContentUtilsTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.akhq.utils;

import org.akhq.models.Record;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.kafka.common.header.Header;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -70,4 +71,11 @@ void testHeaderValueShort() {
assertEquals(testValue, ContentUtils.convertToObject(toBytes(testValue)));
}

@Test
void testHeaderValueLongStringUTF8() {
String testValue = RandomStringUtils.random(10000, true, false);

assertEquals(testValue, ContentUtils.convertToObject(testValue.getBytes(StandardCharsets.UTF_8)));
}

}

0 comments on commit 23ff2bb

Please sign in to comment.