Copilot commented on code in PR #11222:
URL: https://github.com/apache/ozone/pull/11222#discussion_r3969023134
##########
hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/signature/ChunksValidator.java:
##########
@@ -101,15 +103,34 @@ public void validateChunk(String chunkSignature, String
payloadSha256Hex)
String stringToSign = String.join(NEWLINE,
CHUNK_STRING_TO_SIGN_ALGORITHM, dateTime, credentialScope,
previousSignature, EMPTY_STRING_SHA256, payloadSha256Hex);
+ validateSignature(chunkSignature, stringToSign);
+ // The chain feeds this chunk's signature, in hex, into the next chunk's
string-to-sign.
+ // chunkSignature equals expected (verified above), so reuse it normalized
to lower-case.
+ previousSignature = chunkSignature.toLowerCase(Locale.ROOT);
+ }
+
+ /**
+ * Verify the signature of the trailing headers and complete the signature
chain.
+ *
+ * @param trailerSignature the signature from the {@code
x-amz-trailer-signature} header
+ * @param trailingHeadersSha256Hex SHA-256 of the canonical trailing headers
+ * @throws OS3Exception if the computed signature does not match
+ */
+ public void validateTrailer(String trailerSignature, String
trailingHeadersSha256Hex)
+ throws OS3Exception {
+ String stringToSign = String.join(NEWLINE,
+ TRAILER_STRING_TO_SIGN_ALGORITHM, dateTime, credentialScope,
+ previousSignature, trailingHeadersSha256Hex);
+ validateSignature(trailerSignature, stringToSign);
+ }
+
+ private void validateSignature(String signature, String stringToSign) {
byte[] expected = hmacSha256(stringToSign);
// Constant-time comparison to avoid leaking the signature via timing.
Decoding the hex also
// makes the comparison case-insensitive, as the signature may be sent in
either case.
- if (!MessageDigest.isEqual(expected,
DatatypeConverter.parseHexBinary(chunkSignature))) {
+ if (!MessageDigest.isEqual(expected,
DatatypeConverter.parseHexBinary(signature))) {
throw newError(SIGNATURE_DOES_NOT_MATCH, resource);
}
Review Comment:
`DatatypeConverter.parseHexBinary(signature)` throws
`IllegalArgumentException` for malformed/non-hex signatures, which would bypass
the intended `OS3Exception` mapping and can surface as an unexpected error
instead of `SignatureDoesNotMatch (403)`. Wrap the hex decode in a try/catch
and convert decode failures into `SIGNATURE_DOES_NOT_MATCH` (or the project’s
preferred S3 error for malformed signatures) to preserve the contract that
signature failures are consistently reported.
##########
hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/SignedChunksInputStream.java:
##########
@@ -200,11 +225,14 @@ private boolean ensureChunkPayload() throws IOException {
return true;
}
if (remainingData == 0) {
- // final zero-byte chunk: verify it (empty payload) and stop reading
- if (validator != null) {
+ // The final zero-byte chunk has no payload terminator when trailing
headers follow it.
+ if (validator != null && trailerHeader == null) {
readChunkTerminator();
}
validateChunk();
+ if (trailerHeader != null) {
+ validateTrailer();
+ }
Review Comment:
For non-trailer streams (`trailerHeader == null`), the final chunk’s
terminating CRLF should be consumed regardless of whether `validator` is
attached. As written, when `validator == null` the stream can return EOF while
leaving unread bytes in the underlying request body, which can cause
request/connection handling issues (e.g., keep-alive reuse) and makes framing
validation inconsistent. Consider reading the final chunk terminator whenever
`trailerHeader == null` (and only skipping it for the trailer variant where it
is not present).
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]