trusty-mpm 1.8.2

trusty-mpm: unified multi-agent orchestration platform (core, daemon, CLI, TUI, Telegram)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
//! Local per-project trust store gating a project's `[session] plugins` list.
//!
//! Why (issue #3033 security review; narrowed to plugins by #7892): a
//! `[session] plugins` list ships WITH the cloned repo itself, unlike a
//! user-scope registration the operator personally typed into their OWN
//! machine. A Claude Code plugin brings its own skills, slash commands and
//! hooks into every session in the project, and tm's panes run
//! `--dangerously-skip-permissions`, so without an explicit consent step a
//! hostile clone could turn an operator-installed plugin's whole catalog on the
//! first time anyone runs `tm session start` against it. This store is the
//! durable, per-project, USER-scope (never repo-scope) decision that gates
//! that: it lives under `~/.trusty-tools/trusty-mpm/`, the same tm-owned root
//! [`crate::core::trusty_tools_config::managed_claude_config_dir`] nests
//! `claude-config/` under — never inside any project working directory — so a
//! cloned repo cannot flip its own trust bit no matter what it contains.
//! [`crate::core::session_mcp_scope::granted_plugins`] is the one live reader;
//! the `tm project trust` / `tm project trust --revoke` CLI verbs
//! (`bin/tm/commands/project.rs`) are the only way to flip it.
//!
//! #7892 REMOVED THE MCP HALF. Under the Claude Code standard every user-scope
//! server in the protected `.claude.json` loads in every session with no grant,
//! and a project's own `.mcp.json` follows Claude Code's native approval
//! (`enableAllProjectMcpServers` / `enabledMcpjsonServers`, or its prompt), so
//! this store has nothing to say about MCP servers. Claude Code has no
//! equivalent per-project plugin approval, which is why the plugin half stays.
//!
//! What: [`ProjectTrustStore`] persists a set of trusted, canonicalized
//! project paths to `<root>/project-trust.json`. [`ProjectTrustStore::load`]/
//! [`ProjectTrustStore::save`] are the read/write primitives (missing file →
//! empty store, mirroring [`super::project_aliases::ProjectAliasStore`]);
//! [`ProjectTrustStore::trust`]/[`ProjectTrustStore::revoke`] mutate the set
//! and report whether anything changed (so the CLI only writes when needed);
//! [`ProjectTrustStore::is_trusted`] answers a membership query. Paths are
//! canonicalized on both insert and lookup so `tm project trust .` and a
//! later absolute-path lookup from `session_launch` agree even across a
//! trailing slash or a symlinked ancestor; a path that cannot be canonicalized
//! (e.g. it no longer exists) falls back to the path as given rather than
//! failing the operation. [`is_project_trusted`] is the convenience, non-fatal
//! entry point the launch path calls: it resolves the production store root and
//! treats ANY error (unreadable home dir, malformed JSON, I/O failure) as "not
//! trusted" — fail-closed, matching this store's whole purpose.
//! Test: `trust_store_new_is_empty`, `trust_inserts_new_entry`,
//! `trust_is_idempotent`, `revoke_removes_trust`,
//! `revoke_unknown_path_is_noop`, `is_trusted_after_trust_and_revoke`,
//! `save_and_reload_round_trip`, `is_project_trusted_at_reflects_store`,
//! `is_project_trusted_at_fails_closed_when_root_missing`.
//!
//! **Trust is per-directory, NOT per-repo-content (issue #3033, MEDIUM
//! finding 2 — read this before relying on a trust decision).** [`normalize`]
//! canonicalizes and stores a filesystem PATH — it records nothing about what
//! is checked out there (no commit hash, no remote URL, no content digest).
//! Once a directory is trusted, EVERY future `[session] plugins` read from that
//! same canonical path is honored, no matter what later replaces the
//! directory's contents: `rm -rf` + `git clone <different-repo>` into the same
//! path, `git checkout` to an attacker-controlled branch, or any other content
//! swap all inherit the existing trust grant silently. To get a fresh consent
//! decision for genuinely different content, either **clone to a new path**
//! (a new canonical path is untrusted by default) or **explicitly
//! `tm project trust --revoke <path>` before, and re-trust after, replacing a
//! trusted directory's contents.** A future hardening — binding the trust
//! record to the git remote URL and/or toplevel commit rather than the bare
//! path — is tracked as a follow-up (issue #3051, filed fresh since #3045 is
//! the unrelated stale-`.mcp.json`-entry-pruning tracker) and is deliberately
//! NOT implemented here.

use std::collections::BTreeSet;
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};

use crate::core::trusty_tools_config::CRATE_NAME;

/// On-disk filename for the project-trust store, nested under
/// `~/.trusty-tools/trusty-mpm/` (the same root
/// [`crate::core::trusty_tools_config::managed_claude_config_dir`] nests
/// `claude-config/` under).
const TRUST_STORE_FILE: &str = "project-trust.json";

/// Canonicalize `path` best-effort, falling back to `path` unchanged when
/// canonicalization fails (e.g. the directory does not exist in this
/// environment).
///
/// Why: a single normalization rule must be shared by `trust`/`revoke`/
/// `is_trusted` so a path recorded one way is still found looked up another
/// (trailing slash, relative vs. absolute, symlinked ancestor).
/// What: `path.canonicalize()`, falling back to `path.to_path_buf()`.
/// Test: covered transitively by `is_trusted_after_trust_and_revoke`.
fn normalize(path: &Path) -> PathBuf {
    path.canonicalize().unwrap_or_else(|_| path.to_path_buf())
}

/// The exact `tm project trust` invocation that grants trust to `project_dir`.
///
/// Why (#7757): every hint that told an operator to run `tm project trust
/// <path>` printed a form the CLI rejects with exit 2 — `ProjectAction::Trust`
/// takes `--dir <path>` and no positional argument. A hint is only worth
/// printing if pasting it works, so the command line has ONE source and a test
/// feeds that source straight back through the parser.
/// What: `tm project trust --dir <path>`. A path containing whitespace is
/// single-quoted, because the hint is copied into a shell and an unquoted
/// `/work/my project` would reach `tm` as two arguments.
/// Test: `trust_command_hint_parses_as_the_cli_accepts_it`,
/// `trust_command_hint_quotes_a_path_with_spaces`,
/// `session_scope_names_the_trust_command_for_an_untrusted_opt_in`.
pub fn trust_command_hint(project_dir: &Path) -> String {
    let path = project_dir.display().to_string();
    if !path.contains(char::is_whitespace) {
        return format!("tm project trust --dir {path}");
    }
    // POSIX single-quoting: end the quote, emit an escaped quote, reopen.
    let quoted = path.replace('\'', "'\\''");
    format!("tm project trust --dir '{quoted}'")
}

/// In-memory + on-disk registry of operator-trusted project paths.
///
/// Why: mirrors [`super::project_aliases::ProjectAliasStore`]'s
/// load-mutate-save shape so this store follows the SAME established,
/// already-reviewed persistence pattern rather than inventing a new one.
/// What: a `BTreeSet<PathBuf>` of canonicalized trusted paths plus the
/// resolved `file_path`. Mutations are committed by explicit `save()` calls.
/// Test: `trust_store_new_is_empty`, `save_and_reload_round_trip`.
#[derive(Debug, Default)]
pub struct ProjectTrustStore {
    trusted: BTreeSet<PathBuf>,
    file_path: PathBuf,
}

/// On-disk shape: a bare sorted array of path strings, kept intentionally
/// minimal (no timestamps or metadata) since the only question this file ever
/// answers is membership.
#[derive(Debug, Default, Serialize, Deserialize)]
struct TrustStoreData {
    #[serde(default)]
    trusted_paths: Vec<PathBuf>,
}

impl ProjectTrustStore {
    /// Load (or create) the trust store at `<root>/project-trust.json`.
    ///
    /// Why: a missing file means "no project has been trusted yet" — the
    /// fail-closed default this whole module exists to enforce — so it is
    /// treated as an empty store rather than an error.
    /// What: reads and deserializes the JSON file when present; returns an
    /// empty store with the correct path when the file is absent.
    /// Test: `trust_store_new_is_empty`.
    pub fn load(root: &Path) -> anyhow::Result<Self> {
        let file_path = root.join(TRUST_STORE_FILE);
        if !file_path.exists() {
            return Ok(Self {
                trusted: BTreeSet::new(),
                file_path,
            });
        }
        let raw = std::fs::read_to_string(&file_path)
            .map_err(|e| anyhow::anyhow!("failed to read {}: {e}", file_path.display()))?;
        let data: TrustStoreData = serde_json::from_str(&raw)
            .map_err(|e| anyhow::anyhow!("failed to parse {}: {e}", file_path.display()))?;
        Ok(Self {
            trusted: data.trusted_paths.into_iter().collect(),
            file_path,
        })
    }

    /// Persist the store to disk.
    ///
    /// Why: an atomic write-then-rename avoids ever leaving a half-written
    /// trust file (this file gates a security decision, so a torn write must
    /// never be silently read back as "trusted"). On Unix, the file is also
    /// restricted to owner-only `0o600` (issue #3033, LOW finding) — it is a
    /// security-decision record under the operator's own `$HOME`, so it should
    /// never be group/world-readable on a shared machine.
    /// What: creates parent directories if absent, serialises the sorted path
    /// list to pretty JSON, writes atomically via `rename`, then (Unix only)
    /// sets the final file's permissions to `0o600`.
    /// Test: `save_and_reload_round_trip`, `save_sets_owner_only_perms_on_unix`.
    pub fn save(&self) -> anyhow::Result<()> {
        if let Some(parent) = self.file_path.parent() {
            std::fs::create_dir_all(parent)
                .map_err(|e| anyhow::anyhow!("failed to create {}: {e}", parent.display()))?;
        }
        let data = TrustStoreData {
            trusted_paths: self.trusted.iter().cloned().collect(),
        };
        let json = serde_json::to_string_pretty(&data)
            .map_err(|e| anyhow::anyhow!("failed to serialize project-trust: {e}"))?;
        let tmp_path = self.file_path.with_extension("json.tmp");
        std::fs::write(&tmp_path, &json)
            .map_err(|e| anyhow::anyhow!("failed to write {}: {e}", tmp_path.display()))?;
        if let Err(e) = std::fs::rename(&tmp_path, &self.file_path) {
            let _ = std::fs::remove_file(&tmp_path);
            return Err(anyhow::anyhow!(
                "failed to rename {} -> {}: {e}",
                tmp_path.display(),
                self.file_path.display()
            ));
        }
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            std::fs::set_permissions(&self.file_path, std::fs::Permissions::from_mode(0o600))
                .map_err(|e| {
                    anyhow::anyhow!(
                        "failed to set owner-only permissions on {}: {e}",
                        self.file_path.display()
                    )
                })?;
        }
        Ok(())
    }

    /// Mark `path` as trusted.
    ///
    /// What: inserts the canonicalized path; returns `true` when this changed
    /// the store (caller should `save()`), `false` when it was already trusted.
    /// Test: `trust_inserts_new_entry`, `trust_is_idempotent`.
    pub fn trust(&mut self, path: &Path) -> bool {
        self.trusted.insert(normalize(path))
    }

    /// Revoke trust for `path`.
    ///
    /// What: removes the canonicalized path; returns `true` when it was
    /// present (caller should `save()`), `false` when it was already absent.
    /// Test: `revoke_removes_trust`, `revoke_unknown_path_is_noop`.
    pub fn revoke(&mut self, path: &Path) -> bool {
        self.trusted.remove(&normalize(path))
    }

    /// Whether `path` is currently trusted.
    ///
    /// What: membership test against the canonicalized path.
    /// Test: `is_trusted_after_trust_and_revoke`.
    pub fn is_trusted(&self, path: &Path) -> bool {
        self.trusted.contains(&normalize(path))
    }

    /// All trusted paths, sorted.
    ///
    /// Why: lets the `project list`/`project info` CLI surface trust status
    /// without exposing the internal `BTreeSet` type.
    /// What: a cloned, already-sorted `Vec<PathBuf>` (a `BTreeSet` iterates in
    /// order).
    /// Test: covered transitively by `save_and_reload_round_trip`.
    pub fn list(&self) -> Vec<PathBuf> {
        self.trusted.iter().cloned().collect()
    }
}

/// Resolve the production root the trust store is nested under:
/// `~/.trusty-tools/trusty-mpm/`.
///
/// Why: the SAME base [`crate::core::trusty_tools_config::managed_claude_config_dir`]
/// resolves `claude-config/` under, so the trust file lives alongside the
/// rest of tm's user-scope state rather than inventing a new base directory.
/// What: delegates to [`trusty_common::crate_config::crate_config_dir`].
/// Returns `None` only when the home directory cannot be resolved (a
/// stripped CI environment) — callers treat that as "no trust store, so
/// fail closed".
/// Test: exercised indirectly; hermetic tests use [`ProjectTrustStore::load`]
/// against a tempdir root directly instead of this accessor.
pub fn trust_store_root() -> Option<PathBuf> {
    trusty_common::crate_config::crate_config_dir(CRATE_NAME)
}

/// Non-fatal, fail-closed convenience check: is `project_path` trusted?
///
/// Why: the launch path must never let a store read failure
/// (missing home dir, malformed JSON, permissions) silently fall through to
/// "trusted" — the entire point of this module is a fail-closed gate, so
/// every error path here returns `false`.
/// What: resolves [`trust_store_root`]; on `None` (or a load error, logged at
/// `warn!`) returns `false`. Otherwise delegates to
/// [`ProjectTrustStore::is_trusted`].
/// Test: `is_project_trusted_at_reflects_store`,
/// `is_project_trusted_at_fails_closed_when_root_missing` — both via the
/// hermetic [`is_project_trusted_at`]. #7894: the production-wiring pointer
/// named `custom_mcp_tests.rs`, deleted with its module under ADR-0042; the
/// live reader is now `core::session_mcp_scope::granted_plugins`.
pub fn is_project_trusted(project_path: &Path) -> bool {
    match trust_store_root() {
        Some(root) => is_project_trusted_at(project_path, &root),
        None => {
            tracing::warn!(
                "skipping project-trust check: cannot resolve the home directory — \
                 treating project as untrusted (fail-closed)"
            );
            false
        }
    }
}

/// Hermetic core of [`is_project_trusted`] against an explicit store root.
///
/// Why: splitting the root resolution from the actual check lets tests target
/// a tempdir root without touching the real `$HOME`, while production code
/// still goes through the single [`is_project_trusted`] entry point.
/// What: loads the store at `root`; a load error is logged at `warn!` and
/// treated as untrusted (fail-closed, mirroring the native/custom MCP
/// injectors' own "never fatal to launch" posture — except here the fail-safe
/// direction is "skip the bridge", not "allow it").
/// Test: `is_project_trusted_at_reflects_store`,
/// `is_project_trusted_at_fails_closed_when_root_missing`.
pub fn is_project_trusted_at(project_path: &Path, root: &Path) -> bool {
    match ProjectTrustStore::load(root) {
        Ok(store) => store.is_trusted(project_path),
        Err(err) => {
            tracing::warn!(
                "failed to read project-trust store: {err} — treating project as untrusted \
                 (fail-closed)"
            );
            false
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    fn make_store(dir: &TempDir) -> ProjectTrustStore {
        ProjectTrustStore::load(dir.path()).expect("load")
    }

    #[test]
    fn trust_store_new_is_empty() {
        let root = TempDir::new().expect("tmpdir");
        let store = make_store(&root);
        assert!(store.list().is_empty());
    }

    #[test]
    fn trust_inserts_new_entry() {
        let root = TempDir::new().expect("tmpdir");
        let project = TempDir::new().expect("project dir");
        let mut store = make_store(&root);

        assert!(store.trust(project.path()), "new trust should return true");
        assert!(store.is_trusted(project.path()));
    }

    #[test]
    fn trust_is_idempotent() {
        let root = TempDir::new().expect("tmpdir");
        let project = TempDir::new().expect("project dir");
        let mut store = make_store(&root);

        assert!(store.trust(project.path()));
        let changed_again = store.trust(project.path());
        assert!(!changed_again, "re-trusting the same path must be a no-op");
    }

    #[test]
    fn revoke_removes_trust() {
        let root = TempDir::new().expect("tmpdir");
        let project = TempDir::new().expect("project dir");
        let mut store = make_store(&root);

        store.trust(project.path());
        assert!(store.is_trusted(project.path()));

        let revoked = store.revoke(project.path());
        assert!(revoked, "revoke must report the change");
        assert!(!store.is_trusted(project.path()));
    }

    #[test]
    fn revoke_unknown_path_is_noop() {
        let root = TempDir::new().expect("tmpdir");
        let project = TempDir::new().expect("project dir");
        let mut store = make_store(&root);

        let revoked = store.revoke(project.path());
        assert!(!revoked, "revoking an untrusted path must be a no-op");
    }

    #[test]
    fn is_trusted_after_trust_and_revoke() {
        let root = TempDir::new().expect("tmpdir");
        let project_a = TempDir::new().expect("project a");
        let project_b = TempDir::new().expect("project b");
        let mut store = make_store(&root);

        store.trust(project_a.path());
        assert!(store.is_trusted(project_a.path()));
        assert!(
            !store.is_trusted(project_b.path()),
            "an unrelated path must never read as trusted"
        );
    }

    #[test]
    fn save_and_reload_round_trip() {
        let root = TempDir::new().expect("tmpdir");
        let project = TempDir::new().expect("project dir");
        {
            let mut store = make_store(&root);
            store.trust(project.path());
            store.save().expect("save");
        }
        {
            let store2 = make_store(&root);
            assert!(store2.is_trusted(project.path()));
            assert_eq!(store2.list().len(), 1);
        }
    }

    #[test]
    fn is_project_trusted_at_reflects_store() {
        let root = TempDir::new().expect("tmpdir");
        let project = TempDir::new().expect("project dir");

        assert!(
            !is_project_trusted_at(project.path(), root.path()),
            "must be untrusted before any `trust` call"
        );

        let mut store = make_store(&root);
        store.trust(project.path());
        store.save().expect("save");

        assert!(is_project_trusted_at(project.path(), root.path()));
    }

    #[test]
    #[cfg(unix)]
    fn save_sets_owner_only_perms_on_unix() {
        use std::os::unix::fs::PermissionsExt;

        let root = TempDir::new().expect("tmpdir");
        let project = TempDir::new().expect("project dir");
        let mut store = make_store(&root);
        store.trust(project.path());
        store.save().expect("save");

        let meta = std::fs::metadata(root.path().join("project-trust.json")).expect("metadata");
        let mode = meta.permissions().mode() & 0o777;
        assert_eq!(
            mode, 0o600,
            "project-trust.json must be owner-read/write only, got {mode:o}"
        );
    }

    #[test]
    fn is_project_trusted_at_fails_closed_when_root_missing() {
        // A root whose parent does not exist at all still yields a clean
        // empty-store load (load() only checks file existence, not root
        // existence), so this asserts the more realistic failure: a
        // project-trust.json that is present but malformed must fail closed,
        // not panic or default to trusted.
        let root = TempDir::new().expect("tmpdir");
        std::fs::write(root.path().join("project-trust.json"), "not json").unwrap();
        let project = TempDir::new().expect("project dir");

        assert!(
            !is_project_trusted_at(project.path(), root.path()),
            "a malformed trust store must fail closed (untrusted), never panic or default open"
        );
    }
}