-
Notifications
You must be signed in to change notification settings - Fork 1.1k
cubeapi: key the rate limiter on the validated identity, not an unvalidated header #1380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
97ebc78
cf746be
3ed7ef4
b06a526
874fdb4
d7653f9
5f48af7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,21 +10,27 @@ use axum::{ | |
| response::Response, | ||
| }; | ||
|
|
||
| /// Per-API-key token bucket rate limiter middleware. | ||
| /// Reads the X-API-Key header and checks the shared governor limiter. | ||
| /// Returns 429 if the key has exceeded its quota. | ||
| /// Per-identity token bucket rate limiter middleware. | ||
| /// Reads the `RateLimitIdentity` published by `unified_auth` after it validated | ||
| /// the credential, and checks the shared governor limiter. | ||
| /// Returns 429 if that identity has exceeded its quota. | ||
| pub async fn rate_limit( | ||
| State(state): State<AppState>, | ||
| request: Request, | ||
| next: Next, | ||
| ) -> Result<Response, AppError> { | ||
| // Extract key; fall back to IP or "anonymous" | ||
| let key = request | ||
| .headers() | ||
| .get("X-API-Key") | ||
| .and_then(|v| v.to_str().ok()) | ||
| .unwrap_or("anonymous") | ||
| .to_string(); | ||
| let identity = request | ||
| .extensions() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stale doc comments now that the key comes from the validated identity, not the header. The module doc above still reads: "Per-API-key token bucket rate limiter middleware. Reads the X-API-Key header and checks the shared governor limiter." That's no longer accurate — this now keys on the |
||
| .get::<crate::middleware::auth::RateLimitIdentity>() | ||
| .map(|id| id.0.clone()); | ||
|
|
||
| debug_assert!( | ||
| identity.is_some() || !state.config.auth_is_configured(), | ||
| "unified_auth must run before rate_limit: no RateLimitIdentity was published, \ | ||
| so every request would share one bucket" | ||
| ); | ||
|
|
||
| let key = identity.unwrap_or_else(|| "unauthenticated".to_string()); | ||
|
|
||
|
dwin-gharibi marked this conversation as resolved.
|
||
| match state.rate_limiter.check_key(&key) { | ||
| Ok(_) => Ok(next.run(request).await), | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.