diff --git a/pkg/appsec/challenge/spent_set.go b/pkg/appsec/challenge/spent_set.go index 23686fed9b0..f853e411dd9 100644 --- a/pkg/appsec/challenge/spent_set.go +++ b/pkg/appsec/challenge/spent_set.go @@ -4,6 +4,7 @@ package challenge import ( "container/list" + "strings" "sync" "time" ) @@ -65,7 +66,14 @@ func (s *spentSet) checkAndInsert(r string, ttl time.Duration) bool { s.remove(s.order.Front()) } - s.items[r] = s.order.PushBack(&spentEntry{r: r, expiresAt: now.Add(ttl)}) + // Copy before retaining: callers hand us a value sliced out of the parsed + // submission body (url.ParseQuery doesn't copy values that need no + // unescaping), so keeping that view would pin the whole body — several KB of + // fingerprint payload — for the entry's full TTL. Only the insert path pays + // for this; the lookup above doesn't retain its key. + key := strings.Clone(r) + + s.items[key] = s.order.PushBack(&spentEntry{r: key, expiresAt: now.Add(ttl)}) return true } diff --git a/pkg/appsec/challenge/spent_set_test.go b/pkg/appsec/challenge/spent_set_test.go index ca96c20e032..4bf69d86124 100644 --- a/pkg/appsec/challenge/spent_set_test.go +++ b/pkg/appsec/challenge/spent_set_test.go @@ -1,10 +1,13 @@ package challenge import ( + "net/url" + "strings" "sync" "sync/atomic" "testing" "time" + "unsafe" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -100,3 +103,47 @@ func TestSpentSet_ReclaimsExpired(t *testing.T) { func TestSpentSet_StartsEmpty(t *testing.T) { require.Equal(t, 0, newSpentSet(spentSetDefaultMaxEntries).len()) } + +// sharesBacking reports whether s is a view into body starting at off, rather +// than a copy. Taking StringData of the body subslice keeps this pointer +// comparison free of any uintptr arithmetic. +func sharesBacking(body string, off int, s string) bool { + return unsafe.StringData(body[off:]) == unsafe.StringData(s) +} + +// TestSpentSet_DoesNotPinSubmissionBody test is only relevant for PR 4737 +// Before that, the whole submission body from bot detection was escaping to heap +// because of url.ParseQuery fast track mechanisms. +func TestSpentSet_DoesNotPinSubmissionBody(t *testing.T) { + const rValue = "a1b2c3d4e5f60718293a4b5c6d7e8f90" + + // A submission shaped like the real thing: a large %-escaped fingerprint + // (base64-std, so it does get copied) and `r` last. + body := []byte("f=" + strings.Repeat("aB%2Fc%2Bd", 400) + "&ts=1759660000&r=" + rValue) + + parsed := string(body) + + vars, err := url.ParseQuery(parsed) + require.NoError(t, err) + + r := vars.Get("r") + require.Equal(t, rValue, r) + + off := strings.Index(parsed, rValue) + require.Positive(t, off) + require.True(t, sharesBacking(parsed, off, r), + "precondition: url.ParseQuery returns `r` as a view into the body it parsed") + + s := newSpentSet(spentSetDefaultMaxEntries) + require.True(t, s.checkAndInsert(r, time.Minute)) + + entry := s.order.Front().Value.(*spentEntry) + require.Equal(t, rValue, entry.r) + require.False(t, sharesBacking(parsed, off, entry.r), + "spentEntry.r must be a copy, not a view pinning the %d-byte submission body", len(parsed)) + + for key := range s.items { + require.False(t, sharesBacking(parsed, off, key), + "the map key must be a copy, not a view pinning the %d-byte submission body", len(parsed)) + } +}