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
2 changes: 1 addition & 1 deletion pkg/detector/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
66 changes: 66 additions & 0 deletions pkg/detector/detector_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
8 changes: 8 additions & 0 deletions pkg/detector/race_off_test.go
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions pkg/detector/race_on_test.go
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion pkg/detector/stripe_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading