Files
codex-proxy-rs/backend/crates/gateway-api/tests/admin/auth.rs
T
zyycnandClaude c275119f12 fix(admin): remove login throttling to close the lockout DoS
The failure-count lockout keyed on request source was the wrong control
for this deployment: behind the mandated reverse proxy every login shares
the proxy peer address, so an unauthenticated attacker could trip the
shared bucket and lock the real administrator out for the window,
renewably. Remove the throttle end to end — the LoginError::Throttled
state, the LoginCommand source, the AuthStore/AdminAuthStateRepository
failure-window ports and their Redis Lua script, and the login-source
middleware — and let a wrong password return InvalidCredentials directly.
Online brute-force is bounded by the Argon2 verification cost and admin
password strength; deployments wanting rate limiting apply it at the
reverse proxy, which sees real client IPs.

Co-authored-by: Claude <noreply@anthropic.com>
2026-07-27 00:31:10 +08:00

137 lines
3.9 KiB
Rust

use gateway_admin::model::auth::{LoginCommand, LoginError};
use gateway_api::admin::auth::{
AdminLoginData, AdminLoginRequest, AdminLogoutData, AdminSessionStatusData,
};
use serde_json::json;
use super::AdminTestFixture;
#[test]
fn login_request_should_deny_unknown_fields_and_redact_password_debug() {
let password = "admin-password-must-not-leak";
let request = serde_json::from_value::<AdminLoginRequest>(json!({
"username": "admin@example.invalid",
"password": password
}))
.expect("deserialize login request");
assert!(!format!("{request:?}").contains(password));
let (username, parsed_password) = request.into_parts();
assert_eq!(username.as_deref(), Some("admin@example.invalid"));
assert_eq!(parsed_password, password);
assert!(
serde_json::from_value::<AdminLoginRequest>(json!({
"password": password,
"rememberMe": true
}))
.is_err()
);
}
#[test]
fn auth_responses_should_keep_stable_wire_shapes() {
assert_eq!(
serde_json::to_value(AdminLoginData::new("2026-07-18T08:00:00+08:00".to_owned()))
.expect("serialize login"),
json!({ "expiresAt": "2026-07-18T08:00:00+08:00" })
);
assert_eq!(
serde_json::to_value(AdminSessionStatusData::new(true)).expect("serialize status"),
json!({ "authenticated": true })
);
assert_eq!(
serde_json::to_value(AdminLogoutData::new()).expect("serialize logout"),
json!({ "message": "Logged out successfully" })
);
}
#[tokio::test]
async fn default_auth_service_should_initialize_login_validate_and_logout() {
let fixture = AdminTestFixture::new().await;
let service = fixture.services.auth();
let session = service
.login(LoginCommand {
username: Some("admin_1".to_owned()),
password: "strong-admin-password".to_owned(),
})
.await
.expect("login succeeds");
assert!(
service
.validate_session(Some(&session.session_id))
.await
.expect("validate session")
);
assert_eq!(
service
.resolve_admin_user_id(Some(&session.session_id))
.await
.expect("resolve session")
.as_deref(),
Some("admin_1")
);
service
.logout(&session.session_id)
.await
.expect("logout session");
assert!(
!service
.validate_session(Some(&session.session_id))
.await
.expect("validate logged-out session")
);
assert_eq!(fixture.auth.audit_count(), 2);
}
#[tokio::test]
async fn default_auth_service_should_verify_only_full_plaintext_admin_key() {
let fixture = AdminTestFixture::new().await;
let key = format!("admin-{}", "a".repeat(64));
fixture.auth.set_api_key(&key);
assert!(
fixture
.services
.auth()
.verify_admin_api_key(&key)
.await
.unwrap()
);
assert!(
!fixture
.services
.auth()
.verify_admin_api_key("admin-short")
.await
.unwrap()
);
assert!(
!fixture
.services
.auth()
.verify_admin_api_key(&format!("admin-{}", "b".repeat(64)))
.await
.unwrap()
);
}
#[tokio::test]
async fn audit_failure_should_revoke_new_session_before_returning_it() {
let fixture = AdminTestFixture::new().await;
fixture.auth.fail_audit(true);
assert_eq!(
fixture
.services
.auth()
.login(LoginCommand {
username: None,
password: "strong-admin-password".to_owned(),
})
.await
.expect_err("audit failure rejects login"),
LoginError::Unavailable
);
assert_eq!(fixture.auth.session_count(), 0);
}