Skip to content

Commit 5af23e5

Browse files
authored
Allow http in SAS policy. (Azure#18408)
* do not require https in sas policy * Revert "do not require https in sas policy" This reverts commit 07ee3b2. * option to not require https policy.
1 parent 99fed03 commit 5af23e5

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

sdk/core/azure-core/src/main/java/com/azure/core/http/policy/AzureSasCredentialPolicy.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,42 @@
1414

1515
/**
1616
* Pipeline policy that uses an {@link AzureSasCredential} to set the shared access signature for a request.
17-
* <p>
18-
* Requests sent with this pipeline policy are required to use {@code HTTPS}. If the request isn't using {@code HTTPS}
19-
* an exception will be thrown to prevent leaking the shared access signature.
2017
*/
2118
public final class AzureSasCredentialPolicy implements HttpPipelinePolicy {
2219
private final AzureSasCredential credential;
20+
private final boolean requireHttps;
2321

2422
/**
2523
* Creates a policy that uses the passed {@link AzureSasCredential} to append sas to query string.
24+
* <p>
25+
* Requests sent with this pipeline policy are required to use {@code HTTPS}.
26+
* If the request isn't using {@code HTTPS}
27+
* an exception will be thrown to prevent leaking the shared access signature.
2628
*
2729
* @param credential The {@link AzureSasCredential} containing the shared access signature to use.
2830
* @throws NullPointerException If {@code credential} is {@code null}.
2931
*/
3032
public AzureSasCredentialPolicy(AzureSasCredential credential) {
31-
Objects.requireNonNull(credential, "'credential' cannot be null.");
33+
this(credential, true);
34+
}
3235

36+
/**
37+
* Creates a policy that uses the passed {@link AzureSasCredential} to append sas to query string.
38+
*
39+
* @param credential The {@link AzureSasCredential} containing the shared access signature to use.
40+
* @param requireHttps A flag indicating whether {@code HTTPS} is required.
41+
* @throws NullPointerException If {@code credential} is {@code null}.
42+
*/
43+
public AzureSasCredentialPolicy(AzureSasCredential credential, boolean requireHttps) {
44+
Objects.requireNonNull(credential, "'credential' cannot be null.");
3345
this.credential = credential;
46+
this.requireHttps = requireHttps;
3447
}
3548

3649
@Override
3750
public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineNextPolicy next) {
3851
HttpRequest httpRequest = context.getHttpRequest();
39-
if ("http".equals(httpRequest.getUrl().getProtocol())) {
52+
if (requireHttps && "http".equals(httpRequest.getUrl().getProtocol())) {
4053
return Mono.error(new IllegalStateException(
4154
"Shared access signature credentials require HTTPS to prevent leaking the shared access signature."));
4255
}

sdk/core/azure-core/src/test/java/com/azure/core/credential/CredentialsTests.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public void sasCredentialsTest(String signature, String url, String expectedUrl)
113113
}
114114

115115
@Test
116-
public void sasCredentialsHTTPSchemeTest() throws Exception {
116+
public void sasCredentialsRequireHTTPSSchemeTest() throws Exception {
117117
AzureSasCredential credential = new AzureSasCredential("foo");
118118

119119
final HttpPipeline pipeline = new HttpPipelineBuilder()
@@ -126,4 +126,23 @@ public void sasCredentialsHTTPSchemeTest() throws Exception {
126126
.expectErrorMessage("Shared access signature credentials require HTTPS to prevent leaking the shared access signature.")
127127
.verify();
128128
}
129+
130+
@Test
131+
public void sasCredentialsDoNotRequireHTTPSchemeTest() throws Exception {
132+
AzureSasCredential credential = new AzureSasCredential("foo");
133+
134+
HttpPipelinePolicy auditorPolicy = (context, next) -> {
135+
String actualUrl = context.getHttpRequest().getUrl().toString();
136+
Assertions.assertEquals("http://localhost?foo", actualUrl);
137+
return next.process();
138+
};
139+
140+
final HttpPipeline pipeline = new HttpPipelineBuilder()
141+
.httpClient(new NoOpHttpClient())
142+
.policies(new AzureSasCredentialPolicy(credential, false), auditorPolicy)
143+
.build();
144+
145+
HttpRequest request = new HttpRequest(HttpMethod.GET, new URL("http://localhost"));
146+
pipeline.send(request).block();
147+
}
129148
}

0 commit comments

Comments
 (0)