Skip to content

Commit b7dce8a

Browse files
authored
Require TLS protected endpoints for key and SAS authentication (Azure#21832)
Return consistent error message for all failed HTTPS checks. Fix misspelled file name.
1 parent 0699b05 commit b7dce8a

File tree

7 files changed

+62
-5
lines changed

7 files changed

+62
-5
lines changed

sdk/azcore/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* Fixed an issue that could cause some allowed HTTP header values to not show up in logs.
1414
* Include error text instead of error type in traces when the transport returns an error.
1515
* Fixed an issue that could cause an HTTP/2 request to hang when the TCP connection becomes unresponsive.
16+
* Block key and SAS authentication for non TLS protected endpoints.
1617

1718
### Other Changes
1819

sdk/azcore/runtime/policy_bearer_token.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ func (b *BearerTokenPolicy) authenticateAndAuthorize(req *policy.Request) func(p
7272

7373
// Do authorizes a request with a bearer token
7474
func (b *BearerTokenPolicy) Do(req *policy.Request) (*http.Response, error) {
75-
if strings.ToLower(req.Raw().URL.Scheme) != "https" {
76-
return nil, shared.NonRetriableError(errors.New("bearer token authentication is not permitted for non TLS protected (https) endpoints"))
75+
if err := checkHTTPSForAuth(req); err != nil {
76+
return nil, err
7777
}
7878
var err error
7979
if b.authzHandler.OnRequest != nil {
@@ -103,3 +103,10 @@ func (b *BearerTokenPolicy) Do(req *policy.Request) (*http.Response, error) {
103103
}
104104
return res, err
105105
}
106+
107+
func checkHTTPSForAuth(req *policy.Request) error {
108+
if strings.ToLower(req.Raw().URL.Scheme) != "https" {
109+
return shared.NonRetriableError(errors.New("authenticated requests are not permitted for non TLS protected (https) endpoints"))
110+
}
111+
return nil
112+
}

sdk/azcore/runtime/policy_bearer_token_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,12 @@ func TestBearerTokenPolicy_RequiresHTTPS(t *testing.T) {
237237
var nre errorinfo.NonRetriable
238238
require.ErrorAs(t, err, &nre)
239239
}
240+
241+
func TestCheckHTTPSForAuth(t *testing.T) {
242+
req, err := NewRequest(context.Background(), http.MethodGet, "http://contoso.com")
243+
require.NoError(t, err)
244+
require.Error(t, checkHTTPSForAuth(req))
245+
req, err = NewRequest(context.Background(), http.MethodGet, "https://contoso.com")
246+
require.NoError(t, err)
247+
require.NoError(t, checkHTTPSForAuth(req))
248+
}

sdk/azcore/runtime/policy_key_credential.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ func NewKeyCredentialPolicy(cred *exported.KeyCredential, header string, options
4040

4141
// Do implementes the Do method on the [policy.Polilcy] interface.
4242
func (k *KeyCredentialPolicy) Do(req *policy.Request) (*http.Response, error) {
43+
if err := checkHTTPSForAuth(req); err != nil {
44+
return nil, err
45+
}
4346
val := exported.KeyCredentialGet(k.cred)
4447
if k.prefix != "" {
4548
val = k.prefix + val

sdk/azcore/runtime/policy_key_credential_test.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestKeyCredentialPolicy(t *testing.T) {
2626
return &http.Response{}, nil
2727
}), policy)
2828

29-
req, err := NewRequest(context.Background(), http.MethodGet, "http://contoso.com")
29+
req, err := NewRequest(context.Background(), http.MethodGet, "https://contoso.com")
3030
require.NoError(t, err)
3131

3232
_, err = pl.Do(req)
@@ -42,9 +42,26 @@ func TestKeyCredentialPolicy(t *testing.T) {
4242
return &http.Response{}, nil
4343
}), policy)
4444

45-
req, err = NewRequest(context.Background(), http.MethodGet, "http://contoso.com")
45+
req, err = NewRequest(context.Background(), http.MethodGet, "https://contoso.com")
4646
require.NoError(t, err)
4747

4848
_, err = pl.Do(req)
4949
require.NoError(t, err)
5050
}
51+
52+
func TestKeyCredentialPolicy_RequiresHTTPS(t *testing.T) {
53+
cred := exported.NewKeyCredential("foo")
54+
55+
policy := NewKeyCredentialPolicy(cred, "fake-auth", nil)
56+
require.NotNil(t, policy)
57+
58+
pl := exported.NewPipeline(shared.TransportFunc(func(req *http.Request) (*http.Response, error) {
59+
return &http.Response{}, nil
60+
}), policy)
61+
62+
req, err := NewRequest(context.Background(), http.MethodGet, "http://contoso.com")
63+
require.NoError(t, err)
64+
65+
_, err = pl.Do(req)
66+
require.Error(t, err)
67+
}

sdk/azcore/runtime/policy_sas_credential.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ func NewSASCredentialPolicy(cred *exported.SASCredential, header string, options
3434

3535
// Do implementes the Do method on the [policy.Polilcy] interface.
3636
func (k *SASCredentialPolicy) Do(req *policy.Request) (*http.Response, error) {
37+
if err := checkHTTPSForAuth(req); err != nil {
38+
return nil, err
39+
}
3740
req.Raw().Header.Add(k.header, exported.SASCredentialGet(k.cred))
3841
return req.Next()
3942
}

sdk/azcore/runtime/polilcy_sas_credential_test.go renamed to sdk/azcore/runtime/policy_sas_credential_test.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,26 @@ func TestSASCredentialPolicy(t *testing.T) {
2626
return &http.Response{}, nil
2727
}), policy)
2828

29-
req, err := NewRequest(context.Background(), http.MethodGet, "http://contoso.com")
29+
req, err := NewRequest(context.Background(), http.MethodGet, "https://contoso.com")
3030
require.NoError(t, err)
3131

3232
_, err = pl.Do(req)
3333
require.NoError(t, err)
3434
}
35+
36+
func TestSASCredentialPolicy_RequiresHTTPS(t *testing.T) {
37+
cred := exported.NewSASCredential("foo")
38+
39+
policy := NewSASCredentialPolicy(cred, "fake-auth", nil)
40+
require.NotNil(t, policy)
41+
42+
pl := exported.NewPipeline(shared.TransportFunc(func(req *http.Request) (*http.Response, error) {
43+
return &http.Response{}, nil
44+
}), policy)
45+
46+
req, err := NewRequest(context.Background(), http.MethodGet, "http://contoso.com")
47+
require.NoError(t, err)
48+
49+
_, err = pl.Do(req)
50+
require.Error(t, err)
51+
}

0 commit comments

Comments
 (0)