Compare commits
2
Commits
next
...
afe4b3bb70
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
afe4b3bb70 | ||
|
|
3bcd032d37 |
No files matched your search
@@ -137,6 +137,12 @@ path under `/v1/` answers 200, in maintenance mode too.
|
|||||||
authentication with `metrics.username` and `metrics.password`. Answers: 200;
|
authentication with `metrics.username` and `metrics.password`. Answers: 200;
|
||||||
401 without them; 404 when they are not set, as the route then does not exist.
|
401 without them; 404 when they are not set, as the route then does not exist.
|
||||||
|
|
||||||
|
Every response carries an `X-Request-ID` header holding the request's ID, which
|
||||||
|
a client can quote when reporting a problem: the request's own `X-Request-ID`
|
||||||
|
when it sent one, as a reverse proxy in front of pixa may, otherwise one pixa
|
||||||
|
makes up from its host name, a random string chosen at startup and a counter.
|
||||||
|
pixa's log line for the request carries the same ID as `request_id`.
|
||||||
|
|
||||||
Both `POST` routes accept only a form that pixa's own page served: the page puts
|
Both `POST` routes accept only a form that pixa's own page served: the page puts
|
||||||
a token in the form and sets a cookie to match, and a request without both is
|
a token in the form and sets a cookie to match, and a request without both is
|
||||||
refused with 403, so another site cannot submit the form from a visitor's
|
refused with 403, so another site cannot submit the form from a visitor's
|
||||||
|
|||||||
@@ -115,6 +115,20 @@ func (s *Middleware) RateLimit(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RequestIDResponseHeader returns a middleware that sends the request's ID as
|
||||||
|
// the X-Request-Id response header, so a client can quote it when reporting a
|
||||||
|
// problem. The ID is the one chi's RequestID middleware stored in the request
|
||||||
|
// context, so RequestID must run first.
|
||||||
|
func (s *Middleware) RequestIDResponseHeader() func(http.Handler) http.Handler {
|
||||||
|
return func(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set(middleware.RequestIDHeader,
|
||||||
|
middleware.GetReqID(r.Context()))
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type loggingResponseWriter struct {
|
type loggingResponseWriter struct {
|
||||||
http.ResponseWriter
|
http.ResponseWriter
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// requestIDHeader is the header that carries a request's ID.
|
||||||
|
const requestIDHeader = "X-Request-Id"
|
||||||
|
|
||||||
|
// TestResponsesCarryRequestID verifies that every response, whatever its route
|
||||||
|
// and status, carries the request's ID as X-Request-Id, so a client can quote
|
||||||
|
// it when reporting a problem: one pixa made up when the request brought none,
|
||||||
|
// and the request's own X-Request-Id when it brought one.
|
||||||
|
func TestResponsesCarryRequestID(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
const clientRequestID = "client-request-id"
|
||||||
|
|
||||||
|
s := newTestServer(t)
|
||||||
|
|
||||||
|
paths := []string{
|
||||||
|
"/robots.txt",
|
||||||
|
"/no-such-path",
|
||||||
|
unsignedImagePath,
|
||||||
|
encryptedImagePath,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, path := range paths {
|
||||||
|
t.Run(path, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
s.ServeHTTP(rec, httptest.NewRequestWithContext(
|
||||||
|
t.Context(), http.MethodGet, path, nil))
|
||||||
|
t.Logf("status %d, %s %q",
|
||||||
|
rec.Code, requestIDHeader, rec.Header().Get(requestIDHeader))
|
||||||
|
|
||||||
|
if rec.Header().Get(requestIDHeader) == "" {
|
||||||
|
t.Errorf("response has no %s", requestIDHeader)
|
||||||
|
}
|
||||||
|
|
||||||
|
req := httptest.NewRequestWithContext(
|
||||||
|
t.Context(), http.MethodGet, path, nil)
|
||||||
|
req.Header.Set(requestIDHeader, clientRequestID)
|
||||||
|
|
||||||
|
rec = httptest.NewRecorder()
|
||||||
|
s.ServeHTTP(rec, req)
|
||||||
|
|
||||||
|
got := rec.Header().Get(requestIDHeader)
|
||||||
|
if got != clientRequestID {
|
||||||
|
t.Errorf("%s = %q, want the request's own %q",
|
||||||
|
requestIDHeader, got, clientRequestID)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -29,6 +29,7 @@ func (s *Server) SetupRoutes() {
|
|||||||
|
|
||||||
s.router.Use(middleware.Recoverer)
|
s.router.Use(middleware.Recoverer)
|
||||||
s.router.Use(middleware.RequestID)
|
s.router.Use(middleware.RequestID)
|
||||||
|
s.router.Use(s.mw.RequestIDResponseHeader())
|
||||||
s.router.Use(s.mw.ClientIP())
|
s.router.Use(s.mw.ClientIP())
|
||||||
s.router.Use(s.mw.SecurityHeaders())
|
s.router.Use(s.mw.SecurityHeaders())
|
||||||
s.router.Use(s.mw.Logging())
|
s.router.Use(s.mw.Logging())
|
||||||
|
|||||||
Reference in new issue
Block a user