Owner-authorized recovery of accumulated source changes.
Includes accepted work and in-progress changes; NOT a release or acceptance.
Parent baseline 1b6db09bd1. Runtime data, build caches and local credentials excluded.
979 lines
34 KiB
Rust
979 lines
34 KiB
Rust
//! Story 3.8 / AT-3.8-04, AT-3.8-05 — build-wave security regressions for the
|
||
//! Resource-wide revoke (owner decision A, ARC §§3–5).
|
||
//!
|
||
//! Each scenario runs on the same real stack as the pre-build checkpoint: real Admin
|
||
//! HTTP (Owner session + CSRF), real published Resources/Agents, the real MCP HTTP
|
||
//! dispatch for both supported revisions and one PostgreSQL schema. The fixture is
|
||
//! shared with `resources::revocation`; nothing here stubs the product.
|
||
|
||
use std::time::Duration;
|
||
|
||
use reqwest::StatusCode;
|
||
use serde_json::{Value, json};
|
||
use serial_test::serial;
|
||
|
||
use super::helpers::{
|
||
TEST_AUTH_EMAIL, TEST_AUTH_PASSWORD, create_foreign_workspace, create_owned_workspace,
|
||
ensure_viewer_membership, error_signature, header_value, login_client,
|
||
login_client_with_foreign_origin, login_client_without_csrf, read_response, table_row_count,
|
||
};
|
||
use super::revocation::{
|
||
KEPT_CANARY, KEPT_URI, LEGACY_REVISION, MODERN_REVISION, NOT_AVAILABLE_CODE,
|
||
NOT_AVAILABLE_MESSAGE, REVOKED_CANARY, REVOKED_CANARY_V2, REVOKED_NAME, REVOKED_URI,
|
||
create_mcp_key, create_published_resource, create_published_resource_only_agent, listed_uris,
|
||
mcp_list, mcp_read, read_text, spawn_revocation_stand, supported_revisions,
|
||
};
|
||
|
||
pub(super) async fn revoke(
|
||
client: &reqwest::Client,
|
||
server: &str,
|
||
resource_id: &str,
|
||
) -> reqwest::Response {
|
||
client
|
||
.post(format!("{server}/resources/{resource_id}/revoke"))
|
||
.send()
|
||
.await
|
||
.expect("resource revoke request must reach the real Admin router")
|
||
}
|
||
|
||
async fn tombstone_count(pool: &sqlx::PgPool) -> i64 {
|
||
table_row_count(pool, "resource_security_revocations").await
|
||
}
|
||
|
||
async fn assert_denied(body: &Value, revision: &str, agent: &str) {
|
||
assert!(
|
||
body.get("result").is_none(),
|
||
"{agent} on {revision}: a revoked Resource must not be served: {body}"
|
||
);
|
||
assert_eq!(
|
||
body["error"]["code"].as_i64(),
|
||
Some(NOT_AVAILABLE_CODE),
|
||
"{agent} on {revision}: the pinned safe NotAvailable code must be reused: {body}"
|
||
);
|
||
assert_eq!(
|
||
body["error"]["message"].as_str(),
|
||
Some(NOT_AVAILABLE_MESSAGE),
|
||
"{agent} on {revision}: the pinned safe NotAvailable message must be reused: {body}"
|
||
);
|
||
let text = body.to_string();
|
||
for leak in [
|
||
REVOKED_CANARY.trim(),
|
||
REVOKED_CANARY_V2.trim(),
|
||
REVOKED_NAME,
|
||
REVOKED_URI,
|
||
] {
|
||
assert!(
|
||
!text.contains(leak),
|
||
"{agent} on {revision}: a revoked read must not disclose Resource data ({leak}): {body}"
|
||
);
|
||
}
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Audited transition: one event, server-side identity, idempotent repeat
|
||
// ---------------------------------------------------------------------------
|
||
|
||
#[tokio::test]
|
||
async fn story38_revoke_is_audited_once_and_repeats_keep_the_first_event() {
|
||
let stand = spawn_revocation_stand("audit").await;
|
||
let pool = stand.registry.pool();
|
||
|
||
let first = revoke(&stand.admin, &stand.server, &stand.revoked_id).await;
|
||
let first_request_id = header_value(first.headers(), "x-request-id");
|
||
let first_trace_id = header_value(first.headers(), "x-trace-id");
|
||
let status = first.status();
|
||
assert_eq!(
|
||
status,
|
||
StatusCode::NO_CONTENT,
|
||
"the first revoke must answer 204"
|
||
);
|
||
assert_eq!(tombstone_count(pool).await, 1, "exactly one tombstone row");
|
||
|
||
let (actor, request_id, trace_id, revoked_at): (String, String, String, time::OffsetDateTime) =
|
||
sqlx::query_as(
|
||
"select actor_user_id, request_id, trace_id, revoked_at
|
||
from resource_security_revocations",
|
||
)
|
||
.fetch_one(pool)
|
||
.await
|
||
.expect("the tombstone must carry actor/correlation/time");
|
||
assert_eq!(
|
||
request_id, first_request_id,
|
||
"request_id must come from RequestContext"
|
||
);
|
||
assert_eq!(
|
||
trace_id, first_trace_id,
|
||
"trace_id must come from RequestContext"
|
||
);
|
||
let actor_email: String = sqlx::query_scalar("select email from users where id = $1")
|
||
.bind(&actor)
|
||
.fetch_one(pool)
|
||
.await
|
||
.expect("the tombstone actor must be a real user");
|
||
assert_eq!(
|
||
actor_email, TEST_AUTH_EMAIL,
|
||
"the actor must be the authenticated session, never request data"
|
||
);
|
||
|
||
// A repeated authorized command is still guarded and still 204, with no second event.
|
||
let repeat = revoke(&stand.admin, &stand.server, &stand.revoked_id).await;
|
||
assert_eq!(repeat.status(), StatusCode::NO_CONTENT);
|
||
assert_eq!(
|
||
tombstone_count(pool).await,
|
||
1,
|
||
"a repeat must not create a second row"
|
||
);
|
||
let repeated_at: time::OffsetDateTime =
|
||
sqlx::query_scalar("select revoked_at from resource_security_revocations")
|
||
.fetch_one(pool)
|
||
.await
|
||
.unwrap();
|
||
assert_eq!(
|
||
repeated_at, revoked_at,
|
||
"the first event time must be preserved"
|
||
);
|
||
|
||
// Two concurrent commands lock the same Resource row: one INSERT, both 204.
|
||
let (left, right) = tokio::join!(
|
||
revoke(&stand.admin, &stand.server, &stand.revoked_id),
|
||
revoke(&stand.admin, &stand.server, &stand.revoked_id)
|
||
);
|
||
assert_eq!(left.status(), StatusCode::NO_CONTENT);
|
||
assert_eq!(right.status(), StatusCode::NO_CONTENT);
|
||
assert_eq!(
|
||
tombstone_count(pool).await,
|
||
1,
|
||
"concurrent revokes must produce one row"
|
||
);
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// A broken INSERT must never look like a successful audited revoke
|
||
// ---------------------------------------------------------------------------
|
||
|
||
#[tokio::test]
|
||
async fn story38_failed_tombstone_insert_never_answers_a_false_204() {
|
||
let stand = spawn_revocation_stand("insert_failure").await;
|
||
let pool = stand.registry.pool();
|
||
sqlx::raw_sql(
|
||
"create function reject_story38_revocation_insert() returns trigger language plpgsql as $$
|
||
begin
|
||
raise exception 'injected revocation insert failure' using errcode = '23514';
|
||
end $$;
|
||
create trigger reject_story38_revocation_insert
|
||
before insert on resource_security_revocations
|
||
for each row execute function reject_story38_revocation_insert();",
|
||
)
|
||
.execute(pool)
|
||
.await
|
||
.expect("the failure fixture trigger must be created");
|
||
|
||
let failed = revoke(&stand.admin, &stand.server, &stand.revoked_id).await;
|
||
let status = failed.status();
|
||
let body = failed.text().await.unwrap_or_default();
|
||
assert_eq!(
|
||
status,
|
||
StatusCode::INTERNAL_SERVER_ERROR,
|
||
"a failed audited insert must be a sanitized storage error, not a false 204: {body}"
|
||
);
|
||
assert!(
|
||
!body.contains("injected") && !body.contains("resource_security_revocations"),
|
||
"the storage failure must not disclose SQL details: {body}"
|
||
);
|
||
assert_eq!(
|
||
tombstone_count(pool).await,
|
||
0,
|
||
"no undurable event may be recorded"
|
||
);
|
||
|
||
// Fail closed the other way too: without a tombstone the read stays available.
|
||
let still_readable = mcp_read(
|
||
&stand.mcp,
|
||
&stand.mcp_url,
|
||
&stand.mcp_key,
|
||
MODERN_REVISION,
|
||
REVOKED_URI,
|
||
)
|
||
.await;
|
||
assert_eq!(read_text(&still_readable).as_deref(), Some(REVOKED_CANARY));
|
||
|
||
sqlx::raw_sql(
|
||
"drop trigger reject_story38_revocation_insert on resource_security_revocations;
|
||
drop function reject_story38_revocation_insert();",
|
||
)
|
||
.execute(pool)
|
||
.await
|
||
.unwrap();
|
||
assert_eq!(
|
||
revoke(&stand.admin, &stand.server, &stand.revoked_id)
|
||
.await
|
||
.status(),
|
||
StatusCode::NO_CONTENT,
|
||
"a bounded retry after a failed insert must succeed"
|
||
);
|
||
assert_eq!(tombstone_count(pool).await, 1);
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Every version, every warmed Agent, both supported revisions
|
||
// ---------------------------------------------------------------------------
|
||
|
||
#[tokio::test]
|
||
async fn story38_revoke_denies_all_versions_on_both_warmed_agents_and_revisions() {
|
||
let stand = spawn_revocation_stand("versions").await;
|
||
let revisions = supported_revisions();
|
||
|
||
// Positive controls: both Agents serve their exact versions before revoke.
|
||
for revision in &revisions {
|
||
let v1 = mcp_read(
|
||
&stand.mcp,
|
||
&stand.mcp_url,
|
||
&stand.mcp_key,
|
||
revision,
|
||
REVOKED_URI,
|
||
)
|
||
.await;
|
||
assert_eq!(
|
||
read_text(&v1).as_deref(),
|
||
Some(REVOKED_CANARY),
|
||
"{revision}: v1 control"
|
||
);
|
||
let v2 = mcp_read(
|
||
&stand.mcp,
|
||
&stand.mcp_url_second,
|
||
&stand.mcp_key_second,
|
||
revision,
|
||
REVOKED_URI,
|
||
)
|
||
.await;
|
||
assert_eq!(
|
||
read_text(&v2).as_deref(),
|
||
Some(REVOKED_CANARY_V2),
|
||
"{revision}: version 2 control"
|
||
);
|
||
for (url, key) in [
|
||
(&stand.mcp_url, &stand.mcp_key),
|
||
(&stand.mcp_url_second, &stand.mcp_key_second),
|
||
] {
|
||
assert!(
|
||
listed_uris(&mcp_list(&stand.mcp, url, key, revision).await)
|
||
.contains(&REVOKED_URI.to_owned()),
|
||
"{revision}: the target must be listed before revoke"
|
||
);
|
||
}
|
||
}
|
||
|
||
assert_eq!(
|
||
revoke(&stand.admin, &stand.server, &stand.revoked_id)
|
||
.await
|
||
.status(),
|
||
StatusCode::NO_CONTENT
|
||
);
|
||
|
||
for revision in &revisions {
|
||
for (agent, url, key) in [
|
||
("first", &stand.mcp_url, &stand.mcp_key),
|
||
("second", &stand.mcp_url_second, &stand.mcp_key_second),
|
||
] {
|
||
let listed = mcp_list(&stand.mcp, url, key, revision).await;
|
||
let uris = listed_uris(&listed);
|
||
assert!(
|
||
!uris.contains(&REVOKED_URI.to_owned()),
|
||
"{agent} on {revision}: a revoked Resource must disappear from list: {uris:?}"
|
||
);
|
||
assert!(
|
||
uris.contains(&KEPT_URI.to_owned()),
|
||
"{agent} on {revision}: the unrevoked control must stay listed: {uris:?}"
|
||
);
|
||
let text = listed.to_string();
|
||
assert!(
|
||
!text.contains(REVOKED_NAME),
|
||
"{agent} on {revision}: no revoked metadata"
|
||
);
|
||
|
||
let denied = mcp_read(&stand.mcp, url, key, revision, REVOKED_URI).await;
|
||
assert_denied(&denied, revision, agent).await;
|
||
|
||
let kept = mcp_read(&stand.mcp, url, key, revision, KEPT_URI).await;
|
||
assert_eq!(
|
||
read_text(&kept).as_deref(),
|
||
Some(KEPT_CANARY),
|
||
"{agent} on {revision}: another bound Resource must stay readable: {kept}"
|
||
);
|
||
}
|
||
}
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Denial happens before any artifact byte; the counter's positive control is real
|
||
// ---------------------------------------------------------------------------
|
||
|
||
#[tokio::test]
|
||
async fn story38_revoked_read_opens_zero_artifact_bytes() {
|
||
let stand = spawn_revocation_stand("byte_ordering").await;
|
||
assert_eq!(
|
||
revoke(&stand.admin, &stand.server, &stand.revoked_id)
|
||
.await
|
||
.status(),
|
||
StatusCode::NO_CONTENT
|
||
);
|
||
crank_artifacts::test_support::reset_artifact_reads();
|
||
|
||
for revision in supported_revisions() {
|
||
for (url, key) in [
|
||
(&stand.mcp_url, &stand.mcp_key),
|
||
(&stand.mcp_url_second, &stand.mcp_key_second),
|
||
] {
|
||
let denied = mcp_read(&stand.mcp, url, key, &revision, REVOKED_URI).await;
|
||
assert_denied(&denied, &revision, "revoked agent").await;
|
||
}
|
||
}
|
||
assert_eq!(
|
||
crank_artifacts::test_support::artifact_reads(),
|
||
0,
|
||
"a revoked read must be denied before any artifact byte is opened"
|
||
);
|
||
|
||
// Positive control: the same counter observes the allowed control read.
|
||
let kept = mcp_read(
|
||
&stand.mcp,
|
||
&stand.mcp_url,
|
||
&stand.mcp_key,
|
||
LEGACY_REVISION,
|
||
KEPT_URI,
|
||
)
|
||
.await;
|
||
assert_eq!(read_text(&kept).as_deref(), Some(KEPT_CANARY));
|
||
assert!(
|
||
crank_artifacts::test_support::artifact_reads() >= 1,
|
||
"the allowed read must be observable as an artifact read"
|
||
);
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Owner + CSRF + real target only; every refusal writes nothing
|
||
// ---------------------------------------------------------------------------
|
||
|
||
#[tokio::test]
|
||
#[serial]
|
||
async fn story38_revoke_denials_mutate_nothing() {
|
||
let stand = spawn_revocation_stand("denials").await;
|
||
let pool = stand.registry.pool();
|
||
|
||
let anonymous = reqwest::Client::new();
|
||
let status = revoke(&anonymous, &stand.server, &stand.revoked_id)
|
||
.await
|
||
.status();
|
||
assert_eq!(
|
||
status,
|
||
StatusCode::UNAUTHORIZED,
|
||
"no session must be refused"
|
||
);
|
||
|
||
let without_csrf =
|
||
login_client_without_csrf(&stand.server, TEST_AUTH_EMAIL, TEST_AUTH_PASSWORD).await;
|
||
let status = revoke(&without_csrf, &stand.server, &stand.revoked_id)
|
||
.await
|
||
.status();
|
||
assert_eq!(
|
||
status,
|
||
StatusCode::FORBIDDEN,
|
||
"no CSRF token must be refused"
|
||
);
|
||
|
||
let foreign_origin =
|
||
login_client_with_foreign_origin(&stand.server, TEST_AUTH_EMAIL, TEST_AUTH_PASSWORD).await;
|
||
let status = revoke(&foreign_origin, &stand.server, &stand.revoked_id)
|
||
.await
|
||
.status();
|
||
assert_eq!(
|
||
status,
|
||
StatusCode::FORBIDDEN,
|
||
"cross-origin must be refused"
|
||
);
|
||
|
||
let (viewer_email, viewer_password) = ensure_viewer_membership(&stand.registry).await;
|
||
let viewer = login_client(&stand.server, viewer_email, viewer_password).await;
|
||
let (status, text, body) =
|
||
read_response(revoke(&viewer, &stand.server, &stand.revoked_id).await).await;
|
||
assert_eq!(
|
||
status,
|
||
StatusCode::FORBIDDEN,
|
||
"a Viewer must not revoke: {} {text}",
|
||
error_signature(status, &body)
|
||
);
|
||
|
||
let foreign_workspace = create_foreign_workspace(&stand.registry).await;
|
||
let foreign_base = stand
|
||
.server
|
||
.replace("ws_default", foreign_workspace.as_str());
|
||
let status = revoke(&stand.admin, &foreign_base, &stand.revoked_id)
|
||
.await
|
||
.status();
|
||
assert_eq!(
|
||
status,
|
||
StatusCode::FORBIDDEN,
|
||
"a workspace without membership must be refused"
|
||
);
|
||
|
||
let owned_workspace = create_owned_workspace(&stand.registry).await;
|
||
let owned_base = stand.server.replace("ws_default", owned_workspace.as_str());
|
||
// Memberships are part of the authenticated session: log in again after granting it.
|
||
let owner_in_owned = login_client(&stand.server, TEST_AUTH_EMAIL, TEST_AUTH_PASSWORD).await;
|
||
let (status, text, body) =
|
||
read_response(revoke(&owner_in_owned, &owned_base, &stand.revoked_id).await).await;
|
||
assert_eq!(
|
||
status,
|
||
StatusCode::NOT_FOUND,
|
||
"a foreign Resource must answer the sanitized 404: {text}"
|
||
);
|
||
assert!(
|
||
!text.contains(REVOKED_NAME) && !text.contains(REVOKED_URI),
|
||
"the sanitized 404 must not disclose the foreign Resource: {body}"
|
||
);
|
||
|
||
let (status, text, _) =
|
||
read_response(revoke(&stand.admin, &stand.server, "res_story38_unknown").await).await;
|
||
assert_eq!(
|
||
status,
|
||
StatusCode::NOT_FOUND,
|
||
"an unknown Resource must answer 404: {text}"
|
||
);
|
||
|
||
assert_eq!(
|
||
tombstone_count(pool).await,
|
||
0,
|
||
"no refusal may create a tombstone"
|
||
);
|
||
// The Resource is still fully readable after the whole denial matrix.
|
||
let still_readable = mcp_read(
|
||
&stand.mcp,
|
||
&stand.mcp_url,
|
||
&stand.mcp_key,
|
||
MODERN_REVISION,
|
||
REVOKED_URI,
|
||
)
|
||
.await;
|
||
assert_eq!(read_text(&still_readable).as_deref(), Some(REVOKED_CANARY));
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Revoked Resources cannot be republished or rebound; archive/Admin stays intact
|
||
// ---------------------------------------------------------------------------
|
||
|
||
#[tokio::test]
|
||
async fn story38_revoke_blocks_publication_and_binding_but_keeps_archive_and_admin_reads() {
|
||
let stand = spawn_revocation_stand("lifecycle").await;
|
||
|
||
// An Agent version that is already bound to the target but not yet published: the
|
||
// revoke must land before its publication, so `publish_agent` itself has to deny it.
|
||
let pending_agent = super::super::common::assert_success_json(
|
||
stand
|
||
.admin
|
||
.post(format!("{}/agents", stand.server))
|
||
.json(&json!({
|
||
"slug": "story38-pending-agent",
|
||
"display_name": "Story 3.8 pending",
|
||
"description": "Story 3.8 publish denial",
|
||
"instructions": {},
|
||
"tool_selection_policy": {}
|
||
}))
|
||
.send()
|
||
.await
|
||
.expect("pending Agent creation must complete"),
|
||
)
|
||
.await;
|
||
let pending_agent_id = pending_agent["agent_id"].as_str().unwrap().to_owned();
|
||
let pending_etag =
|
||
super::super::common::agent_etag(&stand.admin, &stand.server, &pending_agent_id).await;
|
||
let (status, text, _) = read_response(
|
||
stand
|
||
.admin
|
||
.post(format!(
|
||
"{}/agents/{pending_agent_id}/bindings",
|
||
stand.server
|
||
))
|
||
.header(reqwest::header::IF_MATCH, pending_etag)
|
||
.json(&json!({
|
||
"bindings": [],
|
||
"tool_selection_policy": {},
|
||
"resource_bindings": [
|
||
{ "resource_id": stand.revoked_id, "resource_version": 1 }
|
||
]
|
||
}))
|
||
.send()
|
||
.await
|
||
.expect("pending binding request must complete"),
|
||
)
|
||
.await;
|
||
assert_eq!(
|
||
status,
|
||
StatusCode::OK,
|
||
"binding before revoke must save: {text}"
|
||
);
|
||
|
||
assert_eq!(
|
||
revoke(&stand.admin, &stand.server, &stand.revoked_id)
|
||
.await
|
||
.status(),
|
||
StatusCode::NO_CONTENT
|
||
);
|
||
|
||
let pending_etag =
|
||
super::super::common::agent_etag(&stand.admin, &stand.server, &pending_agent_id).await;
|
||
let (status, text, body) = read_response(
|
||
stand
|
||
.admin
|
||
.post(format!(
|
||
"{}/agents/{pending_agent_id}/publish",
|
||
stand.server
|
||
))
|
||
.header(reqwest::header::IF_MATCH, pending_etag)
|
||
.json(&json!({ "version": 1 }))
|
||
.send()
|
||
.await
|
||
.expect("Agent publish request must complete"),
|
||
)
|
||
.await;
|
||
assert_eq!(
|
||
status,
|
||
StatusCode::CONFLICT,
|
||
"publishing an Agent bound to a revoked Resource must be refused: {text}"
|
||
);
|
||
assert_eq!(
|
||
body["error"]["context"]["error_code"].as_str(),
|
||
Some("resource_revoked"),
|
||
"the Agent publish refusal must use the stable revoked code: {body}"
|
||
);
|
||
|
||
// A later edit does not lift the revoke; the next publication is refused.
|
||
let card = super::helpers::get_card(&stand.admin, &stand.server, &stand.revoked_id).await;
|
||
let updated_at = super::helpers::draft_updated_at(&card);
|
||
let metadata = super::update_metadata("", "text/plain", "internal", &updated_at);
|
||
let (status, text, _) = read_response(
|
||
stand
|
||
.admin
|
||
.put(format!("{}/resources/{}", stand.server, stand.revoked_id))
|
||
.multipart(super::helpers::draft_form(
|
||
&metadata,
|
||
b"post-revoke edit\n",
|
||
"post-revoke.txt",
|
||
"text/plain",
|
||
))
|
||
.send()
|
||
.await
|
||
.expect("post-revoke edit must complete"),
|
||
)
|
||
.await;
|
||
assert_eq!(
|
||
status,
|
||
StatusCode::OK,
|
||
"editing a revoked draft stays allowed: {text}"
|
||
);
|
||
|
||
let card = super::helpers::get_card(&stand.admin, &stand.server, &stand.revoked_id).await;
|
||
let updated_at = super::helpers::draft_updated_at(&card);
|
||
let (status, text, body) = read_response(
|
||
stand
|
||
.admin
|
||
.post(format!(
|
||
"{}/resources/{}/publish",
|
||
stand.server, stand.revoked_id
|
||
))
|
||
.json(&json!({ "expected_updated_at": updated_at }))
|
||
.send()
|
||
.await
|
||
.expect("post-revoke publish must complete"),
|
||
)
|
||
.await;
|
||
assert_eq!(
|
||
status,
|
||
StatusCode::CONFLICT,
|
||
"a revoked Resource must not be published: {text}"
|
||
);
|
||
assert_eq!(
|
||
body["error"]["context"]["error_code"].as_str(),
|
||
Some("resource_revoked"),
|
||
"the refusal must use the stable revoked code: {body}"
|
||
);
|
||
|
||
// A new binding to a revoked Resource is refused in the same transaction.
|
||
let agent = super::super::common::assert_success_json(
|
||
stand
|
||
.admin
|
||
.post(format!("{}/agents", stand.server))
|
||
.json(&json!({
|
||
"slug": "story38-post-revoke-agent",
|
||
"display_name": "Story 3.8 post-revoke",
|
||
"description": "Story 3.8 binding denial",
|
||
"instructions": {},
|
||
"tool_selection_policy": {}
|
||
}))
|
||
.send()
|
||
.await
|
||
.expect("Agent creation must complete"),
|
||
)
|
||
.await;
|
||
let agent_id = agent["agent_id"].as_str().unwrap().to_owned();
|
||
let etag = super::super::common::agent_etag(&stand.admin, &stand.server, &agent_id).await;
|
||
let (status, text, body) = read_response(
|
||
stand
|
||
.admin
|
||
.post(format!("{}/agents/{agent_id}/bindings", stand.server))
|
||
.header(reqwest::header::IF_MATCH, etag)
|
||
.json(&json!({
|
||
"bindings": [],
|
||
"tool_selection_policy": {},
|
||
"resource_bindings": [
|
||
{ "resource_id": stand.revoked_id, "resource_version": 1 }
|
||
]
|
||
}))
|
||
.send()
|
||
.await
|
||
.expect("binding request must complete"),
|
||
)
|
||
.await;
|
||
assert_eq!(
|
||
status,
|
||
StatusCode::CONFLICT,
|
||
"a revoked Resource must not receive new bindings: {text}"
|
||
);
|
||
assert_eq!(
|
||
body["error"]["context"]["error_code"].as_str(),
|
||
Some("resource_revoked"),
|
||
"the binding refusal must use the stable revoked code: {body}"
|
||
);
|
||
|
||
// Archive stays a lifecycle transition, the immutable snapshot and the protected
|
||
// administrative read stay available, and MCP still denies the Resource.
|
||
let card = super::helpers::get_card(&stand.admin, &stand.server, &stand.revoked_id).await;
|
||
let updated_at = super::helpers::draft_updated_at(&card);
|
||
let (status, text, _) = read_response(
|
||
stand
|
||
.admin
|
||
.post(format!(
|
||
"{}/resources/{}/archive",
|
||
stand.server, stand.revoked_id
|
||
))
|
||
.json(&json!({ "expected_updated_at": updated_at }))
|
||
.send()
|
||
.await
|
||
.expect("archive request must complete"),
|
||
)
|
||
.await;
|
||
assert_eq!(
|
||
status,
|
||
StatusCode::OK,
|
||
"archive must not be blocked by a revoke: {text}"
|
||
);
|
||
let (status, text, _) = read_response(
|
||
stand
|
||
.admin
|
||
.get(format!(
|
||
"{}/resources/{}/versions/1",
|
||
stand.server, stand.revoked_id
|
||
))
|
||
.send()
|
||
.await
|
||
.expect("administrative version read must complete"),
|
||
)
|
||
.await;
|
||
assert_eq!(
|
||
status,
|
||
StatusCode::OK,
|
||
"the protected administrative read must survive archive+revoke: {text}"
|
||
);
|
||
let denied = mcp_read(
|
||
&stand.mcp,
|
||
&stand.mcp_url,
|
||
&stand.mcp_key,
|
||
MODERN_REVISION,
|
||
REVOKED_URI,
|
||
)
|
||
.await;
|
||
assert_denied(&denied, MODERN_REVISION, "archived+revoked agent").await;
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Pagination: cursor addresses the original immutable bindings, not a filtered page
|
||
// ---------------------------------------------------------------------------
|
||
|
||
#[tokio::test]
|
||
async fn story38_revoke_mid_pagination_keeps_offsets_without_duplicates_or_shift() {
|
||
let stand = spawn_revocation_stand("pagination").await;
|
||
let extra_one = create_published_resource(
|
||
&stand.admin,
|
||
&stand.server,
|
||
"crank://resources/story38-page-one",
|
||
"story38_page_one",
|
||
"page one\n",
|
||
)
|
||
.await;
|
||
let extra_two = create_published_resource(
|
||
&stand.admin,
|
||
&stand.server,
|
||
"crank://resources/story38-page-two",
|
||
"story38_page_two",
|
||
"page two\n",
|
||
)
|
||
.await;
|
||
let slug = "story38-pagination-agent";
|
||
let agent_id = create_published_resource_only_agent(
|
||
&stand.admin,
|
||
&stand.server,
|
||
slug,
|
||
&[
|
||
(&stand.revoked_id, 1),
|
||
(&stand.kept_id, 1),
|
||
(&extra_one, 1),
|
||
(&extra_two, 1),
|
||
],
|
||
)
|
||
.await;
|
||
let key = create_mcp_key(&stand.admin, &stand.server, &agent_id).await;
|
||
let base = stand
|
||
.mcp_url
|
||
.rsplit_once('/')
|
||
.expect("MCP URL carries the Agent slug")
|
||
.0;
|
||
let url = format!("{base}/{slug}");
|
||
|
||
let full = listed_uris(&mcp_list(&stand.mcp, &url, &key, MODERN_REVISION).await);
|
||
assert_eq!(
|
||
full.len(),
|
||
4,
|
||
"the fixture Agent must bind four Resources: {full:?}"
|
||
);
|
||
|
||
// First page: one element, then revoke the element behind the cursor and one that
|
||
// still lies ahead of it.
|
||
let page_one = mcp_list_with_limit(&stand.mcp, &url, &key, MODERN_REVISION, None, 1).await;
|
||
let first = listed_uris(&page_one);
|
||
assert_eq!(
|
||
first.len(),
|
||
1,
|
||
"limit=1 must yield a short first page: {page_one}"
|
||
);
|
||
let cursor = page_one["result"]["nextCursor"]
|
||
.as_str()
|
||
.expect("a non-final page must carry nextCursor")
|
||
.to_owned();
|
||
let behind = first[0].clone();
|
||
let ahead = full[2].clone();
|
||
|
||
for uri in [&behind, &ahead] {
|
||
let resource_id = match uri.as_str() {
|
||
REVOKED_URI => &stand.revoked_id,
|
||
KEPT_URI => &stand.kept_id,
|
||
"crank://resources/story38-page-one" => &extra_one,
|
||
_ => &extra_two,
|
||
};
|
||
assert_eq!(
|
||
revoke(&stand.admin, &stand.server, resource_id)
|
||
.await
|
||
.status(),
|
||
StatusCode::NO_CONTENT
|
||
);
|
||
}
|
||
|
||
let mut page =
|
||
mcp_list_with_limit(&stand.mcp, &url, &key, MODERN_REVISION, Some(&cursor), 1).await;
|
||
let mut seen: Vec<String> = first.clone();
|
||
let mut continuation: Vec<String> = Vec::new();
|
||
let mut pages = 0;
|
||
loop {
|
||
pages += 1;
|
||
assert!(pages < 10, "pagination must terminate: {page}");
|
||
for uri in listed_uris(&page) {
|
||
assert!(
|
||
!seen.contains(&uri),
|
||
"a continuation must not repeat an element: {uri} in {seen:?}"
|
||
);
|
||
seen.push(uri.clone());
|
||
continuation.push(uri);
|
||
}
|
||
match page["result"]["nextCursor"].as_str() {
|
||
Some(next) => {
|
||
page = mcp_list_with_limit(&stand.mcp, &url, &key, MODERN_REVISION, Some(next), 1)
|
||
.await;
|
||
}
|
||
None => break,
|
||
}
|
||
}
|
||
assert!(
|
||
!continuation.contains(&behind),
|
||
"an element revoked behind the cursor must never reappear after it: {continuation:?}"
|
||
);
|
||
assert!(
|
||
!seen.contains(&ahead),
|
||
"an element revoked ahead of the cursor must be skipped"
|
||
);
|
||
let expected: Vec<String> = full
|
||
.iter()
|
||
.filter(|uri| **uri != behind && **uri != ahead)
|
||
.cloned()
|
||
.collect();
|
||
let mut sorted_seen = continuation.clone();
|
||
sorted_seen.sort();
|
||
let mut sorted_expected = expected.clone();
|
||
sorted_expected.sort();
|
||
assert_eq!(
|
||
sorted_seen, sorted_expected,
|
||
"the continuation must yield exactly the remaining unrevoked elements once"
|
||
);
|
||
}
|
||
|
||
async fn mcp_list_with_limit(
|
||
client: &reqwest::Client,
|
||
mcp_url: &str,
|
||
api_key: &str,
|
||
revision: &str,
|
||
cursor: Option<&str>,
|
||
limit: usize,
|
||
) -> Value {
|
||
let params = match cursor {
|
||
Some(cursor) => json!({ "cursor": cursor, "limit": limit, "_meta": modern_meta(revision) }),
|
||
None => json!({ "limit": limit, "_meta": modern_meta(revision) }),
|
||
};
|
||
let response = client
|
||
.post(mcp_url)
|
||
.header(
|
||
reqwest::header::ACCEPT,
|
||
"application/json, text/event-stream",
|
||
)
|
||
.header(reqwest::header::AUTHORIZATION, format!("Bearer {api_key}"))
|
||
.header("MCP-Protocol-Version", revision)
|
||
.header("Mcp-Method", "resources/list")
|
||
.json(&json!({
|
||
"jsonrpc": "2.0",
|
||
"id": 31,
|
||
"method": "resources/list",
|
||
"params": params
|
||
}))
|
||
.send()
|
||
.await
|
||
.expect("limited resources/list must reach the MCP server");
|
||
response
|
||
.json::<Value>()
|
||
.await
|
||
.expect("limited resources/list must answer JSON-RPC")
|
||
}
|
||
|
||
fn modern_meta(revision: &str) -> Value {
|
||
json!({
|
||
"io.modelcontextprotocol/protocolVersion": revision,
|
||
"io.modelcontextprotocol/clientCapabilities": {},
|
||
"io.modelcontextprotocol/clientInfo": { "name": "crank-story38-pagination", "version": "0.1.0" }
|
||
})
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Lock ordering: revoke waits for an in-flight shared read lock (real pg_locks)
|
||
// ---------------------------------------------------------------------------
|
||
|
||
#[tokio::test]
|
||
async fn story38_revoke_waits_for_an_in_flight_resource_read_lock() {
|
||
use crank_core::WorkspaceId;
|
||
use crank_core::ids::ResourceId;
|
||
use crank_registry::model::resource::RevokeResourceRequest;
|
||
|
||
let stand = spawn_revocation_stand("lock_ordering").await;
|
||
let pool = stand.registry.pool();
|
||
let actor: String = sqlx::query_scalar("select id from users where email = $1")
|
||
.bind(TEST_AUTH_EMAIL)
|
||
.fetch_one(pool)
|
||
.await
|
||
.expect("the fixture owner must exist");
|
||
|
||
// The registry's test pool is single-connection (`connect` → `connect_in_schema`), so the
|
||
// in-flight read must hold its own PostgreSQL connection; otherwise the concurrent revoke
|
||
// would wait for a pool slot instead of the row lock.
|
||
let base_url = crank_test_support::postgres_database_url().await;
|
||
let schema: String = sqlx::query_scalar("select current_schema()")
|
||
.fetch_one(pool)
|
||
.await
|
||
.expect("the test schema must be resolvable");
|
||
let reader_pool =
|
||
sqlx::PgPool::connect(&format!("{base_url}?options=-csearch_path%3D{schema}"))
|
||
.await
|
||
.expect("the in-flight reader pool must connect to the same test schema");
|
||
|
||
// An in-flight authorized read holds FOR SHARE on the Resource row.
|
||
let mut held = reader_pool.begin().await.expect("read-fixture transaction");
|
||
sqlx::query(
|
||
"select 1 from resources
|
||
where workspace_id = 'ws_default' and resource_id = $1
|
||
for share",
|
||
)
|
||
.bind(&stand.revoked_id)
|
||
.execute(&mut *held)
|
||
.await
|
||
.expect("the read fixture must take the Resource lock");
|
||
|
||
let registry = stand.registry.clone();
|
||
let resource_id = ResourceId::new(stand.revoked_id.clone());
|
||
let handle = tokio::spawn(async move {
|
||
registry
|
||
.revoke_resource(RevokeResourceRequest {
|
||
workspace_id: WorkspaceId::new("ws_default"),
|
||
resource_id,
|
||
actor_user_id: actor,
|
||
request_id: "story38-lock-request".to_owned(),
|
||
trace_id: "story38-lock-trace".to_owned(),
|
||
revoked_at: time::OffsetDateTime::now_utc(),
|
||
})
|
||
.await
|
||
.is_ok()
|
||
});
|
||
wait_for_blocked_revoke(&mut held).await;
|
||
assert!(
|
||
!handle.is_finished(),
|
||
"revoke must wait for the in-flight Resource read, not bypass its lock"
|
||
);
|
||
|
||
held.rollback()
|
||
.await
|
||
.expect("release the read fixture lock");
|
||
let completed = tokio::time::timeout(Duration::from_secs(10), handle)
|
||
.await
|
||
.expect("revoke must finish once the read lock is released")
|
||
.expect("revoke task must not panic");
|
||
assert!(
|
||
completed,
|
||
"revoke must succeed after the read lock is released"
|
||
);
|
||
assert_eq!(tombstone_count(pool).await, 1);
|
||
}
|
||
|
||
/// Deterministic barrier: wait until this database reports a backend blocked on a lock
|
||
/// (the concurrent revoke waiting behind the shared reader). `pg_locks` does not expose
|
||
/// row-level waits, so the probe uses `pg_stat_activity` and runs on the fixture
|
||
/// transaction itself, needing no extra pool connection.
|
||
async fn wait_for_blocked_revoke(transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>) {
|
||
for _ in 0..400 {
|
||
let blocked: i64 = sqlx::query_scalar(
|
||
"select count(*) from pg_stat_activity
|
||
where datname = current_database()
|
||
and wait_event_type = 'Lock'
|
||
and pid <> pg_backend_pid()",
|
||
)
|
||
.fetch_one(&mut **transaction)
|
||
.await
|
||
.unwrap_or(0);
|
||
if blocked > 0 {
|
||
return;
|
||
}
|
||
tokio::time::sleep(Duration::from_millis(25)).await;
|
||
}
|
||
let observed: Vec<(String, Option<String>, Option<String>, String)> = sqlx::query_as(
|
||
"select state, wait_event_type, wait_event, left(coalesce(query, ''), 100)
|
||
from pg_stat_activity
|
||
where datname = current_database()
|
||
order by pid",
|
||
)
|
||
.fetch_all(&mut **transaction)
|
||
.await
|
||
.unwrap_or_default();
|
||
panic!("a concurrent revoke never blocked on the Resource row lock: {observed:?}");
|
||
}
|