Просмотр исходного кода

fix(fetcher): redact credential headers in the request debug log

The outgoing request debug log included the Authorization header, which
carries the feed username and password, and the feed Cookie header.
Log these headers, and Proxy-Authorization, as <redacted>.
Frédéric Guillot 4 часов назад
Родитель
Сommit
f3b0e65c90

+ 12 - 1
internal/reader/fetcher/request_builder.go

@@ -328,7 +328,7 @@ func (r *RequestBuilder) ExecuteRequest(requestURL string) (*http.Response, erro
 	slog.Debug("Making outgoing request", slog.Group("request",
 		slog.String("method", req.Method),
 		slog.String("url", req.URL.String()),
-		slog.Any("headers", req.Header),
+		slog.Any("headers", redactedHeaders(req.Header)),
 		slog.Bool("without_redirects", r.withoutRedirects),
 		slog.Bool("use_app_client_proxy", r.useClientProxy),
 		slog.String("client_proxy_url", clientProxyURLRedacted),
@@ -339,6 +339,17 @@ func (r *RequestBuilder) ExecuteRequest(requestURL string) (*http.Response, erro
 	return client.Do(req)
 }
 
+// redactedHeaders returns a copy of headers with the credential values replaced, for logging.
+func redactedHeaders(headers http.Header) http.Header {
+	redacted := headers.Clone()
+	for _, headerName := range []string{"Authorization", "Cookie", "Proxy-Authorization"} {
+		if redacted.Get(headerName) != "" {
+			redacted.Set(headerName, "<redacted>")
+		}
+	}
+	return redacted
+}
+
 // isSameSite reports whether host and siteHost are the same host, or share the
 // same registrable domain, like "www.example.org" and "feeds.example.org".
 // Hosts under a public suffix, like "alice.github.io" and "bob.github.io", are

+ 47 - 0
internal/reader/fetcher/request_builder_test.go

@@ -4,6 +4,8 @@
 package fetcher // import "miniflux.app/v2/internal/reader/fetcher"
 
 import (
+	"bytes"
+	"log/slog"
 	"net/http"
 	"net/http/httptest"
 	"net/url"
@@ -338,6 +340,51 @@ func TestRequestBuilder_WithUsernameAndPassword(t *testing.T) {
 	}
 }
 
+func TestRequestBuilder_CredentialsNotLogged(t *testing.T) {
+	credentialHeaders := map[string]string{
+		"Authorization":       "Basic dGVzdDpwYXNzd29yZA==", // base64 of "test:password"
+		"Cookie":              "session=abc123",
+		"Proxy-Authorization": "Basic cHJveHk6c2VjcmV0", // base64 of "proxy:secret"
+	}
+
+	server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
+		for headerName, expectedValue := range credentialHeaders {
+			if request.Header.Get(headerName) != expectedValue {
+				t.Errorf("Expected %s to be %q, got %q", headerName, expectedValue, request.Header.Get(headerName))
+			}
+		}
+		writer.WriteHeader(http.StatusOK)
+	}))
+	defer server.Close()
+
+	var logs bytes.Buffer
+	previousLogger := slog.Default()
+	slog.SetDefault(slog.New(slog.NewTextHandler(&logs, &slog.HandlerOptions{Level: slog.LevelDebug})))
+	t.Cleanup(func() { slog.SetDefault(previousLogger) })
+
+	resp, err := NewRequestBuilder().
+		WithUsernameAndPassword("test", "password").
+		WithCookie(credentialHeaders["Cookie"], server.URL).
+		WithHeader("Proxy-Authorization", credentialHeaders["Proxy-Authorization"]).
+		ExecuteRequest(server.URL)
+	if err != nil {
+		t.Fatalf("Expected no error, got %v", err)
+	}
+	defer resp.Body.Close()
+
+	if !strings.Contains(logs.String(), "Making outgoing request") {
+		t.Fatalf("Expected the outgoing request to be logged, got:\n%s", logs.String())
+	}
+	for headerName, credential := range credentialHeaders {
+		if strings.Contains(logs.String(), credential) {
+			t.Errorf("Logs contain the %s value %q:\n%s", headerName, credential, logs.String())
+		}
+	}
+	if redactedCount := strings.Count(logs.String(), "<redacted>"); redactedCount != len(credentialHeaders) {
+		t.Errorf("Expected %d redacted headers, got %d:\n%s", len(credentialHeaders), redactedCount, logs.String())
+	}
+}
+
 func TestRequestBuilder_DefaultAcceptHeader(t *testing.T) {
 	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
 		if r.Header.Get("Accept") != defaultAcceptHeader {