WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 4eb606a

Browse files
committed
reverted the cli to old one.
code refactoring
1 parent 33c59be commit 4eb606a

File tree

4 files changed

+40
-32
lines changed

4 files changed

+40
-32
lines changed

pkg/analyzer/analyzers/gitlab/gitlab.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ import (
2121

2222
var _ analyzers.Analyzer = (*Analyzer)(nil)
2323

24+
const (
25+
DefaultGitLabHost = "https://gitlab.com"
26+
)
27+
2428
type Analyzer struct {
2529
Cfg *config.Config
2630
}
@@ -34,7 +38,7 @@ func (a Analyzer) Analyze(_ context.Context, credInfo map[string]string) (*analy
3438
}
3539
host, ok := credInfo["host"]
3640
if !ok {
37-
host = "https://gitlab.com"
41+
host = DefaultGitLabHost
3842
}
3943

4044
info, err := AnalyzePermissions(a.Cfg, key, host)
@@ -274,8 +278,8 @@ func AnalyzePermissions(cfg *config.Config, key string, host string) (*SecretInf
274278
}, nil
275279
}
276280

277-
func AnalyzeAndPrintPermissions(cfg *config.Config, key, host string) {
278-
info, err := AnalyzePermissions(cfg, key, host)
281+
func AnalyzeAndPrintPermissions(cfg *config.Config, key string) {
282+
info, err := AnalyzePermissions(cfg, key, DefaultGitLabHost)
279283
if err != nil {
280284
color.Red("[x] Error: %s", err)
281285
return

pkg/analyzer/cli.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func Run(cmd string) {
8383
case "stripe":
8484
stripe.AnalyzeAndPrintPermissions(secretInfo.Cfg, secretInfo.Parts["key"])
8585
case "gitlab":
86-
gitlab.AnalyzeAndPrintPermissions(secretInfo.Cfg, secretInfo.Parts["key"], "https://gitlab.com")
86+
gitlab.AnalyzeAndPrintPermissions(secretInfo.Cfg, secretInfo.Parts["key"])
8787
case "mailchimp":
8888
mailchimp.AnalyzeAndPrintPermissions(secretInfo.Cfg, secretInfo.Parts["key"])
8989
case "postman":

pkg/detectors/gitlab/v1/gitlab.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,14 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
7070
}
7171

7272
if verify {
73-
isVerified, extraData, host, verificationErr := s.verifyGitlab(ctx, resMatch)
73+
isVerified, extraData, analysisInfo, verificationErr := s.verifyGitlab(ctx, resMatch)
7474
s1.Verified = isVerified
7575
for key, value := range extraData {
7676
s1.ExtraData[key] = value
7777
}
7878

7979
s1.SetVerificationError(verificationErr, resMatch)
80-
s1.AnalysisInfo = map[string]string{
81-
"key": resMatch,
82-
"host": host,
83-
}
80+
s1.AnalysisInfo = analysisInfo
8481
}
8582

8683
results = append(results, s1)
@@ -89,7 +86,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
8986
return results, nil
9087
}
9188

92-
func (s Scanner) verifyGitlab(ctx context.Context, resMatch string) (bool, map[string]string, string, error) {
89+
func (s Scanner) verifyGitlab(ctx context.Context, resMatch string) (bool, map[string]string, map[string]string, error) {
9390
// there are 4 read 'scopes' for a gitlab token: api, read_user, read_repo, and read_registry
9491
// they all grant access to different parts of the API. I couldn't find an endpoint that every
9592
// one of these scopes has access to, so we just check an example endpoint for each scope. If any
@@ -109,43 +106,48 @@ func (s Scanner) verifyGitlab(ctx context.Context, resMatch string) (bool, map[s
109106
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", resMatch))
110107
res, err := client.Do(req)
111108
if err != nil {
112-
return false, nil, baseURL, err
109+
return false, nil, nil, err
113110
}
114111

115112
defer res.Body.Close()
116113

117114
bodyBytes, err := io.ReadAll(res.Body)
118115
if err != nil {
119-
return false, nil, baseURL, err
116+
return false, nil, nil, err
117+
}
118+
119+
analysisInfo := map[string]string{
120+
"key": resMatch,
121+
"host": baseURL,
120122
}
121123

122124
// 200 means good key and has `read_user` scope
123125
// 403 means good key but not the right scope
124126
// 401 is bad key
125127
switch res.StatusCode {
126128
case http.StatusOK:
127-
return json.Valid(bodyBytes), nil, baseURL, nil
129+
return json.Valid(bodyBytes), nil, analysisInfo, nil
128130
case http.StatusForbidden:
129131
// check if the user account is blocked or not
130132
stringBody := string(bodyBytes)
131133
if strings.Contains(stringBody, BlockedUserMessage) {
132134
return true, map[string]string{
133135
"blocked": "True",
134-
}, baseURL, nil
136+
}, analysisInfo, nil
135137
}
136138

137139
// Good key but not the right scope
138-
return true, nil, baseURL, nil
140+
return true, nil, analysisInfo, nil
139141
case http.StatusUnauthorized:
140142
// Nothing to do; zero values are the ones we want
141-
return false, nil, baseURL, nil
143+
return false, nil, nil, nil
142144
default:
143-
return false, nil, baseURL, fmt.Errorf("unexpected HTTP response status %d", res.StatusCode)
145+
return false, nil, nil, fmt.Errorf("unexpected HTTP response status %d", res.StatusCode)
144146
}
145147

146148
}
147149

148-
return false, nil, "", nil
150+
return false, nil, nil, nil
149151
}
150152

151153
func (s Scanner) Type() detectorspb.DetectorType {

pkg/detectors/gitlab/v2/gitlab_v2.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,14 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
5959
}
6060

6161
if verify {
62-
isVerified, extraData, host, verificationErr := s.verifyGitlab(ctx, resMatch)
62+
isVerified, extraData, analysisInfo, verificationErr := s.verifyGitlab(ctx, resMatch)
6363
s1.Verified = isVerified
6464
for key, value := range extraData {
6565
s1.ExtraData[key] = value
6666
}
6767

6868
s1.SetVerificationError(verificationErr, resMatch)
69-
s1.AnalysisInfo = map[string]string{
70-
"key": resMatch,
71-
"host": host,
72-
}
69+
s1.AnalysisInfo = analysisInfo
7370
}
7471

7572
results = append(results, s1)
@@ -78,7 +75,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
7875
return results, nil
7976
}
8077

81-
func (s Scanner) verifyGitlab(ctx context.Context, resMatch string) (bool, map[string]string, string, error) {
78+
func (s Scanner) verifyGitlab(ctx context.Context, resMatch string) (bool, map[string]string, map[string]string, error) {
8279
// there are 4 read 'scopes' for a gitlab token: api, read_user, read_repo, and read_registry
8380
// they all grant access to different parts of the API. I couldn't find an endpoint that every
8481
// one of these scopes has access to, so we just check an example endpoint for each scope. If any
@@ -97,41 +94,46 @@ func (s Scanner) verifyGitlab(ctx context.Context, resMatch string) (bool, map[s
9794
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", resMatch))
9895
res, err := client.Do(req)
9996
if err != nil {
100-
return false, nil, baseURL, err
97+
return false, nil, nil, err
10198
}
10299
defer res.Body.Close()
103100

104101
bodyBytes, err := io.ReadAll(res.Body)
105102
if err != nil {
106-
return false, nil, baseURL, err
103+
return false, nil, nil, err
104+
}
105+
106+
analysisInfo := map[string]string{
107+
"key": resMatch,
108+
"host": baseURL,
107109
}
108110

109111
// 200 means good key and has `read_user` scope
110112
// 403 means good key but not the right scope
111113
// 401 is bad key
112114
switch res.StatusCode {
113115
case http.StatusOK:
114-
return true, nil, baseURL, nil
116+
return true, nil, analysisInfo, nil
115117
case http.StatusForbidden:
116118
// check if the user account is blocked or not
117119
stringBody := string(bodyBytes)
118120
if strings.Contains(stringBody, v1.BlockedUserMessage) {
119121
return true, map[string]string{
120122
"blocked": "True",
121-
}, baseURL, nil
123+
}, analysisInfo, nil
122124
}
123125

124126
// Good key but not the right scope
125-
return true, nil, baseURL, nil
127+
return true, nil, analysisInfo, nil
126128
case http.StatusUnauthorized:
127129
// Nothing to do; zero values are the ones we want
128-
return false, nil, baseURL, nil
130+
return false, nil, nil, nil
129131
default:
130-
return false, nil, baseURL, fmt.Errorf("unexpected HTTP response status %d", res.StatusCode)
132+
return false, nil, nil, fmt.Errorf("unexpected HTTP response status %d", res.StatusCode)
131133
}
132134

133135
}
134-
return false, nil, "", nil
136+
return false, nil, nil, nil
135137
}
136138

137139
func (s Scanner) Type() detectorspb.DetectorType {

0 commit comments

Comments
 (0)