Repository navigation
feat(detectors): add Infisical service token detector #5445
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dustin-decker
merged 4 commits into
trufflesecurity:main
from
Varshith-Kali:feat/infisical-detector-5444
Oct 24, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
43e2b9e
feat(detectors): add Infisical service token detector
Varshith-Kali 84b1917
fix(detectors): make Infisical verification indeterminate-aware and r…
Varshith-Kali 1905401
fix(detectors): verify Infisical service tokens via metadata endpoint
dustin-decker fdc4a39
Merge remote-tracking branch 'origin/main' into fix-infisical-5445
dustin-decker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| package infisical | ||
|
|
||
| import ( | ||
| "cmp" | ||
| "context" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "net/url" | ||
|
|
||
| regexp "github.com/wasilibs/go-re2" | ||
|
|
||
| "github.com/trufflesecurity/trufflehog/v3/pkg/common" | ||
| "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" | ||
| "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detector_typepb" | ||
| ) | ||
|
|
||
| type Scanner struct { | ||
| client *http.Client | ||
| detectors.EndpointSetter | ||
| } | ||
|
|
||
| // Ensure the Scanner satisfies the interfaces at compile time. | ||
| var _ detectors.Detector = (*Scanner)(nil) | ||
| var _ detectors.EndpointCustomizer = (*Scanner)(nil) | ||
| var _ detectors.CloudProvider = (*Scanner)(nil) | ||
|
|
||
| // CloudEndpoint returns the primary Infisical Cloud region (US). | ||
| func (Scanner) CloudEndpoint() string { return "https://app.infisical.com" } | ||
|
|
||
| // additionalCloudEndpoints lists the remaining Infisical Cloud regions. | ||
| // Cloud regions are isolated: a token issued in one region is unknown on | ||
| // the other host, so every region must be tried during verification. | ||
| // Self-hosted instances are not listed here. They are verified only when | ||
| // an operator configures their URL as a custom verification endpoint. | ||
| // https://infisical.com/docs/cli/usage | ||
| var additionalCloudEndpoints = []string{ | ||
| "https://eu.infisical.com", | ||
| } | ||
|
|
||
| var ( | ||
| defaultClient = common.SaneHttpClient() | ||
|
|
||
| // Infisical service tokens have the form st.<id>.<secret>, where <id> is a | ||
| // UUID and <secret> is 32 lowercase hex characters (crypto.randomBytes(16)). | ||
| // See Infisical/infisical backend/src/services/service-token/service-token-service.ts. | ||
| // The dashboard appends a fourth segment of 32 lowercase hex characters | ||
| // before showing the token (frontend/src/hooks/api/serviceTokens/queries.tsx), | ||
| // and the official CLI rejects tokens without it. Group 1 is the full token | ||
| // as found. Group 2 is the three-part form that authenticates to the API. | ||
| 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`) | ||
| ) | ||
|
|
||
| // Keywords are used for efficiently pre-filtering chunks. | ||
| // Use identifiers in the secret preferably, or the provider name. | ||
| func (s Scanner) Keywords() []string { | ||
| return []string{"st."} | ||
| } | ||
|
|
||
| // FromData will find and optionally verify Infisical service tokens in a given set of bytes. | ||
| func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { | ||
| dataStr := string(data) | ||
|
|
||
| // Map each full token to the three-part form sent for verification. | ||
| uniqueMatches := make(map[string]string) | ||
| for _, match := range keyPat.FindAllStringSubmatch(dataStr, -1) { | ||
| uniqueMatches[match[1]] = match[2] | ||
| } | ||
|
|
||
| for resMatch, authToken := range uniqueMatches { | ||
| s1 := detectors.Result{ | ||
| DetectorType: detector_typepb.DetectorType_Infisical, | ||
| Raw: []byte(resMatch), | ||
| SecretParts: map[string]string{"key": resMatch}, | ||
| } | ||
|
|
||
| if verify { | ||
| client := s.VerificationClient(cmp.Or(s.client, defaultClient)) | ||
|
|
||
| // Verification targets the Infisical Cloud regions (US and EU) | ||
| // plus any operator-configured endpoints, such as self-hosted | ||
| // instances. URLs in the scanned data are never used. | ||
| // | ||
| // Only non-nil errors overwrite lastErr so that a clean 404 from | ||
| // the wrong region doesn't erase a transient failure (429, 5xx, | ||
| // timeout) from a region that might be authoritative. | ||
| var lastErr error | ||
| for _, baseURL := range s.Endpoints(additionalCloudEndpoints...) { | ||
| isVerified, vErr := verifyServiceToken(ctx, client, baseURL, authToken) | ||
| s1.Verified = isVerified | ||
| if vErr != nil { | ||
| lastErr = vErr | ||
| } | ||
| if isVerified { | ||
| lastErr = nil | ||
| s1.ExtraData = map[string]string{"endpoint": baseURL} | ||
| break | ||
| } | ||
| } | ||
| s1.SetVerificationError(lastErr, resMatch) | ||
| } | ||
|
|
||
| results = append(results, s1) | ||
| } | ||
|
|
||
| return results, nil | ||
| } | ||
|
|
||
| // verifyServiceToken checks a three-part service token against one Infisical | ||
| // instance. It calls GET /api/v2/service-token, which returns only the | ||
| // token's metadata. The official CLI calls the same route to read token | ||
| // details. Do not switch to /api/v3/secrets/raw: for a token with one exact | ||
| // scope, that route returns the plaintext secret values and writes a secret | ||
| // read to the owner's audit log. | ||
| // | ||
| // The first return value reports whether the token verified. The error is | ||
| // non-nil only when verification is indeterminate (the token's validity | ||
| // could not be established from this response); the caller records it via | ||
| // Result.SetVerificationError so an ambiguous result is not silently | ||
| // dropped as a determinate miss. | ||
| func verifyServiceToken(ctx context.Context, client *http.Client, baseURL, token string) (bool, error) { | ||
| endpoint, err := url.JoinPath(baseURL, "/api/v2/service-token") | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
|
|
||
| req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| req.Header.Set("Authorization", "Bearer "+token) | ||
|
|
||
| res, err := client.Do(req) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| defer func() { | ||
| _, _ = io.Copy(io.Discard, res.Body) | ||
| _ = res.Body.Close() | ||
| }() | ||
|
|
||
| switch { | ||
| case res.StatusCode == http.StatusOK: | ||
| return true, nil | ||
| case res.StatusCode == http.StatusUnauthorized, res.StatusCode == http.StatusNotFound: | ||
| // Determinately invalid: the instance does not recognize this token. | ||
| return false, nil | ||
| case res.StatusCode == http.StatusForbidden: | ||
| // Ambiguous: Infisical returns 403 for an expired token, and a proxy | ||
| // or WAF in front of the instance can reject requests for non-auth | ||
| // reasons. Do not read this as "not a secret". | ||
| return false, fmt.Errorf("forbidden response (HTTP 403): token validity could not be determined") | ||
| case res.StatusCode == http.StatusTooManyRequests: | ||
| // Transient rate limit; retrying later may verify the token. | ||
| return false, fmt.Errorf("rate limited (HTTP 429): token validity could not be determined") | ||
| case res.StatusCode >= 500: | ||
| // Transient server failure; the token's validity is unknown. | ||
| return false, fmt.Errorf("server error (HTTP %d): token validity could not be determined", res.StatusCode) | ||
| default: | ||
| return false, fmt.Errorf("unexpected HTTP response status %d", res.StatusCode) | ||
| } | ||
| } | ||
|
|
||
| func (s Scanner) Type() detector_typepb.DetectorType { | ||
| return detector_typepb.DetectorType_Infisical | ||
| } | ||
|
|
||
| func (s Scanner) Description() string { | ||
| 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." | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.