gnodet-bot commented on code in PR #29694:
URL: https://github.com/apache/camel/pull/29694#discussion_r4548921719
##########
components/camel-as2/camel-as2-api/src/test/java/org/apache/camel/component/as2/api/util/AS2HeaderUtilsTest.java:
##########
@@ -55,4 +61,17 @@ public void parseNameValuePairTest() {
assertArrayEquals(SIGNED_RECEIPT_MICALG_VALUES, parameter.getValues(),
"Unexpected value for parameter values");
}
+ @Test
+ public void addAuthorizationHeaderEncodesCredentialsAsUtf8() {
+ // a password outside US-ASCII: "pa(diaeresis)ssword"
+ final String password = "p\u00e4ssword";
+ final HttpMessage message = new BasicHttpRequest("POST", "/");
+
+ AS2HeaderUtils.addAuthorizationHeader(message, "user", password, null);
+
+ // RFC 7617 asks for UTF-8, and the platform default charset must not
decide what goes on the wire
+ final String expected
+ = "Basic " + Base64.getEncoder().encodeToString(("user:" +
password).getBytes(StandardCharsets.UTF_8));
+ assertEquals(expected,
message.getFirstHeader(AS2Header.AUTHORIZATION).getValue());
+ }
Review Comment:
⚠️ **Test is a tautology on UTF-8 JVMs.** The `expected` value is computed
with `StandardCharsets.UTF_8`, and so is the production code. Since JDK 18 (JEP
400), `Charset.defaultCharset()` returns UTF-8, so `"string".getBytes()` and
`"string".getBytes(StandardCharsets.UTF_8)` produce identical bytes — the test
passes both before and after the fix on any JDK ≥ 18, which is what Camel CI
runs.
Hard-code the expected Base64 string instead, so the assertion is
independent of the runtime charset:
```suggestion
@Test
public void addAuthorizationHeaderEncodesCredentialsAsUtf8() {
// a password outside US-ASCII: "pa(diaeresis)ssword"
final String password = "p\u00e4ssword";
final HttpMessage message = new BasicHttpRequest("POST", "/");
AS2HeaderUtils.addAuthorizationHeader(message, "user", password,
null);
// "user:pässword" in UTF-8 → 75 73 65 72 3A 70 C3 A4 73 73 77 6F 72
64
// Hard-coded so the test fails on any JVM whose charset encodes ä
differently,
// regardless of whether the runtime happens to default to UTF-8.
assertEquals("Basic dXNlcjpww6Rzc3dvcmQ=",
message.getFirstHeader(AS2Header.AUTHORIZATION).getValue());
}
```
##########
components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppConnectionFactory.java:
##########
@@ -171,33 +172,33 @@ private void connectProxy(String host, int port, Socket
socket) throws IOExcepti
InputStream in = socket.getInputStream();
String connectString = "CONNECT " + host + ":" + port + "
HTTP/1.0\r\n";
- out.write(connectString.getBytes());
+ out.write(connectString.getBytes(StandardCharsets.UTF_8));
String username = config.getHttpProxyUsername();
String password = config.getHttpProxyPassword();
if (username != null && password != null) {
String usernamePassword = username + ":" + password;
- byte[] code =
Base64.getEncoder().encode(usernamePassword.getBytes());
- out.write("Proxy-Authorization: Basic ".getBytes());
+ byte[] code =
Base64.getEncoder().encode(usernamePassword.getBytes(StandardCharsets.UTF_8));
+ out.write("Proxy-Authorization: Basic
".getBytes(StandardCharsets.UTF_8));
out.write(code);
- out.write("\r\n".getBytes());
+ out.write("\r\n".getBytes(StandardCharsets.UTF_8));
}
Map<String, String> proxyHeaders = config.getProxyHeaders();
if (proxyHeaders != null) {
for (Map.Entry<String, String> entry :
proxyHeaders.entrySet()) {
- out.write((entry.getKey() + ": " +
entry.getValue()).getBytes());
- out.write("\r\n".getBytes());
+ out.write((entry.getKey() + ": " +
entry.getValue()).getBytes(StandardCharsets.UTF_8));
+ out.write("\r\n".getBytes(StandardCharsets.UTF_8));
}
}
- out.write("\r\n".getBytes());
+ out.write("\r\n".getBytes(StandardCharsets.UTF_8));
out.flush();
int ch = 0;
- BufferedReader reader = IOHelper.buffered(new
InputStreamReader(in));
+ BufferedReader reader = IOHelper.buffered(new
InputStreamReader(in, StandardCharsets.UTF_8));
Review Comment:
ℹ️ **Nit (no change required):** HTTP/1.x response headers are nominally
ISO-8859-1 per RFC 7230 §3.2.4. `UTF-8` is accepted here because (a) it is a
strict superset of ASCII and proxy status lines are ASCII in practice, and (b)
the parsed lines are only used to check the status code and are then discarded.
Just flagging for awareness.
--
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]