Skip to content
Merged
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
8 changes: 6 additions & 2 deletions providers/azure/services/cache/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,14 @@ func NewClient(cred azcore.TokenCredential, subscriptionID, region string) *Clie
}

// NewClientWithHTTP creates a new Azure Cache client with a custom HTTP client (for testing).
// httpClient is used as-is and bypasses the SSRF dial guard of
// httpclient.New(): metadata, NAT64/6to4 and unspecified addresses are
// When httpClient is nil, the SSRF-hardened httpclient.New() is used so the nil
// fallback also blocks IMDS connections. A non-nil httpClient is used as-is and
// bypasses that guard: metadata, NAT64/6to4 and unspecified addresses are
// dialable unless the caller's client blocks them itself.
func NewClientWithHTTP(cred azcore.TokenCredential, subscriptionID, region string, httpClient HTTPClient) *Client {
if httpClient == nil {
httpClient = httpclient.New()
}
return &Client{
cred: cred,
subscriptionID: subscriptionID,
Expand Down
18 changes: 18 additions & 0 deletions providers/azure/services/cache/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1488,3 +1488,21 @@ func TestCacheClient_GetValidResourceTypes_PageCapFires(t *testing.T) {
require.NoError(t, err)
require.NotEmpty(t, skus, "page cap must trigger fallback to common SKUs, not an empty result")
}

// TestNewClientWithHTTP_NilFallbackIsHardened verifies that passing nil as the
// httpClient falls back to the SSRF-hardened httpclient.New(), which rejects
// IMDS connections, instead of leaving a nil client that panics on first use.
func TestNewClientWithHTTP_NilFallbackIsHardened(t *testing.T) {
c := NewClientWithHTTP(nil, "sub-123", "eastus", nil)
require.NotNil(t, c.httpClient)
require.NotEqual(t, http.DefaultClient, c.httpClient,
"nil fallback must never be http.DefaultClient")
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "http://169.254.169.254/metadata/instance", nil)
require.NoError(t, err)
resp, err := c.httpClient.Do(req)
if resp != nil {
defer resp.Body.Close()
}
require.Error(t, err, "nil-fallback client must reject IMDS connections")
assert.Contains(t, err.Error(), "blocked")
}
8 changes: 6 additions & 2 deletions providers/azure/services/compute/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,14 @@ func NewClient(cred azcore.TokenCredential, subscriptionID, region string) *Clie
}

// NewClientWithHTTP creates a new Azure Compute client with a custom HTTP client (for testing).
// httpClient is used as-is and bypasses the SSRF dial guard of
// httpclient.New(): metadata, NAT64/6to4 and unspecified addresses are
// When httpClient is nil, the SSRF-hardened httpclient.New() is used so the nil
// fallback also blocks IMDS connections. A non-nil httpClient is used as-is and
// bypasses that guard: metadata, NAT64/6to4 and unspecified addresses are
// dialable unless the caller's client blocks them itself.
func NewClientWithHTTP(cred azcore.TokenCredential, subscriptionID, region string, httpClient HTTPClient) *Client {
if httpClient == nil {
httpClient = httpclient.New()
}
return &Client{
cred: cred,
subscriptionID: subscriptionID,
Expand Down
18 changes: 18 additions & 0 deletions providers/azure/services/compute/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1852,6 +1852,24 @@ func TestPurchaseBody_SKUAndQuantityStayInMatchingUnits(t *testing.T) {
}
}

// TestNewClientWithHTTP_NilFallbackIsHardened verifies that passing nil as the
// httpClient falls back to the SSRF-hardened httpclient.New(), which rejects
// IMDS connections, instead of leaving a nil client that panics on first use.
func TestNewClientWithHTTP_NilFallbackIsHardened(t *testing.T) {
c := NewClientWithHTTP(nil, "sub-123", "eastus", nil)
require.NotNil(t, c.httpClient)
require.NotEqual(t, http.DefaultClient, c.httpClient,
"nil fallback must never be http.DefaultClient")
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "http://169.254.169.254/metadata/instance", nil)
require.NoError(t, err)
resp, err := c.httpClient.Do(req)
if resp != nil {
defer resp.Body.Close()
}
require.Error(t, err, "nil-fallback client must reject IMDS connections")
assert.Contains(t, err.Error(), "blocked")
}

func TestExtractVMSKUCapabilities_FailsLoudOnBadValues(t *testing.T) {
sku := func(vcpus, mem *string) *armcompute.ResourceSKU {
n1, n2 := "vCPUs", "MemoryGB"
Expand Down
8 changes: 6 additions & 2 deletions providers/azure/services/cosmosdb/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,14 @@ func NewClient(cred azcore.TokenCredential, subscriptionID, region string) *Clie
}

// NewClientWithHTTP creates a new Azure Cosmos DB client with a custom HTTP client (for testing).
// httpClient is used as-is and bypasses the SSRF dial guard of
// httpclient.New(): metadata, NAT64/6to4 and unspecified addresses are
// When httpClient is nil, the SSRF-hardened httpclient.New() is used so the nil
// fallback also blocks IMDS connections. A non-nil httpClient is used as-is and
// bypasses that guard: metadata, NAT64/6to4 and unspecified addresses are
// dialable unless the caller's client blocks them itself.
func NewClientWithHTTP(cred azcore.TokenCredential, subscriptionID, region string, httpClient HTTPClient) *Client {
if httpClient == nil {
httpClient = httpclient.New()
}
return &Client{
cred: cred,
subscriptionID: subscriptionID,
Expand Down
18 changes: 18 additions & 0 deletions providers/azure/services/cosmosdb/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1704,3 +1704,21 @@ func TestReservationResourceTypeCosmosDB_IsCanonical(t *testing.T) {
"Cosmos DB filter value must be the Azure REST API canonical enum \"CosmosDB\"; "+
"\"CosmosDb\" (pre-fix) is not a valid resourceType")
}

// TestNewClientWithHTTP_NilFallbackIsHardened verifies that passing nil as the
// httpClient falls back to the SSRF-hardened httpclient.New(), which rejects
// IMDS connections, instead of leaving a nil client that panics on first use.
func TestNewClientWithHTTP_NilFallbackIsHardened(t *testing.T) {
c := NewClientWithHTTP(nil, "sub-123", "eastus", nil)
require.NotNil(t, c.httpClient)
require.NotEqual(t, http.DefaultClient, c.httpClient,
"nil fallback must never be http.DefaultClient")
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "http://169.254.169.254/metadata/instance", nil)
require.NoError(t, err)
resp, err := c.httpClient.Do(req)
if resp != nil {
defer resp.Body.Close()
}
require.Error(t, err, "nil-fallback client must reject IMDS connections")
assert.Contains(t, err.Error(), "blocked")
}
8 changes: 6 additions & 2 deletions providers/azure/services/database/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,14 @@ func NewClient(cred azcore.TokenCredential, subscriptionID, region string) *Clie
}

// NewClientWithHTTP creates a new Azure Database client with a custom HTTP client (for testing).
// httpClient is used as-is and bypasses the SSRF dial guard of
// httpclient.New(): metadata, NAT64/6to4 and unspecified addresses are
// When httpClient is nil, the SSRF-hardened httpclient.New() is used so the nil
// fallback also blocks IMDS connections. A non-nil httpClient is used as-is and
// bypasses that guard: metadata, NAT64/6to4 and unspecified addresses are
// dialable unless the caller's client blocks them itself.
func NewClientWithHTTP(cred azcore.TokenCredential, subscriptionID, region string, httpClient HTTPClient) *Client {
if httpClient == nil {
httpClient = httpclient.New()
}
return &Client{
cred: cred,
subscriptionID: subscriptionID,
Expand Down
18 changes: 18 additions & 0 deletions providers/azure/services/database/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2118,3 +2118,21 @@ func TestDatabaseClient_ServerInfo_FetchedOnce(t *testing.T) {
assert.Equal(t, 0, serversPager.CallCount,
"servers pager NextPage not called when it has no pages")
}

// TestNewClientWithHTTP_NilFallbackIsHardened verifies that passing nil as the
// httpClient falls back to the SSRF-hardened httpclient.New(), which rejects
// IMDS connections, instead of leaving a nil client that panics on first use.
func TestNewClientWithHTTP_NilFallbackIsHardened(t *testing.T) {
c := NewClientWithHTTP(nil, "sub-123", "eastus", nil)
require.NotNil(t, c.httpClient)
require.NotEqual(t, http.DefaultClient, c.httpClient,
"nil fallback must never be http.DefaultClient")
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "http://169.254.169.254/metadata/instance", nil)
require.NoError(t, err)
resp, err := c.httpClient.Do(req)
if resp != nil {
defer resp.Body.Close()
}
require.Error(t, err, "nil-fallback client must reject IMDS connections")
assert.Contains(t, err.Error(), "blocked")
}
8 changes: 6 additions & 2 deletions providers/azure/services/search/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,14 @@ func NewClient(cred azcore.TokenCredential, subscriptionID, region string) *Clie
}

// NewClientWithHTTP creates a new Azure Search client with a custom HTTP client (for testing).
// httpClient is used as-is and bypasses the SSRF dial guard of
// httpclient.New(): metadata, NAT64/6to4 and unspecified addresses are
// When httpClient is nil, the SSRF-hardened httpclient.New() is used so the nil
// fallback also blocks IMDS connections. A non-nil httpClient is used as-is and
// bypasses that guard: metadata, NAT64/6to4 and unspecified addresses are
// dialable unless the caller's client blocks them itself.
func NewClientWithHTTP(cred azcore.TokenCredential, subscriptionID, region string, httpClient HTTPClient) *Client {
if httpClient == nil {
httpClient = httpclient.New()
}
return &Client{
cred: cred,
subscriptionID: subscriptionID,
Expand Down
18 changes: 18 additions & 0 deletions providers/azure/services/search/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1540,3 +1540,21 @@ func TestSearchClient_PurchaseCommitment_DisplayNameConformsToAzureAllowlist(t *
assert.Regexp(t, `^search-`, capturedDisplayName)
assert.Contains(t, capturedDisplayName, "standard2")
}

// TestNewClientWithHTTP_NilFallbackIsHardened verifies that passing nil as the
// httpClient falls back to the SSRF-hardened httpclient.New(), which rejects
// IMDS connections, instead of leaving a nil client that panics on first use.
func TestNewClientWithHTTP_NilFallbackIsHardened(t *testing.T) {
c := NewClientWithHTTP(nil, "sub-123", "eastus", nil)
require.NotNil(t, c.httpClient)
require.NotEqual(t, http.DefaultClient, c.httpClient,
"nil fallback must never be http.DefaultClient")
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "http://169.254.169.254/metadata/instance", nil)
require.NoError(t, err)
resp, err := c.httpClient.Do(req)
if resp != nil {
defer resp.Body.Close()
}
require.Error(t, err, "nil-fallback client must reject IMDS connections")
assert.Contains(t, err.Error(), "blocked")
}
Loading