test(mcp): a key for a product that takes only a key, and a Host with the default port

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Fable 5.2 committed 2026-11-21 11:39:38 -07:00
1 parent e00e52f2f3
commit 4c36c1f020
2 files changed
+78 -2

No files matched your search

+8 -1
View File
@@ -295,10 +295,17 @@ fn front(books: &Books, headers: &HeaderMap) -> Result<Arc<Door>, Box<Response>>
(StatusCode::SERVICE_UNAVAILABLE, "web assistants are off").into_response(),
)));
};
// The host as the public address spells it: lowercase, and the scheme's
// own port (443) left off whether or not the caller wrote it.
let host = headers
.get(header::HOST)
.and_then(|h| h.to_str().ok())
.map(str::to_ascii_lowercase);
.map(str::to_ascii_lowercase)
.map(|h| {
let plain = door.issuer.starts_with("https://");
let default = if plain { ":443" } else { ":80" };
h.strip_suffix(default).map(str::to_string).unwrap_or(h)
});
if host.as_deref() != Some(door.host.as_str()) {
return Err(Box::new(no_store(
(StatusCode::MISDIRECTED_REQUEST, "not this address").into_response(),
+70 -1
View File
@@ -1685,7 +1685,10 @@ async fn refresh_tokens_survive_a_restart_and_only_their_hashes_are_kept() {
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
assert_eq!(std::fs::metadata(&file).unwrap().permissions().mode() & 0o777, 0o600);
assert_eq!(
std::fs::metadata(&file).unwrap().permissions().mode() & 0o777,
0o600
);
}
// A server that starts again reads it: the same refresh token works.
let again = Arc::new(Remote::new());
@@ -1707,3 +1710,69 @@ async fn refresh_tokens_survive_a_restart_and_only_their_hashes_are_kept() {
// the restart made is not the old one.
assert_ne!(out["access_token"].as_str().unwrap(), access);
}
#[tokio::test(flavor = "multi_thread")]
async fn a_key_made_for_a_product_that_takes_only_a_key_opens_the_door_with_the_same_permissions() {
let h = harness().await;
let (id, key) = match admin(
&h.books,
Request::RemoteMakeKey {
book: "acme".into(),
name: "Some Assistant".into(),
post: false,
},
)
.await
{
Response::RemoteSecret { id, secret, .. } => (id, secret.0),
other => panic!("{other:?}"),
};
assert_eq!(mcp(&h, Some(&key), "acme", initialize()).await.status, 200);
// It is one of "Who can reach these books", with read and draft only.
let listed = match admin(&h.books, Request::TokenList).await {
Response::Tokens(t) => t,
other => panic!("{other:?}"),
};
let row = listed.iter().find(|t| t.id == id).unwrap();
assert_eq!(row.name, "key-some-assistant");
assert_eq!(
row.scopes,
vec!["books:read".to_string(), "books:write".to_string()]
);
let post = call(
3,
"books.post_entry",
json!({"entry_id": 1, "confirmed": true, "request_key": "post-key-0002"}),
);
assert!(tool_text(&mcp(&h, Some(&key), "acme", post).await).contains("books:post"));
// Another company is not reached with it.
assert_eq!(mcp(&h, Some(&key), "beta", initialize()).await.status, 403);
// Revoked, it ends.
admin(
&h.books,
Request::TokenRevoke {
book: "acme".into(),
name: row.name.clone(),
id: Some(id),
},
)
.await;
assert_eq!(mcp(&h, Some(&key), "acme", initialize()).await.status, 401);
}
#[tokio::test(flavor = "multi_thread")]
async fn a_host_with_the_default_port_is_the_same_host() {
let h = harness().await;
let mut bytes = proxy::encode(
"203.0.113.9:1".parse().unwrap(),
"198.51.100.1:443".parse().unwrap(),
);
bytes.extend(request_bytes(
&format!("{}:443", h.host()),
"GET",
"/.well-known/oauth-authorization-server",
&[],
"",
));
assert_eq!(parse(&raw(h.addr, &bytes).await).status, 200);
}