Skip to content

Commit b6bd73b

Browse files
authored
Include query params in signed URL (#3678)
1 parent 492dfdd commit b6bd73b

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public SignedUrl getSignedUrlWithCannedPolicy(CannedSignerRequest request) {
146146
String protocol = uri.getScheme();
147147
String domain = uri.getHost();
148148
String encodedPath = uri.getPath()
149-
+ (request.resourceUrl().indexOf('?') >= 0 ? "&" : "?")
149+
+ (uri.getQuery() != null ? "?" + uri.getQuery() + "&" : "?")
150150
+ "Expires=" + request.expirationDate().getEpochSecond()
151151
+ "&Signature=" + urlSafeSignature
152152
+ "&Key-Pair-Id=" + request.keyPairId();
@@ -255,7 +255,7 @@ public SignedUrl getSignedUrlWithCustomPolicy(CustomSignerRequest request) {
255255
String protocol = uri.getScheme();
256256
String domain = uri.getHost();
257257
String encodedPath = uri.getPath()
258-
+ (request.resourceUrl().indexOf('?') >= 0 ? "&" : "?")
258+
+ (uri.getQuery() != null ? "?" + uri.getQuery() + "&" : "?")
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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,25 @@ void getSignedURLWithCannedPolicy_producesValidUrl() {
8989
assertThat(expected).isEqualTo(url);
9090
}
9191

92+
@Test
93+
void getSignedURLWithCannedPolicy_withQueryParams_producesValidUrl() {
94+
Instant expirationDate = LocalDate.of(2024, 1, 1).atStartOfDay().toInstant(ZoneOffset.of("Z"));
95+
String resourceUrlWithQueryParams = "https://d1npcfkc2mojrf.cloudfront.net/s3ObjectKey?a=b&c=d";
96+
SignedUrl signedUrl =
97+
cloudFrontUtilities.getSignedUrlWithCannedPolicy(r -> r
98+
.resourceUrl(resourceUrlWithQueryParams)
99+
.privateKey(keyPair.getPrivate())
100+
.keyPairId("keyPairId")
101+
.expirationDate(expirationDate));
102+
String url = signedUrl.url();
103+
104+
String signature = url.substring(url.indexOf("&Signature"), url.indexOf("&Key-Pair-Id"));
105+
String expected = "https://d1npcfkc2mojrf.cloudfront.net/s3ObjectKey?a=b&c=d&Expires=1704067200"
106+
+ signature
107+
+ "&Key-Pair-Id=keyPairId";
108+
assertThat(expected).isEqualTo(url);
109+
}
110+
92111
@Test
93112
void getSignedURLWithCustomPolicy_producesValidUrl() throws Exception {
94113
Instant activeDate = LocalDate.of(2022, 1, 1).atStartOfDay().toInstant(ZoneOffset.of("Z"));
@@ -116,6 +135,30 @@ void getSignedURLWithCustomPolicy_producesValidUrl() throws Exception {
116135
assertThat(expected).isEqualTo(url);
117136
}
118137

138+
@Test
139+
void getSignedURLWithCustomPolicy_withQueryParams_producesValidUrl() {
140+
Instant activeDate = LocalDate.of(2022, 1, 1).atStartOfDay().toInstant(ZoneOffset.of("Z"));
141+
Instant expirationDate = LocalDate.of(2024, 1, 1).atStartOfDay().toInstant(ZoneOffset.of("Z"));
142+
String ipRange = "1.2.3.4";
143+
String resourceUrlWithQueryParams = "https://d1npcfkc2mojrf.cloudfront.net/s3ObjectKey?a=b&c=d";
144+
SignedUrl signedUrl =
145+
cloudFrontUtilities.getSignedUrlWithCustomPolicy(r -> r
146+
.resourceUrl(resourceUrlWithQueryParams)
147+
.privateKey(keyPair.getPrivate())
148+
.keyPairId("keyPairId")
149+
.expirationDate(expirationDate)
150+
.activeDate(activeDate)
151+
.ipRange(ipRange));
152+
String url = signedUrl.url();
153+
String policy = url.substring(url.indexOf("Policy=") + 7, url.indexOf("&Signature"));
154+
String signature = url.substring(url.indexOf("&Signature"), url.indexOf("&Key-Pair-Id"));
155+
String expected = "https://d1npcfkc2mojrf.cloudfront.net/s3ObjectKey?a=b&c=d&Policy="
156+
+ policy
157+
+ signature
158+
+ "&Key-Pair-Id=keyPairId";
159+
assertThat(expected).isEqualTo(url);
160+
}
161+
119162
@Test
120163
void getSignedURLWithCustomPolicy_withIpRangeOmitted_producesValidUrl() throws Exception {
121164
Instant activeDate = LocalDate.of(2022, 1, 1).atStartOfDay().toInstant(ZoneOffset.of("Z"));

0 commit comments

Comments
 (0)