Skip to content

Fixed an issue in sync clients where empty response payloads could cause a null pointer exception. #3371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/next-release/bugfix-AWSSDKforJavav2-a18a347.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "bugfix",
"category": "AWS SDK for Java v2",
"contributor": "",
"description": "Fixed an issue in sync clients where empty response payloads could cause a null pointer exception."
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ private HttpResponseHandlerAdapter(HttpResponseHandler<OutputT> httpResponseHand
@Override
public ReturnT handle(SdkHttpFullResponse response, ExecutionAttributes executionAttributes) throws Exception {
OutputT resp = httpResponseHandler.handle(response, executionAttributes);
return transformResponse(resp, response.content().orElse(null));
return transformResponse(resp, response.content().orElseGet(AbortableInputStream::createEmpty));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ static <ResponseT> ResponseTransformer<ResponseT, ResponseBytes<ResponseT>> toBy
return (response, inputStream) -> {
try {
InterruptMonitor.checkInterrupted();
return ResponseBytes.fromByteArray(response, IoUtils.toByteArray(inputStream));
return ResponseBytes.fromByteArrayUnsafe(response, IoUtils.toByteArray(inputStream));
} catch (IOException e) {
throw RetryableException.builder().message("Failed to read response.").cause(e).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static software.amazon.awssdk.utils.Validate.paramNotNull;

import java.io.ByteArrayInputStream;
import java.io.FilterInputStream;
import java.io.InputStream;
import software.amazon.awssdk.annotations.SdkProtectedApi;
Expand Down Expand Up @@ -61,6 +62,10 @@ public static AbortableInputStream create(InputStream delegate) {
return new AbortableInputStream(delegate, () -> { });
}

public static AbortableInputStream createEmpty() {
return create(new ByteArrayInputStream(new byte[0]));
}

@Override
public void abort() {
abortable.abort();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.http.urlconnection;

import static org.assertj.core.api.Assertions.assertThat;
import static software.amazon.awssdk.testutils.service.S3BucketUtils.temporaryBucketName;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.services.s3.S3Client;

public class EmptyFileS3IntegrationTest extends UrlHttpConnectionS3IntegrationTestBase {
private static final String BUCKET = temporaryBucketName(EmptyFileS3IntegrationTest.class);

@BeforeClass
public static void setup() {
createBucket(BUCKET);
}

@AfterClass
public static void cleanup() {
deleteBucketAndAllContents(BUCKET);
}

@Test
public void s3EmptyFileGetAsBytesWorksWithoutChecksumValidationEnabled() {
try (S3Client s3 = s3ClientBuilder().serviceConfiguration(c -> c.checksumValidationEnabled(false))
.build()) {
s3.putObject(r -> r.bucket(BUCKET).key("x"), RequestBody.empty());
assertThat(s3.getObjectAsBytes(r -> r.bucket(BUCKET).key("x")).asUtf8String()).isEmpty();
}
}

@Test
public void s3EmptyFileContentLengthIsCorrectWithoutChecksumValidationEnabled() {
try (S3Client s3 = s3ClientBuilder().serviceConfiguration(c -> c.checksumValidationEnabled(false))
.build()) {
s3.putObject(r -> r.bucket(BUCKET).key("x"), RequestBody.empty());
assertThat(s3.getObject(r -> r.bucket(BUCKET).key("x")).response().contentLength()).isEqualTo(0);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,19 @@

package software.amazon.awssdk.http.urlconnection;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Iterator;
import java.util.List;
import org.junit.BeforeClass;
import software.amazon.awssdk.core.ClientType;
import software.amazon.awssdk.core.interceptor.Context;
import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
import software.amazon.awssdk.core.interceptor.ExecutionInterceptor;
import software.amazon.awssdk.http.apache.ApacheHttpClient;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3AsyncClient;
import software.amazon.awssdk.services.s3.S3AsyncClientBuilder;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.S3ClientBuilder;
import software.amazon.awssdk.services.s3.model.BucketLocationConstraint;
import software.amazon.awssdk.services.s3.model.CreateBucketConfiguration;
import software.amazon.awssdk.services.s3.model.CreateBucketRequest;
import software.amazon.awssdk.services.s3.model.DeleteBucketRequest;
import software.amazon.awssdk.services.s3.model.DeleteObjectRequest;
import software.amazon.awssdk.services.s3.model.ListObjectVersionsRequest;
import software.amazon.awssdk.services.s3.model.ListObjectVersionsResponse;
import software.amazon.awssdk.services.s3.model.ListObjectsRequest;
import software.amazon.awssdk.services.s3.model.ListObjectsResponse;
import software.amazon.awssdk.services.s3.model.NoSuchBucketException;
import software.amazon.awssdk.services.s3.model.S3Exception;
import software.amazon.awssdk.services.s3.model.S3Object;
import software.amazon.awssdk.testutils.Waiter;
import software.amazon.awssdk.testutils.service.AwsTestBase;
Expand Down Expand Up @@ -79,6 +66,7 @@ protected static void createBucket(String bucket) {
Waiter.run(() -> s3.createBucket(r -> r.bucket(bucket)))
.ignoringException(NoSuchBucketException.class)
.orFail();
s3.waiter().waitUntilBucketExists(r -> r.bucket(bucket));
}

protected static void deleteBucketAndAllContents(String bucketName) {
Expand Down