Skip to content

Commit 514bb4c

Browse files
Fix code scanning alert no. 2: Clear-text logging of sensitive information
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent 451aa4d commit 514bb4c

File tree

3 files changed

+100
-12
lines changed

3 files changed

+100
-12
lines changed

http_client_bearer.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
)
7+
8+
// HttpTransportWithBearer wraps the default RoundTripper to add the Authorization header.
9+
type HttpTransportWithBearer struct {
10+
BaseTransport http.RoundTripper
11+
Token string
12+
}
13+
14+
// RoundTrip implements the RoundTripper interface to modify the request.
15+
func (t *HttpTransportWithBearer) RoundTrip(req *http.Request) (*http.Response, error) {
16+
// Clone the request to avoid side effects
17+
reqClone := req.Clone(req.Context())
18+
19+
// Add the Authorization header
20+
reqClone.Header.Set("Authorization", fmt.Sprintf("Bearer %s", t.Token))
21+
22+
// Use the base RoundTripper to perform the request
23+
return t.BaseTransport.RoundTrip(reqClone)
24+
}
25+
26+
func NewHttpClientWithBearerTransport(token string) *http.Client {
27+
// Create a new HTTP client with the custom transport
28+
return &http.Client{
29+
Transport: &HttpTransportWithBearer{
30+
BaseTransport: http.DefaultTransport,
31+
Token: token,
32+
},
33+
}
34+
}

http_client_bearer_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"net/http"
7+
"net/http/httptest"
8+
"testing"
9+
)
10+
11+
// TestHttpClientWithBearerTransport tests the addition of the Authorization header.
12+
func TestHttpClientWithBearerTransport(t *testing.T) {
13+
// Define the expected Bearer token
14+
token := "test_bearer_token"
15+
16+
// Set up a test HTTP server
17+
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
18+
// Retrieve the Authorization header from the request
19+
authHeader := r.Header.Get("Authorization")
20+
expectedHeader := fmt.Sprintf("Bearer %s", token)
21+
22+
// Check if the Authorization header matches the expected value
23+
if authHeader != expectedHeader {
24+
http.Error(w, "Unauthorized", http.StatusUnauthorized)
25+
return
26+
}
27+
28+
// Return a success response
29+
w.WriteHeader(http.StatusOK)
30+
io.WriteString(w, "Success")
31+
}))
32+
defer testServer.Close()
33+
34+
// Create an HTTP client with the custom transport
35+
client := NewHttpClientWithBearerTransport(token)
36+
37+
// Create a new HTTP request to the test server
38+
req, err := http.NewRequest("GET", testServer.URL, nil)
39+
if err != nil {
40+
t.Fatalf("Failed to create request: %v", err)
41+
}
42+
43+
// Perform the request using the custom client
44+
resp, err := client.Do(req)
45+
if err != nil {
46+
t.Fatalf("Request failed: %v", err)
47+
}
48+
defer resp.Body.Close()
49+
50+
// Check if the status code is 200 OK
51+
if resp.StatusCode != http.StatusOK {
52+
t.Errorf("Expected status code 200 OK, got %d", resp.StatusCode)
53+
}
54+
}

main.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"time"
1515

1616
"github.com/gin-gonic/gin"
17-
retryablehttp "github.com/hashicorp/go-retryablehttp"
1817
"github.com/tmc/langchaingo/llms"
1918
"github.com/tmc/langchaingo/llms/ollama"
2019
"github.com/tmc/langchaingo/llms/openai"
@@ -141,22 +140,23 @@ func createLLM() (llms.Model, error) {
141140
if host == "" {
142141
host = "http://127.0.0.1:11434"
143142
}
144-
// custom http client (retryable http client) if bearer token is wanted
145-
retryClient := retryablehttp.NewClient()
146-
retryClient.RetryMax = 10
143+
ollamaOptions := []ollama.Option{
144+
ollama.WithModel(llmModel),
145+
ollama.WithServerURL(host),
146+
}
147147
bearerToken := os.Getenv("OLLAMA_BEARER_TOKEN")
148148
if bearerToken != "" {
149-
retryClient.RequestLogHook = func(l retryablehttp.Logger, r *http.Request, i int) {
150-
r.Header.Set("Authorization", fmt.Sprintf("Bearer %s", bearerToken))
151-
shortenedBearerToken := fmt.Sprintf("%s...", r.Header.Get("Authorization")[:5])
152-
log.Printf("Request with bearer %s token to %s %s", shortenedBearerToken, r.Method, r.URL)
153-
}
149+
log.Println("Using bearer token for OLLAMA authentication")
150+
ollamaOptions = append(
151+
ollamaOptions,
152+
ollama.WithHTTPClient(
153+
NewHttpClientWithBearerTransport(bearerToken),
154+
),
155+
)
154156
}
155157

156158
return ollama.New(
157-
ollama.WithModel(llmModel),
158-
ollama.WithServerURL(host),
159-
ollama.WithHTTPClient(retryClient.StandardClient()),
159+
ollamaOptions...,
160160
)
161161
default:
162162
return nil, fmt.Errorf("unsupported LLM provider: %s", llmProvider)

0 commit comments

Comments
 (0)