Skip to content

Commit aab03c8

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

File tree

5 files changed

+15
-11
lines changed

5 files changed

+15
-11
lines changed

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

+4-4
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

+1-1
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

+4
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/crt/internal/signer/DefaultAwsCrtV4aHttpSignerTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public void sign_WithChunkEncodingTrue_DelegatesToAwsChunkedPayloadSigner() {
223223

224224
assertThat(signedRequest.request().firstMatchingHeader("x-amz-content-sha256"))
225225
.hasValue(STREAMING_AWS4_ECDSA_P256_SHA256_PAYLOAD);
226-
assertThat(signedRequest.request().firstMatchingHeader(Header.CONTENT_LENGTH)).hasValue("354");
226+
assertThat(signedRequest.request().firstMatchingHeader(Header.CONTENT_LENGTH)).hasValue("353");
227227
assertThat(signedRequest.request().firstMatchingHeader("x-amz-decoded-content-length")).hasValue("20");
228228

229229
// Ensures that CRT runs correctly and without throwing an exception
@@ -245,7 +245,7 @@ public void sign_WithChunkEncodingTrueAndChecksumAlgorithm_DelegatesToAwsChunked
245245

246246
assertThat(signedRequest.request().firstMatchingHeader("x-amz-content-sha256"))
247247
.hasValue(STREAMING_AWS4_ECDSA_P256_SHA256_PAYLOAD_TRAILER);
248-
assertThat(signedRequest.request().firstMatchingHeader(Header.CONTENT_LENGTH)).hasValue("555");
248+
assertThat(signedRequest.request().firstMatchingHeader(Header.CONTENT_LENGTH)).hasValue("554");
249249
assertThat(signedRequest.request().firstMatchingHeader("x-amz-decoded-content-length")).hasValue("20");
250250
assertThat(signedRequest.request().firstMatchingHeader("x-amz-trailer")).hasValue("x-amz-checksum-crc32");
251251

@@ -269,7 +269,7 @@ public void sign_WithPayloadSigningFalseAndChunkEncodingTrueAndTrailer_Delegates
269269

270270
assertThat(signedRequest.request().firstMatchingHeader("x-amz-content-sha256"))
271271
.hasValue(STREAMING_UNSIGNED_PAYLOAD_TRAILER);
272-
assertThat(signedRequest.request().firstMatchingHeader(Header.CONTENT_LENGTH)).hasValue("63");
272+
assertThat(signedRequest.request().firstMatchingHeader(Header.CONTENT_LENGTH)).hasValue("62");
273273
assertThat(signedRequest.request().firstMatchingHeader("x-amz-decoded-content-length")).hasValue("20");
274274
assertThat(signedRequest.request().firstMatchingHeader("x-amz-trailer")).hasValue("x-amz-checksum-crc32");
275275

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

+3-3
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)