use std::collections::BTreeMap; use std::path::Path; use std::process::Command; fn main() { // Phase 17 (Track E.1): always emit the seccomp policy table to // OUT_DIR. Gated runtime via `#[cfg(target_os = "linux")]`, but the // codegen runs on every host so `cargo check` on macOS still emits // the file (the include never actually compiles on non-Linux). emit_seccomp_policy(); // Only relevant when the serve feature is active. if std::env::var("CARGO_FEATURE_SERVE").is_err() { return; } let dist_dir = Path::new("src/server/assets/dist"); let index_html = dist_dir.join("index.html"); // Re-run build.rs only when dist output is missing/changed println!("cargo:rerun-if-changed=src/server/assets/dist/index.html"); if index_html.exists() { // Dist already built, nothing to do return; } // Dist missing, try to build frontend let frontend_dir = Path::new("frontend"); if !frontend_dir.join("package.json").exists() { emit_placeholder_and_warn(dist_dir); return; } // Run npm install + build println!("cargo:warning=Frontend dist not found, running npm install && npm run build..."); let npm_install = Command::new("npm") .arg("install") .current_dir(frontend_dir) .status(); match npm_install { Ok(s) if s.success() => {} _ => { emit_placeholder_and_warn(dist_dir); return; } } let npm_build = Command::new("npm") .arg("run") .arg("build") .current_dir(frontend_dir) .status(); match npm_build { Ok(s) if s.success() => { println!("cargo:warning=Frontend built successfully."); } _ => { emit_placeholder_and_warn(dist_dir); } } } fn emit_placeholder_and_warn(dist_dir: &Path) { // Create minimal placeholder files so compilation succeeds std::fs::create_dir_all(dist_dir).ok(); std::fs::write( dist_dir.join("index.html"), "
Run: cd frontend && npm install && npm run build
", ) .ok(); std::fs::write(dist_dir.join("app.js"), "// frontend not built\n").ok(); std::fs::write(dist_dir.join("style.css"), "/* frontend not built */\n").ok(); println!( "cargo:warning=Node.js/npm not available — wrote placeholder frontend assets. Run 'cd frontend && npm install && npm run build' for the real UI." ); } // ── Phase 17 (Track E.1) — seccomp policy codegen ──────────────────────────── const SECCOMP_POLICY_PATH: &str = "src/dynamic/sandbox/seccomp/seccomp_policy.toml"; /// Cap-name → Cap bit value table. Mirrors the `bitflags!` block in /// `src/labels/mod.rs`. Keep in sync when adding/removing `Cap` /// constants. const CAP_BIT_FOR_NAME: &[(&str, u32)] = &[ ("ENV_VAR", 1 << 0), ("HTML_ESCAPE", 1 << 1), ("SHELL_ESCAPE", 1 << 2), ("URL_ENCODE", 1 << 3), ("JSON_PARSE", 1 << 4), ("FILE_IO", 1 << 5), ("FMT_STRING", 1 << 6), ("SQL_QUERY", 1 << 7), ("DESERIALIZE", 1 << 8), ("SSRF", 1 << 9), ("CODE_EXEC", 1 << 10), ("CRYPTO", 1 << 11), ("UNAUTHORIZED_ID", 1 << 12), ("DATA_EXFIL", 1 << 13), ("LDAP_INJECTION", 1 << 14), ("XPATH_INJECTION", 1 << 15), ("HEADER_INJECTION", 1 << 16), ("OPEN_REDIRECT", 1 << 17), ("SSTI", 1 << 18), ("XXE", 1 << 19), ("PROTOTYPE_POLLUTION", 1 << 20), ]; fn emit_seccomp_policy() { println!("cargo:rerun-if-changed={}", SECCOMP_POLICY_PATH); let out_dir = std::env::var("OUT_DIR").expect("OUT_DIR must be set by cargo"); let out_path = Path::new(&out_dir).join("seccomp_policy.rs"); // Read the policy file; on missing file (e.g. fresh checkout on a // foreign target), emit empty tables so compilation still succeeds. let toml_text = match std::fs::read_to_string(SECCOMP_POLICY_PATH) { Ok(s) => s, Err(_) => { std::fs::write( &out_path, "pub static BASE: &[&str] = &[];\npub static CAP: &[(u32, &[&str])] = &[];\n", ) .expect("write empty seccomp policy stub"); return; } }; let parsed = parse_seccomp_toml(&toml_text); let mut out = String::new(); out.push_str("// generated by build.rs from seccomp_policy.toml — do not edit\n\n"); // Base allowlist. out.push_str("pub static BASE: &[&str] = &[\n"); for name in &parsed.base { out.push_str(&format!(" \"{}\",\n", escape(name))); } out.push_str("];\n\n"); // Per-cap allowlists. out.push_str("pub static CAP: &[(u32, &[&str])] = &[\n"); for (cap_name, allow) in &parsed.caps { let bit = CAP_BIT_FOR_NAME .iter() .find(|(n, _)| *n == cap_name.as_str()) .map(|(_, b)| *b) .unwrap_or_else(|| panic!( "seccomp_policy.toml references unknown Cap '{cap_name}' — \ add it to CAP_BIT_FOR_NAME in build.rs first" )); out.push_str(&format!(" (0x{bit:08x}_u32, &[\n")); for name in allow { out.push_str(&format!(" \"{}\",\n", escape(name))); } out.push_str(" ]),\n"); } out.push_str("];\n"); std::fs::write(&out_path, out).expect("write seccomp policy table"); } #[derive(Default)] struct SeccompPolicy { base: Vec