| @@ -10,34 +10,39 @@ import ( |
| 10 | 10 | "regexp" |
| 11 | 11 | ) |
| 12 | 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. |
| 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. |
| 16 | 19 | type Rule struct { |
| 17 | | ID string |
| 18 | | keyword string |
| 19 | | re *regexp.Regexp |
| 20 | ID string |
| 21 | keywords []string |
| 22 | re *regexp.Regexp |
| 20 | 23 | } |
| 21 | 24 | |
| 25 | func kw(k ...string) []string { return k } |
| 26 | |
| 22 | 27 | var 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`)}, |
| 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`)}, |
| 41 | 46 | } |
| 42 | 47 | |
| 43 | 48 | // Hit is one match inside a blob. |
| @@ -54,7 +59,7 @@ const maxHitsPerBlob = 10 |
| 54 | 59 | func Find(data []byte) []Hit { |
| 55 | 60 | var out []Hit |
| 56 | 61 | for _, r := range rules { |
| 57 | | if !bytes.Contains(data, []byte(r.keyword)) { |
| 62 | if !containsAny(data, r.keywords) { |
| 58 | 63 | continue |
| 59 | 64 | } |
| 60 | 65 | last := 0 |
| @@ -78,3 +83,12 @@ func Find(data []byte) []Hit { |
| 78 | 83 | func binary(data []byte) bool { |
| 79 | 84 | return bytes.IndexByte(data[:min(len(data), 8000)], 0) >= 0 |
| 80 | 85 | } |
| 86 | |
| 87 | func 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 | } |