|
41 | 41 |
|
42 | 42 | const (
|
43 | 43 | retryBackoff = 500 * time.Millisecond
|
44 |
| - // The maximum key length for unauthorized request keys to qualify for caching. |
45 |
| - maxUnauthorizedCachedKeySize = 10000 |
| 44 | + // The maximum length of requester-controlled attributes to allow caching. |
| 45 | + maxControlledAttrCacheSize = 10000 |
46 | 46 | )
|
47 | 47 |
|
48 | 48 | // Ensure Webhook implements the authorizer.Authorizer interface.
|
@@ -197,10 +197,10 @@ func (w *WebhookAuthorizer) Authorize(attr authorizer.Attributes) (decision auth
|
197 | 197 | return w.decisionOnError, "", err
|
198 | 198 | }
|
199 | 199 | r.Status = result.Status
|
200 |
| - if r.Status.Allowed { |
201 |
| - w.responseCache.Add(string(key), r.Status, w.authorizedTTL) |
202 |
| - } else { |
203 |
| - if callerControlledAttributeSize(attr) < maxUnauthorizedCachedKeySize { |
| 200 | + if shouldCache(attr) { |
| 201 | + if r.Status.Allowed { |
| 202 | + w.responseCache.Add(string(key), r.Status, w.authorizedTTL) |
| 203 | + } else { |
204 | 204 | w.responseCache.Add(string(key), r.Status, w.unauthorizedTTL)
|
205 | 205 | }
|
206 | 206 | }
|
@@ -269,13 +269,16 @@ func (t *subjectAccessReviewClient) Create(subjectAccessReview *authorization.Su
|
269 | 269 | return result, err
|
270 | 270 | }
|
271 | 271 |
|
272 |
| -func callerControlledAttributeSize(attr authorizer.Attributes) int64 { |
273 |
| - return int64(len(attr.GetNamespace())) + |
| 272 | +// shouldCache determines whether it is safe to cache the given request attributes. If the |
| 273 | +// requester-controlled attributes are too large, this may be a DoS attempt, so we skip the cache. |
| 274 | +func shouldCache(attr authorizer.Attributes) bool { |
| 275 | + controlledAttrSize := int64(len(attr.GetNamespace())) + |
274 | 276 | int64(len(attr.GetVerb())) +
|
275 | 277 | int64(len(attr.GetAPIGroup())) +
|
276 | 278 | int64(len(attr.GetAPIVersion())) +
|
277 | 279 | int64(len(attr.GetResource())) +
|
278 | 280 | int64(len(attr.GetSubresource())) +
|
279 | 281 | int64(len(attr.GetName())) +
|
280 | 282 | int64(len(attr.GetPath()))
|
| 283 | + return controlledAttrSize < maxControlledAttrCacheSize |
281 | 284 | }
|
0 commit comments