Skip to content
Merged
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 pkg/appsec/challenge/spent_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package challenge

import (
"container/list"
"strings"
"sync"
"time"
)
Expand Down Expand Up @@ -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
}
Expand Down
47 changes: 47 additions & 0 deletions pkg/appsec/challenge/spent_set_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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")
Comment on lines +134 to +135

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))
}
}
Loading