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
//! Private mutable state beneath `~/.trusty-code/` (#5426, epic #2892).
//!
//! Why: transcripts, logs, compression telemetry, daemon discovery files, and
//! channel cursors are per-user runtime state, not project configuration. They
//! were already landing in `~/.trusty-code` via `workstreams::default_data_dir`,
//! but nothing named that directory as PRIVATE and nothing set its mode, so a
//! default-umask machine left `0755` — world-readable transcripts of whatever
//! the harness was asked to work on.
//!
//! What: [`private_state_dir_at`] is the hermetic resolver (an explicit `$HOME`,
//! so tests never touch a real home directory and never mutate the environment);
//! [`private_state_dir`] is the production wrapper.
//! [`ensure_private_state_dir_at`] creates the tree and TIGHTENS the mode to
//! `0700` on Unix — including on a directory that already existed with a
//! permissive mode, since the common case is an upgrade over an existing
//! `~/.trusty-code`. [`is_restrictive`] is the predicate the tests and the
//! `tcode config paths` diagnostic both assert on.
//!
//! Test: `paths::private_state_tests::*`.
use io;
use ;
use TRUSTY_CODE_DIRNAME;
/// The Unix mode private state is held at: owner-only, no group, no other.
///
/// Why: `0700` is the same bar `~/.ssh` sets, and the state here (transcripts,
/// daemon tokens' neighbours, logs) is comparably sensitive.
/// What: `0o700`.
/// Test: `paths::private_state_tests::ensure_tightens_a_permissive_existing_dir`.
pub const PRIVATE_DIR_MODE: u32 = 0o700;
/// The permission bits that must be clear for a directory to count as private.
///
/// Why: comparing the full mode would fail on a directory that is `0700` plus a
/// sticky or setgid bit, which is not a privacy problem.
/// What: `0o077` — every group and other bit.
/// Test: `paths::private_state_tests::permissive_mode_is_not_restrictive`.
pub const NON_OWNER_BITS: u32 = 0o077;
/// Resolve the private state directory beneath an explicit home directory.
///
/// Why: the hermetic core. Every test points `home` at a temp dir, so none of
/// them reads or writes the developer's real `~/.trusty-code` and none needs to
/// mutate `$HOME` (an env mutation would race every other test in the binary).
/// What: `<home>/.trusty-code`.
/// Test: `paths::private_state_tests::private_state_dir_at_is_dot_trusty_code`.
/// Resolve the production private state directory, `~/.trusty-code`.
///
/// Why: the one place `dirs::home_dir()` is consulted for this purpose, so the
/// no-home fallback is decided once instead of per call site.
/// What: [`private_state_dir_at`] on `dirs::home_dir()`. With no resolvable home
/// — a stripped container, a daemon launched with no `$HOME` — it falls back to
/// a process-relative `.trusty-code` and logs at `warn` with the path used,
/// rather than panicking a harness that is otherwise fine.
/// Test: `paths::private_state_tests::private_state_dir_matches_home_when_available`.
/// Create the private state directory and tighten it to owner-only.
///
/// Why: creating it with `create_dir_all` alone applies the process umask, which
/// on a default `022` machine yields `0755`. An upgrade path matters as much as
/// a fresh install: `~/.trusty-code` already exists on every machine that has
/// run `tcode`, so this tightens an EXISTING permissive directory rather than
/// only getting new ones right.
/// What: `create_dir_all`, then on Unix `set_permissions` to
/// [`PRIVATE_DIR_MODE`] whenever any of [`NON_OWNER_BITS`] is set. A failure to
/// tighten is logged at `warn` with the path and the mode observed, and the
/// directory is still returned — the harness must run on a filesystem that
/// cannot represent Unix modes. Non-Unix targets create the directory and rely
/// on the platform's own per-user home ACLs.
/// Test: `paths::private_state_tests::ensure_creates_restrictive_dir`,
/// `paths::private_state_tests::ensure_tightens_a_permissive_existing_dir`.
/// Create `dir` and tighten it, on an already-resolved path.
///
/// Why: #6999 — the create-and-tighten body was written twice, once per
/// `ensure_*` wrapper, and [`crate::workstreams::default_data_dir`] needs a
/// third caller that already holds the resolved path (its home resolution is
/// [`private_state_dir`]'s job, including the no-home fallback). One body, three
/// callers, rather than a copy per entry point.
/// What: `create_dir_all` then [`harden`]. Errors only on the create — a
/// failure to chmod is logged by [`harden`] and does not fail the call.
/// Test: `paths::private_state_tests::ensure_creates_restrictive_dir`,
/// `workstreams::path_tests::ensure_or_report_falls_back_when_the_path_is_a_file`.
pub
/// Create and tighten the production `~/.trusty-code`.
///
/// Why: the production wrapper over [`ensure_private_state_dir_at`], so callers
/// never resolve the home directory themselves. #6999: `tcode paths import` was
/// its only caller, so the mode guarantee applied to exactly one subcommand;
/// [`crate::workstreams::default_data_dir`] now calls it too, which is the
/// resolver every private-state writer already routes through.
/// What: [`private_state_dir`] then the same create-and-tighten sequence.
/// Test: covered hermetically by `ensure_private_state_dir_at`'s tests; the
/// wrapper adds only home resolution.
/// `tests/cli_e2e.rs::a_non_import_command_tightens_a_permissive_private_state_dir`
/// covers the call from `default_data_dir`.
/// Tighten `dir` to [`PRIVATE_DIR_MODE`] when any non-owner bit is set.
///
/// Why: separated from [`ensure_private_state_dir_at`] so the "already private"
/// case does a single `metadata` call and no write at all.
/// What: no-op when [`is_restrictive`] already holds; otherwise
/// `set_permissions`, logging a `warn` if that fails.
/// Test: `paths::private_state_tests::ensure_tightens_a_permissive_existing_dir`.
/// Non-Unix targets have no mode bits to tighten.
/// Whether a directory's permissions keep it private to its owner.
///
/// Why: the assertion the permissive-permissions test makes, and the field the
/// `tcode config paths` diagnostic reports, must come from ONE rule.
/// What: on Unix, `true` when none of [`NON_OWNER_BITS`] is set; `Err` when the
/// directory cannot be stat'd. On non-Unix targets, always `true` — the platform
/// has no comparable bits and the home directory's ACL governs.
/// Test: `paths::private_state_tests::permissive_mode_is_not_restrictive`,
/// `paths::private_state_tests::ensure_creates_restrictive_dir`.
/// See the Unix variant — non-Unix targets have no mode bits to inspect.