Skip to content

Commit

Permalink
Merge pull request #154 from scireum/jvo/153-response-header-overrides
Browse files Browse the repository at this point in the history
Fixes Hashing when Query Contains Space
  • Loading branch information
jakobvogel authored Sep 22, 2020
2 parents 7c57284 + 0902564 commit 6776e8d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/ninja/Aws4HashCalculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private void appendQueryStringValue(String name,
}
canonicalRequest.append(Strings.urlEncode(name));
canonicalRequest.append("=");
canonicalRequest.append(Strings.urlEncode(value));
canonicalRequest.append(Strings.urlEncode(value).replace("+", "%20"));
}

private String hashedCanonicalRequest(final StringBuilder canonicalRequest) {
Expand Down
35 changes: 35 additions & 0 deletions src/test/java/BaseAWSSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ import com.amazonaws.services.s3.AmazonS3Client
import com.amazonaws.services.s3.model.AmazonS3Exception
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest
import com.amazonaws.services.s3.model.ObjectMetadata
import com.amazonaws.services.s3.model.ResponseHeaderOverrides
import com.amazonaws.services.s3.transfer.TransferManagerBuilder
import com.google.common.base.Charsets
import com.google.common.io.ByteStreams
import com.google.common.io.Files
import sirius.kernel.BaseSpecification

import java.time.Instant
import java.time.temporal.ChronoUnit

abstract class BaseAWSSpec extends BaseSpecification {

abstract AmazonS3Client getClient()
Expand Down Expand Up @@ -194,4 +198,35 @@ abstract class BaseAWSSpec extends BaseSpecification {
downloadedData == content
}

// reported in https://github.com/scireum/s3ninja/issues/153
def "PUT and then GET on presigned URL with ResponseHeaderOverrides works as expected"() {
given:
def client = getClient()
when:
if (!client.doesBucketExist("test")) {
client.createBucket("test")
}
and:
client.putObject(
"test",
"test",
new ByteArrayInputStream("Test".getBytes(Charsets.UTF_8)),
new ObjectMetadata())
def content = new String(
ByteStreams.toByteArray(client.getObject("test", "test").getObjectContent()),
Charsets.UTF_8)
and:
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest("test", "test")
.withExpiration(Date.from(Instant.now().plus(1, ChronoUnit.HOURS)))
.withResponseHeaders(
new ResponseHeaderOverrides()
.withContentDisposition("inline; filename=\"hello.txt\""))
URLConnection c = new URL(getClient().generatePresignedUrl(request).toString()).openConnection()
and:
String downloadedData = new String(ByteStreams.toByteArray(c.getInputStream()), Charsets.UTF_8)
then:
content == "Test"
and:
downloadedData == "Test"
}
}

0 comments on commit 6776e8d

Please sign in to comment.