Преглед изворни кода

fix(pinboard): keep the auth token out of error messages

The token is sent as a query parameter, so errors that included the
request URL leaked it into the logs. Remove the URL from the status
error and unwrap *url.Error on network failures.
Frédéric Guillot пре 3 часа
родитељ
комит
702bb69c93

+ 20 - 8
internal/integration/pinboard/pinboard.go

@@ -18,14 +18,18 @@ import (
 var errPostNotFound = errors.New("pinboard: post not found")
 var errMissingCredentials = errors.New("pinboard: missing auth token")
 
-const defaultClientTimeout = 10 * time.Second
+const (
+	defaultClientTimeout = 10 * time.Second
+	defaultAPIBaseURL    = "https://api.pinboard.in/v1"
+)
 
 type Client struct {
-	authToken string
+	apiBaseURL string
+	authToken  string
 }
 
 func NewClient(authToken string) *Client {
-	return &Client{authToken: authToken}
+	return &Client{apiBaseURL: defaultAPIBaseURL, authToken: authToken}
 }
 
 func (c *Client) CreateBookmark(entryURL, entryTitle, pinboardTags string, markAsUnread bool) error {
@@ -52,7 +56,7 @@ func (c *Client) CreateBookmark(entryURL, entryTitle, pinboardTags string, markA
 	values.Add("auth_token", c.authToken)
 	post.AddValues(values)
 
-	apiEndpoint := "https://api.pinboard.in/v1/posts/add?" + values.Encode()
+	apiEndpoint := c.apiBaseURL + "/posts/add?" + values.Encode()
 	request, err := http.NewRequest(http.MethodGet, apiEndpoint, nil)
 	if err != nil {
 		return fmt.Errorf("pinboard: unable to create request: %v", err)
@@ -63,12 +67,12 @@ func (c *Client) CreateBookmark(entryURL, entryTitle, pinboardTags string, markA
 	httpClient := client.NewClientWithOptions(client.Options{Timeout: defaultClientTimeout})
 	response, err := httpClient.Do(request)
 	if err != nil {
-		return fmt.Errorf("pinboard: unable to send request: %v", err)
+		return fmt.Errorf("pinboard: unable to send request: %w", withoutURL(err))
 	}
 	defer response.Body.Close()
 
 	if response.StatusCode >= 400 {
-		return fmt.Errorf("pinboard: unable to create a bookmark: url=%s status=%d", apiEndpoint, response.StatusCode)
+		return fmt.Errorf("pinboard: unable to create a bookmark, status=%d", response.StatusCode)
 	}
 
 	return nil
@@ -84,7 +88,7 @@ func (c *Client) getBookmark(entryURL string) (*Post, error) {
 	values.Add("auth_token", c.authToken)
 	values.Add("url", entryURL)
 
-	apiEndpoint := "https://api.pinboard.in/v1/posts/get?" + values.Encode()
+	apiEndpoint := c.apiBaseURL + "/posts/get?" + values.Encode()
 	request, err := http.NewRequest(http.MethodGet, apiEndpoint, nil)
 	if err != nil {
 		return nil, fmt.Errorf("pinboard: unable to create request: %v", err)
@@ -95,7 +99,7 @@ func (c *Client) getBookmark(entryURL string) (*Post, error) {
 	httpClient := client.NewClientWithOptions(client.Options{Timeout: defaultClientTimeout})
 	response, err := httpClient.Do(request)
 	if err != nil {
-		return nil, fmt.Errorf("pinboard: unable fetch bookmark: %v", err)
+		return nil, fmt.Errorf("pinboard: unable fetch bookmark: %w", withoutURL(err))
 	}
 	defer response.Body.Close()
 
@@ -115,3 +119,11 @@ func (c *Client) getBookmark(entryURL string) (*Post, error) {
 
 	return &results.Posts[0], nil
 }
+
+// withoutURL drops the request URL, which contains the auth token, from HTTP client errors.
+func withoutURL(err error) error {
+	if urlErr, ok := errors.AsType[*url.Error](err); ok {
+		return urlErr.Err
+	}
+	return err
+}

+ 99 - 0
internal/integration/pinboard/pinboard_test.go

@@ -0,0 +1,99 @@
+// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
+// SPDX-License-Identifier: Apache-2.0
+
+package pinboard
+
+import (
+	"net/http"
+	"net/http/httptest"
+	"net/url"
+	"strings"
+	"testing"
+)
+
+func TestCreateBookmarkErrorsDoNotContainAuthToken(t *testing.T) {
+	const authToken = "user:0123456789ABCDEF"
+
+	closeConnection := func(t *testing.T, w http.ResponseWriter) {
+		connection, _, err := w.(http.Hijacker).Hijack()
+		if err != nil {
+			t.Fatalf("unable to hijack connection: %v", err)
+		}
+		connection.Close()
+	}
+
+	notFound := func(w http.ResponseWriter) {
+		w.Write([]byte(`<posts></posts>`))
+	}
+
+	tests := []struct {
+		name            string
+		handler         func(t *testing.T, w http.ResponseWriter, r *http.Request)
+		wantErrContains string
+	}{
+		{
+			name: "lookup connection fails",
+			handler: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
+				closeConnection(t, w)
+			},
+			wantErrContains: "pinboard: unable fetch bookmark",
+		},
+		{
+			name: "lookup returns an error status",
+			handler: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
+				w.WriteHeader(http.StatusUnauthorized)
+			},
+			wantErrContains: "status=401",
+		},
+		{
+			name: "add connection fails",
+			handler: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
+				if r.URL.Path == "/posts/get" {
+					notFound(w)
+					return
+				}
+				closeConnection(t, w)
+			},
+			wantErrContains: "pinboard: unable to send request",
+		},
+		{
+			name: "add returns an error status",
+			handler: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
+				if r.URL.Path == "/posts/get" {
+					notFound(w)
+					return
+				}
+				w.WriteHeader(http.StatusTooManyRequests)
+			},
+			wantErrContains: "status=429",
+		},
+	}
+
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+				if got := r.URL.Query().Get("auth_token"); got != authToken {
+					t.Errorf("auth_token = %q, want %q", got, authToken)
+				}
+				tt.handler(t, w, r)
+			}))
+			defer server.Close()
+
+			client := NewClient(authToken)
+			client.apiBaseURL = server.URL
+
+			err := client.CreateBookmark("https://example.org/article", "Article", "tag", false)
+			if err == nil {
+				t.Fatal("expected an error, got nil")
+			}
+			if !strings.Contains(err.Error(), tt.wantErrContains) {
+				t.Errorf("error = %q, want it to contain %q", err, tt.wantErrContains)
+			}
+			for _, secret := range []string{authToken, url.QueryEscape(authToken)} {
+				if strings.Contains(err.Error(), secret) {
+					t.Errorf("error contains the auth token: %q", err)
+				}
+			}
+		})
+	}
+}