#[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 super::*;
const INPUT: &str = r#"{"schema":"instinct.check-pack/v1","namespace":"fixture","license":"Apache-2.0","checks":[{"schema":"instinct.check/v1","id":"equal","revision":1,"inputs":{"a":"string","b":"string"},"claim":{"op":"eq","left":{"fact":"a"},"right":{"fact":"b"}},"scope":{"project":"lab","persona":"test-persona","harness":"fixture","version":"1","machine":"isolated"}}]}"#;
fn confirmation(raw: &str) -> String {
let p = json::parse(preview(raw.as_bytes()).unwrap().as_bytes()).unwrap();
string(field(object(&p).unwrap(), "export_digest").unwrap())
.unwrap()
.to_owned()
}
#[test]
fn wrapper_allowance_preserves_kernel_depth_limit() {
for depth in [29, 30, 31, 40] {
let mut claim = r#"{"op":"eq","left":{"fact":"a"},"right":{"fact":"a"}}"#.to_owned();
for _ in 0..depth {
claim = format!("{{\"op\":\"all\",\"args\":[{claim}]}}");
}
let definition = format!(
r#"{{"schema":"instinct.check/v1","id":"deep","revision":1,"inputs":{{"a":"string"}},"claim":{claim}}}"#
);
let request = format!("{{\"check\":{definition}}}");
let (_, bad) = kernel::evaluate(Mode::Validate, request.as_bytes());
let raw=format!("{{\"schema\":\"instinct.check-pack/v1\",\"namespace\":\"test\",\"license\":\"MIT\",\"checks\":[{definition}]}}");
if bad {
assert!(preview(raw.as_bytes()).is_err(), "depth {depth}");
} else {
let p = preview(raw.as_bytes()).unwrap();
let value = json::parse_with_limits(p.as_bytes(), json::MAX_BYTES, json::MAX_DEPTH + 2)
.unwrap();
let hash = string(field(object(&value).unwrap(), "export_digest").unwrap()).unwrap();
let exported = export(raw.as_bytes(), hash).unwrap();
let quarantined = quarantine(exported.as_bytes()).unwrap();
assert!(
json::parse_with_limits(
quarantined.as_bytes(),
json::MAX_BYTES,
json::MAX_DEPTH + 3
)
.is_ok(),
"depth {depth}"
);
if depth == 30 {
assert!(json::parse(exported.as_bytes()).is_err());
assert!(json::parse(quarantined.as_bytes()).is_err());
}
}
}
}
#[test]
fn digest_is_object_order_independent() {
let value = json::parse(INPUT.as_bytes()).unwrap();
let sorted = serialized(&canonical(&value));
assert_eq!(confirmation(INPUT), confirmation(&sorted));
let wire = export(INPUT.as_bytes(), &confirmation(&sorted)).unwrap();
let reordered = serialized(&canonical(&json::parse(wire.as_bytes()).unwrap()));
assert!(quarantine(reordered.as_bytes()).is_ok());
}
#[test]
fn confirmation_binds_exact_definition_and_scope() {
let hash = confirmation(INPUT);
assert!(export(INPUT.as_bytes(), "wrong").is_err());
assert!(export(INPUT.replace("\"lab\"", "\"other\"").as_bytes(), &hash).is_err());
assert!(export(INPUT.as_bytes(), &hash).is_ok());
}
#[test]
fn imported_pack_has_no_authority_and_preserves_every_scope() {
let exported = export(INPUT.as_bytes(), &confirmation(INPUT)).unwrap();
let out = quarantine(exported.as_bytes()).unwrap();
let q = json::parse(out.as_bytes()).unwrap();
let o = object(&q).unwrap();
assert_eq!(string(field(o, "state").unwrap()).unwrap(), "quarantined");
assert_eq!(string(field(o, "authority").unwrap()).unwrap(), "none");
let package = field(o, "package").unwrap();
let p = field(object(package).unwrap(), "payload").unwrap();
assert_eq!(
serialized(p),
serialized(&json::parse(INPUT.as_bytes()).unwrap())
);
}
#[test]
fn private_facts_and_transcripts_are_not_pack_fields() {
for key in [
"facts",
"receipts",
"transcript",
"environment",
"path",
"authority",
"code",
"adapter",
] {
let bad = INPUT.replacen("{", &format!("{{\"{key}\":\"PRIVATE_SENTINEL\","), 1);
let err = preview(bad.as_bytes()).unwrap_err();
assert!(!err.contains("PRIVATE_SENTINEL"));
assert!(export(bad.as_bytes(), "wrong").is_err());
}
}
#[test]
fn tampered_digest_and_poisoned_definition_reject() {
let out = export(INPUT.as_bytes(), &confirmation(INPUT)).unwrap();
assert!(quarantine(out.replace("\"lab\"", "\"other\"").as_bytes()).is_err());
let bad = INPUT.replace("\"eq\"", "\"shell\"");
assert!(preview(bad.as_bytes()).is_err());
}
#[test]
fn unknown_envelope_fields_do_not_grant_authority() {
let out = export(INPUT.as_bytes(), &confirmation(INPUT)).unwrap();
let bad = out.replacen("{", "{\"verified\":true,", 1);
assert!(quarantine(bad.as_bytes()).is_err());
}
#[test]
fn duplicate_keys_are_rejected() {
let bad = INPUT.replacen("{", "{\"namespace\":\"duplicate\",", 1);
assert!(preview(bad.as_bytes()).is_err());
}
#[test]
fn creation_is_exclusive_and_never_activates_a_bank() {
let dir = std::env::temp_dir().join(format!("pack-create-{}", std::process::id()));
std::fs::create_dir(&dir).unwrap();
let input = dir.join("definition.json");
std::fs::write(&input, INPUT).unwrap();
let output = dir.join("pack.json");
let args = vec![
"export".into(),
"--input".into(),
input.to_string_lossy().into_owned(),
"--confirm".into(),
confirmation(INPUT),
"--output".into(),
output.to_string_lossy().into_owned(),
];
if cfg!(not(unix)) {
let err = cli(&args).unwrap_err();
assert!(err.contains("unsupported_platform"));
assert!(!output.exists());
std::fs::remove_dir_all(&dir).unwrap();
return;
}
let result = cli(&args).unwrap();
assert!(result.contains("\"activated\":false"));
let bytes = std::fs::read(&output).unwrap();
assert!(cli(&args).is_err());
assert_eq!(bytes, std::fs::read(&output).unwrap());
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
assert_eq!(
std::fs::metadata(&output).unwrap().permissions().mode() & 0o077,
0
);
}
assert_eq!(std::fs::read_dir(&dir).unwrap().count(), 2);
std::fs::remove_dir_all(&dir).unwrap();
}