| @@ -37,8 +37,16 @@ var ( |
| 37 | keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"gitlab"}) + `\b([a-zA-Z0-9][a-zA-Z0-9\-=_]{19,21})\b`) | 37 | keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"gitlab"}) + `\b([a-zA-Z0-9][a-zA-Z0-9\-=_]{19,21})\b`) |
| 38 | | 38 | |
| 39 | BlockedUserMessage = "403 Forbidden - Your account has been blocked" | 39 | BlockedUserMessage = "403 Forbidden - Your account has been blocked" |
| | 40 | +added: |
| | 41 | +added: // GitLab's forbidden! helper formats every reasoned 403 as "403 Forbidden - <reason>". |
| | 42 | +added: forbiddenWithReasonPrefix = "403 Forbidden - " |
| 40 | ) | 43 | ) |
| 41 | | 44 | |
| | 45 | +added: type gitlabErrorResponse struct { |
| | 46 | +added: Message string `json:"message"` |
| | 47 | +added: Error string `json:"error"` |
| | 48 | +added: } |
| | 49 | +added: |
| 42 | func (s Scanner) getClient() *http.Client { | 50 | func (s Scanner) getClient() *http.Client { |
| 43 | if s.client != nil { | 51 | if s.client != nil { |
| 44 | return s.client | 52 | return s.client |
| @@ -140,22 +148,34 @@ func VerifyGitlab(ctx context.Context, client *http.Client, baseEndpoint, resMat |
| 140 | } | 148 | } |
| 141 | | 149 | |
| 142 | // 200 means good key and has `read_user` scope | 150 | // 200 means good key and has `read_user` scope |
| 143 | −removed: // 403 means good key but not the right scope | 151 | +added: // 403 from GitLab means good key, but either the scope or the account state denies access |
| 144 | // 401 is bad key | 152 | // 401 is bad key |
| 145 | switch res.StatusCode { | 153 | switch res.StatusCode { |
| 146 | case http.StatusOK: | 154 | case http.StatusOK: |
| 147 | return json.Valid(bodyBytes), nil, nil | 155 | return json.Valid(bodyBytes), nil, nil |
| 148 | case http.StatusForbidden: | 156 | case http.StatusForbidden: |
| 149 | −removed: // check if the user account is blocked or not | 157 | +added: var errResp gitlabErrorResponse |
| 150 | −removed: stringBody := string(bodyBytes) | 158 | +added: if err := json.Unmarshal(bodyBytes, &errResp); err != nil { |
| 151 | −removed: if strings.Contains(stringBody, BlockedUserMessage) { | 159 | +added: return false, nil, fmt.Errorf("unexpected 403 response, unable to verify token") |
| 152 | −removed: return true, map[string]string{ | 160 | +added: } |
| 153 | −removed: "blocked": "True", | 161 | +added: |
| 154 | −removed: }, nil | 162 | +added: switch { |
| | 163 | +added: // GitLab only rejects an account (blocked, deactivated, password expired, unconfirmed |
| | 164 | +added: // email, etc.) after the token has resolved to a user. |
| | 165 | +added: case strings.HasPrefix(errResp.Message, forbiddenWithReasonPrefix): |
| | 166 | +added: extraData := map[string]string{ |
| | 167 | +added: "access_denied_reason": strings.TrimPrefix(errResp.Message, forbiddenWithReasonPrefix), |
| | 168 | +added: } |
| | 169 | +added: if strings.HasPrefix(errResp.Message, BlockedUserMessage) { |
| | 170 | +added: extraData["blocked"] = "True" |
| | 171 | +added: } |
| | 172 | +added: return true, extraData, nil |
| | 173 | +added: case errResp.Error == "insufficient_scope", errResp.Error == "insufficient_granular_scope": |
| | 174 | +added: // Good key but not the right scope |
| | 175 | +added: return true, nil, nil |
| 155 | } | 176 | } |
| 156 | | 177 | |
| 157 | −removed: // Good key but not the right scope | 178 | +added: return false, nil, fmt.Errorf("unexpected 403 response, unable to verify token") |
| 158 | −removed: return true, nil, nil | | |
| 159 | case http.StatusUnauthorized: | 179 | case http.StatusUnauthorized: |
| 160 | // Nothing to do; zero values are the ones we want | 180 | // Nothing to do; zero values are the ones we want |
| 161 | return false, nil, nil | 181 | return false, nil, nil |