Cotton code
54 lines · 2.9 KB · RustRaw
//! A table no tool draws is read by no path: credentials included.mod support;use rusqlite::types::Value as SqlValue;use serde_json::json;use support::{    mcp::{call_tool_raw, mint_token},    Harness,};/// RULE (credentials, the one zone a guard is kept — architect, 21 Oct 2026):/// a row of a table no tool draws is read through no MCP path — list_records,/// get_record (by table and by kind), get_record_by_table, search_records,/// summarize_records — any more than through the phone's read; each answers/// the typed `no_tool_draws_this_table` refusal or no hit. Real databases/// still hold assistant keys (`cotton_mcp_tokens`, the `assistant` table)/// and the retired built-in row that once drew them, though no tool draws/// that table now; if any read path skipped the definition, an assistant/// could list the keys' names, scopes and hashes. This replaces/// `an_assistant_read_never_carries_the_keys_secret` and/// `live_assistant_records_hide_hash_and_refuse_generic_writes`, deleted with/// the assistant definition.#[test]fn a_table_no_tool_draws_is_read_by_no_path() {    let mut h = Harness::new();    let token = mint_token(&mut h, &["read", "write"], "Synthetic assistant");    let key = h.one("SELECT id, token_hash FROM cotton_mcp_tokens WHERE name = 'Synthetic assistant'", &[]).expect("the key's row");    let (id, hash) = (key["id"].as_str().unwrap().to_string(), key["token_hash"].as_str().unwrap().to_string());    // The row a real database keeps from when the assistant table was built in.    h.run(        "INSERT INTO cotton_table_definitions (id, workspace_id, definition, builtin, created_at, updated_at) \         VALUES ('assistant', ?, '{\"id\":\"assistant\"}', 1, '2026-09-01T00:00:00Z', '2026-09-01T00:00:00Z')",        &[SqlValue::Text(h.workspace_id.clone())],    );    let refused = [        ("list_records", json!({"table": "assistant"})),        ("summarize_records", json!({"table": "assistant"})),        ("get_record_by_table", json!({"table": "assistant", "id": id})),        ("get_record", json!({"table": "assistant", "id": id})),        ("get_record", json!({"kind": "location", "id": id})),    ];    for (tool, args) in refused {        let answer = call_tool_raw(&h, &token, tool, args.clone());        assert!(answer.is_error, "{tool} {args} read a table no tool draws: {}", answer.text);        assert!(            answer.text.contains(&cotton_core::definitions::no_tool_draws_this_table(&[]))                || answer.text.contains(cotton_core::definitions::NO_TOOL_DRAWS_THIS_TABLE),            "{tool} {args}: {}",            answer.text        );        assert!(!answer.text.contains(&hash), "{tool} carried a key's hash");    }    let searched = call_tool_raw(&h, &token, "search_records", json!({"query": "Synthetic assistant", "kinds": ["assistant"]}));    assert!(!searched.text.contains(&id) && !searched.text.contains(&hash), "search found a key: {}", searched.text);}