Skip to content

Commit 52ba5e9

Browse files
fix(detectors): skip GitLab v1 candidates with no digits to reduce false positives
The v1 GitLab detector uses PrefixRegex which searches up to 40 chars ahead of a "gitlab" keyword, crossing newlines. In Dockerfiles like: ARG GITLAB_ACCESS_TOKEN_TYPE=Private-Token ARG GITLAB_ACCESS_TOKEN ARG MAVEN_SETTINGS_PROFILE=test "MAVEN_SETTINGS_PROFILE" (22 chars, all [a-zA-Z0-9_]) is within 40 characters of the second GITLAB keyword and passes the Shannon entropy check (~4.1 > 3.6) because its letters are varied. It is then reported as a GitLab secret — a false positive. Real GitLab personal access tokens are randomly generated and always contain at least one digit. Variable names like MAVEN_SETTINGS_PROFILE never do. Add a KeyIsRandom guard (already used elsewhere in the codebase) to discard digit-free candidates before verification. Closes #4756 Signed-off-by: Oleksandr Sanin <alexaaander.sanin@gmail.com>
1 parent 9f0b97f commit 52ba5e9

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

pkg/detectors/gitlab/v1/gitlab.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
7979
continue
8080
}
8181

82+
// real tokens are random and always contain at least one digit;
83+
// variable names like MAVEN_SETTINGS_PROFILE have no digits and
84+
// are a common source of false positives when they appear within
85+
// 40 characters of a "gitlab" keyword on a preceding line.
86+
if !detectors.KeyIsRandom(resMatch) {
87+
continue
88+
}
89+
8290
for _, endpoint := range s.Endpoints() {
8391
s1 := detectors.Result{
8492
DetectorType: detector_typepb.DetectorType_Gitlab,

pkg/detectors/gitlab/v1/gitlab_v1_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ func TestGitLab_Pattern(t *testing.T) {
4545
input: "GITLAB_TOKEN=ABc123456789dEFghIJK",
4646
want: []string{"ABc123456789dEFghIJKhttps://gitlab.com"},
4747
},
48+
{
49+
// Regression test for https://github.com/trufflesecurity/trufflehog/issues/4756
50+
// ARG variable names that appear after GITLAB_* args in a Dockerfile must not be
51+
// flagged as secrets because they contain no digits (KeyIsRandom check).
52+
name: "no false positive for Dockerfile ARG variable name after GITLAB_ACCESS_TOKEN",
53+
input: `ARG GITLAB_ACCESS_TOKEN_TYPE=Private-Token
54+
ARG GITLAB_ACCESS_TOKEN
55+
ARG MAVEN_SETTINGS_PROFILE=test`,
56+
want: []string{},
57+
},
4858
}
4959

5060
for _, test := range tests {

0 commit comments

Comments
 (0)