Ver código fonte

fix(integration): redact passwords in base URLs in logs and errors

Credentials in an integration base URL, such as user:pass@host, were
printed as is in the HTTP status errors of 13 integrations and in the
ntfy, Gotify and Apprise debug logs. Replace the password with xxxxx
and keep the rest of the URL.
Fred 13 horas atrás
pai
commit
d60a55bc70

+ 2 - 2
internal/integration/apprise/apprise.go

@@ -60,7 +60,7 @@ func (c *Client) SendNotification(feed *model.Feed, entries model.Entries) error
 		request.Header.Set("User-Agent", "Miniflux/"+version.Version)
 
 		slog.Debug("Sending Apprise notification",
-			slog.String("apprise_url", c.baseURL),
+			slog.String("apprise_url", urllib.RedactedURL(c.baseURL)),
 			slog.String("services_url", c.servicesURL),
 			slog.String("title", feed.Title),
 			slog.String("body", message),
@@ -75,7 +75,7 @@ func (c *Client) SendNotification(feed *model.Feed, entries model.Entries) error
 		response.Body.Close()
 
 		if response.StatusCode >= 400 {
-			return fmt.Errorf("apprise: unable to send a notification: url=%s status=%d", apiEndpoint, response.StatusCode)
+			return fmt.Errorf("apprise: unable to send a notification: url=%s status=%d", urllib.RedactedURL(apiEndpoint), response.StatusCode)
 		}
 	}
 

+ 1 - 1
internal/integration/betula/betula.go

@@ -54,7 +54,7 @@ func (c *Client) CreateBookmark(entryURL, entryTitle string, tags []string) erro
 	defer response.Body.Close()
 
 	if response.StatusCode >= 400 {
-		return fmt.Errorf("betula: unable to create bookmark: url=%s status=%d", apiEndpoint, response.StatusCode)
+		return fmt.Errorf("betula: unable to create bookmark: url=%s status=%d", urllib.RedactedURL(apiEndpoint), response.StatusCode)
 	}
 
 	return nil

+ 1 - 1
internal/integration/espial/espial.go

@@ -51,7 +51,7 @@ func (c *Client) CreateLink(entryURL, entryTitle, espialTags string) error {
 		responseBody := new(bytes.Buffer)
 		responseBody.ReadFrom(response.Body)
 
-		return fmt.Errorf("espial: unable to create link: url=%s status=%d body=%s", apiEndpoint, response.StatusCode, responseBody.String())
+		return fmt.Errorf("espial: unable to create link: url=%s status=%d body=%s", urllib.RedactedURL(apiEndpoint), response.StatusCode, responseBody.String())
 	}
 
 	return nil

+ 2 - 2
internal/integration/gotify/gotify.go

@@ -90,7 +90,7 @@ func (c *Client) SendMessage(title, message string, priority int, extras map[str
 	defer response.Body.Close()
 
 	if response.StatusCode >= 400 {
-		return fmt.Errorf("gotify: unable to send message: url=%s status=%d", apiEndpoint, response.StatusCode)
+		return fmt.Errorf("gotify: unable to send message: url=%s status=%d", urllib.RedactedURL(apiEndpoint), response.StatusCode)
 	}
 
 	return nil
@@ -121,7 +121,7 @@ func (c *Client) SendMessages(feed *model.Feed, entries model.Entries) error {
 		}
 
 		slog.Debug("Sending Gotify notification",
-			slog.String("url", c.baseURL),
+			slog.String("url", urllib.RedactedURL(c.baseURL)),
 			slog.String("title", title),
 			slog.String("entry_title", entry.Title),
 			slog.String("entry_url", entry.URL),

+ 21 - 0
internal/integration/gotify/gotify_test.go

@@ -520,6 +520,27 @@ func TestNewClientPriorityClamping(t *testing.T) {
 	}
 }
 
+func TestSendMessageErrorDoesNotContainURLPassword(t *testing.T) {
+	configureIntegrationAllowPrivateNetworksOption(t)
+
+	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		w.WriteHeader(http.StatusInternalServerError)
+	}))
+	defer server.Close()
+
+	baseURL := strings.Replace(server.URL, "http://", "http://user:secret-password@", 1)
+	err := NewClient(baseURL, "token", 0).SendMessage("title", "msg", 0, nil)
+	if err == nil {
+		t.Fatal("expected an error, got nil")
+	}
+	if strings.Contains(err.Error(), "secret-password") {
+		t.Fatalf("expected the error to not contain the password, got %q", err)
+	}
+	if !strings.Contains(err.Error(), "user:xxxxx@") {
+		t.Fatalf("expected the error to contain the redacted URL, got %q", err)
+	}
+}
+
 func configureIntegrationAllowPrivateNetworksOption(t *testing.T) {
 	t.Helper()
 

+ 1 - 1
internal/integration/linkace/linkace.go

@@ -56,7 +56,7 @@ func (c *Client) AddURL(entryURL, entryTitle string) error {
 	defer response.Body.Close()
 
 	if response.StatusCode >= 400 {
-		return fmt.Errorf("linkace: unable to create item: url=%s status=%d", apiEndpoint, response.StatusCode)
+		return fmt.Errorf("linkace: unable to create item: url=%s status=%d", urllib.RedactedURL(apiEndpoint), response.StatusCode)
 	}
 
 	return nil

+ 1 - 1
internal/integration/linkding/linkding.go

@@ -54,7 +54,7 @@ func (c *Client) CreateBookmark(entryURL, entryTitle string) error {
 	defer response.Body.Close()
 
 	if response.StatusCode >= 400 {
-		return fmt.Errorf("linkding: unable to create bookmark: url=%s status=%d", apiEndpoint, response.StatusCode)
+		return fmt.Errorf("linkding: unable to create bookmark: url=%s status=%d", urllib.RedactedURL(apiEndpoint), response.StatusCode)
 	}
 
 	return nil

+ 4 - 3
internal/integration/matrixbot/client.go

@@ -14,6 +14,7 @@ import (
 	"miniflux.app/v2/internal/config"
 	"miniflux.app/v2/internal/crypto"
 	"miniflux.app/v2/internal/http/client"
+	"miniflux.app/v2/internal/urllib"
 	"miniflux.app/v2/internal/version"
 )
 
@@ -50,7 +51,7 @@ func (c *Client) DiscoverEndpoints() (*DiscoveryEndpointResponse, error) {
 	defer response.Body.Close()
 
 	if response.StatusCode >= 400 {
-		return nil, fmt.Errorf("matrix: unexpected response from %s status code is %d", endpointURL, response.StatusCode)
+		return nil, fmt.Errorf("matrix: unexpected response from %s status code is %d", urllib.RedactedURL(endpointURL), response.StatusCode)
 	}
 
 	var discoveryEndpointResponse DiscoveryEndpointResponse
@@ -99,7 +100,7 @@ func (c *Client) Login(homeServerURL, matrixUsername, matrixPassword string) (*L
 	defer response.Body.Close()
 
 	if response.StatusCode >= 400 {
-		return nil, fmt.Errorf("matrix: unexpected response from %s status code is %d", endpointURL, response.StatusCode)
+		return nil, fmt.Errorf("matrix: unexpected response from %s status code is %d", urllib.RedactedURL(endpointURL), response.StatusCode)
 	}
 
 	var loginResponse LoginResponse
@@ -148,7 +149,7 @@ func (c *Client) SendFormattedTextMessage(homeServerURL, accessToken, roomID, te
 	defer response.Body.Close()
 
 	if response.StatusCode >= 400 {
-		return nil, fmt.Errorf("matrix: unexpected response from %s status code is %d", endpointURL, response.StatusCode)
+		return nil, fmt.Errorf("matrix: unexpected response from %s status code is %d", urllib.RedactedURL(endpointURL), response.StatusCode)
 	}
 
 	var eventResponse RoomEventResponse

+ 3 - 2
internal/integration/ntfy/ntfy.go

@@ -15,6 +15,7 @@ import (
 	"miniflux.app/v2/internal/config"
 	"miniflux.app/v2/internal/http/client"
 	"miniflux.app/v2/internal/model"
+	"miniflux.app/v2/internal/urllib"
 	"miniflux.app/v2/internal/version"
 )
 
@@ -69,7 +70,7 @@ func (c *Client) SendMessages(feed *model.Feed, entries model.Entries) error {
 		}
 
 		slog.Debug("Sending Ntfy message",
-			slog.String("url", c.ntfyURL),
+			slog.String("url", urllib.RedactedURL(c.ntfyURL)),
 			slog.String("topic", c.ntfyTopic),
 			slog.Int("priority", ntfyMessage.Priority),
 			slog.String("message", ntfyMessage.Message),
@@ -116,7 +117,7 @@ func (c *Client) makeRequest(payload any) error {
 	defer response.Body.Close()
 
 	if response.StatusCode >= 400 {
-		return fmt.Errorf("ntfy: incorrect response status code %d for url %s", response.StatusCode, c.ntfyURL)
+		return fmt.Errorf("ntfy: incorrect response status code %d for url %s", response.StatusCode, urllib.RedactedURL(c.ntfyURL))
 	}
 
 	return nil

+ 1 - 1
internal/integration/nunuxkeeper/nunuxkeeper.go

@@ -65,7 +65,7 @@ func (c *Client) AddEntry(entryURL, entryTitle, entryContent string) error {
 	defer response.Body.Close()
 
 	if response.StatusCode != http.StatusOK {
-		return fmt.Errorf("nunux-keeper: unable to create document: url=%s status=%d", apiEndpoint, response.StatusCode)
+		return fmt.Errorf("nunux-keeper: unable to create document: url=%s status=%d", urllib.RedactedURL(apiEndpoint), response.StatusCode)
 	}
 
 	return nil

+ 1 - 1
internal/integration/readeck/readeck.go

@@ -130,7 +130,7 @@ func (c *Client) CreateBookmark(entryURL, entryTitle string, entryContent string
 	defer response.Body.Close()
 
 	if response.StatusCode >= 400 {
-		return fmt.Errorf("readeck: unable to create bookmark: url=%s status=%d", apiEndpoint, response.StatusCode)
+		return fmt.Errorf("readeck: unable to create bookmark: url=%s status=%d", urllib.RedactedURL(apiEndpoint), response.StatusCode)
 	}
 
 	return nil

+ 21 - 0
internal/integration/readeck/readeck_test.go

@@ -263,6 +263,27 @@ func TestNewClient(t *testing.T) {
 	}
 }
 
+func TestCreateBookmarkErrorDoesNotContainURLPassword(t *testing.T) {
+	configureIntegrationAllowPrivateNetworksOption(t)
+
+	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		w.WriteHeader(http.StatusInternalServerError)
+	}))
+	defer server.Close()
+
+	baseURL := strings.Replace(server.URL, "http://", "http://user:secret-password@", 1)
+	err := NewClient(baseURL, "test-api-key", "", true).CreateBookmark("https://example.org/entry", "title", "content")
+	if err == nil {
+		t.Fatal("expected an error, got nil")
+	}
+	if strings.Contains(err.Error(), "secret-password") {
+		t.Fatalf("expected the error to not contain the password, got %q", err)
+	}
+	if !strings.Contains(err.Error(), "user:xxxxx@") {
+		t.Fatalf("expected the error to contain the redacted URL, got %q", err)
+	}
+}
+
 func configureIntegrationAllowPrivateNetworksOption(t *testing.T) {
 	t.Helper()
 

+ 1 - 1
internal/integration/shaarli/shaarli.go

@@ -51,7 +51,7 @@ func (c *Client) CreateLink(entryURL, entryTitle string) error {
 	defer response.Body.Close()
 
 	if response.StatusCode != http.StatusCreated {
-		return fmt.Errorf("shaarli: unable to add link: url=%s status=%d", apiEndpoint, response.StatusCode)
+		return fmt.Errorf("shaarli: unable to add link: url=%s status=%d", urllib.RedactedURL(apiEndpoint), response.StatusCode)
 	}
 
 	return nil

+ 2 - 2
internal/integration/shiori/shiori.go

@@ -57,7 +57,7 @@ func (c *Client) CreateBookmark(entryURL, entryTitle string) error {
 	defer response.Body.Close()
 
 	if response.StatusCode != http.StatusOK {
-		return fmt.Errorf("shiori: unable to create bookmark: url=%s status=%d", apiEndpoint, response.StatusCode)
+		return fmt.Errorf("shiori: unable to create bookmark: url=%s status=%d", urllib.RedactedURL(apiEndpoint), response.StatusCode)
 	}
 
 	return nil
@@ -80,7 +80,7 @@ func (c *Client) authenticate() (string, error) {
 	defer response.Body.Close()
 
 	if response.StatusCode != http.StatusOK {
-		return "", fmt.Errorf("shiori: unable to authenticate: url=%s status=%d", apiEndpoint, response.StatusCode)
+		return "", fmt.Errorf("shiori: unable to authenticate: url=%s status=%d", urllib.RedactedURL(apiEndpoint), response.StatusCode)
 	}
 
 	var authResponse authResponse

+ 2 - 2
internal/integration/wallabag/wallabag.go

@@ -94,7 +94,7 @@ func (c *Client) createEntry(accessToken, entryURL, entryTitle, entryContent, ta
 	defer response.Body.Close()
 
 	if response.StatusCode >= 400 {
-		return fmt.Errorf("wallabag: unable to save entry: url=%s status=%d", apiEndpoint, response.StatusCode)
+		return fmt.Errorf("wallabag: unable to save entry: url=%s status=%d", urllib.RedactedURL(apiEndpoint), response.StatusCode)
 	}
 
 	return nil
@@ -130,7 +130,7 @@ func (c *Client) getAccessToken() (string, error) {
 	defer response.Body.Close()
 
 	if response.StatusCode >= 400 {
-		return "", fmt.Errorf("wallabag: unable to get access token: url=%s status=%d", apiEndpoint, response.StatusCode)
+		return "", fmt.Errorf("wallabag: unable to get access token: url=%s status=%d", urllib.RedactedURL(apiEndpoint), response.StatusCode)
 	}
 
 	var responseBody tokenResponse

+ 46 - 0
internal/integration/wallabag/wallabag_test.go

@@ -324,6 +324,52 @@ func TestNewClient(t *testing.T) {
 	}
 }
 
+func TestCreateEntryErrorsDoNotContainURLPassword(t *testing.T) {
+	configureIntegrationAllowPrivateNetworksOption(t)
+
+	tests := []struct {
+		name    string
+		handler http.HandlerFunc
+	}{
+		{
+			name: "access token request fails",
+			handler: func(w http.ResponseWriter, r *http.Request) {
+				w.WriteHeader(http.StatusUnauthorized)
+			},
+		},
+		{
+			name: "save entry request fails",
+			handler: func(w http.ResponseWriter, r *http.Request) {
+				if strings.Contains(r.URL.Path, "/oauth/v2/token") {
+					json.NewEncoder(w).Encode(map[string]any{"access_token": "test-token"})
+					return
+				}
+				w.WriteHeader(http.StatusInternalServerError)
+			},
+		},
+	}
+
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			server := httptest.NewServer(tt.handler)
+			defer server.Close()
+
+			baseURL := strings.Replace(server.URL, "http://", "http://user:secret-password@", 1)
+			client := NewClient(baseURL, "clientId", "clientSecret", "username", "password", "", true)
+			err := client.CreateEntry("https://example.org/entry", "title", "content")
+			if err == nil {
+				t.Fatal("expected an error, got nil")
+			}
+			if strings.Contains(err.Error(), "secret-password") {
+				t.Fatalf("expected the error to not contain the password, got %q", err)
+			}
+			if !strings.Contains(err.Error(), "user:xxxxx@") {
+				t.Fatalf("expected the error to contain the redacted URL, got %q", err)
+			}
+		})
+	}
+}
+
 func configureIntegrationAllowPrivateNetworksOption(t *testing.T) {
 	t.Helper()
 

+ 11 - 0
internal/urllib/url.go

@@ -212,6 +212,17 @@ func JoinBaseURLAndPath(baseURL, path string) (string, error) {
 	return finalURL, nil
 }
 
+// RedactedURL returns rawURL with the password of its userinfo replaced
+// by "xxxxx", for logs and errors. It returns "xxxxx" when rawURL cannot
+// be parsed, since the raw string could hold a password.
+func RedactedURL(rawURL string) string {
+	parsedURL, err := url.Parse(rawURL)
+	if err != nil {
+		return "xxxxx"
+	}
+	return parsedURL.Redacted()
+}
+
 // IsNonPublicIP returns true if the given IP is not safe to access from the network.
 func IsNonPublicIP(ip net.IP) bool {
 	addr, ok := netip.AddrFromSlice(ip)

+ 21 - 0
internal/urllib/url_test.go

@@ -265,6 +265,27 @@ func TestJoinBaseURLAndPath(t *testing.T) {
 	}
 }
 
+func TestRedactedURL(t *testing.T) {
+	tests := []struct {
+		name   string
+		rawURL string
+		want   string
+	}{
+		{"password", "https://user:secret@example.org/api/bookmarks", "https://user:xxxxx@example.org/api/bookmarks"},
+		{"password and port", "http://user:secret@127.0.0.1:8080/oauth/v2/token", "http://user:xxxxx@127.0.0.1:8080/oauth/v2/token"},
+		{"username only", "https://user@example.org/api", "https://user@example.org/api"},
+		{"no userinfo", "https://example.org/api?page=1", "https://example.org/api?page=1"},
+		{"unparsable", "https://user:secret@example.org/" + string(rune(0x7f)), "xxxxx"},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			if got := RedactedURL(tt.rawURL); got != tt.want {
+				t.Errorf("RedactedURL(%q) = %q, want %q", tt.rawURL, got, tt.want)
+			}
+		})
+	}
+}
+
 func TestIsNonPublicIP(t *testing.T) {
 	testCases := []struct {
 		name     string