Skip to content

Commit

Permalink
removed unmarshalling logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kashifkhan0771 committed Oct 28, 2024
1 parent 294a2fe commit 56e8f9b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 22 deletions.
21 changes: 8 additions & 13 deletions pkg/detectors/gitlab/v1/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ type Scanner struct {
detectors.EndpointSetter
}

type GitLabMessage struct {
Message string `json:"message"`
}

// Ensure the Scanner satisfies the interfaces at compile time.
var (
_ detectors.Detector = (*Scanner)(nil)
Expand Down Expand Up @@ -111,7 +107,8 @@ func (s Scanner) verifyGitlab(ctx context.Context, resMatch string) (bool, map[s
}

defer res.Body.Close()
body, err := io.ReadAll(res.Body)

bodyBytes, err := io.ReadAll(res.Body)
if err != nil {
return false, nil, err
}
Expand All @@ -121,16 +118,14 @@ func (s Scanner) verifyGitlab(ctx context.Context, resMatch string) (bool, map[s
// 401 is bad key
switch res.StatusCode {
case http.StatusOK:
return json.Valid(body), nil, nil
return json.Valid(bodyBytes), nil, nil
case http.StatusForbidden:
// check if the user account is blocked or not
var apiResp GitLabMessage
if err := json.Unmarshal(body, &apiResp); err == nil {
if apiResp.Message == BlockedUserMessage {
return true, map[string]string{
"blocked": "True",
}, nil
}
stringBody := string(bodyBytes)
if strings.Contains(stringBody, BlockedUserMessage) {
return true, map[string]string{
"blocked": "True",
}, nil
}

// Good key but not the right scope
Expand Down
16 changes: 7 additions & 9 deletions pkg/detectors/gitlab/v2/gitlab_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gitlab

import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -95,7 +94,8 @@ func (s Scanner) verifyGitlab(ctx context.Context, resMatch string) (bool, map[s
return false, nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)

bodyBytes, err := io.ReadAll(res.Body)
if err != nil {
return false, nil, err
}
Expand All @@ -108,13 +108,11 @@ func (s Scanner) verifyGitlab(ctx context.Context, resMatch string) (bool, map[s
return true, nil, nil
case http.StatusForbidden:
// check if the user account is blocked or not
var apiResp v1.GitLabMessage
if err := json.Unmarshal(body, &apiResp); err == nil {
if apiResp.Message == v1.BlockedUserMessage {
return true, map[string]string{
"blocked": "True",
}, nil
}
stringBody := string(bodyBytes)
if strings.Contains(stringBody, v1.BlockedUserMessage) {
return true, map[string]string{
"blocked": "True",
}, nil
}

// Good key but not the right scope
Expand Down

0 comments on commit 56e8f9b

Please sign in to comment.