What this change means
Atlas added infisical to its detector catalog in this update.
| @@ -0,0 +1,170 @@ | ||
| 1 | added: package infisical | |
| 2 | added: | |
| 3 | added: import ( | |
| 4 | added: "cmp" | |
| 5 | added: "context" | |
| 6 | added: "fmt" | |
| 7 | added: "io" | |
| 8 | added: "net/http" | |
| 9 | added: "net/url" | |
| 10 | added: | |
| 11 | added: regexp "github.com/wasilibs/go-re2" | |
| 12 | added: | |
| 13 | added: "github.com/trufflesecurity/trufflehog/v3/pkg/common" | |
| 14 | added: "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" | |
| 15 | added: "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detector_typepb" | |
| 16 | added: ) | |
| 17 | added: | |
| 18 | added: type Scanner struct { | |
| 19 | added: client *http.Client | |
| 20 | added: detectors.EndpointSetter | |
| 21 | added: } | |
| 22 | added: | |
| 23 | added: // Ensure the Scanner satisfies the interfaces at compile time. | |
| 24 | added: var _ detectors.Detector = (*Scanner)(nil) | |
| 25 | added: var _ detectors.EndpointCustomizer = (*Scanner)(nil) | |
| 26 | added: var _ detectors.CloudProvider = (*Scanner)(nil) | |
| 27 | added: | |
| 28 | added: // CloudEndpoint returns the primary Infisical Cloud region (US). | |
| 29 | added: func (Scanner) CloudEndpoint() string { return "https://app.infisical.com" } | |
| 30 | added: | |
| 31 | added: // additionalCloudEndpoints lists the remaining Infisical Cloud regions. | |
| 32 | added: // Cloud regions are isolated: a token issued in one region is unknown on | |
| 33 | added: // the other host, so every region must be tried during verification. | |
| 34 | added: // Self-hosted instances are not listed here. They are verified only when | |
| 35 | added: // an operator configures their URL as a custom verification endpoint. | |
| 36 | added: // https://infisical.com/docs/cli/usage | |
| 37 | added: var additionalCloudEndpoints = []string{ | |
| 38 | added: "https://eu.infisical.com", | |
| 39 | added: } | |
| 40 | added: | |
| 41 | added: var ( | |
| 42 | added: defaultClient = common.SaneHttpClient() | |
| 43 | added: | |
| 44 | added: // Infisical service tokens have the form st.<id>.<secret>, where <id> is a | |
| 45 | added: // UUID and <secret> is 32 lowercase hex characters (crypto.randomBytes(16)). | |
| 46 | added: // See Infisical/infisical backend/src/services/service-token/service-token-service.ts. | |
| 47 | added: // The dashboard appends a fourth segment of 32 lowercase hex characters | |
| 48 | added: // before showing the token (frontend/src/hooks/api/serviceTokens/queries.tsx), | |
| 49 | added: // and the official CLI rejects tokens without it. Group 1 is the full token | |
| 50 | added: // as found. Group 2 is the three-part form that authenticates to the API. | |
| 51 | added: keyPat = regexp.MustCompile(`\b((st\.[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\.[0-9a-f]{32})(?:\.[0-9a-f]{32})?)\b`) | |
| 52 | added: ) | |
| 53 | added: | |
| 54 | added: // Keywords are used for efficiently pre-filtering chunks. | |
| 55 | added: // Use identifiers in the secret preferably, or the provider name. | |
| 56 | added: func (s Scanner) Keywords() []string { | |
| 57 | added: return []string{"st."} | |
| 58 | added: } | |
| 59 | added: | |
| 60 | added: // FromData will find and optionally verify Infisical service tokens in a given set of bytes. | |
| 61 | added: func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { | |
| 62 | added: dataStr := string(data) | |
| 63 | added: | |
| 64 | added: // Map each full token to the three-part form sent for verification. | |
| 65 | added: uniqueMatches := make(map[string]string) | |
| 66 | added: for _, match := range keyPat.FindAllStringSubmatch(dataStr, -1) { | |
| 67 | added: uniqueMatches[match[1]] = match[2] | |
| 68 | added: } | |
| 69 | added: | |
| 70 | added: for resMatch, authToken := range uniqueMatches { | |
| 71 | added: s1 := detectors.Result{ | |
| 72 | added: DetectorType: detector_typepb.DetectorType_Infisical, | |
| 73 | added: Raw: []byte(resMatch), | |
| 74 | added: SecretParts: map[string]string{"key": resMatch}, | |
| 75 | added: } | |
| 76 | added: | |
| 77 | added: if verify { | |
| 78 | added: client := s.VerificationClient(cmp.Or(s.client, defaultClient)) | |
| 79 | added: | |
| 80 | added: // Verification targets the Infisical Cloud regions (US and EU) | |
| 81 | added: // plus any operator-configured endpoints, such as self-hosted | |
| 82 | added: // instances. URLs in the scanned data are never used. | |
| 83 | added: // | |
| 84 | added: // Only non-nil errors overwrite lastErr so that a clean 404 from | |
| 85 | added: // the wrong region doesn't erase a transient failure (429, 5xx, | |
| 86 | added: // timeout) from a region that might be authoritative. | |
| 87 | added: var lastErr error | |
| 88 | added: for _, baseURL := range s.Endpoints(additionalCloudEndpoints...) { | |
| 89 | added: isVerified, vErr := verifyServiceToken(ctx, client, baseURL, authToken) | |
| 90 | added: s1.Verified = isVerified | |
| 91 | added: if vErr != nil { | |
| 92 | added: lastErr = vErr | |
| 93 | added: } | |
| 94 | added: if isVerified { | |
| 95 | added: lastErr = nil | |
| 96 | added: s1.ExtraData = map[string]string{"endpoint": baseURL} | |
| 97 | added: break | |
| 98 | added: } | |
| 99 | added: } | |
| 100 | added: s1.SetVerificationError(lastErr, resMatch) | |
| 101 | added: } | |
| 102 | added: | |
| 103 | added: results = append(results, s1) | |
| 104 | added: } | |
| 105 | added: | |
| 106 | added: return results, nil | |
| 107 | added: } | |
| 108 | added: | |
| 109 | added: // verifyServiceToken checks a three-part service token against one Infisical | |
| 110 | added: // instance. It calls GET /api/v2/service-token, which returns only the | |
| 111 | added: // token's metadata. The official CLI calls the same route to read token | |
| 112 | added: // details. Do not switch to /api/v3/secrets/raw: for a token with one exact | |
| 113 | added: // scope, that route returns the plaintext secret values and writes a secret | |
| 114 | added: // read to the owner's audit log. | |
| 115 | added: // | |
| 116 | added: // The first return value reports whether the token verified. The error is | |
| 117 | added: // non-nil only when verification is indeterminate (the token's validity | |
| 118 | added: // could not be established from this response); the caller records it via | |
| 119 | added: // Result.SetVerificationError so an ambiguous result is not silently | |
| 120 | added: // dropped as a determinate miss. | |
| 121 | added: func verifyServiceToken(ctx context.Context, client *http.Client, baseURL, token string) (bool, error) { | |
| 122 | added: endpoint, err := url.JoinPath(baseURL, "/api/v2/service-token") | |
| 123 | added: if err != nil { | |
| 124 | added: return false, err | |
| 125 | added: } | |
| 126 | added: | |
| 127 | added: req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil) | |
| 128 | added: if err != nil { | |
| 129 | added: return false, err | |
| 130 | added: } | |
| 131 | added: req.Header.Set("Authorization", "Bearer "+token) | |
| 132 | added: | |
| 133 | added: res, err := client.Do(req) | |
| 134 | added: if err != nil { | |
| 135 | added: return false, err | |
| 136 | added: } | |
| 137 | added: defer func() { | |
| 138 | added: _, _ = io.Copy(io.Discard, res.Body) | |
| 139 | added: _ = res.Body.Close() | |
| 140 | added: }() | |
| 141 | added: | |
| 142 | added: switch { | |
| 143 | added: case res.StatusCode == http.StatusOK: | |
| 144 | added: return true, nil | |
| 145 | added: case res.StatusCode == http.StatusUnauthorized, res.StatusCode == http.StatusNotFound: | |
| 146 | added: // Determinately invalid: the instance does not recognize this token. | |
| 147 | added: return false, nil | |
| 148 | added: case res.StatusCode == http.StatusForbidden: | |
| 149 | added: // Ambiguous: Infisical returns 403 for an expired token, and a proxy | |
| 150 | added: // or WAF in front of the instance can reject requests for non-auth | |
| 151 | added: // reasons. Do not read this as "not a secret". | |
| 152 | added: return false, fmt.Errorf("forbidden response (HTTP 403): token validity could not be determined") | |
| 153 | added: case res.StatusCode == http.StatusTooManyRequests: | |
| 154 | added: // Transient rate limit; retrying later may verify the token. | |
| 155 | added: return false, fmt.Errorf("rate limited (HTTP 429): token validity could not be determined") | |
| 156 | added: case res.StatusCode >= 500: | |
| 157 | added: // Transient server failure; the token's validity is unknown. | |
| 158 | added: return false, fmt.Errorf("server error (HTTP %d): token validity could not be determined", res.StatusCode) | |
| 159 | added: default: | |
| 160 | added: return false, fmt.Errorf("unexpected HTTP response status %d", res.StatusCode) | |
| 161 | added: } | |
| 162 | added: } | |
| 163 | added: | |
| 164 | added: func (s Scanner) Type() detector_typepb.DetectorType { | |
| 165 | added: return detector_typepb.DetectorType_Infisical | |
| 166 | added: } | |
| 167 | added: | |
| 168 | added: func (s Scanner) Description() string { | |
| 169 | added: return "Infisical is an open-source secrets management platform. Service tokens can be used to read and write secrets within their scoped environments and paths." | |
| 170 | added: } | |