Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

fix(auth): ignore stale bearer when auth is disabled

Vitor Vilas Boas64a88c464564
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a6f92c69..5941197a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
+### Fixed
+- Authentication-disabled HTTP servers now ignore stale or unexpected Bearer
+ headers and preserve anonymous access. Previously, a client retaining an old
+ `AI_MEMORY_AUTH_TOKEN` received `401 Unauthorized` even though the server
+ reported `auth=false`; invalid Bearers remain rejected whenever static or
+ human authentication is enabled. (#639)
+
## [2.0.3] - 2026-09-04
### Changed
diff --git a/crates/ai-memory-mcp/src/auth.rs b/crates/ai-memory-mcp/src/auth.rs
index 8fb3075f..363fa9c4 100644
--- a/crates/ai-memory-mcp/src/auth.rs
+++ b/crates/ai-memory-mcp/src/auth.rs
@@ -406,7 +406,11 @@ pub async fn require_bearer(
match authenticate_bearer(&state, &mut req).await {
Ok(BearerAuth::Authenticated) => next.run(req).await,
Err(resp) => resp,
- Ok(BearerAuth::Absent) if !state.enabled() => {
+ Ok(BearerAuth::Absent | BearerAuth::Rejected) if !state.enabled() => {
+ // With no configured authority the wire gate is disabled. A
+ // client may still carry a stale bearer from an older secured
+ // deployment; treat it like an anonymous request instead of
+ // making the no-auth server stricter than an absent header.
req.extensions_mut().insert(ActorContext::anonymous());
req.extensions_mut().insert(AuthLevel::Anonymous);
next.run(req).await
@@ -551,6 +555,22 @@ mod tests {
assert_eq!(resp.status(), StatusCode::OK);
}
+ #[tokio::test]
+ async fn no_token_configured_ignores_an_unexpected_bearer() {
+ let r = router_with_auth(None);
+ let resp = r
+ .oneshot(
+ Request::builder()
+ .uri("/probe")
+ .header("Authorization", "Bearer stale-client-token")
+ .body(Body::empty())
+ .unwrap(),
+ )
+ .await
+ .unwrap();
+ assert_eq!(resp.status(), StatusCode::OK);
+ }
+
#[tokio::test]
async fn missing_header_returns_401_with_www_authenticate() {
let r = router_with_auth(Some("secret"));
diff --git a/crates/ai-memory-mcp/src/human_auth.rs b/crates/ai-memory-mcp/src/human_auth.rs
index 8c3830a4..bcfb6ffb 100644
--- a/crates/ai-memory-mcp/src/human_auth.rs
+++ b/crates/ai-memory-mcp/src/human_auth.rs
@@ -1019,22 +1019,22 @@ pub async fn require_dual_auth(
mut req: Request<axum::body::Body>,
next: Next,
) -> Response {
- match crate::auth::authenticate_bearer(&state, &mut req).await {
+ let bearer = match crate::auth::authenticate_bearer(&state, &mut req).await {
Ok(crate::auth::BearerAuth::Authenticated) => {
req.extensions_mut().insert(state.clone());
return next.run(req).await;
}
Err(resp) => return resp,
- Ok(crate::auth::BearerAuth::Rejected) => {
- return crate::auth::unauthorized_bearer();
- }
- Ok(crate::auth::BearerAuth::Absent) => {}
- }
+ Ok(outcome) => outcome,
+ };
let human_configured = match human_auth_configured(&state).await {
Ok(configured) => configured,
Err(response) => return response,
};
+ if bearer == crate::auth::BearerAuth::Rejected && (state.enabled() || human_configured) {
+ return crate::auth::unauthorized_bearer();
+ }
if !human_configured {
if !state.enabled() {
req.extensions_mut().insert(ActorContext::anonymous());
@@ -1372,6 +1372,23 @@ mod tests {
))
}
+ #[tokio::test]
+ async fn dual_auth_ignores_an_unexpected_bearer_when_auth_is_disabled() {
+ let resp = dual_router(Arc::new(AuthState::new(None)))
+ .oneshot(
+ axum::http::Request::builder()
+ .uri("/probe")
+ .header("authorization", "Bearer stale-client-token")
+ .body(axum::body::Body::empty())
+ .unwrap(),
+ )
+ .await
+ .unwrap();
+ assert_eq!(resp.status(), StatusCode::OK);
+ let body = axum::body::to_bytes(resp.into_body(), 1024).await.unwrap();
+ assert_eq!(&body[..], b"anonymous");
+ }
+
async fn json_error(resp: axum::http::Response<axum::body::Body>) -> serde_json::Value {
let bytes = axum::body::to_bytes(resp.into_body(), 4096).await.unwrap();
serde_json::from_slice(&bytes).unwrap()