internal/leaks/rules.go
94 lines · 4130 bytes
8 symbols in this file
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. Every match contains one of keywords,
14// which are checked before the expression, so most content costs a few
15// substring searches and nothing else. They must be specific: Go's regexp
16// runs an expression that starts with \b byte by byte over the whole blob,
17// and a keyword common in source text (a bare "A") made a full-history scan
18// of this repository take minutes.
19type Rule struct {
20 ID string
21 keywords []string
22 re *regexp.Regexp
23}
24
25func kw(k ...string) []string { return k }
26
27var rules = []Rule{
28 {"private-key", kw("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,}`)},
29 {"age-secret-key", kw("AGE-SECRET-KEY-1"), regexp.MustCompile(`AGE-SECRET-KEY-1[QPZRY9X8GF2TVDW0S3JN54KHCE6MUA7L]{58}`)},
30 {"aws-access-key-id", kw("AKIA", "ASIA", "ABIA", "ACCA"), regexp.MustCompile(`\b(?:AKIA|ASIA|ABIA|ACCA)[A-Z2-7]{16}\b`)},
31 {"github-token", kw("ghp_", "gho_", "ghu_", "ghs_", "ghr_"), regexp.MustCompile(`\bgh[pousr]_[A-Za-z0-9]{36}\b`)},
32 {"github-fine-grained-token", kw("github_pat_"), regexp.MustCompile(`\bgithub_pat_[A-Za-z0-9_]{82}\b`)},
33 {"gitlab-token", kw("glpat-"), regexp.MustCompile(`\bglpat-[A-Za-z0-9_\-]{20,}\b`)},
34 {"gitbay-token", kw("gb_"), regexp.MustCompile(`\bgb_[0-9a-f]{64}\b`)},
35 {"slack-token", kw("xoxb-", "xoxa-", "xoxp-", "xoxr-", "xoxs-"), regexp.MustCompile(`\bxox[baprs]-[0-9]{6,}-[A-Za-z0-9-]{10,}\b`)},
36 {"slack-webhook", kw("hooks.slack.com/services/"), regexp.MustCompile(`https://hooks\.slack\.com/services/T[A-Z0-9]{6,}/B[A-Z0-9]{6,}/[A-Za-z0-9]{20,}`)},
37 {"stripe-live-key", kw("sk_live_", "rk_live_"), regexp.MustCompile(`\b[rs]k_live_[A-Za-z0-9]{24,}\b`)},
38 {"google-api-key", kw("AIza"), regexp.MustCompile(`\bAIza[0-9A-Za-z_\-]{35}\b`)},
39 {"npm-token", kw("npm_"), regexp.MustCompile(`\bnpm_[A-Za-z0-9]{36}\b`)},
40 {"pypi-token", kw("pypi-AgEIcHlwaS5vcmc"), regexp.MustCompile(`\bpypi-AgEIcHlwaS5vcmc[A-Za-z0-9_\-]{50,}`)},
41 {"openai-key", kw("sk-proj-", "sk-svcacct-", "sk-admin-"), regexp.MustCompile(`\bsk-(?:proj|svcacct|admin)-[A-Za-z0-9_\-]{40,}`)},
42 {"anthropic-key", kw("sk-ant-"), regexp.MustCompile(`\bsk-ant-(?:api|admin)[0-9]{2}-[A-Za-z0-9_\-]{80,}`)},
43 {"sendgrid-key", kw("SG."), regexp.MustCompile(`\bSG\.[A-Za-z0-9_\-]{22}\.[A-Za-z0-9_\-]{43}\b`)},
44 {"digitalocean-token", kw("dop_v1_", "doo_v1_", "dor_v1_"), regexp.MustCompile(`\bdo[opr]_v1_[a-f0-9]{64}\b`)},
45 {"shopify-token", kw("shpat_", "shpca_", "shppa_", "shpss_"), regexp.MustCompile(`\bshp(?:at|ca|pa|ss)_[a-fA-F0-9]{32}\b`)},
46}
47
48// Hit is one match inside a blob.
49type Hit struct {
50 Line int `json:"line"`
51 Rule string `json:"rule"`
52}
53
54// maxHitsPerBlob bounds what one file reports; a file with more is not
55// made more urgent by listing them all.
56const maxHitsPerBlob = 10
57
58// Find returns where data matches a rule, one hit per rule and line.
59func Find(data []byte) []Hit {
60 var out []Hit
61 for _, r := range rules {
62 if !containsAny(data, r.keywords) {
63 continue
64 }
65 last := 0
66 for _, loc := range r.re.FindAllIndex(data, maxHitsPerBlob) {
67 line := 1 + bytes.Count(data[:loc[0]], []byte("\n"))
68 if line == last {
69 continue
70 }
71 last = line
72 out = append(out, Hit{Line: line, Rule: r.ID})
73 if len(out) >= maxHitsPerBlob {
74 return out
75 }
76 }
77 }
78 return out
79}
80
81// binary reports whether data looks like something other than text: a NUL
82// in the first 8000 bytes, git's own test.
83func binary(data []byte) bool {
84 return bytes.IndexByte(data[:min(len(data), 8000)], 0) >= 0
85}
86
87func containsAny(data []byte, keywords []string) bool {
88 for _, k := range keywords {
89 if bytes.Contains(data, []byte(k)) {
90 return true
91 }
92 }
93 return false
94}