What this change means
Atlas added composio to its detector catalog in this update.
| @@ -0,0 +1,269 @@ | ||
| 1 | added: package composio | |
| 2 | added: | |
| 3 | added: import ( | |
| 4 | added: "cmp" | |
| 5 | added: "context" | |
| 6 | added: "encoding/json" | |
| 7 | added: "fmt" | |
| 8 | added: "io" | |
| 9 | added: "net/http" | |
| 10 | added: "strings" | |
| 11 | added: | |
| 12 | added: regexp "github.com/wasilibs/go-re2" | |
| 13 | added: | |
| 14 | added: "github.com/trufflesecurity/trufflehog/v3/pkg/common" | |
| 15 | added: "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" | |
| 16 | added: "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detector_typepb" | |
| 17 | added: ) | |
| 18 | added: | |
| 19 | added: type Scanner struct { | |
| 20 | added: client *http.Client | |
| 21 | added: detectors.EndpointSetter | |
| 22 | added: } | |
| 23 | added: | |
| 24 | added: // Ensure the Scanner satisfies the interfaces at compile time. | |
| 25 | added: var _ detectors.Detector = (*Scanner)(nil) | |
| 26 | added: var _ detectors.EndpointCustomizer = (*Scanner)(nil) | |
| 27 | added: var _ detectors.CloudProvider = (*Scanner)(nil) | |
| 28 | added: | |
| 29 | added: func (Scanner) CloudEndpoint() string { return "https://backend.composio.dev" } | |
| 30 | added: | |
| 31 | added: var defaultClient = common.SaneHttpClient() | |
| 32 | added: | |
| 33 | added: // getClient returns the client verification requests go through. The | |
| 34 | added: // VerificationClient wrap is what attaches the operator's access token when | |
| 35 | added: // --verifier points at an auth-protected endpoint; without it those requests | |
| 36 | added: // go out unauthenticated and every key comes back looking dead. | |
| 37 | added: func (s Scanner) getClient() *http.Client { | |
| 38 | added: return s.VerificationClient(cmp.Or(s.client, defaultClient)) | |
| 39 | added: } | |
| 40 | added: | |
| 41 | added: const rotationGuide = "https://docs.composio.dev/reference/api-reference/api-keys/postApiKeyRevocation" | |
| 42 | added: | |
| 43 | added: // maxErrorBodyBytes bounds how much of an error response is read. Composio's | |
| 44 | added: // error envelope is a few hundred bytes; anything larger is not one. | |
| 45 | added: const maxErrorBodyBytes = 64 * 1024 | |
| 46 | added: | |
| 47 | added: // Composio issues three key types, all a fixed prefix followed by URL-safe | |
| 48 | added: // nanoid characters. oak_ and uak_ contain "ak_", so every pattern requires a | |
| 49 | added: // non-alphabet character (or start of input) before the prefix. The trailing | |
| 50 | added: // boundary is checked in code rather than in the pattern: a consuming trailing | |
| 51 | added: // group would swallow the single delimiter between two adjacent keys and drop | |
| 52 | added: // the second one. | |
| 53 | added: // | |
| 54 | added: // Each key type authenticates through its own header and its own routes. A key | |
| 55 | added: // sent under another type's header is answered with the same 401 as no header | |
| 56 | added: // at all, so verification cannot share one endpoint across types: every type | |
| 57 | added: // carries the cheapest read-only route it can authenticate. | |
| 58 | added: type keyPattern struct { | |
| 59 | added: keyType string | |
| 60 | added: re *regexp.Regexp | |
| 61 | added: verifyPath string | |
| 62 | added: header string | |
| 63 | added: } | |
| 64 | added: | |
| 65 | added: var keyPatterns = []keyPattern{ | |
| 66 | added: { | |
| 67 | added: keyType: "project", | |
| 68 | added: re: regexp.MustCompile(`(?:^|[^A-Za-z0-9_-])(ak_[A-Za-z0-9_-]{20})`), | |
| 69 | added: verifyPath: "/api/v3/toolkits/categories", | |
| 70 | added: header: "x-api-key", | |
| 71 | added: }, | |
| 72 | added: { | |
| 73 | added: keyType: "org", | |
| 74 | added: re: regexp.MustCompile(`(?:^|[^A-Za-z0-9_-])(oak_[A-Za-z0-9_-]{20})`), | |
| 75 | added: verifyPath: "/api/v3/org/owner/project/list", | |
| 76 | added: header: "x-org-api-key", | |
| 77 | added: }, | |
| 78 | added: { | |
| 79 | added: keyType: "user", | |
| 80 | added: re: regexp.MustCompile(`(?:^|[^A-Za-z0-9_-])(uak_[A-Za-z0-9_-]{43})`), | |
| 81 | added: verifyPath: "/api/v3/org/list?limit=1", | |
| 82 | added: header: "x-user-api-key", | |
| 83 | added: }, | |
| 84 | added: } | |
| 85 | added: | |
| 86 | added: // isKeyAlphabet reports whether b can appear inside a Composio key. | |
| 87 | added: func isKeyAlphabet(b byte) bool { | |
| 88 | added: return (b >= 'A' && b <= 'Z') || (b >= 'a' && b <= 'z') || (b >= '0' && b <= '9') || b == '_' || b == '-' | |
| 89 | added: } | |
| 90 | added: | |
| 91 | added: // endsAtBoundary reports whether the key ending at end is followed by end of | |
| 92 | added: // input or a character that cannot be part of a key. | |
| 93 | added: func endsAtBoundary(data string, end int) bool { | |
| 94 | added: return end >= len(data) || !isKeyAlphabet(data[end]) | |
| 95 | added: } | |
| 96 | added: | |
| 97 | added: // Keywords are used for efficiently pre-filtering chunks. "ak_" is a substring | |
| 98 | added: // of all three prefixes. | |
| 99 | added: func (s Scanner) Keywords() []string { | |
| 100 | added: return []string{"ak_"} | |
| 101 | added: } | |
| 102 | added: | |
| 103 | added: // FromData finds Composio API keys in a given set of bytes and, when asked, | |
| 104 | added: // verifies each one against the Composio API. | |
| 105 | added: func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { | |
| 106 | added: dataStr := string(data) | |
| 107 | added: | |
| 108 | added: seen := make(map[string]struct{}) | |
| 109 | added: for _, pattern := range keyPatterns { | |
| 110 | added: for _, loc := range pattern.re.FindAllStringSubmatchIndex(dataStr, -1) { | |
| 111 | added: start, end := loc[2], loc[3] | |
| 112 | added: if !endsAtBoundary(dataStr, end) { | |
| 113 | added: continue | |
| 114 | added: } | |
| 115 | added: token := dataStr[start:end] | |
| 116 | added: if _, dup := seen[token]; dup { | |
| 117 | added: continue | |
| 118 | added: } | |
| 119 | added: seen[token] = struct{}{} | |
| 120 | added: | |
| 121 | added: r := detectors.Result{ | |
| 122 | added: DetectorType: detector_typepb.DetectorType_Composio, | |
| 123 | added: Raw: []byte(token), | |
| 124 | added: SecretParts: map[string]string{"key": token}, | |
| 125 | added: ExtraData: map[string]string{ | |
| 126 | added: "key_type": pattern.keyType, | |
| 127 | added: "rotation_guide": rotationGuide, | |
| 128 | added: }, | |
| 129 | added: } | |
| 130 | added: | |
| 131 | added: if verify { | |
| 132 | added: client := s.getClient() | |
| 133 | added: | |
| 134 | added: // Only the cloud endpoint and operator-configured endpoints are | |
| 135 | added: // used; URLs found in scanned data are never called. Only | |
| 136 | added: // non-nil errors overwrite lastErr so a clean 401 from one | |
| 137 | added: // endpoint does not erase a transient failure from another. | |
| 138 | added: var lastErr error | |
| 139 | added: for _, baseURL := range s.Endpoints() { | |
| 140 | added: verified, extra, vErr := verifyMatch(ctx, client, baseURL, pattern, token) | |
| 141 | added: r.Verified = verified | |
| 142 | added: if vErr != nil { | |
| 143 | added: lastErr = vErr | |
| 144 | added: } | |
| 145 | added: if verified { | |
| 146 | added: lastErr = nil | |
| 147 | added: r.ExtraData["endpoint"] = baseURL | |
| 148 | added: for k, v := range extra { | |
| 149 | added: r.ExtraData[k] = v | |
| 150 | added: } | |
| 151 | added: break | |
| 152 | added: } | |
| 153 | added: } | |
| 154 | added: r.SetVerificationError(lastErr, token) | |
| 155 | added: } | |
| 156 | added: | |
| 157 | added: results = append(results, r) | |
| 158 | added: } | |
| 159 | added: } | |
| 160 | added: | |
| 161 | added: return results, nil | |
| 162 | added: } | |
| 163 | added: | |
| 164 | added: // apiError is the envelope every Composio API error carries. The slug is | |
| 165 | added: // stable across releases; the message is human copy. | |
| 166 | added: type apiError struct { | |
| 167 | added: Error struct { | |
| 168 | added: Message string `json:"message"` | |
| 169 | added: Slug string `json:"slug"` | |
| 170 | added: Code int `json:"code"` | |
| 171 | added: } `json:"error"` | |
| 172 | added: } | |
| 173 | added: | |
| 174 | added: // verifyMatch sends the key on the route its type can authenticate and maps | |
| 175 | added: // the answer to verified, dead, or undecided. Composio deliberately answers | |
| 176 | added: // several unrelated conditions with 401, so the verdict comes from the error | |
| 177 | added: // slug in the body, never from the status code alone: | |
| 178 | added: // | |
| 179 | added: // - 200: live. | |
| 180 | added: // - 401 APIKey_InvalidAPIKey, APIKey_APIKeyExpired, UserApiKey_Unauthorized, | |
| 181 | added: // and HTTP_Unauthorized "Invalid x-org-api-key": the hash matched no live | |
| 182 | added: // row. Dead, no error. | |
| 183 | added: // - 401 Auth_Unauthorized naming the key's IP allowlist: the key exists and | |
| 184 | added: // works from allowlisted addresses. Live, reported with the restriction. | |
| 185 | added: // - 403 carrying the envelope: Composio only refuses with 403 after the | |
| 186 | added: // credential resolved (a scoped key without the route's permission, an | |
| 187 | added: // org-level restriction). Live, reported with the restriction. | |
| 188 | added: // - Anything else (a banned scanner address answers "Access denied", a | |
| 189 | added: // stripped header answers Auth_NoAuthProvided, a WAF page has no | |
| 190 | added: // envelope, 429, 5xx): undecided, surfaced as a verification error. | |
| 191 | added: // | |
| 192 | added: // The org key's dead slug HTTP_Unauthorized is shared with the banned-address | |
| 193 | added: // answer, so that one branch also reads the message; a copy change there | |
| 194 | added: // degrades to an undecided error, never to a wrong verdict. | |
| 195 | added: func verifyMatch( | |
| 196 | added: ctx context.Context, | |
| 197 | added: client *http.Client, | |
| 198 | added: baseURL string, | |
| 199 | added: pattern keyPattern, | |
| 200 | added: token string, | |
| 201 | added: ) (bool, map[string]string, error) { | |
| 202 | added: req, err := http.NewRequestWithContext(ctx, http.MethodGet, strings.TrimRight(baseURL, "/")+pattern.verifyPath, nil) | |
| 203 | added: if err != nil { | |
| 204 | added: return false, nil, err | |
| 205 | added: } | |
| 206 | added: req.Header.Set(pattern.header, token) | |
| 207 | added: req.Header.Set("Accept", "application/json") | |
| 208 | added: | |
| 209 | added: res, err := client.Do(req) | |
| 210 | added: if err != nil { | |
| 211 | added: return false, nil, err | |
| 212 | added: } | |
| 213 | added: defer func() { | |
| 214 | added: _, _ = io.Copy(io.Discard, res.Body) | |
| 215 | added: _ = res.Body.Close() | |
| 216 | added: }() | |
| 217 | added: | |
| 218 | added: switch res.StatusCode { | |
| 219 | added: case http.StatusOK: | |
| 220 | added: return true, nil, nil | |
| 221 | added: | |
| 222 | added: case http.StatusUnauthorized, http.StatusForbidden: | |
| 223 | added: var body apiError | |
| 224 | added: dec := json.NewDecoder(io.LimitReader(res.Body, maxErrorBodyBytes)) | |
| 225 | added: if err := dec.Decode(&body); err != nil || body.Error.Slug == "" { | |
| 226 | added: return false, nil, fmt.Errorf("HTTP %d without a Composio error envelope", res.StatusCode) | |
| 227 | added: } | |
| 228 | added: return classifyRejection(res.StatusCode, body) | |
| 229 | added: | |
| 230 | added: default: | |
| 231 | added: return false, nil, fmt.Errorf("unexpected HTTP response status %d", res.StatusCode) | |
| 232 | added: } | |
| 233 | added: } | |
| 234 | added: | |
| 235 | added: func classifyRejection(status int, body apiError) (bool, map[string]string, error) { | |
| 236 | added: slug, message := body.Error.Slug, body.Error.Message | |
| 237 | added: | |
| 238 | added: switch { | |
| 239 | added: case status == http.StatusUnauthorized && (slug == "APIKey_InvalidAPIKey" || | |
| 240 | added: slug == "APIKey_APIKeyExpired" || slug == "UserApiKey_Unauthorized"): | |
| 241 | added: return false, nil, nil | |
| 242 | added: | |
| 243 | added: case status == http.StatusUnauthorized && slug == "HTTP_Unauthorized" && | |
| 244 | added: strings.HasPrefix(message, "Invalid x-org-api-key"): | |
| 245 | added: return false, nil, nil | |
| 246 | added: | |
| 247 | added: case status == http.StatusUnauthorized && slug == "Auth_Unauthorized" && strings.Contains(message, "IP allowlist"): | |
| 248 | added: return true, map[string]string{"restriction": "ip_allowlist"}, nil | |
| 249 | added: | |
| 250 | added: case status == http.StatusForbidden && slug == "APIKey_InsufficientPermissions": | |
| 251 | added: return true, map[string]string{"restriction": "insufficient_permissions"}, nil | |
| 252 | added: | |
| 253 | added: case status == http.StatusForbidden: | |
| 254 | added: return true, map[string]string{"restriction": slug}, nil | |
| 255 | added: | |
| 256 | added: default: | |
| 257 | added: return false, nil, fmt.Errorf("HTTP %d %s did not judge the key: %s", status, slug, message) | |
| 258 | added: } | |
| 259 | added: } | |
| 260 | added: | |
| 261 | added: func (s Scanner) Type() detector_typepb.DetectorType { | |
| 262 | added: return detector_typepb.DetectorType_Composio | |
| 263 | added: } | |
| 264 | added: | |
| 265 | added: func (s Scanner) Description() string { | |
| 266 | added: return "Composio is an integration platform for AI agents. Composio API " + | |
| 267 | added: "keys authenticate calls that execute tools, read connected accounts " + | |
| 268 | added: "and manage triggers for a project, an organization or a user." | |
| 269 | added: } | |