From 4124e38beef5c6bea239f0d3d82486cac84ce8a9 Mon Sep 17 00:00:00 2001 From: Thibault Koechlin Date: Mon, 5 Oct 2026 14:07:13 +0200 Subject: [PATCH 1/3] clone the string to avoid leak --- pkg/appsec/challenge/spent_set.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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 } From ef17fb8677266a7011b14505078d49ebfa84a2c5 Mon Sep 17 00:00:00 2001 From: Thibault Koechlin Date: Mon, 5 Oct 2026 14:07:47 +0200 Subject: [PATCH 2/3] minor test to showcase the memleak, could even be removed --- pkg/appsec/challenge/spent_set_test.go | 50 ++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/pkg/appsec/challenge/spent_set_test.go b/pkg/appsec/challenge/spent_set_test.go index ca96c20e032..abc1ed9a75e 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,50 @@ 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 guards against retaining the whole +// challenge submission per burned ticket. ValidateChallengeResponse parses +// string(body), and url.ParseQuery hands back values that alias that string +// whenever they need no unescaping — which is always the case for the 32-char +// hex `r`. Storing that view keeps the entire body (several KB of fingerprint) +// alive for ticketAgeBackstop, so the set must own its keys. +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)) + } +} From 7b47286d20de3a095bc0275e82c17a5db564447e Mon Sep 17 00:00:00 2001 From: "Thibault \"bui\" Koechlin" Date: Mon, 5 Oct 2026 15:24:02 +0200 Subject: [PATCH 3/3] Update spent_set_test.go --- pkg/appsec/challenge/spent_set_test.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/pkg/appsec/challenge/spent_set_test.go b/pkg/appsec/challenge/spent_set_test.go index abc1ed9a75e..4bf69d86124 100644 --- a/pkg/appsec/challenge/spent_set_test.go +++ b/pkg/appsec/challenge/spent_set_test.go @@ -111,12 +111,9 @@ func sharesBacking(body string, off int, s string) bool { return unsafe.StringData(body[off:]) == unsafe.StringData(s) } -// TestSpentSet_DoesNotPinSubmissionBody guards against retaining the whole -// challenge submission per burned ticket. ValidateChallengeResponse parses -// string(body), and url.ParseQuery hands back values that alias that string -// whenever they need no unescaping — which is always the case for the 32-char -// hex `r`. Storing that view keeps the entire body (several KB of fingerprint) -// alive for ticketAgeBackstop, so the set must own its keys. +// 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"