#[cfg(windows)]
#[allow(dead_code)]
const SYS_CURL: &str = "C:\\Windows\\System32\\curl.exe";
#[cfg(not(windows))]
#[allow(dead_code)]
const SYS_CURL: &str = "curl";
#[allow(dead_code)]
fn _helix_allowed(var: &str, what: &str) -> bool {
let ok = std::env::var(var).map(|v| v == "1").unwrap_or(false);
if !ok {
eprintln!("helix: BLOCKED {} -- permission denied (set {}=1 to allow; `hxc run` allows by default, a `hxc build`/`export-rust` binary does not)", what, var);
}
ok
}
#[cfg(windows)]
#[allow(dead_code)]
fn _nw_exec(mut c: std::process::Command) -> std::process::Command {
if !_helix_allowed("HELIX_ALLOW_RUN", "process execution") {
std::process::exit(13);
}
use std::os::windows::process::CommandExt;
c.creation_flags(0x08000000);
c
}
#[cfg(not(windows))]
#[allow(dead_code)]
fn _nw_exec(c: std::process::Command) -> std::process::Command {
if !_helix_allowed("HELIX_ALLOW_RUN", "process execution") {
std::process::exit(13);
}
c
}
#[cfg(windows)]
#[allow(dead_code)]
fn _nw_net(mut c: std::process::Command) -> std::process::Command {
if !_helix_allowed("HELIX_ALLOW_NET", "network access") {
std::process::exit(13);
}
use std::os::windows::process::CommandExt;
c.creation_flags(0x08000000);
c
}
#[cfg(not(windows))]
#[allow(dead_code)]
fn _nw_net(c: std::process::Command) -> std::process::Command {
if !_helix_allowed("HELIX_ALLOW_NET", "network access") {
std::process::exit(13);
}
c
}
use std::{
io::Write,
process::{Command, Stdio},
};
fn call(input: &str) -> (bool, String) {
let mut c = Command::new(env!("CARGO_BIN_EXE_instinct"))
.args(["check", "export-boundary", "--input", "-"])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.unwrap();
c.stdin.take().unwrap().write_all(input.as_bytes()).ok();
let out = c.wait_with_output().unwrap();
(out.status.success(), String::from_utf8(out.stdout).unwrap())
}
const INPUT: &str = r#"{"schema":"instinct.export-boundary/v1","policy":{"allowed_recipients":["team-a"],"allowed_fields":["total"],"allowed_destinations":["internal"],"allowed_labels":["aggregate"]},"export":{"recipients":["team-a"],"fields":["total"],"destination":"internal","labels":["aggregate"]},"conditions":{"complete_inventory":true,"explicit_policy_authority":true}}"#;
#[test]
fn matching_export_passes_without_authorizing_or_sending() {
let (ok, out) = call(INPUT);
assert!(ok, "{out}");
for s in [
"\"verdict\":\"pass\"",
"\"sent\":false",
"\"authorization\":\"not_evaluated\"",
"caller_reported_inventory_and_policy",
] {
assert!(out.contains(s), "{out}");
}
}
#[test]
fn every_export_dimension_can_fail_without_leaking_witness() {
for (from, to) in [
(
"\"recipients\":[\"team-a\"]",
"\"recipients\":[\"PRIVATE_SENTINEL\"]",
),
(
"\"fields\":[\"total\"]",
"\"fields\":[\"PRIVATE_SENTINEL\"]",
),
(
"\"destination\":\"internal\"",
"\"destination\":\"PRIVATE_SENTINEL\"",
),
(
"\"labels\":[\"aggregate\"]",
"\"labels\":[\"PRIVATE_SENTINEL\"]",
),
] {
let (ok, out) = call(&INPUT.replace(from, to));
assert!(ok && out.contains("\"verdict\":\"fail\""), "{out}");
assert!(!out.contains("PRIVATE_SENTINEL"));
}
}
#[test]
fn missing_or_false_authority_and_inventory_abstain() {
for key in ["complete_inventory", "explicit_policy_authority"] {
let bad = INPUT.replace(&format!("\"{key}\":true"), &format!("\"{key}\":false"));
let (ok, out) = call(&bad);
assert!(ok && out.contains("\"verdict\":\"abstain\""), "{out}");
}
let input = INPUT.replace(
",\"conditions\":{\"complete_inventory\":true,\"explicit_policy_authority\":true}",
"",
);
let (ok, out) = call(&input);
assert!(ok && out.contains("abstain"), "{out}");
}
#[test]
fn missing_policy_fields_abstain_not_wildcard() {
let input = INPUT.replace("\"allowed_recipients\":[\"team-a\"],", "");
let (ok, out) = call(&input);
assert!(ok && out.contains("abstain"), "{out}");
}
#[test]
fn invalid_duplicate_and_executable_fields_reject() {
for input in [
INPUT.replacen("{", "{\"command\":\"PRIVATE_SENTINEL\",", 1),
INPUT.replace(
"\"recipients\":[\"team-a\"]",
"\"recipients\":[\"team-a\",\"team-a\"]",
),
INPUT.replace(
"\"complete_inventory\":true",
"\"complete_inventory\":\"PRIVATE_SENTINEL\"",
),
] {
let (ok, out) = call(&input);
assert!(!ok, "{out}");
assert!(!out.contains("PRIVATE_SENTINEL"));
}
}
#[test]
fn all_boolean_policy_combinations_match_independent_oracle() {
for mask in 0..64 {
let mut input = INPUT.to_owned();
for (bit, from, to) in [
(
1,
"\"recipients\":[\"team-a\"]",
"\"recipients\":[\"other\"]",
),
(2, "\"fields\":[\"total\"]", "\"fields\":[\"secret\"]"),
(
4,
"\"destination\":\"internal\"",
"\"destination\":\"public\"",
),
(8, "\"labels\":[\"aggregate\"]", "\"labels\":[\"private\"]"),
(
16,
"\"complete_inventory\":true",
"\"complete_inventory\":false",
),
(
32,
"\"explicit_policy_authority\":true",
"\"explicit_policy_authority\":false",
),
] {
if mask & bit != 0 {
input = input.replace(from, to);
}
}
let expected = if mask & 48 != 0 {
"abstain"
} else if mask & 15 != 0 {
"fail"
} else {
"pass"
};
let (ok, out) = call(&input);
assert!(ok, "{out}");
assert!(
out.contains(&format!("\"verdict\":\"{expected}\"")),
"mask {mask}: {out}"
);
}
}
#[test]
fn exact_scope_is_required() {
let input = INPUT.replace(
"\"policy\":{",
"\"policy\":{\"scope\":{\"project\":\"lab\"},",
);
let (ok, out) = call(&input);
assert!(ok && out.contains("abstain"), "{out}");
let input = input.replacen("{", "{\"scope\":{\"project\":\"other\"},", 1);
let (ok, out) = call(&input);
assert!(ok && out.contains("abstain"), "{out}");
}