Skip to content
Open
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
2 changes: 1 addition & 1 deletion client/bloodhound/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (s *BHEClient) Ingest(ctx context.Context, in <-chan []any) bool {
s.log.Error(err, unrecoverableErrMsg)
return true
} else {
req.Header.Set("User-Agent", constants.UserAgent())
req.Header.Set("User-Agent", rest.UserAgent())
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Encoding", "gzip")

Expand Down
49 changes: 49 additions & 0 deletions client/bloodhound/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"sync"
"testing"

"github.com/bloodhoundad/azurehound/v2/config"
"github.com/bloodhoundad/azurehound/v2/constants"
"github.com/go-logr/logr"
"github.com/stretchr/testify/require"
"golang.org/x/net/http2"
Expand Down Expand Up @@ -141,4 +143,51 @@ func TestBHEClient_Ingest(t *testing.T) {

require.True(t, hadErrors)
})

t.Run("custom user agent applied", func(t *testing.T) {
const custom = "test-agent/9.9.9"
config.UserAgent.Set(custom)
t.Cleanup(func() { config.UserAgent.Set("") })

var got string
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
got = r.Header.Get("User-Agent")
w.WriteHeader(http.StatusAccepted)
}))
defer testServer.Close()

testUrl, _ := url.Parse(testServer.URL)
client, err := NewBHEClient(*testUrl, "tokenId", "token", "", 1, 1, logr.Logger{})
require.NoError(t, err)

data := make(chan []any, 1)
data <- []any{"test"}
close(data)

require.False(t, client.Ingest(context.Background(), data))
require.Equal(t, custom, got)
})

t.Run("default user agent when config empty", func(t *testing.T) {
config.UserAgent.Set("")
t.Cleanup(func() { config.UserAgent.Set("") })

var got string
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
got = r.Header.Get("User-Agent")
w.WriteHeader(http.StatusAccepted)
}))
defer testServer.Close()

testUrl, _ := url.Parse(testServer.URL)
client, err := NewBHEClient(*testUrl, "tokenId", "token", "", 1, 1, logr.Logger{})
require.NoError(t, err)

data := make(chan []any, 1)
data <- []any{"test"}
close(data)

require.False(t, client.Ingest(context.Background(), data))
require.Equal(t, constants.UserAgent(), got)
})
}
17 changes: 11 additions & 6 deletions client/rest/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,17 @@ func NewRequest(
}

// set azurehound as user-agent, use custom if set in config
ua := config.UserAgent.Value()
if s, ok := ua.(string); ok && s != "" {
req.Header.Set("User-Agent", s)
} else {
req.Header.Set("User-Agent", constants.UserAgent())
}
req.Header.Set("User-Agent", UserAgent())
return req, nil
}
}

// UserAgent returns the configured User-Agent header value, falling back to
// the default azurehound/<version> string when the --user-agent flag is unset
// or empty.
func UserAgent() string {
if s, ok := config.UserAgent.Value().(string); ok && s != "" {
return s
}
return constants.UserAgent()
}
75 changes: 75 additions & 0 deletions client/rest/http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright (C) 2026 Specter Ops, Inc.
//
// This file is part of AzureHound.
//
// AzureHound is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// AzureHound is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

package rest

import (
"context"
"net/url"
"testing"

"github.com/bloodhoundad/azurehound/v2/config"
"github.com/bloodhoundad/azurehound/v2/constants"
)

func TestUserAgent_DefaultsWhenUnset(t *testing.T) {
config.UserAgent.Set("")
t.Cleanup(func() { config.UserAgent.Set("") })

if got, want := UserAgent(), constants.UserAgent(); got != want {
t.Fatalf("UserAgent() = %q, want default %q", got, want)
}
}

func TestUserAgent_HonorsConfigValue(t *testing.T) {
const custom = "my-custom-agent/1.2.3"
config.UserAgent.Set(custom)
t.Cleanup(func() { config.UserAgent.Set("") })

if got := UserAgent(); got != custom {
t.Fatalf("UserAgent() = %q, want %q", got, custom)
}
}

func TestNewRequest_AppliesCustomUserAgent(t *testing.T) {
const custom = "my-custom-agent/1.2.3"
config.UserAgent.Set(custom)
t.Cleanup(func() { config.UserAgent.Set("") })

endpoint, _ := url.Parse("http://example.com/")
req, err := NewRequest(context.Background(), "GET", endpoint, nil, nil, nil)
if err != nil {
t.Fatalf("NewRequest error: %v", err)
}
if got := req.Header.Get("User-Agent"); got != custom {
t.Fatalf("User-Agent header = %q, want %q", got, custom)
}
}

func TestNewRequest_DefaultUserAgentWhenConfigEmpty(t *testing.T) {
config.UserAgent.Set("")
t.Cleanup(func() { config.UserAgent.Set("") })

endpoint, _ := url.Parse("http://example.com/")
req, err := NewRequest(context.Background(), "GET", endpoint, nil, nil, nil)
if err != nil {
t.Fatalf("NewRequest error: %v", err)
}
if got, want := req.Header.Get("User-Agent"), constants.UserAgent(); got != want {
t.Fatalf("User-Agent header = %q, want default %q", got, want)
}
}
Loading