Skip to content

Commit 1ea28c4

Browse files
committed
Fix off-by-one error
1 parent e96ef09 commit 1ea28c4

File tree

4 files changed

+12
-8
lines changed

4 files changed

+12
-8
lines changed

core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/crt/internal/signer/AwsChunkedV4aPayloadSigner.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public void beforeSigning(SdkHttpRequest.Builder request, ContentStreamProvider
113113

114114
switch (checksum) {
115115
case STREAMING_ECDSA_SIGNED_PAYLOAD: {
116-
long extensionsLength = 161; // ;chunk-signature:<sigv4a-ecsda hex signature, 64 bytes>
116+
long extensionsLength = 161; // ;chunk-signature:<sigv4a-ecsda hex signature, 144 bytes>
117117
encodedContentLength += calculateChunksLength(contentLength, extensionsLength);
118118
break;
119119
}
@@ -124,12 +124,12 @@ public void beforeSigning(SdkHttpRequest.Builder request, ContentStreamProvider
124124
encodedContentLength += calculateChunksLength(contentLength, 0);
125125
break;
126126
case STREAMING_ECDSA_SIGNED_PAYLOAD_TRAILER: {
127-
long extensionsLength = 161; // ;chunk-signature:<sigv4a-ecsda hex signature, 64 bytes>
127+
long extensionsLength = 161; // ;chunk-signature:<sigv4a-ecsda hex signature, 144 bytes>
128128
encodedContentLength += calculateChunksLength(contentLength, extensionsLength);
129129
if (checksumAlgorithm != null) {
130130
encodedContentLength += calculateChecksumTrailerLength(checksumHeaderName(checksumAlgorithm));
131131
}
132-
encodedContentLength += 170; // x-amz-trailer-signature:<sigv4a-ecsda hex signature, 64 bytes>\r\n
132+
encodedContentLength += 170; // x-amz-trailer-signature:<sigv4a-ecsda hex signature, 144 bytes>\r\n
133133
break;
134134
}
135135
default:
@@ -177,7 +177,7 @@ private long calculateChunksLength(long contentLength, long extensionsLength) {
177177
long remainingBytes = contentLength % chunkSize;
178178
if (remainingBytes > 0) {
179179
long remainingChunkHeaderLength = Long.toHexString(remainingBytes).length();
180-
lengthInBytes += remainingChunkHeaderLength + 1 + extensionsLength + 2 + remainingBytes + 2;
180+
lengthInBytes += remainingChunkHeaderLength + extensionsLength + 2 + remainingBytes + 2;
181181
}
182182

183183
// final chunk

core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/AwsChunkedV4PayloadSigner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ private long calculateChunksLength(long contentLength, long extensionsLength) {
194194
long remainingBytes = contentLength % chunkSize;
195195
if (remainingBytes > 0) {
196196
long remainingChunkHeaderLength = Long.toHexString(remainingBytes).length();
197-
lengthInBytes += remainingChunkHeaderLength + 1 + extensionsLength + 2 + remainingBytes + 2;
197+
lengthInBytes += remainingChunkHeaderLength + extensionsLength + 2 + remainingBytes + 2;
198198
}
199199

200200
// final chunk

core/http-auth-aws/src/main/java/software/amazon/awssdk/http/auth/aws/internal/signer/util/SignerUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,10 @@ public static byte[] hash(String text) {
255255
return hash(text.getBytes(StandardCharsets.UTF_8));
256256
}
257257

258+
/**
259+
* Consume entire stream and return the number of bytes - the stream will NOT be reset upon completion, so if it needs to
260+
* be read again, the caller MUST reset the stream.
261+
*/
258262
private static int readAll(InputStream inputStream) {
259263
try {
260264
byte[] buffer = new byte[4096];

core/http-auth-aws/src/test/java/software/amazon/awssdk/http/auth/aws/internal/signer/DefaultAwsV4HttpSignerTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public void sign_WithChunkEncodingTrue_DelegatesToAwsChunkedPayloadSigner() {
175175

176176
assertThat(signedRequest.request().firstMatchingHeader("x-amz-content-sha256"))
177177
.hasValue("STREAMING-AWS4-HMAC-SHA256-PAYLOAD");
178-
Assertions.assertThat(signedRequest.request().firstMatchingHeader(Header.CONTENT_LENGTH)).hasValue("194");
178+
Assertions.assertThat(signedRequest.request().firstMatchingHeader(Header.CONTENT_LENGTH)).hasValue("193");
179179
assertThat(signedRequest.request().firstMatchingHeader("x-amz-decoded-content-length")).hasValue("20");
180180
}
181181

@@ -194,7 +194,7 @@ public void sign_WithChunkEncodingTrueAndChecksumAlgorithm_DelegatesToAwsChunked
194194

195195
assertThat(signedRequest.request().firstMatchingHeader("x-amz-content-sha256"))
196196
.hasValue("STREAMING-AWS4-HMAC-SHA256-PAYLOAD-TRAILER");
197-
Assertions.assertThat(signedRequest.request().firstMatchingHeader(Header.CONTENT_LENGTH)).hasValue("315");
197+
Assertions.assertThat(signedRequest.request().firstMatchingHeader(Header.CONTENT_LENGTH)).hasValue("314");
198198
assertThat(signedRequest.request().firstMatchingHeader("x-amz-decoded-content-length")).hasValue("20");
199199
assertThat(signedRequest.request().firstMatchingHeader("x-amz-trailer")).hasValue("x-amz-checksum-crc32");
200200
}
@@ -215,7 +215,7 @@ public void sign_WithPayloadSigningFalseAndChunkEncodingTrueAndTrailer_Delegates
215215

216216
assertThat(signedRequest.request().firstMatchingHeader("x-amz-content-sha256"))
217217
.hasValue("STREAMING-UNSIGNED-PAYLOAD-TRAILER");
218-
Assertions.assertThat(signedRequest.request().firstMatchingHeader(Header.CONTENT_LENGTH)).hasValue("63");
218+
Assertions.assertThat(signedRequest.request().firstMatchingHeader(Header.CONTENT_LENGTH)).hasValue("62");
219219
assertThat(signedRequest.request().firstMatchingHeader("x-amz-decoded-content-length")).hasValue("20");
220220
assertThat(signedRequest.request().firstMatchingHeader("x-amz-trailer")).hasValue("x-amz-checksum-crc32");
221221
}

0 commit comments

Comments
 (0)