| 1 | package scan |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "io" |
| 6 | "regexp" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | // Finding represents a category of sensitive data found in a log. |
| 11 | type Finding struct { |
| 12 | Category string |
| 13 | } |
| 14 | |
| 15 | var patterns = []struct { |
| 16 | re *regexp.Regexp |
| 17 | category string |
| 18 | }{ |
| 19 | { |
| 20 | re: regexp.MustCompile(`(?:^|[^0-9])(?:10\.\d{1,3}\.\d{1,3}\.\d{1,3}|172\.(?:1[6-9]|2[0-9]|3[01])\.\d{1,3}\.\d{1,3}|192\.168\.\d{1,3}\.\d{1,3})(?:[^0-9]|$)`), |
| 21 | category: privateIPCategory, |
| 22 | }, |
| 23 | { |
| 24 | re: regexp.MustCompile(`OPENSSH PRIVATE KEY|RSA PRIVATE KEY|EC PRIVATE KEY|DSA PRIVATE KEY`), |
| 25 | category: "SSH private key material", |
| 26 | }, |
| 27 | { |
| 28 | re: regexp.MustCompile(`(?i)(?:password|token|secret|api_key|apikey|api-key)\s*[=:]`), |
| 29 | category: "credential patterns (password/token/secret/api_key)", |
| 30 | }, |
| 31 | { |
| 32 | re: regexp.MustCompile(`\.age\b.*:\s*\S`), |
| 33 | category: ".age secret file references", |
| 34 | }, |
| 35 | } |
| 36 | |
| 37 | // CheckSensitivity scans the content from r for common sensitive data patterns. |
| 38 | // Returns a list of findings. An empty list means the content is clean. |
| 39 | func CheckSensitivity(r io.Reader) []Finding { |
| 40 | return CheckSensitivityAllowing(r, nil) |
| 41 | } |
| 42 | |
| 43 | // CheckSensitivityAllowing is CheckSensitivity with an allow-list of literal |
| 44 | // hosts (typically the run's own git remotes). A private-IP match counts only |
| 45 | // when it is not one of allowedHosts; any other private address still flags. |
| 46 | // Lines of any length are scanned. If the content cannot be read to the end, |
| 47 | // a finding is reported so the caller keeps it private. |
| 48 | func CheckSensitivityAllowing(r io.Reader, allowedHosts []string) []Finding { |
| 49 | allowed := make(map[string]bool, len(allowedHosts)) |
| 50 | for _, h := range allowedHosts { |
| 51 | if h != "" { |
| 52 | allowed[h] = true |
| 53 | } |
| 54 | } |
| 55 | found := make(map[string]bool) |
| 56 | var findings []Finding |
| 57 | add := func(cat string) { |
| 58 | if !found[cat] { |
| 59 | found[cat] = true |
| 60 | findings = append(findings, Finding{Category: cat}) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | br := bufio.NewReader(r) |
| 65 | for { |
| 66 | line, err := br.ReadString('\n') |
| 67 | if line != "" { |
| 68 | for _, p := range patterns { |
| 69 | if found[p.category] { |
| 70 | continue |
| 71 | } |
| 72 | if p.category == privateIPCategory { |
| 73 | if hasDisallowedPrivateIP(line, allowed) { |
| 74 | add(p.category) |
| 75 | } |
| 76 | } else if p.re.MatchString(line) { |
| 77 | add(p.category) |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | if err == io.EOF { |
| 82 | break |
| 83 | } |
| 84 | if err != nil { |
| 85 | add(incompleteCategory) |
| 86 | break |
| 87 | } |
| 88 | } |
| 89 | return findings |
| 90 | } |
| 91 | |
| 92 | const ( |
| 93 | privateIPCategory = "private IP addresses" |
| 94 | incompleteCategory = "could not scan the whole file" |
| 95 | ) |
| 96 | |
| 97 | var privateIPRe = regexp.MustCompile(`(?:^|[^0-9])(10\.\d{1,3}\.\d{1,3}\.\d{1,3}|172\.(?:1[6-9]|2[0-9]|3[01])\.\d{1,3}\.\d{1,3}|192\.168\.\d{1,3}\.\d{1,3})(?:[^0-9]|$)`) |
| 98 | |
| 99 | // hasDisallowedPrivateIP reports whether line holds a private address that is |
| 100 | // not in allowed. Matches are found one at a time from just past the previous |
| 101 | // address, since the regexp consumes the delimiter after each one. |
| 102 | func hasDisallowedPrivateIP(line string, allowed map[string]bool) bool { |
| 103 | for len(line) > 0 { |
| 104 | loc := privateIPRe.FindStringSubmatchIndex(line) |
| 105 | if loc == nil { |
| 106 | return false |
| 107 | } |
| 108 | if !allowed[line[loc[2]:loc[3]]] { |
| 109 | return true |
| 110 | } |
| 111 | line = line[loc[3]:] |
| 112 | } |
| 113 | return false |
| 114 | } |
| 115 | |
| 116 | // RemoteHost extracts the host from a git remote URL, in scp-style |
| 117 | // (git@10.0.2.1:repo.git) or URL-style (ssh://git@10.0.2.1:22/repo.git). |
| 118 | // It returns "" when no host can be found (e.g. a local path). |
| 119 | func RemoteHost(remote string) string { |
| 120 | remote = strings.TrimSpace(remote) |
| 121 | if i := strings.Index(remote, "://"); i >= 0 { |
| 122 | rest := remote[i+3:] |
| 123 | if j := strings.IndexAny(rest, "/?#"); j >= 0 { |
| 124 | rest = rest[:j] |
| 125 | } |
| 126 | if j := strings.LastIndex(rest, "@"); j >= 0 { |
| 127 | rest = rest[j+1:] |
| 128 | } |
| 129 | if strings.HasPrefix(rest, "[") { |
| 130 | if j := strings.Index(rest, "]"); j >= 0 { |
| 131 | return rest[1:j] |
| 132 | } |
| 133 | } |
| 134 | if j := strings.Index(rest, ":"); j >= 0 { |
| 135 | rest = rest[:j] |
| 136 | } |
| 137 | return rest |
| 138 | } |
| 139 | // scp-style: [user@]host:path, where no slash precedes the colon. |
| 140 | colon := strings.Index(remote, ":") |
| 141 | if colon < 0 || strings.Contains(remote[:colon], "/") { |
| 142 | return "" |
| 143 | } |
| 144 | host := remote[:colon] |
| 145 | if j := strings.LastIndex(host, "@"); j >= 0 { |
| 146 | host = host[j+1:] |
| 147 | } |
| 148 | return strings.Trim(host, "[]") |
| 149 | } |