Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion grpc/interceptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ const (
meaningfulWorkOverhead = 100 * time.Millisecond
clientRequestTimeKey = "client-request-time"
userAgentKey = "acme-client-user-agent"
// We use the "-bin" suffix to tell grpc's metadata package not to require the user-agent
// be printable ASCII. It's rare, but ACME clients can send non-ASCII user-agents.
// Remove `userAgentKey` once this has been deployed.
userAgentKey2 = "acme-client-user-agent-bin"
)

type serverInterceptor interface {
Expand Down Expand Up @@ -91,7 +95,9 @@ func (smi *serverMetadataInterceptor) Unary(
return nil, err
}
}
if len(md[userAgentKey]) > 0 {
if len(md[userAgentKey2]) > 0 {
ctx = web.WithUserAgent(ctx, md[userAgentKey2][0])
} else if len(md[userAgentKey]) > 0 {
ctx = web.WithUserAgent(ctx, md[userAgentKey][0])
}
}
Expand Down Expand Up @@ -276,6 +282,7 @@ func (cmi *clientMetadataInterceptor) Unary(
reqMD := metadata.New(map[string]string{
clientRequestTimeKey: nowTS,
userAgentKey: web.UserAgent(ctx),
userAgentKey2: web.UserAgent(ctx),
})
// Configure the localCtx with the metadata so it gets sent along in the request
localCtx = metadata.NewOutgoingContext(localCtx, reqMD)
Expand Down Expand Up @@ -384,6 +391,7 @@ func (cmi *clientMetadataInterceptor) Stream(
reqMD := metadata.New(map[string]string{
clientRequestTimeKey: nowTS,
userAgentKey: web.UserAgent(ctx),
userAgentKey2: web.UserAgent(ctx),
})
// Configure the localCtx with the metadata so it gets sent along in the request
localCtx = metadata.NewOutgoingContext(localCtx, reqMD)
Expand Down
4 changes: 4 additions & 0 deletions web/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ func (th *TopHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

userAgent := r.Header.Get("User-Agent")
// If someone sends a very long User-Agent we're only interested in logging the beginning.
if len(userAgent) > 100 {
userAgent = userAgent[:100] + "..."
}

logEvent := &RequestEvent{
RealIP: realIP,
Expand Down
36 changes: 36 additions & 0 deletions web/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"regexp"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -39,6 +41,40 @@ func TestLogCode(t *testing.T) {
}
}

// TestLogUA tests that user-agents are truncated before logging, and faithfully pass along non-ASCII.
func TestLogUA(t *testing.T) {
mockLog := blog.UseMock()
th := NewTopHandler(mockLog, myHandler{})
req, err := http.NewRequest("GET", "/thisisignored", &bytes.Reader{})
if err != nil {
t.Fatal(err)
}
req.Header.Add("User-Agent", "🪨"+strings.Repeat("a", 200))
th.ServeHTTP(httptest.NewRecorder(), req)

matching := mockLog.GetAllMatching("JSON=")
if len(matching) != 1 {
t.Errorf("Expected exactly one log line. Got: %s",
strings.Join(mockLog.GetAllMatching(".*"), "\n"))
}
re := regexp.MustCompile(`JSON=({.*})$`)
m := re.FindStringSubmatch(matching[0])
if len(m) < 2 {
t.Fatalf("logging user-agent: no regexp match")
}
var ua struct {
UA string
}
err = json.Unmarshal([]byte(m[1]), &ua)
if err != nil {
t.Fatal(err)
}
expected := "🪨aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa..."
if ua.UA != expected {
t.Errorf("logging user-agent: got %x, want %x", ua.UA, expected)
}
}

type codeHandler struct{}

func (ch codeHandler) ServeHTTP(e *RequestEvent, w http.ResponseWriter, r *http.Request) {
Expand Down
Loading