| @@ -2,6 +2,7 @@ package saucelabs |
| 2 | | 2 | |
| 3 | import ( | 3 | import ( |
| 4 | "context" | 4 | "context" |
| | 5 | +added: "encoding/json" |
| 5 | "fmt" | 6 | "fmt" |
| 6 | "io" | 7 | "io" |
| 7 | "net/http" | 8 | "net/http" |
| @@ -14,6 +15,7 @@ import ( |
| 14 | ) | 15 | ) |
| 15 | | 16 | |
| 16 | type Scanner struct { | 17 | type Scanner struct { |
| | 18 | +added: client *http.Client |
| 17 | detectors.DefaultMultiPartCredentialProvider | 19 | detectors.DefaultMultiPartCredentialProvider |
| 18 | } | 20 | } |
| @@ -21,17 +23,28 @@ type Scanner struct { |
| 21 | var _ detectors.Detector = (*Scanner)(nil) | 23 | var _ detectors.Detector = (*Scanner)(nil) |
| 22 | | 24 | |
| 23 | var ( | 25 | var ( |
| 24 | −removed: client = common.SaneHttpClient() | 26 | +added: defaultClient = common.SaneHttpClient() |
| 25 | | 27 | |
| 26 | // Make sure that your group is surrounded in boundary characters such as below to reduce false positives. | 28 | // Make sure that your group is surrounded in boundary characters such as below to reduce false positives. |
| 27 | // as per signup page username can be between 2 to 70 characters and must only contain letters, numbers, or characters (_-.) | 29 | // as per signup page username can be between 2 to 70 characters and must only contain letters, numbers, or characters (_-.) |
| 28 | usernamePat = regexp.MustCompile(detectors.PrefixRegex([]string{"saucelabs", "username"}) + `\b([a-zA-Z0-9_\.-]{2,70})`) | 30 | usernamePat = regexp.MustCompile(detectors.PrefixRegex([]string{"saucelabs", "username"}) + `\b([a-zA-Z0-9_\.-]{2,70})`) |
| 29 | keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"saucelabs"}) + `\b([a-z0-9]{8}\-[a-z0-9]{4}\-[a-z0-9]{4}\-[a-z0-9]{4}\-[a-z0-9]{12})\b`) | 31 | keyPat = regexp.MustCompile(detectors.PrefixRegex([]string{"saucelabs"}) + `\b([a-z0-9]{8}\-[a-z0-9]{4}\-[a-z0-9]{4}\-[a-z0-9]{4}\-[a-z0-9]{12})\b`) |
| 30 | −removed: baseUrlPat = regexp.MustCompile(`\b(api\.(?:us|eu)-(?:west|east|central)-[0-9].saucelabs\.com)\b`) | 32 | +added: baseUrlPat = regexp.MustCompile(`\b(api\.(?:us|eu)-(?:west|east|central)-[0-9]\.saucelabs\.com)\b`) |
| 31 | | 33 | |
| 32 | fixedBaseURL = "api.us-west-1.saucelabs.com" | 34 | fixedBaseURL = "api.us-west-1.saucelabs.com" |
| 33 | ) | 35 | ) |
| 34 | | 36 | |
| | 37 | +added: type identityResponse struct { |
| | 38 | +added: Sub string `json:"sub"` |
| | 39 | +added: } |
| | 40 | +added: |
| | 41 | +added: func (s Scanner) getClient() *http.Client { |
| | 42 | +added: if s.client != nil { |
| | 43 | +added: return s.client |
| | 44 | +added: } |
| | 45 | +added: return defaultClient |
| | 46 | +added: } |
| | 47 | +added: |
| 35 | // Keywords are used for efficiently pre-filtering chunks. | 48 | // Keywords are used for efficiently pre-filtering chunks. |
| 36 | // Use identifiers in the secret preferably, or the provider name. | 49 | // Use identifiers in the secret preferably, or the provider name. |
| 37 | func (s Scanner) Keywords() []string { | 50 | func (s Scanner) Keywords() []string { |
| @@ -79,7 +92,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result |
| 79 | } | 92 | } |
| 80 | | 93 | |
| 81 | if verify { | 94 | if verify { |
| 82 | −removed: isVerified, verificationErr := verifySauceLabKey(ctx, client, userName, key, baseURL) | 95 | +added: isVerified, verificationErr := verifySauceLabKey(ctx, s.getClient(), userName, key, baseURL) |
| 83 | s1.Verified = isVerified | 96 | s1.Verified = isVerified |
| 84 | s1.SetVerificationError(verificationErr, key) | 97 | s1.SetVerificationError(verificationErr, key) |
| 85 | } | 98 | } |
| @@ -100,10 +113,12 @@ func (s Scanner) Description() string { |
| 100 | return "A service for cross browser testing, API keys can create and access tests from potentially sensitive internal websites" | 113 | return "A service for cross browser testing, API keys can create and access tests from potentially sensitive internal websites" |
| 101 | } | 114 | } |
| 102 | | 115 | |
| | 116 | +added: // verifySauceLabKey calls the identity endpoint, which accepts any valid credential regardless of role |
| | 117 | +added: // and is documented to return only 200 or 401. Any other response (e.g. a 403 from a proxy) is indeterminate. |
| 103 | func verifySauceLabKey(ctx context.Context, client *http.Client, userName, key, baseURL string) (bool, error) { | 118 | func verifySauceLabKey(ctx context.Context, client *http.Client, userName, key, baseURL string) (bool, error) { |
| 104 | −removed: apiURL := fmt.Sprintf("https://%s/team-management/v1/teams", baseURL) | 119 | +added: apiURL := fmt.Sprintf("https://%s/auth/v1/identity", baseURL) |
| 105 | | 120 | |
| 106 | −removed: req, err := http.NewRequestWithContext(ctx, http.MethodGet, apiURL, nil) | 121 | +added: req, err := http.NewRequestWithContext(ctx, http.MethodGet, apiURL, http.NoBody) |
| 107 | if err != nil { | 122 | if err != nil { |
| 108 | return false, err | 123 | return false, err |
| 109 | } | 124 | } |
| @@ -120,7 +135,14 @@ func verifySauceLabKey(ctx context.Context, client *http.Client, userName, key, |
| 120 | }() | 135 | }() |
| 121 | | 136 | |
| 122 | switch resp.StatusCode { | 137 | switch resp.StatusCode { |
| 123 | −removed: case http.StatusOK, http.StatusForbidden: | 138 | +added: case http.StatusOK: |
| | 139 | +added: var identity identityResponse |
| | 140 | +added: if err := json.NewDecoder(resp.Body).Decode(&identity); err != nil { |
| | 141 | +added: return false, fmt.Errorf("unexpected response body: %w", err) |
| | 142 | +added: } |
| | 143 | +added: if identity.Sub == "" { |
| | 144 | +added: return false, fmt.Errorf("unexpected response body: missing account id") |
| | 145 | +added: } |
| 124 | return true, nil | 146 | return true, nil |
| 125 | case http.StatusUnauthorized: | 147 | case http.StatusUnauthorized: |
| 126 | return false, nil | 148 | return false, nil |