feat(assessment): record capture hints and keep one window per attempt

This commit is contained in:
shira committed 2026-10-11 13:16:52 +03:00
1 parent c975a0d506
commit bb917bf62e
108 files changed
+6649 -139

No files matched your search

@@ -12,10 +12,15 @@ const SessionCookieName = "__Host-lincodex_session"
type sessionRequestMetadataKey struct{}
// maxUserAgentBytes bounds the User-Agent that is kept for the request. A
// browser sends a few hundred bytes; the rest is not a browser.
const maxUserAgentBytes = 512
type sessionRequestMetadata struct {
origin string
originValid bool
incomingToken domain.SecretToken
userAgent string
}
// SessionRequestMetadata records the browser Origin and an optional incoming
@@ -24,7 +29,7 @@ type sessionRequestMetadata struct {
func SessionRequestMetadata() func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
metadata := sessionRequestMetadata{}
metadata := sessionRequestMetadata{userAgent: boundedUserAgent(r.Header.Get("User-Agent"))}
origins := r.Header.Values("Origin")
if len(origins) == 1 {
metadata.origin = origins[0]
@@ -55,6 +60,21 @@ func RequestOrigin(ctx context.Context) (string, bool) {
return metadata.origin, ok && metadata.originValid
}
// RequestUserAgent returns the User-Agent of the request, cut to a safe length,
// or an empty string when it had none. It exists to be hashed with a key into a
// signal of the attempt; callers never store or log it.
func RequestUserAgent(ctx context.Context) string {
metadata, _ := ctx.Value(sessionRequestMetadataKey{}).(sessionRequestMetadata)
return metadata.userAgent
}
func boundedUserAgent(value string) string {
if len(value) > maxUserAgentBytes {
return value[:maxUserAgentBytes]
}
return value
}
// IncomingSessionToken returns a parsed token from exactly one session cookie.
// The zero value means missing, malformed, or duplicated input.
func IncomingSessionToken(ctx context.Context) domain.SecretToken {
@@ -3,6 +3,7 @@ package middleware
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"lincodex/backend/internal/domain"
@@ -94,3 +95,32 @@ func TestSessionCookieMatchesOnlyTheBoundaryToken(t *testing.T) {
t.Fatalf("SessionCookieMatches() = %v", matches)
}
}
func TestRequestUserAgentIsBoundedAndAbsentWithoutTheMiddleware(t *testing.T) {
var seen string
handler := SessionRequestMetadata()(http.HandlerFunc(func(_ http.ResponseWriter, request *http.Request) {
seen = RequestUserAgent(request.Context())
}))
request := httptest.NewRequest(http.MethodGet, "/api/v1/me/assessment-attempts", nil)
request.Header.Set("User-Agent", "TestBrowser/1.0")
handler.ServeHTTP(httptest.NewRecorder(), request)
if seen != "TestBrowser/1.0" {
t.Fatalf("RequestUserAgent() = %q, want the header value", seen)
}
request = httptest.NewRequest(http.MethodGet, "/api/v1/me/assessment-attempts", nil)
request.Header.Set("User-Agent", strings.Repeat("A", 4*maxUserAgentBytes))
handler.ServeHTTP(httptest.NewRecorder(), request)
if len(seen) != maxUserAgentBytes {
t.Fatalf("RequestUserAgent() length = %d, want it cut to %d bytes", len(seen), maxUserAgentBytes)
}
request = httptest.NewRequest(http.MethodGet, "/api/v1/me/assessment-attempts", nil)
handler.ServeHTTP(httptest.NewRecorder(), request)
if seen != "" {
t.Fatalf("RequestUserAgent() = %q, want empty for a request without the header", seen)
}
if got := RequestUserAgent(request.Context()); got != "" {
t.Fatalf("RequestUserAgent() without the middleware = %q, want empty", got)
}
}