Copilot commented on code in PR #14515:
URL: https://github.com/apache/gravitino/pull/14515#discussion_r4461560077


##########
common/src/main/java/org/apache/gravitino/auth/ActiveRolesParser.java:
##########
@@ -119,4 +127,98 @@ public static ActiveRoles parse(@Nullable String rawValue) 
{
     }
     return ActiveRoles.of(names);
   }
+
+  // Servlet containers such as Jetty expose each header byte as one 
ISO-8859-1 char. Re-read those
+  // bytes as UTF-8 so a role name sent as raw UTF-8 matches. A value holding 
a char above U+00FF
+  // was already decoded by the container and is kept as is.
+  private static String recoverUtf8(String rawValue) {
+    boolean hasNonAscii = false;
+    for (int i = 0; i < rawValue.length(); i++) {
+      char c = rawValue.charAt(i);
+      if (c > 0xFF) {
+        return rawValue;
+      }
+      hasNonAscii |= c >= 0x80;
+    }
+    return hasNonAscii
+        ? decodeUtf8(rawValue.getBytes(StandardCharsets.ISO_8859_1), rawValue)
+        : rawValue;
+  }
+
+  private static String decodeRoleName(String token, String value) {
+    if (token.indexOf('%') < 0) {
+      return token;
+    }
+    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
+    int i = 0;
+    while (i < token.length()) {
+      if (token.charAt(i) == '%') {
+        bytes.write(decodeEscape(token, i, value));
+        i += 3;
+        continue;
+      }
+      int next = token.indexOf('%', i);
+      int end = next < 0 ? token.length() : next;
+      byte[] literal = token.substring(i, 
end).getBytes(StandardCharsets.UTF_8);
+      bytes.write(literal, 0, literal.length);
+      i = end;
+    }
+    return decodeUtf8(bytes.toByteArray(), value);
+  }
+
+  // Decoding can produce names the raw header could not carry: reject blank 
names (e.g. "%20") here
+  // so they get a 400, and control characters so a name cannot forge lines in 
logs.
+  private static String checkRoleName(String name, String value) {
+    if (name.trim().isEmpty()) {

Review Comment:
   `String.trim()` only removes characters up to U+0020, so `%E2%80%83` (U+2003 
EM SPACE) passes both checks and becomes a named role instead of returning 400 
for a blank decoded name. This also differs from 
`RoleCreateRequest.validate()`, which rejects Unicode-blank names using 
`StringUtils.isNotBlank`. Use Unicode-aware blank detection here and add a case 
for a percent-encoded Unicode whitespace-only name.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to