Skip to content

Commit 6aabb1e

Browse files
authored
Use getRawPath and getRawQuery for signed URLs (#3795)
* Use getRawPath and getRawQuery for signed URLs * Rename test url
1 parent fa1440e commit 6aabb1e

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

services/cloudfront/src/main/java/software/amazon/awssdk/services/cloudfront/CloudFrontUtilities.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ public SignedUrl getSignedUrlWithCannedPolicy(CannedSignerRequest request) {
145145
URI uri = URI.create(resourceUrl);
146146
String protocol = uri.getScheme();
147147
String domain = uri.getHost();
148-
String encodedPath = uri.getPath()
149-
+ (uri.getQuery() != null ? "?" + uri.getQuery() + "&" : "?")
148+
String encodedPath = uri.getRawPath()
149+
+ (uri.getQuery() != null ? "?" + uri.getRawQuery() + "&" : "?")
150150
+ "Expires=" + request.expirationDate().getEpochSecond()
151151
+ "&Signature=" + urlSafeSignature
152152
+ "&Key-Pair-Id=" + request.keyPairId();
@@ -254,8 +254,8 @@ public SignedUrl getSignedUrlWithCustomPolicy(CustomSignerRequest request) {
254254
URI uri = URI.create(resourceUrl);
255255
String protocol = uri.getScheme();
256256
String domain = uri.getHost();
257-
String encodedPath = uri.getPath()
258-
+ (uri.getQuery() != null ? "?" + uri.getQuery() + "&" : "?")
257+
String encodedPath = uri.getRawPath()
258+
+ (uri.getQuery() != null ? "?" + uri.getRawQuery() + "&" : "?")
259259
+ "Policy=" + urlSafePolicy
260260
+ "&Signature=" + urlSafeSignature
261261
+ "&Key-Pair-Id=" + request.keyPairId();

services/cloudfront/src/test/java/software/amazon/awssdk/services/cloudfront/CloudFrontUtilitiesTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,52 @@ void getSignedURLWithCustomPolicy_withMissingExpirationDate_shouldThrowException
214214
assertThat(exception.getMessage().contains("Expiration date must be provided to sign CloudFront URLs"));
215215
}
216216

217+
@Test
218+
void getSignedURLWithCannedPolicy_withEncodedUrl_doesNotDecodeUrl() {
219+
String encodedUrl = "https://distributionDomain/s3ObjectKey/%40blob?v=1n1dm%2F01n1dm0";
220+
Instant expirationDate = LocalDate.of(2024, 1, 1).atStartOfDay().toInstant(ZoneOffset.of("Z"));
221+
SignedUrl signedUrl =
222+
cloudFrontUtilities.getSignedUrlWithCannedPolicy(r -> r
223+
.resourceUrl(encodedUrl)
224+
.privateKey(keyPair.getPrivate())
225+
.keyPairId("keyPairId")
226+
.expirationDate(expirationDate));
227+
String url = signedUrl.url();
228+
String signature = url.substring(url.indexOf("&Signature"), url.indexOf("&Key-Pair-Id"));
229+
String expected = "https://distributionDomain/s3ObjectKey/%40blob?v=1n1dm%2F01n1dm0&Expires=1704067200"
230+
+ signature
231+
+ "&Key-Pair-Id=keyPairId";
232+
assertThat(expected).isEqualTo(url);
233+
}
234+
235+
@Test
236+
void getSignedURLWithCustomPolicy_withEncodedUrl_doesNotDecodeUrl() {
237+
String encodedUrl = "https://distributionDomain/s3ObjectKey/%40blob?v=1n1dm%2F01n1dm0";
238+
Instant activeDate = LocalDate.of(2022, 1, 1).atStartOfDay().toInstant(ZoneOffset.of("Z"));
239+
Instant expirationDate = LocalDate.of(2024, 1, 1).atStartOfDay().toInstant(ZoneOffset.of("Z"));
240+
String ipRange = "1.2.3.4";
241+
SignedUrl signedUrl = cloudFrontUtilities.getSignedUrlWithCustomPolicy(r -> {
242+
try {
243+
r.resourceUrl(encodedUrl)
244+
.privateKey(keyFilePath)
245+
.keyPairId("keyPairId")
246+
.expirationDate(expirationDate)
247+
.activeDate(activeDate)
248+
.ipRange(ipRange);
249+
} catch (Exception e) {
250+
throw new RuntimeException(e);
251+
}
252+
});
253+
String url = signedUrl.url();
254+
String policy = url.substring(url.indexOf("Policy=") + 7, url.indexOf("&Signature"));
255+
String signature = url.substring(url.indexOf("&Signature"), url.indexOf("&Key-Pair-Id"));
256+
String expected = "https://distributionDomain/s3ObjectKey/%40blob?v=1n1dm%2F01n1dm0&Policy="
257+
+ policy
258+
+ signature
259+
+ "&Key-Pair-Id=keyPairId";
260+
assertThat(expected).isEqualTo(url);
261+
}
262+
217263
@Test
218264
void getCookiesForCannedPolicy_producesValidCookies() throws Exception {
219265
Instant expirationDate = LocalDate.of(2024, 1, 1).atStartOfDay().toInstant(ZoneOffset.of("Z"));

0 commit comments

Comments
 (0)