-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Add Gitlab V3 Detector #4563
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
mustansir14
merged 10 commits into
trufflesecurity:main
from
mustansir14:INS-172-Update-GitLab-Detector-Regex
Dec 4, 2025
Merged
Add Gitlab V3 Detector #4563
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
23f7838
add gitlab v3 detector
mustansir14 a751ce0
add detectors build tag to test
mustansir14 a78a972
add gitlab v3 detector to engine defaults
mustansir14 6af8762
enhance regex
mustansir14 d3fc9a5
add comment to link gitlab's PR
mustansir14 57cf511
Merge branch 'main' into INS-172-Update-GitLab-Detector-Regex
mustansir14 c322fa0
Merge branch 'main' into INS-172-Update-GitLab-Detector-Regex
mustansir14 5f9f9b0
Merge branch 'main' into INS-172-Update-GitLab-Detector-Regex
mustansir14 6e95b65
Merge branch 'main' into INS-172-Update-GitLab-Detector-Regex
mustansir14 0a7b8f7
Merge branch 'main' into INS-172-Update-GitLab-Detector-Regex
mustansir14 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
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,102 @@ | ||
| package gitlab | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "maps" | ||
| "net/http" | ||
| "strings" | ||
|
|
||
| regexp "github.com/wasilibs/go-re2" | ||
|
|
||
| "github.com/trufflesecurity/trufflehog/v3/pkg/common" | ||
| "github.com/trufflesecurity/trufflehog/v3/pkg/detectors" | ||
| v1 "github.com/trufflesecurity/trufflehog/v3/pkg/detectors/gitlab/v1" | ||
| "github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb" | ||
| ) | ||
|
|
||
| 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.Versioner = (*Scanner)(nil) | ||
|
|
||
| func (Scanner) Version() int { return 3 } | ||
| func (Scanner) CloudEndpoint() string { return "https://gitlab.com" } | ||
|
|
||
| var ( | ||
| defaultClient = common.SaneHttpClient() | ||
| // pattern taken from gitlab's PR for the format change: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/169322 | ||
| keyPat = regexp.MustCompile(`\b(glpat-[a-zA-Z0-9\-=_]{27,300}.[0-9a-z]{2}.[a-z0-9]{9})\b`) | ||
| ) | ||
|
|
||
| func (s Scanner) getClient() *http.Client { | ||
| if s.client != nil { | ||
| return s.client | ||
| } | ||
|
|
||
| return defaultClient | ||
| } | ||
|
|
||
| // 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{"glpat-"} } | ||
|
|
||
| func (s Scanner) Type() detectorspb.DetectorType { | ||
| return detectorspb.DetectorType_Gitlab | ||
| } | ||
|
|
||
| func (s Scanner) Description() string { | ||
| return "GitLab is a web-based DevOps lifecycle tool that provides a Git repository manager providing wiki, issue-tracking, and CI/CD pipeline features. GitLab Personal Access Tokens (PATs) can be used to authenticate and access GitLab resources." | ||
| } | ||
|
|
||
| // FromData will find and optionally verify Gitlab 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) | ||
|
|
||
| matches := keyPat.FindAllStringSubmatch(dataStr, -1) | ||
| for _, match := range matches { | ||
| resMatch := strings.TrimSpace(match[1]) | ||
|
|
||
| for _, endpoint := range s.Endpoints() { | ||
| s1 := detectors.Result{ | ||
| DetectorType: detectorspb.DetectorType_Gitlab, | ||
| Raw: []byte(resMatch), | ||
| RawV2: []byte(resMatch + endpoint), | ||
| ExtraData: map[string]string{ | ||
| "rotation_guide": "https://howtorotate.com/docs/tutorials/gitlab/", | ||
| "version": fmt.Sprintf("%d", s.Version()), | ||
| }, | ||
| } | ||
|
|
||
| if verify { | ||
|
|
||
| isVerified, extraData, verificationErr := v1.VerifyGitlab(ctx, s.getClient(), endpoint, resMatch) | ||
| s1.Verified = isVerified | ||
| maps.Copy(s1.ExtraData, extraData) | ||
|
|
||
| s1.SetVerificationError(verificationErr) | ||
|
|
||
| // for verified keys set the analysis info | ||
| if s1.Verified { | ||
| s1.AnalysisInfo = map[string]string{ | ||
| "key": resMatch, | ||
| "host": endpoint, | ||
| } | ||
|
|
||
| // if secret is verified with one endpoint, break the loop to continue to next secret | ||
| results = append(results, s1) | ||
| break | ||
| } | ||
| } | ||
|
|
||
| results = append(results, s1) | ||
| } | ||
| } | ||
|
|
||
| return results, nil | ||
| } |
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.