Skip to content
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

Avoid encoding of slashes when signing an url #1347

Closed
wants to merge 6 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ public URL signUrl(BlobInfo blobInfo, long duration, TimeUnit unit, SignUrlOptio
if (blobInfo.getName().startsWith("/")) {
path.setLength(path.length() - 1);
}
path.append(UrlEscapers.urlPathSegmentEscaper().escape(blobInfo.getName()));
path.append(UrlEscapers.urlFragmentEscaper().escape(blobInfo.name()));
stBuilder.append(path);
try {
byte[] signatureBytes = authCredentials.sign(stBuilder.toString().getBytes(UTF_8));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1259,7 +1259,7 @@ public void testSignUrlLeadingSlash() throws NoSuchAlgorithmException, InvalidKe
storage = options.toBuilder().authCredentials(authCredentials).build().service();
URL url =
storage.signUrl(BlobInfo.newBuilder(BUCKET_NAME1, blobName).build(), 14, TimeUnit.DAYS);
String escapedBlobName = UrlEscapers.urlPathSegmentEscaper().escape(blobName);
String escapedBlobName = UrlEscapers.urlFragmentEscaper().escape(blobName);
String stringUrl = url.toString();
String expectedUrl = new StringBuilder("https://storage.googleapis.com/").append(BUCKET_NAME1)
.append(escapedBlobName).append("?GoogleAccessId=").append(ACCOUNT).append("&Expires=")
Expand Down Expand Up @@ -1323,7 +1323,7 @@ public void testSignUrlForBlobWithSpecialChars() throws NoSuchAlgorithmException
String blobName = "/a" + specialChar + "b";
URL url =
storage.signUrl(BlobInfo.newBuilder(BUCKET_NAME1, blobName).build(), 14, TimeUnit.DAYS);
String escapedBlobName = UrlEscapers.urlPathSegmentEscaper().escape(blobName);
String escapedBlobName = UrlEscapers.urlFragmentEscaper().escape(blobName);
String stringUrl = url.toString();
String expectedUrl = new StringBuilder("https://storage.googleapis.com/").append(BUCKET_NAME1)
.append(escapedBlobName).append("?GoogleAccessId=").append(ACCOUNT).append("&Expires=")
Expand All @@ -1343,6 +1343,37 @@ public void testSignUrlForBlobWithSpecialChars() throws NoSuchAlgorithmException
}
}

@Test
public void testSignUrlForBlobWithSlashes() throws NoSuchAlgorithmException,
InvalidKeyException, SignatureException, UnsupportedEncodingException {
EasyMock.replay(storageRpcMock);
ServiceAccountAuthCredentials authCredentials =
ServiceAccountAuthCredentials.createFor(ACCOUNT, privateKey);
storage = options.toBuilder().authCredentials(authCredentials).build().service();

String blobName = "/foo/bar/baz #%20other cool stuff.txt";
URL url =
storage.signUrl(BlobInfo.newBuilder(BUCKET_NAME1, blobName).build(), 14, TimeUnit.DAYS);

String escapedBlobName = UrlEscapers.urlFragmentEscaper().escape(blobName);
String stringUrl = url.toString();
String expectedUrl = new StringBuilder("https://storage.googleapis.com/").append(BUCKET_NAME1)
.append(escapedBlobName).append("?GoogleAccessId=").append(ACCOUNT).append("&Expires=")
.append(42L + 1209600).append("&Signature=").toString();
assertTrue(stringUrl.startsWith(expectedUrl));
String signature = stringUrl.substring(expectedUrl.length());

StringBuilder signedMessageBuilder = new StringBuilder();
signedMessageBuilder.append(HttpMethod.GET).append("\n\n\n").append(42L + 1209600)
.append("\n/").append(BUCKET_NAME1).append(escapedBlobName);

Signature signer = Signature.getInstance("SHA256withRSA");
signer.initVerify(publicKey);
signer.update(signedMessageBuilder.toString().getBytes(UTF_8));
assertTrue(signer.verify(BaseEncoding.base64().decode(
URLDecoder.decode(signature, UTF_8.name()))));
}

@Test
public void testGetAllArray() {
BlobId blobId1 = BlobId.of(BUCKET_NAME1, BLOB_NAME1);
Expand Down