From 7fc62b895affc7a5393e56b1f8bae8e05e142c7f Mon Sep 17 00:00:00 2001 From: Alexis-Maurer Fortin Date: Fri, 2 Oct 2026 15:38:26 -0400 Subject: [PATCH] Detectors allocate a findings slice only when they find something Registry.DetectAll preallocated a slice sized to the whole registry on every call, and the Stripe detector preallocated room for four findings, even though almost every scanned line finds nothing. Both now start from a nil slice and append on a match. Findings, order and fields are unchanged. Co-Authored-By: Claude Opus 5.5 --- pkg/detector/detector.go | 2 +- pkg/detector/detector_test.go | 66 +++++++++++++++++++++++++++++++++++ pkg/detector/race_off_test.go | 8 +++++ pkg/detector/race_on_test.go | 10 ++++++ pkg/detector/stripe_key.go | 2 +- 5 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 pkg/detector/detector_test.go create mode 100644 pkg/detector/race_off_test.go create mode 100644 pkg/detector/race_on_test.go diff --git a/pkg/detector/detector.go b/pkg/detector/detector.go index 4bf0d58..64907e5 100644 --- a/pkg/detector/detector.go +++ b/pkg/detector/detector.go @@ -63,7 +63,7 @@ func (r *Registry) DetectAll(content string, ctx *models.DetectionContext) []mod // Propagate the registry's fingerprint salt to the detection context ctx.FingerprintSalt = r.fingerprintSalt - findings := make([]models.Finding, 0, len(r.detectors)) + var findings []models.Finding for _, det := range r.detectors { detectorFindings := det.Detect(content, ctx) diff --git a/pkg/detector/detector_test.go b/pkg/detector/detector_test.go new file mode 100644 index 0000000..fe284ca --- /dev/null +++ b/pkg/detector/detector_test.go @@ -0,0 +1,66 @@ +// Copyright (C) 2026 boostsecurity.io +// SPDX-License-Identifier: GPL-3.0-or-later + +package detector + +import ( + "testing" + + "github.com/boostsecurityio/bagel/pkg/models" + "github.com/stretchr/testify/assert" +) + +var cleanLines = []string{ + "", + "export PATH=/usr/local/bin:$PATH", + ` "name": "my-package",`, + "password = token key secret", + "sk_ ghp_ AKIA eyJ xoxb- npm_ pypi-", +} + +func TestRegistry_DetectAllCleanLineAllocatesNothing(t *testing.T) { + if raceEnabled { + t.Skip("allocation counts are skewed under -race") + } + r := NewDefaultRegistry() + ctx := &models.DetectionContext{Source: "file:/tmp/x", ProbeName: "test"} + + for _, line := range cleanLines { + t.Run(line, func(t *testing.T) { + allocs := testing.AllocsPerRun(100, func() { + r.DetectAll(line, ctx) + }) + assert.Zero(t, allocs) + assert.Empty(t, r.DetectAll(line, ctx)) + }) + } +} + +func TestRegistry_DetectAllKeepsRegistrationOrder(t *testing.T) { + t.Parallel() + + r := NewDefaultRegistry() + ctx := &models.DetectionContext{Source: "file:/tmp/x", ProbeName: "test", LineNumber: 3} + line := "sk_live_" + "abcdefghijklmnopqrstuvwx1234 ghp_" + "1234567890abcdefghijklmnopqrstuvwxyz" + + findings := r.DetectAll(line, ctx) + + ids := make([]string, len(findings)) + for i, f := range findings { + ids[i] = f.ID + assert.Equal(t, "test", f.Probe) + assert.Equal(t, 3, f.Metadata["line_number"]) + } + assert.Equal(t, []string{"github-token-classic-pat", "stripe-secret-key"}, ids) +} + +func BenchmarkRegistry_DetectAllCleanLines(b *testing.B) { + r := NewDefaultRegistry() + ctx := &models.DetectionContext{Source: "file:/tmp/x", ProbeName: "test"} + b.ReportAllocs() + for b.Loop() { + for _, line := range cleanLines { + r.DetectAll(line, ctx) + } + } +} diff --git a/pkg/detector/race_off_test.go b/pkg/detector/race_off_test.go new file mode 100644 index 0000000..cea7c2f --- /dev/null +++ b/pkg/detector/race_off_test.go @@ -0,0 +1,8 @@ +// Copyright (C) 2026 boostsecurity.io +// SPDX-License-Identifier: GPL-3.0-or-later + +//go:build !race + +package detector + +const raceEnabled = false diff --git a/pkg/detector/race_on_test.go b/pkg/detector/race_on_test.go new file mode 100644 index 0000000..ce7100d --- /dev/null +++ b/pkg/detector/race_on_test.go @@ -0,0 +1,10 @@ +// Copyright (C) 2026 boostsecurity.io +// SPDX-License-Identifier: GPL-3.0-or-later + +//go:build race + +package detector + +// The race detector changes escape analysis, so allocation counts are only +// meaningful without it. +const raceEnabled = true diff --git a/pkg/detector/stripe_key.go b/pkg/detector/stripe_key.go index 536bb49..5532228 100644 --- a/pkg/detector/stripe_key.go +++ b/pkg/detector/stripe_key.go @@ -73,7 +73,7 @@ func (d *StripeKeyDetector) Detect( content string, ctx *models.DetectionContext, ) []models.Finding { - findings := make([]models.Finding, 0, 4) + var findings []models.Finding seen := make(map[string]bool) // kind is the human-readable variant label ("secret" / "restricted"