GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat(secrets): add gitleaks-powered secret detection package · majorcontext/keep@914d6bb

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • internal/secrets
    • secrets.go
    • secrets_test.go
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎internal/secrets/secrets.go‎

Copy file name to clipboard
+77Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package secrets
2+
3+
import (
4+
"fmt"
5+
"sort"
6+
"strings"
7+
8+
"github.com/zricethezav/gitleaks/v8/detect"
9+
)
10+
11+
// Detector wraps the gitleaks detector for secret detection.
12+
type Detector struct {
13+
d *detect.Detector
14+
}
15+
16+
// Finding represents a detected secret.
17+
type Finding struct {
18+
RuleID string // e.g. "aws-access-key"
19+
Description string // human-readable description from gitleaks
20+
Match string // the matched text
21+
}
22+
23+
// NewDetector creates a Detector with gitleaks default config (~160 patterns).
24+
func NewDetector() (*Detector, error) {
25+
d, err := detect.NewDetectorDefaultConfig()
26+
if err != nil {
27+
return nil, fmt.Errorf("secrets: init gitleaks: %w", err)
28+
}
29+
return &Detector{d: d}, nil
30+
}
31+
32+
// Detect scans text and returns all secret findings.
33+
// Safe to call on nil receiver (returns nil).
34+
func (d *Detector) Detect(text string) []Finding {
35+
if d == nil {
36+
return nil
37+
}
38+
gitleaksFindings := d.d.DetectString(text)
39+
if len(gitleaksFindings) == 0 {
40+
return nil
41+
}
42+
findings := make([]Finding, len(gitleaksFindings))
43+
for i, f := range gitleaksFindings {
44+
findings[i] = Finding{
45+
RuleID: f.RuleID,
46+
Description: f.Description,
47+
Match: f.Match,
48+
}
49+
}
50+
return findings
51+
}
52+
53+
// Redact scans text, replaces each detected secret with [REDACTED:<RuleID>],
54+
// and returns the redacted string plus the findings.
55+
// Safe to call on nil receiver (returns original text, nil).
56+
func (d *Detector) Redact(text string) (string, []Finding) {
57+
if d == nil {
58+
return text, nil
59+
}
60+
findings := d.Detect(text)
61+
if len(findings) == 0 {
62+
return text, nil
63+
}
64+
65+
// Sort findings by length of match descending so longer matches are
66+
// replaced first, preventing partial replacements.
67+
sort.Slice(findings, func(i, j int) bool {
68+
return len(findings[i].Match) > len(findings[j].Match)
69+
})
70+
71+
redacted := text
72+
for _, f := range findings {
73+
placeholder := fmt.Sprintf("[REDACTED:%s]", f.RuleID)
74+
redacted = strings.ReplaceAll(redacted, f.Match, placeholder)
75+
}
76+
return redacted, findings
77+
}
Collapse file

‎internal/secrets/secrets_test.go‎

Copy file name to clipboard
+149Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package secrets
2+
3+
import (
4+
"strings"
5+
"testing"
6+
)
7+
8+
// awsKey is a fake AWS access key that matches the gitleaks pattern.
9+
// It does not end in "EXAMPLE" (which is allowlisted) and has the correct format.
10+
const awsKey = "AKIAIOSFODNN7REALKEY"
11+
12+
// githubPAT is a fake GitHub PAT that matches the gitleaks github-pat rule.
13+
// It is exactly 36 alphanumeric chars after "ghp_" and does not contain
14+
// the "abcdefghijklmnopqrstuvwxyz" stopword.
15+
const githubPAT = "ghp_0123456789ABCDEFGHIJ0123456789ABCDEF"
16+
17+
// stripeKey matches the gitleaks stripe-access-token rule.
18+
const stripeKey = "sk_live_abcdefghijklmnopqrstuvwx"
19+
20+
func TestDetect_AWSKey(t *testing.T) {
21+
d, err := NewDetector()
22+
if err != nil {
23+
t.Fatal(err)
24+
}
25+
findings := d.Detect("access key is " + awsKey)
26+
if len(findings) == 0 {
27+
t.Fatal("expected to detect AWS access key")
28+
}
29+
found := false
30+
for _, f := range findings {
31+
if strings.Contains(f.Match, awsKey) {
32+
found = true
33+
}
34+
}
35+
if !found {
36+
t.Errorf("expected finding to contain the AWS key, got %+v", findings)
37+
}
38+
}
39+
40+
func TestDetect_GitHubPAT(t *testing.T) {
41+
d, err := NewDetector()
42+
if err != nil {
43+
t.Fatal(err)
44+
}
45+
findings := d.Detect(githubPAT)
46+
if len(findings) == 0 {
47+
t.Fatal("expected to detect GitHub PAT")
48+
}
49+
}
50+
51+
func TestDetect_StripeKey(t *testing.T) {
52+
d, err := NewDetector()
53+
if err != nil {
54+
t.Fatal(err)
55+
}
56+
findings := d.Detect("stripe key " + stripeKey)
57+
if len(findings) == 0 {
58+
t.Fatal("expected to detect Stripe key")
59+
}
60+
}
61+
62+
func TestDetect_NoMatch(t *testing.T) {
63+
d, err := NewDetector()
64+
if err != nil {
65+
t.Fatal(err)
66+
}
67+
findings := d.Detect("this is just normal text with no secrets")
68+
if len(findings) != 0 {
69+
t.Errorf("expected no findings, got %d: %+v", len(findings), findings)
70+
}
71+
}
72+
73+
func TestDetect_Multiple(t *testing.T) {
74+
d, err := NewDetector()
75+
if err != nil {
76+
t.Fatal(err)
77+
}
78+
text := "aws key " + awsKey + " and github " + githubPAT
79+
findings := d.Detect(text)
80+
if len(findings) < 2 {
81+
t.Errorf("expected at least 2 findings, got %d: %+v", len(findings), findings)
82+
}
83+
}
84+
85+
func TestRedact_ReplacesWithRuleID(t *testing.T) {
86+
d, err := NewDetector()
87+
if err != nil {
88+
t.Fatal(err)
89+
}
90+
text := "my key is " + awsKey + " ok"
91+
redacted, findings := d.Redact(text)
92+
if len(findings) == 0 {
93+
t.Fatal("expected findings")
94+
}
95+
if strings.Contains(redacted, awsKey) {
96+
t.Errorf("expected key to be redacted, got: %s", redacted)
97+
}
98+
if !strings.Contains(redacted, "[REDACTED:") {
99+
t.Errorf("expected [REDACTED:...] placeholder, got: %s", redacted)
100+
}
101+
}
102+
103+
func TestRedact_ReturnsFindings(t *testing.T) {
104+
d, err := NewDetector()
105+
if err != nil {
106+
t.Fatal(err)
107+
}
108+
_, findings := d.Redact("key is " + awsKey)
109+
if len(findings) == 0 {
110+
t.Fatal("expected findings")
111+
}
112+
f := findings[0]
113+
if f.RuleID == "" {
114+
t.Error("expected non-empty RuleID")
115+
}
116+
if !strings.Contains(f.Match, awsKey) {
117+
t.Errorf("expected Match to contain the key, got: %s", f.Match)
118+
}
119+
}
120+
121+
func TestRedact_NoMatch(t *testing.T) {
122+
d, err := NewDetector()
123+
if err != nil {
124+
t.Fatal(err)
125+
}
126+
text := "nothing secret here"
127+
redacted, findings := d.Redact(text)
128+
if redacted != text {
129+
t.Errorf("expected unchanged text, got: %s", redacted)
130+
}
131+
if len(findings) != 0 {
132+
t.Errorf("expected no findings, got %d", len(findings))
133+
}
134+
}
135+
136+
func TestNilDetector(t *testing.T) {
137+
var d *Detector
138+
findings := d.Detect(awsKey)
139+
if findings != nil {
140+
t.Errorf("expected nil from nil detector, got %+v", findings)
141+
}
142+
redacted, findings := d.Redact(awsKey)
143+
if redacted != awsKey {
144+
t.Errorf("expected original text from nil detector, got: %s", redacted)
145+
}
146+
if findings != nil {
147+
t.Errorf("expected nil findings from nil detector, got %+v", findings)
148+
}
149+
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.