What this change means
Source files changed in ways the catalog does not model (for example logic inside Verify). Read the diff to judge the effect.
| @@ -6,6 +6,8 @@ import ( | ||
| 6 | 6 | "crypto/sha1" |
| 7 | 7 | "encoding/base64" |
| 8 | 8 | "encoding/hex" |
| 9 | added: "encoding/json" | |
| 10 | added: "errors" | |
| 9 | 11 | "fmt" |
| 10 | 12 | "io" |
| 11 | 13 | "net/http" |
| @@ -40,6 +42,22 @@ var ( | ||
| 40 | 42 | apiHost = regexp.MustCompile(`\b([a-z0-9-]{6,}\.duosecurity\.com)\b`) |
| 41 | 43 | ) |
| 42 | 44 | |
| 45 | added: const ( | |
| 46 | added: adminAPIPath = "/admin/v1/info/summary" // https://duo.com/docs/adminapi#account-info | |
| 47 | added: authAPIPath = "/auth/v2/check" // https://duo.com/docs/authapi#check | |
| 48 | added: | |
| 49 | added: adminApplication = "Admin API" | |
| 50 | added: authApplication = "Auth API" | |
| 51 | added: | |
| 52 | added: duoCodeInvalidSignature = 40103 | |
| 53 | added: duoCodeAccessForbidden = 40301 | |
| 54 | added: ) | |
| 55 | added: | |
| 56 | added: type duoReply struct { | |
| 57 | added: status int | |
| 58 | added: code int | |
| 59 | added: } | |
| 60 | added: | |
| 43 | 61 | // Keywords are used for efficiently pre-filtering chunks. |
| 44 | 62 | // Use identifiers in the secret preferably, or the provider name. |
| 45 | 63 | func (s Scanner) Keywords() []string { |
| @@ -53,7 +71,7 @@ func (s Scanner) getClient() *http.Client { | ||
| 53 | 71 | return defaultClient |
| 54 | 72 | } |
| 55 | 73 | |
| 56 | removed: // FromData will find and optionally verify DeepSeek secrets in a given set of bytes. | |
| 74 | added: // FromData will find and optionally verify Duo secrets in a given set of bytes. | |
| 57 | 75 | func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (results []detectors.Result, err error) { |
| 58 | 76 | dataStr := string(data) |
| @@ -77,31 +95,19 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result | ||
| 77 | 95 | Raw: []byte(apiKey), |
| 78 | 96 | RawV2: []byte(fmt.Sprintf("%s:%s:%s", host, apiKey, apiSecret)), |
| 79 | 97 | ExtraData: map[string]string{ |
| 80 | removed: "application": "Admin API", | |
| 98 | added: "application": adminApplication, | |
| 81 | 99 | }, |
| 82 | 100 | SecretParts: map[string]string{ |
| 83 | 101 | "host": host, |
| 84 | 102 | "ikey": apiKey, |
| 85 | 103 | "skey": apiSecret, |
| 86 | removed: "application": "Admin API", | |
| 104 | added: "application": adminApplication, | |
| 87 | 105 | }, |
| 88 | 106 | } |
| 89 | 107 | if verify { |
| 90 | removed: verified, verificationErr := VerifyAdminToken(ctx, s.getClient(), host, apiKey, apiSecret) | |
| 91 | removed: // we will try auth api verification if admin api verification fails without error, | |
| 92 | removed: // which is in the case of it returning 401 unauthorized due to invalid credentials. | |
| 93 | removed: if !verified && verificationErr == nil { | |
| 94 | removed: var authStatusCode int | |
| 95 | removed: verified, authStatusCode, verificationErr = VerifyAuthToken(ctx, s.getClient(), host, apiKey, apiSecret) | |
| 96 | removed: // The Auth API responds 403 (not 401) when ikey/skey are | |
| 97 | removed: // structurally valid Admin API keys, active or not. That still | |
| 98 | removed: // counts as verified, but the keys remain Admin API keys, so | |
| 99 | removed: // only relabel to Auth API when the check actually succeeded (200). | |
| 100 | removed: if authStatusCode != http.StatusForbidden { | |
| 101 | removed: s1.ExtraData["application"] = "Auth API" | |
| 102 | removed: s1.SecretParts["application"] = "Auth API" | |
| 103 | removed: } | |
| 104 | removed: } | |
| 108 | added: verified, application, verificationErr := verifyMatch(ctx, s.getClient(), host, apiKey, apiSecret) | |
| 109 | added: s1.ExtraData["application"] = application | |
| 110 | added: s1.SecretParts["application"] = application | |
| 105 | 111 | s1.SetVerificationError(verificationErr, host, apiKey, apiSecret) |
| 106 | 112 | s1.Verified = verified |
| 107 | 113 | } |
| @@ -112,49 +118,56 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result | ||
| 112 | 118 | return results, nil |
| 113 | 119 | } |
| 114 | 120 | |
| 115 | removed: // returns verified=true if credentials are valid and belong to auth api, false if creds are invalid, and error if creds belong to auth api or for anything else (e.g., network error). | |
| 116 | removed: // The HTTP status code is also returned so callers can distinguish a genuine 200 from a | |
| 117 | removed: // 403, which the Auth API returns for structurally valid Admin API keys (active or not). | |
| 118 | removed: func VerifyAuthToken( | |
| 121 | added: // verifyMatch checks the credentials against the Admin API, then the Auth API, and | |
| 122 | added: // returns whether they are valid and which application they belong to. | |
| 123 | added: // | |
| 124 | added: // Only a 200 from either API proves the credentials valid, and only error code 40103 | |
| 125 | added: // (invalid signature) from either API proves them invalid: that API recognized the | |
| 126 | added: // integration key and rejected the secret key. Other responses are reported as a verification error. | |
| 127 | added: func verifyMatch( | |
| 119 | 128 | ctx context.Context, |
| 120 | 129 | client *http.Client, |
| 121 | 130 | host, ikey, skey string, |
| 122 | removed: ) (bool, int, error) { | |
| 123 | removed: // Docs: https://duo.com/docs/authapi#check | |
| 124 | removed: return verifyDuoRequest( | |
| 125 | removed: ctx, | |
| 126 | removed: client, | |
| 127 | removed: host, | |
| 128 | removed: ikey, | |
| 129 | removed: skey, | |
| 130 | removed: "/auth/v2/check", | |
| 131 | removed: ) | |
| 132 | removed: } | |
| 131 | added: ) (bool, string, error) { | |
| 132 | added: admin, adminErr := duoRequest(ctx, client, host, ikey, skey, adminAPIPath) | |
| 133 | added: if adminErr == nil && admin.status == http.StatusOK { | |
| 134 | added: return true, adminApplication, nil | |
| 135 | added: } | |
| 133 | 136 | |
| 134 | removed: // returns 401 unauthorized if credentials are invalid, 200 OK if valid, and error for anything else | |
| 135 | removed: func VerifyAdminToken( | |
| 136 | removed: ctx context.Context, | |
| 137 | removed: client *http.Client, | |
| 138 | removed: host, ikey, skey string, | |
| 139 | removed: ) (bool, error) { | |
| 140 | removed: // Docs: https://duo.com/docs/adminapi#account-info | |
| 141 | removed: verified, _, err := verifyDuoRequest( | |
| 142 | removed: ctx, | |
| 143 | removed: client, | |
| 144 | removed: host, | |
| 145 | removed: ikey, | |
| 146 | removed: skey, | |
| 147 | removed: "/admin/v1/info/summary", | |
| 137 | added: auth, authErr := duoRequest(ctx, client, host, ikey, skey, authAPIPath) | |
| 138 | added: if authErr == nil && auth.status == http.StatusOK { | |
| 139 | added: return true, authApplication, nil | |
| 140 | added: } | |
| 141 | added: | |
| 142 | added: if (adminErr == nil && admin.code == duoCodeInvalidSignature) || | |
| 143 | added: (authErr == nil && auth.code == duoCodeInvalidSignature) { | |
| 144 | added: return false, adminApplication, nil | |
| 145 | added: } | |
| 146 | added: if adminErr != nil { | |
| 147 | added: return false, adminApplication, adminErr | |
| 148 | added: } | |
| 149 | added: if authErr != nil { | |
| 150 | added: return false, adminApplication, authErr | |
| 151 | added: } | |
| 152 | added: | |
| 153 | added: msg := fmt.Sprintf( | |
| 154 | added: "indeterminate Duo verification: admin API returned HTTP %d (code %d), auth API returned HTTP %d (code %d)", | |
| 155 | added: admin.status, admin.code, auth.status, auth.code, | |
| 148 | 156 | ) |
| 149 | removed: return verified, err | |
| 157 | added: if admin.code == duoCodeAccessForbidden { | |
| 158 | added: msg += `; an Admin API integration needs the "Grant read information" permission to be verified` | |
| 159 | added: } | |
| 160 | added: return false, adminApplication, errors.New(msg) | |
| 150 | 161 | } |
| 151 | 162 | |
| 152 | removed: func verifyDuoRequest( | |
| 163 | added: // duoRequest sends a signed GET request to the given Duo API path and returns the HTTP | |
| 164 | added: // status and Duo error code without interpreting them. | |
| 165 | added: func duoRequest( | |
| 153 | 166 | ctx context.Context, |
| 154 | 167 | client *http.Client, |
| 155 | 168 | host, ikey, skey string, |
| 156 | 169 | path string, |
| 157 | removed: ) (bool, int, error) { | |
| 170 | added: ) (duoReply, error) { | |
| 158 | 171 | |
| 159 | 172 | timestamp := time.Now().UTC().Format(http.TimeFormat) |
| @@ -185,7 +198,7 @@ func verifyDuoRequest( | ||
| 185 | 198 | nil, |
| 186 | 199 | ) |
| 187 | 200 | if err != nil { |
| 188 | removed: return false, 0, err | |
| 201 | added: return duoReply{}, err | |
| 189 | 202 | } |
| 190 | 203 | |
| 191 | 204 | req.Header.Set("Authorization", "Basic "+auth) |
| @@ -193,23 +206,23 @@ func verifyDuoRequest( | ||
| 193 | 206 | |
| 194 | 207 | res, err := client.Do(req) |
| 195 | 208 | if err != nil { |
| 196 | removed: return false, 0, err | |
| 209 | added: return duoReply{}, err | |
| 197 | 210 | } |
| 198 | 211 | defer func() { |
| 199 | 212 | _, _ = io.Copy(io.Discard, res.Body) |
| 200 | 213 | _ = res.Body.Close() |
| 201 | 214 | }() |
| 202 | 215 | |
| 203 | removed: switch res.StatusCode { | |
| 204 | removed: case http.StatusOK: | |
| 205 | removed: return true, res.StatusCode, nil | |
| 206 | removed: case http.StatusUnauthorized: | |
| 207 | removed: return false, res.StatusCode, nil | |
| 208 | removed: case http.StatusForbidden: // Auth API returns 403 if credentials are admin keys be it active or inactive | |
| 209 | removed: return true, res.StatusCode, nil | |
| 210 | removed: default: | |
| 211 | removed: return false, res.StatusCode, fmt.Errorf("unexpected HTTP status %d", res.StatusCode) | |
| 216 | added: reply := duoReply{status: res.StatusCode} | |
| 217 | added: if res.StatusCode != http.StatusOK { | |
| 218 | added: var body struct { | |
| 219 | added: Code int `json:"code"` | |
| 220 | added: } | |
| 221 | added: if json.NewDecoder(io.LimitReader(res.Body, 64<<10)).Decode(&body) == nil { | |
| 222 | added: reply.code = body.Code | |
| 223 | added: } | |
| 212 | 224 | } |
| 225 | added: return reply, nil | |
| 213 | 226 | } |
| 214 | 227 | |
| 215 | 228 | func (s Scanner) Type() detector_typepb.DetectorType { |