What this change means
Atlas added sonarqube to its detector catalog in this update.
| @@ -0,0 +1,185 @@ | ||
| 1 | added: package sonarqube | |
| 2 | added: | |
| 3 | added: import ( | |
| 4 | added: "context" | |
| 5 | added: "encoding/json" | |
| 6 | added: "errors" | |
| 7 | added: "fmt" | |
| 8 | added: "io" | |
| 9 | added: "net/http" | |
| 10 | added: "slices" | |
| 11 | added: "strings" | |
| 12 | added: | |
| 13 | added: regexp "github.com/wasilibs/go-re2" | |
| 14 | added: | |
| 15 | added: "github.com/trufflesecurity/trufflehog/v3/pkg/common" | |
| 16 | added: "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" | |
| 17 | added: "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detector_typepb" | |
| 18 | added: "github.com/trufflesecurity/trufflehog/v3/pkg/ssrf" | |
| 19 | added: ) | |
| 20 | added: | |
| 21 | added: // Scanner finds SonarQube Server tokens: user (squ_), global analysis (sqa_) | |
| 22 | added: // and project analysis (sqp_). A token has no host in it, so it is verified | |
| 23 | added: // against endpoints the user configured and hosts found in the scanned data | |
| 24 | added: // (see findEndpoints). | |
| 25 | added: type Scanner struct { | |
| 26 | added: detectors.DefaultMultiPartCredentialProvider | |
| 27 | added: detectors.EndpointSetter | |
| 28 | added: client *http.Client | |
| 29 | added: } | |
| 30 | added: | |
| 31 | added: // Ensure the Scanner satisfies the interface at compile time. | |
| 32 | added: var _ detectors.Detector = (*Scanner)(nil) | |
| 33 | added: var _ detectors.EndpointCustomizer = (*Scanner)(nil) | |
| 34 | added: | |
| 35 | added: // There is no public SonarQube Server, so there is no cloud endpoint. | |
| 36 | added: func (Scanner) CloudEndpoint() string { return "" } | |
| 37 | added: | |
| 38 | added: var ( | |
| 39 | added: defaultClient = common.SaneHttpClient() | |
| 40 | added: keyPat = regexp.MustCompile(`\b(sq[apu]_[0-9a-f]{40})\b`) | |
| 41 | added: | |
| 42 | added: // Host after a sonar config key, any port, for example | |
| 43 | added: // sonar.host.url=https://sonar.example.com or SONAR_HOST_URL: sonar.example.com:8080 | |
| 44 | added: hostURLPat = regexp.MustCompile(`(?i)\bsonar[._](?:host[._]url|host|url)\b["'\s:=,>]*((?:https?://)?[a-z0-9](?:[a-z0-9.-]*[a-z0-9])?(?::\d{1,5})?)`) | |
| 45 | added: ) | |
| 46 | added: | |
| 47 | added: // Keywords are used for efficiently pre-filtering chunks. | |
| 48 | added: func (s Scanner) Keywords() []string { | |
| 49 | added: return []string{"squ_", "sqa_", "sqp_"} | |
| 50 | added: } | |
| 51 | added: | |
| 52 | added: // findEndpoints returns the hosts in the data named by a sonar config key, | |
| 53 | added: // each one once. | |
| 54 | added: func findEndpoints(data string) []string { | |
| 55 | added: var endpoints []string | |
| 56 | added: seen := make(map[string]struct{}) | |
| 57 | added: for _, match := range hostURLPat.FindAllStringSubmatch(data, -1) { | |
| 58 | added: if _, ok := seen[match[1]]; !ok { | |
| 59 | added: seen[match[1]] = struct{}{} | |
| 60 | added: endpoints = append(endpoints, match[1]) | |
| 61 | added: } | |
| 62 | added: } | |
| 63 | added: return endpoints | |
| 64 | added: } | |
| 65 | added: | |
| 66 | added: // FromData will find and optionally verify SonarQube secrets in a given set of bytes. | |
| 67 | added: func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { | |
| 68 | added: uniqueMatches := make(map[string]struct{}) | |
| 69 | added: for _, match := range keyPat.FindAllStringSubmatch(string(data), -1) { | |
| 70 | added: uniqueMatches[match[1]] = struct{}{} | |
| 71 | added: } | |
| 72 | added: | |
| 73 | added: configured := s.Endpoints() | |
| 74 | added: endpoints := s.Endpoints(findEndpoints(string(data))...) | |
| 75 | added: | |
| 76 | added: for match := range uniqueMatches { | |
| 77 | added: result := detectors.Result{ | |
| 78 | added: DetectorType: detector_typepb.DetectorType_SonarQube, | |
| 79 | added: Raw: []byte(match), | |
| 80 | added: SecretParts: map[string]string{"key": match}, | |
| 81 | added: } | |
| 82 | added: | |
| 83 | added: // nowhere to verify, tell the user how to fix that | |
| 84 | added: if len(endpoints) == 0 { | |
| 85 | added: result.ExtraData = map[string]string{ | |
| 86 | added: "message": "No SonarQube Server URL was found or configured. To verify this token, set the server URL as a custom endpoint, e.g. --verifier SonarQube=https://sonar.example.com", | |
| 87 | added: } | |
| 88 | added: } | |
| 89 | added: | |
| 90 | added: if verify { | |
| 91 | added: // try each endpoint, stop at the first one that accepts the token | |
| 92 | added: var verifyErr error | |
| 93 | added: for _, endpoint := range endpoints { | |
| 94 | added: // Hosts found in scanned data are not trusted, so they get a client | |
| 95 | added: // that refuses local and private addresses. | |
| 96 | added: client := s.client | |
| 97 | added: if client == nil { | |
| 98 | added: client = s.VerificationClient(defaultClient) | |
| 99 | added: if !slices.Contains(configured, endpoint) { | |
| 100 | added: client = detectors.DetectorHttpClientWithNoLocalAddresses | |
| 101 | added: } | |
| 102 | added: } | |
| 103 | added: | |
| 104 | added: isVerified, extraData, err := verifyMatch(ctx, client, endpoint, match) | |
| 105 | added: // a found host like localhost is the developer's machine, not ours, skip it | |
| 106 | added: if errors.Is(err, ssrf.ErrEgressBlocked) { | |
| 107 | added: continue | |
| 108 | added: } | |
| 109 | added: if err != nil { | |
| 110 | added: verifyErr = err | |
| 111 | added: continue | |
| 112 | added: } | |
| 113 | added: if isVerified { | |
| 114 | added: result.Verified = true | |
| 115 | added: result.ExtraData = extraData | |
| 116 | added: verifyErr = nil | |
| 117 | added: break | |
| 118 | added: } | |
| 119 | added: } | |
| 120 | added: result.SetVerificationError(verifyErr, match) | |
| 121 | added: } | |
| 122 | added: | |
| 123 | added: results = append(results, result) | |
| 124 | added: } | |
| 125 | added: | |
| 126 | added: return | |
| 127 | added: } | |
| 128 | added: | |
| 129 | added: type currentUserResponse struct { | |
| 130 | added: IsLoggedIn bool `json:"isLoggedIn"` | |
| 131 | added: Login string `json:"login"` | |
| 132 | added: } | |
| 133 | added: | |
| 134 | added: // verifyMatch asks the server who the token belongs to. | |
| 135 | added: // The token goes in the basic auth username with an empty password. | |
| 136 | added: // A wrong token gets a 401. We check isLoggedIn too, because a server | |
| 137 | added: // that allows anonymous access answers 200 for any request. | |
| 138 | added: func verifyMatch(ctx context.Context, client *http.Client, endpoint, token string) (bool, map[string]string, error) { | |
| 139 | added: // users pass "host:9000" or "http://host:9000", default to https | |
| 140 | added: if !strings.Contains(endpoint, "://") { | |
| 141 | added: endpoint = "https://" + endpoint | |
| 142 | added: } | |
| 143 | added: url := strings.TrimRight(endpoint, "/") + "/api/users/current" | |
| 144 | added: | |
| 145 | added: req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) | |
| 146 | added: if err != nil { | |
| 147 | added: return false, nil, err | |
| 148 | added: } | |
| 149 | added: req.SetBasicAuth(token, "") | |
| 150 | added: req.Header.Set("Accept", "application/json") | |
| 151 | added: | |
| 152 | added: res, err := client.Do(req) | |
| 153 | added: if err != nil { | |
| 154 | added: return false, nil, err | |
| 155 | added: } | |
| 156 | added: defer func() { | |
| 157 | added: _, _ = io.Copy(io.Discard, res.Body) | |
| 158 | added: _ = res.Body.Close() | |
| 159 | added: }() | |
| 160 | added: | |
| 161 | added: switch res.StatusCode { | |
| 162 | added: case http.StatusOK: | |
| 163 | added: var user currentUserResponse | |
| 164 | added: if err := json.NewDecoder(res.Body).Decode(&user); err != nil { | |
| 165 | added: return false, nil, fmt.Errorf("failed to decode response: %w", err) | |
| 166 | added: } | |
| 167 | added: if !user.IsLoggedIn { | |
| 168 | added: return false, nil, nil | |
| 169 | added: } | |
| 170 | added: return true, map[string]string{"endpoint": endpoint, "login": user.Login}, nil | |
| 171 | added: case http.StatusUnauthorized: | |
| 172 | added: // the token is definitely not valid on this server | |
| 173 | added: return false, nil, nil | |
| 174 | added: default: | |
| 175 | added: return false, nil, fmt.Errorf("unexpected HTTP response status %d", res.StatusCode) | |
| 176 | added: } | |
| 177 | added: } | |
| 178 | added: | |
| 179 | added: func (s Scanner) Type() detector_typepb.DetectorType { | |
| 180 | added: return detector_typepb.DetectorType_SonarQube | |
| 181 | added: } | |
| 182 | added: | |
| 183 | added: func (s Scanner) Description() string { | |
| 184 | added: return "SonarQube Server is a code quality and security analysis platform. Its user, global analysis and project analysis tokens authenticate people and CI pipelines to the SonarQube API." | |
| 185 | added: } | |