Przeglądaj źródła

fix(http): log the request path instead of the full URI

Query strings can carry credentials, such as the Google Reader
ClientLogin password or the Fever API key. A failed ClientLogin with
credentials in the query string logged the password at the warning
level.
Frédéric Guillot 2 godzin temu
rodzic
commit
d454d74232

+ 9 - 9
internal/api/middleware.go

@@ -44,7 +44,7 @@ func (m *middleware) validateAPIKeyAuth(next http.Handler) http.Handler {
 			slog.Debug("[API] Skipped API token authentication because no API Key has been provided",
 				slog.String("client_ip", clientIP),
 				slog.String("user_agent", r.UserAgent()),
-				slog.String("request_uri", r.RequestURI),
+				slog.String("request_uri", r.URL.Path),
 			)
 			next.ServeHTTP(w, r)
 			return
@@ -61,7 +61,7 @@ func (m *middleware) validateAPIKeyAuth(next http.Handler) http.Handler {
 				slog.Bool("authentication_failed", true),
 				slog.String("client_ip", clientIP),
 				slog.String("user_agent", r.UserAgent()),
-				slog.String("request_uri", r.RequestURI),
+				slog.String("request_uri", r.URL.Path),
 			)
 			response.JSONUnauthorized(w, r)
 			return
@@ -72,7 +72,7 @@ func (m *middleware) validateAPIKeyAuth(next http.Handler) http.Handler {
 			slog.String("client_ip", clientIP),
 			slog.String("user_agent", r.UserAgent()),
 			slog.String("username", user.Username),
-			slog.String("request_uri", r.RequestURI),
+			slog.String("request_uri", r.URL.Path),
 		)
 
 		m.store.SetLastLogin(user.ID)
@@ -104,7 +104,7 @@ func (m *middleware) validateBasicAuth(next http.Handler) http.Handler {
 				slog.Bool("authentication_failed", true),
 				slog.String("client_ip", clientIP),
 				slog.String("user_agent", r.UserAgent()),
-				slog.String("request_uri", r.RequestURI),
+				slog.String("request_uri", r.URL.Path),
 			)
 			response.JSONUnauthorized(w, r)
 			return
@@ -115,7 +115,7 @@ func (m *middleware) validateBasicAuth(next http.Handler) http.Handler {
 				slog.Bool("authentication_failed", true),
 				slog.String("client_ip", clientIP),
 				slog.String("user_agent", r.UserAgent()),
-				slog.String("request_uri", r.RequestURI),
+				slog.String("request_uri", r.URL.Path),
 			)
 			response.JSONUnauthorized(w, r)
 			return
@@ -126,7 +126,7 @@ func (m *middleware) validateBasicAuth(next http.Handler) http.Handler {
 				slog.Bool("authentication_failed", true),
 				slog.String("client_ip", clientIP),
 				slog.String("user_agent", r.UserAgent()),
-				slog.String("request_uri", r.RequestURI),
+				slog.String("request_uri", r.URL.Path),
 			)
 			response.JSONUnauthorized(w, r)
 			return
@@ -138,7 +138,7 @@ func (m *middleware) validateBasicAuth(next http.Handler) http.Handler {
 				slog.String("client_ip", clientIP),
 				slog.String("user_agent", r.UserAgent()),
 				slog.String("username", username),
-				slog.String("request_uri", r.RequestURI),
+				slog.String("request_uri", r.URL.Path),
 			)
 			response.JSONUnauthorized(w, r)
 			return
@@ -156,7 +156,7 @@ func (m *middleware) validateBasicAuth(next http.Handler) http.Handler {
 				slog.String("client_ip", clientIP),
 				slog.String("user_agent", r.UserAgent()),
 				slog.String("username", username),
-				slog.String("request_uri", r.RequestURI),
+				slog.String("request_uri", r.URL.Path),
 			)
 			response.JSONUnauthorized(w, r)
 			return
@@ -167,7 +167,7 @@ func (m *middleware) validateBasicAuth(next http.Handler) http.Handler {
 			slog.String("client_ip", clientIP),
 			slog.String("user_agent", r.UserAgent()),
 			slog.String("username", username),
-			slog.String("request_uri", r.RequestURI),
+			slog.String("request_uri", r.URL.Path),
 		)
 
 		m.store.SetLastLogin(user.ID)

+ 1 - 1
internal/googlereader/handler.go

@@ -941,7 +941,7 @@ func (h *greaderHandler) fallbackHandler(w http.ResponseWriter, r *http.Request)
 	clientIP := request.ClientIP(r)
 
 	slog.Debug("[GoogleReader] API endpoint not implemented yet",
-		slog.Any("url", r.RequestURI),
+		slog.String("url", r.URL.Path),
 		slog.String("client_ip", clientIP),
 		slog.String("user_agent", r.UserAgent()),
 	)

+ 6 - 6
internal/http/response/html.go

@@ -20,7 +20,7 @@ func HTMLServerError(w http.ResponseWriter, r *http.Request, err error) {
 		slog.String("client_ip", request.ClientIP(r)),
 		slog.Group("request",
 			slog.String("method", r.Method),
-			slog.String("uri", r.RequestURI),
+			slog.String("uri", r.URL.Path),
 			slog.String("user_agent", r.UserAgent()),
 		),
 		slog.Group("response",
@@ -44,7 +44,7 @@ func HTMLBadRequest(w http.ResponseWriter, r *http.Request, err error) {
 		slog.String("client_ip", request.ClientIP(r)),
 		slog.Group("request",
 			slog.String("method", r.Method),
-			slog.String("uri", r.RequestURI),
+			slog.String("uri", r.URL.Path),
 			slog.String("user_agent", r.UserAgent()),
 		),
 		slog.Group("response",
@@ -67,7 +67,7 @@ func HTMLForbidden(w http.ResponseWriter, r *http.Request) {
 		slog.String("client_ip", request.ClientIP(r)),
 		slog.Group("request",
 			slog.String("method", r.Method),
-			slog.String("uri", r.RequestURI),
+			slog.String("uri", r.URL.Path),
 			slog.String("user_agent", r.UserAgent()),
 		),
 		slog.Group("response",
@@ -89,7 +89,7 @@ func HTMLNotFound(w http.ResponseWriter, r *http.Request) {
 		slog.String("client_ip", request.ClientIP(r)),
 		slog.Group("request",
 			slog.String("method", r.Method),
-			slog.String("uri", r.RequestURI),
+			slog.String("uri", r.URL.Path),
 			slog.String("user_agent", r.UserAgent()),
 		),
 		slog.Group("response",
@@ -111,7 +111,7 @@ func HTMLNotAcceptable(w http.ResponseWriter, r *http.Request) {
 		slog.String("client_ip", request.ClientIP(r)),
 		slog.Group("request",
 			slog.String("method", r.Method),
-			slog.String("uri", r.RequestURI),
+			slog.String("uri", r.URL.Path),
 			slog.String("user_agent", r.UserAgent()),
 			slog.String("accept_encoding", r.Header.Get("Accept-Encoding")),
 		),
@@ -143,7 +143,7 @@ func HTMLRequestedRangeNotSatisfiable(w http.ResponseWriter, r *http.Request, co
 		slog.String("client_ip", request.ClientIP(r)),
 		slog.Group("request",
 			slog.String("method", r.Method),
-			slog.String("uri", r.RequestURI),
+			slog.String("uri", r.URL.Path),
 			slog.String("user_agent", r.UserAgent()),
 		),
 		slog.Group("response",

+ 5 - 5
internal/http/response/json.go

@@ -58,7 +58,7 @@ func JSONServerError(w http.ResponseWriter, r *http.Request, err error) {
 		slog.String("client_ip", request.ClientIP(r)),
 		slog.Group("request",
 			slog.String("method", r.Method),
-			slog.String("uri", r.RequestURI),
+			slog.String("uri", r.URL.Path),
 			slog.String("user_agent", r.UserAgent()),
 		),
 		slog.Group("response",
@@ -80,7 +80,7 @@ func JSONBadRequest(w http.ResponseWriter, r *http.Request, err error) {
 		slog.String("client_ip", request.ClientIP(r)),
 		slog.Group("request",
 			slog.String("method", r.Method),
-			slog.String("uri", r.RequestURI),
+			slog.String("uri", r.URL.Path),
 			slog.String("user_agent", r.UserAgent()),
 		),
 		slog.Group("response",
@@ -101,7 +101,7 @@ func JSONUnauthorized(w http.ResponseWriter, r *http.Request) {
 		slog.String("client_ip", request.ClientIP(r)),
 		slog.Group("request",
 			slog.String("method", r.Method),
-			slog.String("uri", r.RequestURI),
+			slog.String("uri", r.URL.Path),
 			slog.String("user_agent", r.UserAgent()),
 		),
 		slog.Group("response",
@@ -122,7 +122,7 @@ func JSONForbidden(w http.ResponseWriter, r *http.Request) {
 		slog.String("client_ip", request.ClientIP(r)),
 		slog.Group("request",
 			slog.String("method", r.Method),
-			slog.String("uri", r.RequestURI),
+			slog.String("uri", r.URL.Path),
 			slog.String("user_agent", r.UserAgent()),
 		),
 		slog.Group("response",
@@ -143,7 +143,7 @@ func JSONNotFound(w http.ResponseWriter, r *http.Request) {
 		slog.String("client_ip", request.ClientIP(r)),
 		slog.Group("request",
 			slog.String("method", r.Method),
-			slog.String("uri", r.RequestURI),
+			slog.String("uri", r.URL.Path),
 			slog.String("user_agent", r.UserAgent()),
 		),
 		slog.Group("response",

+ 51 - 0
internal/http/response/response_test.go

@@ -4,8 +4,12 @@
 package response // import "miniflux.app/v2/internal/http/response"
 
 import (
+	"bytes"
+	"errors"
+	"log/slog"
 	"net/http"
 	"net/http/httptest"
+	"strings"
 	"testing"
 )
 
@@ -36,3 +40,50 @@ func TestNoContentResponse(t *testing.T) {
 		t.Fatalf(`Unexpected content type, got %q instead of empty string`, actualContentType)
 	}
 }
+
+func TestErrorResponsesDoNotLogQueryString(t *testing.T) {
+	const secret = "Abcdef123456"
+
+	helpers := map[string]func(http.ResponseWriter, *http.Request){
+		"HTMLServerError": func(writer http.ResponseWriter, request *http.Request) {
+			HTMLServerError(writer, request, errors.New("some error"))
+		},
+		"HTMLBadRequest": func(writer http.ResponseWriter, request *http.Request) {
+			HTMLBadRequest(writer, request, errors.New("some error"))
+		},
+		"HTMLForbidden":     HTMLForbidden,
+		"HTMLNotFound":      HTMLNotFound,
+		"HTMLNotAcceptable": HTMLNotAcceptable,
+		"HTMLRequestedRangeNotSatisfiable": func(writer http.ResponseWriter, request *http.Request) {
+			HTMLRequestedRangeNotSatisfiable(writer, request, "bytes */0")
+		},
+		"JSONServerError": func(writer http.ResponseWriter, request *http.Request) {
+			JSONServerError(writer, request, errors.New("some error"))
+		},
+		"JSONBadRequest": func(writer http.ResponseWriter, request *http.Request) {
+			JSONBadRequest(writer, request, errors.New("some error"))
+		},
+		"JSONUnauthorized": JSONUnauthorized,
+		"JSONForbidden":    JSONForbidden,
+		"JSONNotFound":     JSONNotFound,
+	}
+
+	for name, helper := range helpers {
+		t.Run(name, func(t *testing.T) {
+			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) })
+
+			request := httptest.NewRequest(http.MethodPost, "/accounts/ClientLogin?Email=alice&Passwd="+secret, nil)
+			helper(httptest.NewRecorder(), request)
+
+			if !strings.Contains(logs.String(), "/accounts/ClientLogin") {
+				t.Fatalf("Expected the request path to be logged, got:\n%s", logs.String())
+			}
+			if strings.Contains(logs.String(), secret) {
+				t.Errorf("Logs contain the query string:\n%s", logs.String())
+			}
+		})
+	}
+}

+ 1 - 1
internal/http/server/middleware.go

@@ -32,7 +32,7 @@ func middleware(next http.Handler) http.Handler {
 				slog.String("client_ip", clientIP),
 				slog.Group("request",
 					slog.String("method", r.Method),
-					slog.String("uri", r.RequestURI),
+					slog.String("uri", r.URL.Path),
 					slog.String("protocol", r.Proto),
 					slog.Duration("execution_time", time.Since(t1)),
 				),

+ 27 - 0
internal/http/server/server_test.go

@@ -4,9 +4,13 @@
 package server
 
 import (
+	"bytes"
+	"log/slog"
 	"net/http"
+	"net/http/httptest"
 	"os"
 	"runtime"
+	"strings"
 	"testing"
 	"time"
 
@@ -252,3 +256,26 @@ func TestCreateUnixSocketListenerPermissions(t *testing.T) {
 		t.Errorf("Unix socket permissions = %04o, want %04o", got, want)
 	}
 }
+
+func TestMiddlewareDoesNotLogQueryString(t *testing.T) {
+	const secret = "Abcdef123456"
+
+	previousOptions := config.Opts
+	config.Opts = config.NewConfigOptions()
+	t.Cleanup(func() { config.Opts = previousOptions })
+
+	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) })
+
+	handler := middleware(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
+	handler.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/fever/?api&api_key="+secret, nil))
+
+	if !strings.Contains(logs.String(), "Incoming request") || !strings.Contains(logs.String(), "/fever/") {
+		t.Fatalf("Expected the request path to be logged, got:\n%s", logs.String())
+	}
+	if strings.Contains(logs.String(), secret) {
+		t.Errorf("Logs contain the query string:\n%s", logs.String())
+	}
+}

+ 1 - 1
internal/ui/csrf_middleware.go

@@ -59,7 +59,7 @@ func (m *csrfMiddleware) validate(w http.ResponseWriter, r *http.Request) bool {
 	}
 
 	slog.Warn("Invalid or missing CSRF token",
-		slog.String("url", r.RequestURI),
+		slog.String("url", r.URL.Path),
 	)
 
 	if r.URL.Path == "/login" {