internal/leaks/rules.go

80 lines · 3405 bytes

 1// Package leaks finds credentials in git content. The rules are token
 2// formats that identify themselves — a vendor prefix, a fixed length, a
 3// key block with a body — rather than entropy guesses, so a finding is
 4// worth stopping a push for. Only where a match is (path, line, rule) ever
 5// leaves this package; the matched text does not.
 6package leaks
 7
 8import (
 9	"bytes"
10	"regexp"
11)
12
13// Rule is one credential format. keyword is a literal every match
14// contains, checked before the expression so most content costs a substring
15// search and nothing else.
16type Rule struct {
17	ID      string
18	keyword string
19	re      *regexp.Regexp
20}
21
22var rules = []Rule{
23	{"private-key", "PRIVATE KEY", regexp.MustCompile(`-----BEGIN[ A-Z0-9]*PRIVATE KEY(?: BLOCK)?-----[ \t]*\r?\n(?:[A-Za-z0-9-]+: [^\n]*\r?\n)*\s*[A-Za-z0-9+/=]{40,}`)},
24	{"age-secret-key", "AGE-SECRET-KEY-1", regexp.MustCompile(`AGE-SECRET-KEY-1[QPZRY9X8GF2TVDW0S3JN54KHCE6MUA7L]{58}`)},
25	{"aws-access-key-id", "A", regexp.MustCompile(`\b(?:AKIA|ASIA|ABIA|ACCA)[A-Z2-7]{16}\b`)},
26	{"github-token", "gh", regexp.MustCompile(`\bgh[pousr]_[A-Za-z0-9]{36}\b`)},
27	{"github-fine-grained-token", "github_pat_", regexp.MustCompile(`\bgithub_pat_[A-Za-z0-9_]{82}\b`)},
28	{"gitlab-token", "glpat-", regexp.MustCompile(`\bglpat-[A-Za-z0-9_\-]{20,}\b`)},
29	{"gitbay-token", "gb_", regexp.MustCompile(`\bgb_[0-9a-f]{64}\b`)},
30	{"slack-token", "xox", regexp.MustCompile(`\bxox[baprs]-[0-9]{6,}-[A-Za-z0-9-]{10,}\b`)},
31	{"slack-webhook", "hooks.slack.com", regexp.MustCompile(`https://hooks\.slack\.com/services/T[A-Z0-9]{6,}/B[A-Z0-9]{6,}/[A-Za-z0-9]{20,}`)},
32	{"stripe-live-key", "_live_", regexp.MustCompile(`\b[rs]k_live_[A-Za-z0-9]{24,}\b`)},
33	{"google-api-key", "AIza", regexp.MustCompile(`\bAIza[0-9A-Za-z_\-]{35}\b`)},
34	{"npm-token", "npm_", regexp.MustCompile(`\bnpm_[A-Za-z0-9]{36}\b`)},
35	{"pypi-token", "pypi-AgEIcHlwaS5vcmc", regexp.MustCompile(`\bpypi-AgEIcHlwaS5vcmc[A-Za-z0-9_\-]{50,}`)},
36	{"openai-key", "sk-", regexp.MustCompile(`\bsk-(?:proj|svcacct|admin)-[A-Za-z0-9_\-]{40,}`)},
37	{"anthropic-key", "sk-ant-", regexp.MustCompile(`\bsk-ant-(?:api|admin)[0-9]{2}-[A-Za-z0-9_\-]{80,}`)},
38	{"sendgrid-key", "SG.", regexp.MustCompile(`\bSG\.[A-Za-z0-9_\-]{22}\.[A-Za-z0-9_\-]{43}\b`)},
39	{"digitalocean-token", "_v1_", regexp.MustCompile(`\bdo[opr]_v1_[a-f0-9]{64}\b`)},
40	{"shopify-token", "shp", regexp.MustCompile(`\bshp(?:at|ca|pa|ss)_[a-fA-F0-9]{32}\b`)},
41}
42
43// Hit is one match inside a blob.
44type Hit struct {
45	Line int    `json:"line"`
46	Rule string `json:"rule"`
47}
48
49// maxHitsPerBlob bounds what one file reports; a file with more is not
50// made more urgent by listing them all.
51const maxHitsPerBlob = 10
52
53// Find returns where data matches a rule, one hit per rule and line.
54func Find(data []byte) []Hit {
55	var out []Hit
56	for _, r := range rules {
57		if !bytes.Contains(data, []byte(r.keyword)) {
58			continue
59		}
60		last := 0
61		for _, loc := range r.re.FindAllIndex(data, maxHitsPerBlob) {
62			line := 1 + bytes.Count(data[:loc[0]], []byte("\n"))
63			if line == last {
64				continue
65			}
66			last = line
67			out = append(out, Hit{Line: line, Rule: r.ID})
68			if len(out) >= maxHitsPerBlob {
69				return out
70			}
71		}
72	}
73	return out
74}
75
76// binary reports whether data looks like something other than text: a NUL
77// in the first 8000 bytes, git's own test.
78func binary(data []byte) bool {
79	return bytes.IndexByte(data[:min(len(data), 8000)], 0) >= 0
80}