#![cfg(any(test, feature = "test-backdoors"))]
use std::sync::atomic::{AtomicBool, Ordering};
static SAFETY_DECIDED: AtomicBool = AtomicBool::new(false);
static SAFETY_OK: AtomicBool = AtomicBool::new(false);
pub fn assert_test_backdoor_safe() {
if SAFETY_DECIDED.load(Ordering::Acquire) {
if SAFETY_OK.load(Ordering::Acquire) {
return;
}
abort_with_message();
}
let ok = is_recognized_test_environment();
SAFETY_OK.store(ok, Ordering::Release);
SAFETY_DECIDED.store(true, Ordering::Release);
if !ok {
abort_with_message();
}
}
fn is_recognized_test_environment() -> bool {
if std::env::var_os("ENVSEAL_TEST_BACKDOORS_OK").is_some() {
return true;
}
for marker in [
"CARGO_TARGET_TMPDIR", "INSTA_WORKSPACE_ROOT", "CARGO_MANIFEST_DIR", ] {
if std::env::var_os(marker).is_some() {
return true;
}
}
if std::env::var_os("CARGO_FUZZ_TARGET").is_some() || std::env::var_os("FUZZ_TARGET").is_some()
{
return true;
}
if let Some(arg0) = std::env::args_os().next() {
if let Some(s) = arg0.to_str() {
let lower = s.to_ascii_lowercase();
if lower.contains("test")
|| lower.contains("bench")
|| lower.contains("fuzz")
|| lower.ends_with("-runner")
{
return true;
}
}
}
false
}
fn abort_with_message() -> ! {
eprintln!(
"envseal: SECURITY ABORT — `test-backdoors` feature reached \
from outside a recognized Cargo test / fuzz / bench harness.\n\
This binary was compiled with `--features test-backdoors`, which \
exposes Vault::open_with_key and MasterKey::from_test_bytes — \
constructors that bypass the passphrase, hardware seal, and \
FIDO2 layers. They must NEVER be reachable in a production \
binary.\n\n\
If you are an operator who genuinely needs this functionality \
(e.g. for property-based testing of the compiled binary), set \
ENVSEAL_TEST_BACKDOORS_OK=1 in the environment of the test \
harness. Otherwise this is a build-supply-chain incident; \
rebuild from a known-clean source and audit which dependency \
turned the feature on.\n\n\
Aborting."
);
std::process::abort();
}