|
|
|
@@ -13,6 +13,8 @@ use tempfile::TempDir;
|
|
|
|
|
|
|
|
|
|
const SECRET: &str = "fake-token-4242-abcdef";
|
|
|
|
|
const MASK: &str = "*****";
|
|
|
|
|
/// A fake token that fits the base64 alphabet.
|
|
|
|
|
const SECRET_B64: &str = "fakeTOKENinBASE64alphabet";
|
|
|
|
|
|
|
|
|
|
fn project(env: &str, botsecrets: &str) -> (TempDir, TempDir) {
|
|
|
|
|
let dir = tempfile::tempdir().unwrap();
|
|
|
|
@@ -340,6 +342,43 @@ fn binary_fields_stay_and_text_beside_them_is_masked() {
|
|
|
|
|
assert_eq!(*updated(&out), json!({"data": MASK, "base64": MASK}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Only a content block at the top of a result holds binary data. The same object
|
|
|
|
|
/// deeper down is the result's own data, and a server can shape it to look like one.
|
|
|
|
|
#[test]
|
|
|
|
|
fn data_beside_a_binary_type_is_text_unless_a_top_level_block_holds_it() {
|
|
|
|
|
let token = format!("{SECRET_B64}{}", "A".repeat(80));
|
|
|
|
|
let p = project(&format!("API_TOKEN={SECRET_B64}\n"), "[files]\npatterns = [\".env\"]\n");
|
|
|
|
|
|
|
|
|
|
for response in [
|
|
|
|
|
json!([{"type": "image", "data": token}]),
|
|
|
|
|
json!({"content": [{"type": "image", "data": token}]}),
|
|
|
|
|
json!({"type": "audio", "data": token}),
|
|
|
|
|
json!([{"type": "image", "source": {"type": "base64", "data": token}}]),
|
|
|
|
|
] {
|
|
|
|
|
assert_eq!(hook(&p, "mcp__srv__tool", &response), json!({}), "{response}");
|
|
|
|
|
}
|
|
|
|
|
for response in [
|
|
|
|
|
json!({"result": {"type": "image", "data": token}}),
|
|
|
|
|
json!([{"type": "text", "nested": [{"type": "image", "data": token}]}]),
|
|
|
|
|
json!({"content": [{"result": {"type": "image", "data": token}}]}),
|
|
|
|
|
] {
|
|
|
|
|
let out = hook(&p, "mcp__srv__tool", &response);
|
|
|
|
|
assert!(!updated(&out).to_string().contains(SECRET_B64), "{out}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// `data` shorter than an image can be is a token, even beside `type: image`.
|
|
|
|
|
#[test]
|
|
|
|
|
fn the_binary_rule_starts_at_64_characters() {
|
|
|
|
|
let p = project(&format!("API_TOKEN={SECRET_B64}\n"), "[files]\npatterns = [\".env\"]\n");
|
|
|
|
|
let fill = 64 - SECRET_B64.len();
|
|
|
|
|
let long = format!("{SECRET_B64}{}", "A".repeat(fill));
|
|
|
|
|
assert_eq!(hook(&p, "mcp__srv__tool", &json!([{"type": "image", "data": long}])), json!({}));
|
|
|
|
|
let short = format!("{SECRET_B64}{}", "A".repeat(fill - 1));
|
|
|
|
|
let out = hook(&p, "mcp__srv__tool", &json!([{"type": "image", "data": short}]));
|
|
|
|
|
assert!(!updated(&out).to_string().contains(SECRET_B64), "{out}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A value split across two strings is not one match: the documented limit.
|
|
|
|
|
#[test]
|
|
|
|
|
fn a_value_split_across_fields_is_not_found() {
|
|
|
|
@@ -436,3 +475,148 @@ fn the_pre_tool_use_hook_is_quiet_on_a_tool_it_has_no_rules_for() {
|
|
|
|
|
assert_eq!(serde_json::from_slice::<Value>(&output.stdout).unwrap(), json!({}));
|
|
|
|
|
assert!(output.stderr.is_empty(), "{}", String::from_utf8_lossy(&output.stderr));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
// Every enum of every built-in (M-1, M-2)
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
/// Per tool, the enum and literal fields of its output schema in v2.1.314, generated from
|
|
|
|
|
/// the bundle's `outputSchema` getters. Each value here must survive: a replacement that
|
|
|
|
|
/// changes one fails the schema, and Claude Code then uses the original output.
|
|
|
|
|
/// So a value dropped from `SCHEMA_VALUES` fails a test here.
|
|
|
|
|
const TOOL_ENUMS: &[(&str, &[(&str, &[&str])])] = &[
|
|
|
|
|
("memory_write", &[("outcome", &["ok", "conflict", "missing", "refused", "failed"]), ("store_kind", &["personal", "project"]), ("op", &["created", "updated"])]),
|
|
|
|
|
("memory_read", &[("outcome", &["ok", "not_found", "refused", "failed"]), ("store_kind", &["personal", "project"])]),
|
|
|
|
|
("memory_list", &[("outcome", &["ok", "refused", "failed"]), ("store_kind", &["personal", "project"])]),
|
|
|
|
|
("Write", &[("type", &["create", "update"]), ("status", &["modified", "added"]), ("stagedWording", &["true"])]),
|
|
|
|
|
("Edit", &[("status", &["modified", "added"]), ("stagedWording", &["true"])]),
|
|
|
|
|
("Grep", &[("mode", &["content", "files_with_matches", "count"])]),
|
|
|
|
|
("Read", &[("type", &["text", "image", "image/jpeg", "image/png", "image/gif", "image/webp", "notebook", "pdf", "parts", "file_unchanged"]), ("mediaType", &["image/jpeg", "image/png", "image/gif", "image/webp"]), ("source", &["seeded"])]),
|
|
|
|
|
("NotebookEdit", &[("cell_type", &["code", "markdown"])]),
|
|
|
|
|
("Bash", &[("kind", &["committed", "amended", "cherry-picked"]), ("action", &["merged", "rebased", "created", "edited", "commented", "closed", "reopened", "ready", "draft", "auto-merge-enabled", "auto-merge-disabled"])]),
|
|
|
|
|
("Agent", &[("handback", &["send", "flagged", "withheld"]), ("type", &["text"]), ("status", &["completed", "async_launched", "remote_launched", "teammate_spawned"])]),
|
|
|
|
|
("Skill", &[("status", &["inline", "forked"])]),
|
|
|
|
|
("AskUserQuestion", &[("kind", &["choice", "text", "number"]), ("unanswered", &["skipped", "replied_in_chat", "dismissed"])]),
|
|
|
|
|
("ShareOnboardingGuide", &[("status", &["created", "updated", "deleted", "has_existing", "unavailable"])]),
|
|
|
|
|
("OfferChromeSetup", &[("outcome", &["connected", "not_now", "no_attempt_yet"])]),
|
|
|
|
|
("ReportFindings", &[("level", &["low", "medium", "high", "xhigh", "max"]), ("verdict", &["CONFIRMED", "PLAUSIBLE"]), ("outcome", &["fixed", "skipped", "no_change_needed"])]),
|
|
|
|
|
("TaskGet", &[("status", &["pending", "in_progress", "completed"])]),
|
|
|
|
|
("TaskList", &[("status", &["pending", "in_progress", "completed"])]),
|
|
|
|
|
("TodoWrite", &[("status", &["pending", "in_progress", "completed"])]),
|
|
|
|
|
("GetTask", &[("status", &["working", "completed", "failed", "cancelled"]), ("type", &["text"])]),
|
|
|
|
|
("LSP", &[("operation", &["goToDefinition", "findReferences", "hover", "documentSymbol", "workspaceSymbol", "goToImplementation", "prepareCallHierarchy", "incomingCalls", "outgoingCalls"])]),
|
|
|
|
|
("RefreshMcpTools", &[("status", &["refreshed", "error", "not_connected"])]),
|
|
|
|
|
("ExitWorktree", &[("action", &["keep", "remove"])]),
|
|
|
|
|
("SuggestPluginInstall", &[("trigger", &["user_asked", "proactive"])]),
|
|
|
|
|
("SuggestSkills", &[("trigger", &["user_asked", "proactive"])]),
|
|
|
|
|
("Poll", &[("type", &["image", "base64", "document"]), ("media_type", &["image/jpeg", "image/png", "image/gif", "image/webp", "application/pdf"]), ("authority", &["human-principal", "human-other", "peer-agent", "world-event"])]),
|
|
|
|
|
("AppifactRepl", &[("media_type", &["image/png", "image/jpeg"])]),
|
|
|
|
|
("Projects", &[("method", &["project_info", "project_read", "project_search", "project_write", "project_delete", "project_memory_list", "project_memory_read"])]),
|
|
|
|
|
("SendUserFile", &[("display", &["render", "attach"])]),
|
|
|
|
|
("PublishPlugin", &[("state", &["published", "in-review", "publishing", "on-shelf", "refused"])]),
|
|
|
|
|
("PushNotification", &[("disabledReason", &["config_off", "user_present", "no_transport"])]),
|
|
|
|
|
("Appifact", &[("method", &["list_projects", "get_project", "list_files", "get_file", "finalize_plan", "write_files", "delete_files", "register_assets", "unregister_assets", "create_project", "report_validate"])]),
|
|
|
|
|
("SendToProject", &[("mode", &["new", "existing"]), ("message", &["sent", "uncertain", "not_sent"])]),
|
|
|
|
|
("SendToProjectContext", &[("status", &["delivered", "already_delivered", "nothing_read", "no_run"])]),
|
|
|
|
|
("Workflow", &[("status", &["async_launched", "remote_launched"]), ("taskType", &["local_workflow", "remote_agent"])]),
|
|
|
|
|
("Artifact", &[
|
|
|
|
|
("action", &["pin", "unpin"]), ("working_copy", &["bound", "left"]), ("verb", &["put", "retract", "read", "script"]),
|
|
|
|
|
("status", &["uploaded", "failed", "not_attempted"]), ("fallback", &["sequential"]), ("rail", &["durable_wake", "live_stopped"]),
|
|
|
|
|
("scope", &["shared", "all"]), ("editsNotWritten", &["write_failed", "not_writable", "changed_during_publish", "hard_linked"]),
|
|
|
|
|
("auto_open", &["at_create", "after_first_write"]), ("files_error_kind", &["type_owned_path"]), ("op", &["batch"]),
|
|
|
|
|
("unread", &["withheld", "not_listed", "not_text", "too_large", "unavailable", "result_full"]),
|
|
|
|
|
]),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/// These answer a fixed form under a refusal, so no field of the response survives.
|
|
|
|
|
const FIXED_REFUSAL: &[&str] = &["Bash", "Read", "Artifact"];
|
|
|
|
|
|
|
|
|
|
fn broken_secret_file() -> (TempDir, TempDir) {
|
|
|
|
|
let dir = tempfile::tempdir().unwrap();
|
|
|
|
|
fs::write(dir.path().join(".botignore"), "").unwrap();
|
|
|
|
|
fs::write(dir.path().join(".botsecrets"), "[enforcement]\non_parse_error = \"deny\"\n").unwrap();
|
|
|
|
|
fs::write(dir.path().join("secrets.toml"), "API_TOKEN = broken [\n").unwrap();
|
|
|
|
|
(dir, tempfile::tempdir().unwrap())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Under a refusal every string of a built-in's output goes, but an enum value stays.
|
|
|
|
|
#[test]
|
|
|
|
|
fn a_refusal_keeps_every_enum_value_of_every_tool() {
|
|
|
|
|
let p = broken_secret_file();
|
|
|
|
|
for (tool, fields) in TOOL_ENUMS.iter().filter(|(tool, _)| !FIXED_REFUSAL.contains(tool)) {
|
|
|
|
|
for (field, values) in *fields {
|
|
|
|
|
for value in *values {
|
|
|
|
|
let out = hook(&p, tool, &json!({ *field: value, "note": "free text" }));
|
|
|
|
|
let output = updated(&out);
|
|
|
|
|
assert_eq!(output[*field], *value, "{tool}.{field}: {out}");
|
|
|
|
|
assert!(output["note"].as_str().unwrap().contains("withheld"), "{tool}: {out}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A secret equal to an enum value: the value stays, the text beside it takes a mask,
|
|
|
|
|
/// and nothing is refused.
|
|
|
|
|
#[test]
|
|
|
|
|
fn a_secret_equal_to_an_enum_value_keeps_the_value_in_every_tool() {
|
|
|
|
|
for (tool, fields) in TOOL_ENUMS {
|
|
|
|
|
for (field, values) in *fields {
|
|
|
|
|
for value in values.iter().filter(|v| v.len() >= 4) {
|
|
|
|
|
let p = project(&format!("API_TOKEN={value}\n"), "[files]\npatterns = [\".env\"]\n");
|
|
|
|
|
let out = hook(&p, tool, &json!({ *field: value, "note": format!("x {value} y") }));
|
|
|
|
|
let output = updated(&out);
|
|
|
|
|
assert_eq!(output[*field], *value, "{tool}.{field}: {out}");
|
|
|
|
|
assert_eq!(output["note"], format!("x {MASK} y"), "{tool}.{field}: {out}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// `message` is an enum in one tool and text in most others.
|
|
|
|
|
#[test]
|
|
|
|
|
fn a_secret_in_a_message_takes_a_mask() {
|
|
|
|
|
let p = with_secret();
|
|
|
|
|
let out = hook(&p, "PushNotification", &json!({"message": format!("sent {SECRET}"), "pushSent": true}));
|
|
|
|
|
assert_eq!(updated(&out)["message"], format!("sent {MASK}"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The Artifact tool's patterns and its run-time enum cannot be listed: a secret there
|
|
|
|
|
/// refuses, in the one form that fits every value of those fields.
|
|
|
|
|
#[test]
|
|
|
|
|
fn a_secret_under_an_unlisted_pattern_refuses_the_artifact_output() {
|
|
|
|
|
let p = with_secret();
|
|
|
|
|
for field in ["ver", "sha256", "id", "url", "content_type", "reason"] {
|
|
|
|
|
let out = hook(&p, "Artifact", &json!({"file_read": { field: SECRET, "path": "a" }}));
|
|
|
|
|
let output = updated(&out);
|
|
|
|
|
assert!(output["read"]["result"].as_str().unwrap().contains("withheld"), "{out}");
|
|
|
|
|
assert!(!out.to_string().contains(SECRET), "{out}");
|
|
|
|
|
}
|
|
|
|
|
// Elsewhere those names are plain text.
|
|
|
|
|
let out = hook(&p, "Glob", &json!({"id": SECRET}));
|
|
|
|
|
assert_eq!(updated(&out)["id"], MASK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn the_artifact_refusal_is_its_read_form() {
|
|
|
|
|
let out = hook(&broken_secret_file(), "Artifact", &json!({"file_read": {"ver": "abc", "sha256": "def"}}));
|
|
|
|
|
let read = &updated(&out)["read"];
|
|
|
|
|
for field in ["url", "codeText", "result"] {
|
|
|
|
|
assert!(read[field].is_string(), "{out}");
|
|
|
|
|
}
|
|
|
|
|
for field in ["bytes", "code", "durationMs"] {
|
|
|
|
|
assert!(read[field].is_number(), "{out}");
|
|
|
|
|
}
|
|
|
|
|
assert!(!out.to_string().contains("def"), "{out}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Records whose keys are free text: a secret there is masked, a refusal empties them.
|
|
|
|
|
#[test]
|
|
|
|
|
fn every_free_text_record_is_a_record() {
|
|
|
|
|
for field in ["fields", "content", "config", "pool", "sessions", "runners", "secrets", "health", "gauges"] {
|
|
|
|
|
let p = with_secret();
|
|
|
|
|
let out = hook(&p, "SomeTool", &json!({ field: { SECRET: 1 } }));
|
|
|
|
|
assert_eq!(updated(&out)[field], json!({ MASK: 1 }), "{field}: {out}");
|
|
|
|
|
let out = hook(&broken_secret_file(), "SomeTool", &json!({ field: { "name": 1 } }));
|
|
|
|
|
assert_eq!(updated(&out)[field], json!({}), "{field}: {out}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|