-
Notifications
You must be signed in to change notification settings - Fork 2.5k
feat: add Black Duck API token detector #5063
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
Open
New1Direction
wants to merge
1
commit into
trufflesecurity:main
Choose a base branch
from
New1Direction:feat/blackduck-detector
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+255
−2
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| package blackduck | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/base64" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "strings" | ||
|
|
||
| regexp "github.com/wasilibs/go-re2" | ||
|
|
||
| "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" | ||
| "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detector_typepb" | ||
| ) | ||
|
|
||
| type Scanner struct { | ||
| client *http.Client | ||
| detectors.DefaultMultiPartCredentialProvider | ||
| } | ||
|
|
||
| // Ensure the Scanner satisfies the interface at compile time. | ||
| var _ detectors.Detector = (*Scanner)(nil) | ||
|
|
||
| var ( | ||
| defaultClient = detectors.DetectorHttpClientWithLocalAddresses | ||
|
|
||
| // Black Duck API tokens are base64("<uuid>:<uuid>"), i.e. 100 base64 chars | ||
| // ending in "==". isValidTokenFormat confirms the decoded uuid pair below. | ||
| keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"blackduck", "black_duck"}) + `\b([A-Za-z0-9+/]{96,140}={0,2})`) | ||
| // Black Duck is self-hosted, so we need the server URL to verify. | ||
| endpointPat = regexp.MustCompile(detectors.PrefixRegex([]string{"blackduck", "black_duck"}) + `\b(https?://[a-zA-Z0-9.-]+(?::[0-9]{2,5})?)`) | ||
|
|
||
| uuidPat = regexp.MustCompile(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`) | ||
| ) | ||
|
|
||
| // 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{"blackduck", "black_duck"} | ||
| } | ||
|
|
||
| // isValidTokenFormat checks the candidate base64-decodes to a "uuid:uuid" pair. | ||
| func isValidTokenFormat(candidate string) bool { | ||
| decoded, err := base64.StdEncoding.DecodeString(candidate) | ||
| if err != nil { | ||
| decoded, err = base64.RawStdEncoding.DecodeString(strings.TrimRight(candidate, "=")) | ||
| if err != nil { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| parts := strings.Split(string(decoded), ":") | ||
| if len(parts) != 2 { | ||
| return false | ||
| } | ||
| return uuidPat.MatchString(parts[0]) && uuidPat.MatchString(parts[1]) | ||
| } | ||
|
|
||
| // FromData will find and optionally verify Black Duck secrets 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) | ||
|
|
||
| endpointMatches := endpointPat.FindAllStringSubmatch(dataStr, -1) | ||
|
|
||
| uniqueTokens := make(map[string]struct{}) | ||
| for _, match := range keyPat.FindAllStringSubmatch(dataStr, -1) { | ||
| token := strings.TrimSpace(match[1]) | ||
| if isValidTokenFormat(token) { | ||
| uniqueTokens[token] = struct{}{} | ||
| } | ||
| } | ||
|
|
||
| for token := range uniqueTokens { | ||
| for _, endpointMatch := range endpointMatches { | ||
| resEndpointMatch := strings.TrimSpace(endpointMatch[1]) | ||
|
|
||
| u, err := detectors.ParseURLAndStripPathAndParams(resEndpointMatch) | ||
| if err != nil { | ||
| // skip invalid URLs | ||
| continue | ||
| } | ||
| u.Path = "/api/tokens/authenticate" | ||
|
|
||
| s1 := detectors.Result{ | ||
| DetectorType: detector_typepb.DetectorType_BlackDuck, | ||
| Raw: []byte(token), | ||
| RawV2: []byte(token + resEndpointMatch), | ||
| SecretParts: map[string]string{ | ||
| "key": token, | ||
| "url": resEndpointMatch, | ||
| }, | ||
| } | ||
|
|
||
| if verify { | ||
| client := s.client | ||
| if client == nil { | ||
| client = defaultClient | ||
| } | ||
| isVerified, verErr := verifyToken(ctx, client, u.String(), token) | ||
| s1.Verified = isVerified | ||
| if verErr != nil { | ||
| s1.SetVerificationError(verErr, token) | ||
| } | ||
| } | ||
|
|
||
| results = append(results, s1) | ||
| } | ||
| } | ||
|
|
||
| return results, nil | ||
| } | ||
|
|
||
| // verifyToken calls the Black Duck token-auth endpoint. 200 means valid, | ||
| // 401/403 mean invalid, anything else is treated as an indeterminate error. | ||
| func verifyToken(ctx context.Context, client *http.Client, url, token string) (bool, error) { | ||
| req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, nil) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| req.Header.Set("Authorization", "token "+token) | ||
| req.Header.Set("Accept", "application/vnd.blackducksoftware.user-4+json") | ||
|
|
||
| res, err := client.Do(req) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| defer func() { | ||
| _, _ = io.Copy(io.Discard, res.Body) | ||
| _ = res.Body.Close() | ||
| }() | ||
|
|
||
| switch res.StatusCode { | ||
| case http.StatusOK: | ||
| return true, nil | ||
| case http.StatusUnauthorized, http.StatusForbidden: | ||
| // invalid token, nothing to do | ||
| return false, nil | ||
| default: | ||
| return false, fmt.Errorf("unexpected HTTP response status %d", res.StatusCode) | ||
| } | ||
| } | ||
|
|
||
| func (s Scanner) Type() detector_typepb.DetectorType { | ||
| return detector_typepb.DetectorType_BlackDuck | ||
| } | ||
|
|
||
| func (s Scanner) Description() string { | ||
| return "Black Duck is a software composition analysis (SCA) tool used to identify security and license risks in open-source dependencies. Black Duck API tokens authenticate to the Black Duck server's REST API and can expose scan results and project data." | ||
| } | ||
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,96 @@ | ||
| package blackduck | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
|
|
||
| "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" | ||
| "github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick" | ||
| ) | ||
|
|
||
| var ( | ||
| // base64("a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d:f0e1d2c3-b4a5-4968-8778-695a4b3c2d1e") | ||
| validToken = "YTFiMmMzZDQtZTVmNi00YTdiLThjOWQtMGUxZjJhM2I0YzVkOmYwZTFkMmMzLWI0YTUtNDk2OC04Nzc4LTY5NWE0YjNjMmQxZQ==" | ||
| // Correct length/alphabet, but decodes to plain text instead of "<uuid>:<uuid>". | ||
| invalidToken = "dGhpcy1pcy1ub3QtYS1yZWFsLWJsYWNrZHVjay10b2tlbi1qdXN0LWZpbGxlci1jb250ZW50LXBhZGRpbmcteHl6MDEyMzQ1Ng==" | ||
| validEndpoint = "https://blackduck.example.com" | ||
| keyword = "blackduck" | ||
| ) | ||
|
|
||
| func TestBlackduck_Pattern(t *testing.T) { | ||
| d := Scanner{} | ||
| ahoCorasickCore := ahocorasick.NewAhoCorasickCore([]detectors.Detector{d}) | ||
| tests := []struct { | ||
| name string | ||
| input string | ||
| want []string | ||
| }{ | ||
| { | ||
| name: "valid pattern - token and url", | ||
| input: fmt.Sprintf("%s api token - '%s'\n%s url - '%s'\n", keyword, validToken, keyword, validEndpoint), | ||
| want: []string{validToken + validEndpoint}, | ||
| }, | ||
| { | ||
| name: "invalid pattern - token does not decode to <uuid>:<uuid>", | ||
| input: fmt.Sprintf("%s api token - '%s'\n%s url - '%s'\n", keyword, invalidToken, keyword, validEndpoint), | ||
| want: []string{}, | ||
| }, | ||
| { | ||
| name: "no result without an endpoint - verification needs the server url", | ||
| input: fmt.Sprintf("%s api token - '%s'\n", keyword, validToken), | ||
| want: []string{}, | ||
| }, | ||
| { | ||
| // Underscore form only, and a host with no "blackduck" substring, so | ||
| // the chunk matches solely on the "black_duck" keyword. | ||
| name: "valid pattern - black_duck underscore form (env-var style)", | ||
| input: fmt.Sprintf("BLACK_DUCK_API_TOKEN='%s'\nBLACK_DUCK_URL='https://bd.example.com'\n", validToken), | ||
| want: []string{validToken + "https://bd.example.com"}, | ||
| }, | ||
| } | ||
|
|
||
| for _, test := range tests { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| matchedDetectors := ahoCorasickCore.FindDetectorMatches([]byte(test.input)) | ||
| if len(matchedDetectors) == 0 { | ||
| t.Errorf("keywords '%v' not matched by: %s", d.Keywords(), test.input) | ||
| return | ||
| } | ||
|
|
||
| results, err := d.FromData(context.Background(), false, []byte(test.input)) | ||
| if err != nil { | ||
| t.Errorf("error = %v", err) | ||
| return | ||
| } | ||
|
|
||
| if len(results) != len(test.want) { | ||
| if len(results) == 0 { | ||
| t.Errorf("did not receive result") | ||
| } else { | ||
| t.Errorf("expected %d results, only received %d", len(test.want), len(results)) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| actual := make(map[string]struct{}, len(results)) | ||
| for _, r := range results { | ||
| if len(r.RawV2) > 0 { | ||
| actual[string(r.RawV2)] = struct{}{} | ||
| } else { | ||
| actual[string(r.Raw)] = struct{}{} | ||
| } | ||
| } | ||
| expected := make(map[string]struct{}, len(test.want)) | ||
| for _, v := range test.want { | ||
| expected[v] = struct{}{} | ||
| } | ||
|
|
||
| if diff := cmp.Diff(expected, actual); diff != "" { | ||
| t.Errorf("%s diff: (-want +got)\n%s", test.name, diff) | ||
| } | ||
| }) | ||
| } | ||
| } |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
|---|---|---|
|
|
@@ -1054,4 +1054,5 @@ enum DetectorType { | |
| GitLabOauth2 = 1050; | ||
| SpectralOps = 1051; | ||
| AWSAppSync = 1052; | ||
| BlackDuck = 1053; | ||
| } | ||
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.