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
//! Runtime guard for the `test-backdoors` cargo feature (audit M21).
//!
//! `MasterKey::from_test_bytes` and `Vault::open_with_key` exist so
//! integration tests in `core/tests/` and `cargo-fuzz` targets can
//! drive the vault without going through the real passphrase / GUI /
//! authenticator stack. They are gated behind the `test-backdoors`
//! cargo feature, which is only enabled via the dev-dependency alias
//! in `core/Cargo.toml`.
//!
//! That feature gate is *not* sufficient on its own: a downstream
//! consumer (or a compromised build script in a transitive
//! dependency) could enable `test-backdoors` and ship a release
//! `envseal-cli` whose `Vault::open_with_key` exposes the entire
//! vault to passphrase-less unlock. The audit's recommendation was to
//! "move test-only constructors into a separate unpublished
//! `envseal-test-helpers` crate, *or* guard with `#[cfg(test)]` only".
//!
//! `#[cfg(test)]` alone does not work — integration tests link
//! against the lib compiled *without* `cfg(test)`, so the symbols
//! would be invisible. Extracting to a separate crate forces every
//! caller through a `pub` API on `envseal` proper, which is exactly
//! the surface we are trying to keep closed.
//!
//! This module is the third option: keep the feature, but **abort
//! the process** if the test-only constructors are reached outside a
//! recognized test / fuzz / bench harness. Cargo sets a number of
//! environment variables when it runs tests, fuzz targets, or
//! benchmarks — none of which are set in a `cargo install`-produced
//! binary that an end user runs. We use that as a "this is not a
//! production binary" signal. A loud `std::process::abort` is the
//! correct response: a malicious actor flipping the feature in their
//! supply chain gets a hard-fail at runtime, not silent unlock.
//!
//! Operators who legitimately need to run a binary built with
//! `--features test-backdoors` outside a Cargo harness (e.g. a
//! property-test runner that drives the compiled binary) can opt in
//! explicitly with `ENVSEAL_TEST_BACKDOORS_OK=1`. That env var is
//! deliberately verbose — anyone who sets it has acknowledged in
//! writing that the binary they are running is a test backdoor build.
use ;
/// Cached result of the env-probe so the first call pays the cost
/// and subsequent calls in a hot-path test are free. Reset path
/// not provided — the answer cannot change for a given process.
static SAFETY_DECIDED: AtomicBool = new;
static SAFETY_OK: AtomicBool = new;
/// Aborts the process with a security-focused message if the test
/// backdoors are reached from outside a recognized harness.
///
/// Called from the bodies of [`crate::vault::keychain::MasterKey::from_test_bytes`]
/// and [`crate::vault::Vault::open_with_key`] before any work happens.
/// Probes the process environment for any signal that we are running
/// under a Cargo test / fuzz / bench harness. None of these env vars
/// are set in a user-installed `envseal` binary.
!