Files
crank/apps/mcp-server/tests/integration/static_resources/safety.rs
T
bsodfather 54ce770682
CI / Rust Checks (pull_request) Failing after 13m41s
CI / UI Checks (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Deploy (pull_request) Skipped
Community Release / release (push) Failing after 13m33s
feat(epic3): deliver the accepted Epic 3 scope through Story 3.17
Recovery delivery of the accumulated, TL-accepted Epic 3 work that had never been
committed to Git (stories 3.6-3.17, see the crank board). Story 3.18 is not accepted
yet and is deliberately excluded: its logging surface, its capability registration,
its oracle projections and its tests are not part of this tree.

Accepted scope
- 3.6 typed static Resource Drafts; 3.7 publication and exact binding of static
  Resource Versions; 3.8 MCP discovery/read and revocation of static Resources;
- 3.9 Resource import/export in API and UI plus TLS dependency maintenance;
  3.10 REST-backed Resource templates; 3.11 shared REST read runtime and the MCP
  revision/capability foundation;
- 3.12 parameterised Prompt Drafts with preview; 3.13 Prompt publication and exact
  version binding to Agents; 3.14 MCP prompts; 3.15 Prompt import/export;
- 3.16 composite Prompts with published Resources (dependency content scan plus
  dynamic read through the accepted 3.11 runtime, admin preview and MCP prompts/get);
- 3.17 bounded MCP completion suggestions from typed published schemas and the
  resources/list admission fix for bound template publications.

Accepted evidence being reused (unchanged code paths)
- 3.16 candidate: core 7, admin-api 22, registry 4, mcp-server 4, UI 6; one final
  `just verify` run 1651 passed / 0 failed / 2 ignored.
- 3.17 candidate: full run 1672 passed / 0 failed / 2 ignored, QA targeted reruns and
  a real MCP Inspector 2.12.0 client smoke.

Delivery engineering in this commit
- CI: one event per change (feature branches are gated by their pull request, push
  runs only on main), story events run the changed packages, the full workspace suite
  stays the feature/release gate; no cache/sharding without a measured runner check.
- Release: the packaged archives, checksums and SBOM are now uploaded to the Gitea
  release entry of the tag instead of dying with the runner workspace.

Not included on purpose: Story 3.18 work, build caches, runtime databases, local
credentials and scratch evidence.

Refs: crank kanban t_e39d7263; baseline 1b6db09bd1.
2026-10-25 09:08:22 +00:00

334 lines
13 KiB
Rust

//! Story 3.8 AC03 / AT-3.8-03 and AC05 safe-error cases: unknown, Draft and unbound URIs stay
//! indistinguishable and leak no metadata, authorization rejects before any Resource lookup, the
//! required modern name header keeps its existing validation, and a corrupt published blob fails
//! safely instead of returning bytes.
//!
//! Both controls (`tools/list` on the same endpoint identity, modern `Mcp-Name` validation) are GREEN
//! before the build: they prove the fixture's auth and dispatch are real, so a failing Resource case
//! is a missing product capability, not a broken setup.
//!
//! NOT PROVEN HERE (needs a build seam, reported to TL): "artifact bytes are not read before
//! authorization" needs an observable artifact-read hook in the MCP app. The app currently has no
//! artifact store at all, so the suite proves the 401 happens before any Resource result but cannot
//! observe the absence of a byte read.
use serde_json::json;
use super::super::common::{initialize_session, post_jsonrpc, post_jsonrpc_with_headers};
use super::fixtures::*;
#[tokio::test]
async fn read_unknown_draft_and_unbound_uris_share_one_safe_outcome() {
let endpoint = standard_endpoint("read_safe_errors").await;
for revision in supported_revisions() {
let bodies = [
json_body(
read_resource(
&endpoint,
&revision,
"crank://resources/does-not-exist",
None,
)
.await,
)
.await,
json_body(read_resource(&endpoint, &revision, &endpoint.draft_uri, None).await).await,
json_body(read_resource(&endpoint, &revision, &endpoint.unbound_uri, None).await).await,
// A Published Resource of another workspace is not part of this Agent snapshot either.
json_body(read_resource(&endpoint, &revision, &endpoint.foreign_uri, None).await).await,
];
let codes: Vec<i64> = bodies.iter().map(error_code).collect();
assert_ne!(
codes[0], -32601,
"the read must be handled by the Resource product, not the unsupported-method answer on {revision}: {bodies:?}"
);
assert!(
codes.iter().all(|code| *code == codes[0]),
"unknown, Draft and unbound URIs must not be distinguishable by protocol outcome on {revision}: {:?} / {bodies:?}",
codes
);
for body in &bodies {
assert!(
body.get("result").is_none(),
"a denied read must not return Resource contents on {revision}: {body}"
);
let text = body.to_string();
for leak in [
endpoint.draft_name.as_str(),
endpoint.unbound_name.as_str(),
"draft text v1",
"unbound text v1",
"sha256:",
"text/plain",
] {
assert!(
!text.contains(leak),
"the safe error must not disclose Resource metadata ({leak}) on {revision}: {body}"
);
}
}
}
}
#[tokio::test]
async fn unauthenticated_read_is_rejected_before_any_resource_result() {
let endpoint = standard_endpoint("read_unauthorized").await;
let response = read_resource_without_credentials(&endpoint, &endpoint.text.uri).await;
let status = response.status().as_u16();
let body = response.text().await.unwrap();
assert_eq!(
status, 401,
"an unauthenticated resources/read must be rejected with 401 before any Resource lookup: {body}"
);
let canary = String::from_utf8(endpoint.text.bytes.clone()).expect("text fixture is UTF-8");
assert!(
!body.contains(canary.trim()),
"a rejected read must not disclose published bytes: {body}"
);
assert!(
!body.contains(endpoint.text.name.as_str()),
"a rejected read must not disclose published metadata: {body}"
);
let legacy = read_resource_without_credentials_legacy(&endpoint, &endpoint.text.uri).await;
let legacy_status = legacy.status().as_u16();
let legacy_body = legacy.text().await.unwrap();
assert_eq!(
legacy_status, 401,
"an unauthenticated legacy resources/read must be rejected with 401 as well: {legacy_body}"
);
assert!(
!legacy_body.contains(canary.trim()) && !legacy_body.contains(endpoint.text.name.as_str()),
"a rejected legacy read must not disclose published bytes or metadata: {legacy_body}"
);
}
/// AC03: no artifact byte is opened before authorization, scope, binding or size is decided. The
/// success at the end is the positive control that proves the counter actually observes reads.
#[tokio::test]
async fn read_never_touches_artifact_bytes_before_authorization_scope_or_size_denial() {
let endpoint = standard_endpoint("read_byte_ordering").await;
crank_artifacts::test_support::reset_artifact_reads();
for denied in [
read_resource_without_credentials(&endpoint, &endpoint.text.uri).await,
read_resource_without_credentials_legacy(&endpoint, &endpoint.text.uri).await,
] {
assert_eq!(
denied.status().as_u16(),
401,
"a credential-less read must be denied at the transport stage"
);
}
assert_eq!(
crank_artifacts::test_support::artifact_reads(),
0,
"an unauthenticated read must not touch artifact bytes"
);
for revision in supported_revisions() {
for uri in [
"crank://resources/does-not-exist".to_owned(),
endpoint.draft_uri.clone(),
endpoint.unbound_uri.clone(),
endpoint.foreign_uri.clone(),
] {
let body = json_body(read_resource(&endpoint, &revision, &uri, None).await).await;
assert!(
body.get("result").is_none(),
"a denied Resource read must not return contents on {revision}: {body}"
);
}
}
assert_eq!(
crank_artifacts::test_support::artifact_reads(),
0,
"unknown/Draft/unbound/foreign reads must not touch artifact bytes"
);
// Positive control: the authorized bound read does open the published bytes.
let allowed =
json_body(read_resource(&endpoint, MODERN_REVISION, &endpoint.text.uri, None).await).await;
assert!(
allowed.get("result").is_some(),
"the bound Resource must be served: {allowed}"
);
assert!(
crank_artifacts::test_support::artifact_reads() >= 1,
"the authorized read must be observable as an artifact read"
);
}
/// AC05 integrity: publication metadata that disagrees with the stored blob is denied from metadata
/// alone, before any artifact byte is opened, and discloses nothing.
///
/// The table check constraint forbids a size claim above the artifact-store ceiling, so the in-process
/// size ceiling is defense-in-depth and cannot be reproduced as an end-to-end oversize fixture.
#[tokio::test]
async fn inconsistent_publication_metadata_is_denied_before_any_artifact_read() {
let endpoint = standard_endpoint("read_oversize").await;
endpoint.inconsistent_text_publication_size().await;
crank_artifacts::test_support::reset_artifact_reads();
for revision in supported_revisions() {
let body =
json_body(read_resource(&endpoint, &revision, &endpoint.text.uri, None).await).await;
assert!(
body.get("result").is_none(),
"an inconsistent publication must not return contents on {revision}: {body}"
);
assert_ne!(
error_code(&body),
-32601,
"the read must reach the Resource handler on {revision}: {body}"
);
let text = body.to_string();
assert!(
!text.contains(&endpoint.root.path().display().to_string()),
"the safe failure must not disclose the artifact root on {revision}: {body}"
);
assert!(
!text.contains("bound text v1"),
"the safe failure must not disclose published bytes on {revision}: {body}"
);
assert!(
!text.contains(&digest_hex(&endpoint.text.bytes)),
"the safe failure must not disclose the published digest on {revision}: {body}"
);
}
assert_eq!(
crank_artifacts::test_support::artifact_reads(),
0,
"a size/integrity denial must happen before any artifact byte is read"
);
}
/// AC05 missing artifact: the read fails safely instead of serving bytes or a path.
#[tokio::test]
async fn missing_published_artifact_fails_safely() {
let endpoint = standard_endpoint("read_missing").await;
endpoint.delete_binary_blob();
for revision in supported_revisions() {
let body =
json_body(read_resource(&endpoint, &revision, &endpoint.binary.uri, None).await).await;
assert!(
body.get("result").is_none(),
"a missing published artifact must not return contents on {revision}: {body}"
);
assert_ne!(
error_code(&body),
-32601,
"the read must reach the Resource handler on {revision}: {body}"
);
let text = body.to_string();
assert!(
!text.contains(&endpoint.root.path().display().to_string()),
"the safe failure must not disclose the artifact root on {revision}: {body}"
);
assert!(
!text.contains(&base64_of(&endpoint.binary.bytes)),
"the safe failure must not disclose the artifact reference on {revision}: {body}"
);
}
}
/// Existing modern protocol contract: `Mcp-Name` must match `params.uri` and is validated before any
/// handler. This control must stay GREEN after Resource registration (no handler may bypass it).
#[tokio::test]
async fn modern_read_name_header_is_still_validated_before_any_resource_handler() {
let endpoint = standard_endpoint("read_name_validation").await;
let headers = [
("MCP-Protocol-Version", MODERN_REVISION),
("Mcp-Method", "resources/read"),
("Mcp-Name", "crank://resources/some-other-uri"),
];
let response = post_jsonrpc_with_headers(
&endpoint.client,
&endpoint.mcp_url,
&endpoint.api_key,
&headers,
json!({
"jsonrpc": "2.0",
"id": 41,
"method": "resources/read",
"params": { "uri": endpoint.text.uri, "_meta": modern_meta(MODERN_REVISION) }
}),
)
.await;
let status = response.status().as_u16();
let body = json_body(response).await;
assert_eq!(
status, 400,
"a mismatched Mcp-Name must be a header-validation failure: {body}"
);
assert_eq!(
error_code(&body),
-32020,
"a mismatched Mcp-Name must keep the existing -32020 diagnostic: {body}"
);
assert!(
body.get("result").is_none(),
"a mismatched Mcp-Name must never serve Resource contents: {body}"
);
}
/// Same-endpoint positive control: the published tool catalog still works for the fixture Agent.
#[tokio::test]
async fn published_tool_listing_remains_green_on_the_same_endpoint() {
let endpoint = standard_endpoint("read_tools_control").await;
let session = initialize_session(&endpoint.client, &endpoint.mcp_url, &endpoint.api_key).await;
let body = post_jsonrpc(
&endpoint.client,
&endpoint.mcp_url,
&endpoint.api_key,
Some(&session),
json!({ "jsonrpc": "2.0", "id": 51, "method": "tools/list", "params": {} }),
)
.await;
let tools = body["result"]["tools"]
.as_array()
.unwrap_or_else(|| panic!("tools/list must return the published catalog: {body}"));
assert!(
tools
.iter()
.any(|tool| tool["name"].as_str() == Some(endpoint.tool_name.as_str())),
"the fixture Agent must expose its published tool {}: {body}",
endpoint.tool_name
);
}
#[tokio::test]
async fn corrupt_published_artifact_fails_safely_without_bytes_or_paths() {
let endpoint = standard_endpoint("read_corrupt").await;
endpoint.corrupt_binary_blob();
for revision in supported_revisions() {
let body =
json_body(read_resource(&endpoint, &revision, &endpoint.binary.uri, None).await).await;
assert!(
body.get("result").is_none(),
"a corrupt published artifact must not return contents on {revision}: {body}"
);
let code = error_code(&body);
assert_ne!(
code, -32601,
"the read must reach the Resource handler instead of the unsupported-method answer on {revision}: {body}"
);
let text = body.to_string();
let root = endpoint.root.path().display().to_string();
assert!(
!text.contains(&root),
"the safe failure must not disclose the artifact root on {revision}: {body}"
);
assert!(
!text.contains("PNG-but-valid-utf8-bytes"),
"the safe failure must not disclose blob bytes on {revision}: {body}"
);
}
}