Files
pruefwerk/compliance-core/src/exec_policy.rs
T
sharang c6d2d07999
CI / Check (push) Skipped
CI / Detect Changes (push) Successful in 3s
CI / Deploy Docs (push) Successful in 57s
CI / Deploy MCP (push) Successful in 3m41s
CI / Deploy Dashboard (push) Successful in 5m26s
CI / Deploy Agent (push) Successful in 6m48s
fix(sbom): stop resolving tenant manifests in shared deployments (#330) (#333)
2026-09-23 08:59:35 +00:00

133 lines
5.2 KiB
Rust

//! Whether this deployment may execute a tenant's code.
//!
//! Most of the scan pipeline only *reads* customer code: semgrep, gitleaks,
//! syft and the PLC parser parse files. A few stages instead **run** it, inside
//! the agent container, as root, with every secret in the process environment
//! and a Mongo user that can reach every tenant's database:
//!
//! * `cargo clippy` compiles the repo, so its `build.rs` and proc macros run;
//! * ESLint is executed from the repo's own `node_modules/.bin`, and its config
//! files are JavaScript;
//! * `pip-compile` builds source distributions to resolve metadata, which runs
//! `setup.py`;
//! * the firmware reproducible build shells out to Nix with `sandbox = false`.
//!
//! A second kind of stage runs nothing of the tenant's but lets the tenant choose
//! **where the scanner connects**. Dependency resolvers fetch whatever the
//! manifest names — a URL dependency in `package.json`, a git dependency in
//! `Cargo.toml`, a registry in a committed `.npmrc` — so resolving a tenant's
//! manifest is egress to hosts the tenant picks, outside `net_guard` (#330):
//!
//! * `npm install --package-lock-only` and `cargo generate-lockfile`, which
//! create a lockfile the repository did not commit;
//! * `cargo metadata`, which downloads every dependency to read its licence.
//!
//! The same policy gates both kinds: the question in each case is whether this
//! deployment is a boundary a tenant may act across.
//!
//! Scanning hostile code is the product's whole job, so those stages are off
//! wherever a deployment holds more than one tenant's data, and the findings
//! they produced (lint quality warnings, a firmware build SBOM) are worth less
//! than the blast radius. They return as sandboxed Werkbank jobs; firmware
//! SBOMs arrive as an imported Firmwerk bundle.
use crate::config::AppEnv;
/// Whether the tenant-code-executing stages may run.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ExecPolicy {
allow_untrusted_code: bool,
}
impl Default for ExecPolicy {
fn default() -> Self {
Self::trusted_only()
}
}
impl ExecPolicy {
/// Never execute tenant code.
pub fn trusted_only() -> Self {
Self {
allow_untrusted_code: false,
}
}
/// Execute it. Single-tenant local development, or a sandboxed runner whose
/// container is itself the boundary.
pub fn allow_untrusted_code() -> Self {
Self {
allow_untrusted_code: true,
}
}
/// Read the policy from the environment. `PW_ALLOW_UNTRUSTED_CODE_EXEC=1`
/// turns the stages back on, and a hardened deployment ignores it — there
/// the agent shares a process with other tenants' data.
pub fn from_env(app_env: AppEnv) -> Self {
let opted_in = std::env::var("PW_ALLOW_UNTRUSTED_CODE_EXEC")
.map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
.unwrap_or(false);
if opted_in && app_env.is_hardened() {
eprintln!(
"PW_ALLOW_UNTRUSTED_CODE_EXEC is ignored in a hardened deployment: \
clippy, ESLint, pip-compile and the Nix firmware build stay disabled"
);
}
Self {
allow_untrusted_code: opted_in && !app_env.is_hardened(),
}
}
/// Whether a stage that executes tenant code may run.
pub fn untrusted_code_allowed(self) -> bool {
self.allow_untrusted_code
}
/// The line to log when a dependency resolver is skipped: it would fetch from
/// hosts the scanned repository names.
pub fn resolution_skip_reason(self, tool: &str) -> String {
format!(
"{tool} fetches from hosts the scanned repository names (registries, URL and git \
dependencies) and is disabled in this deployment (set PW_ALLOW_UNTRUSTED_CODE_EXEC=1 \
on a single-tenant or sandboxed deployment to enable it)"
)
}
/// The line to log when a stage is skipped, naming what to do instead.
pub fn skip_reason(self, stage: &str) -> String {
format!(
"{stage} executes code from the scanned repository and is disabled in this \
deployment (set PW_ALLOW_UNTRUSTED_CODE_EXEC=1 on a single-tenant or sandboxed \
deployment to enable it)"
)
}
}
#[cfg(test)]
mod tests {
use super::ExecPolicy;
#[test]
fn the_default_never_executes_tenant_code() {
assert!(!ExecPolicy::default().untrusted_code_allowed());
assert!(!ExecPolicy::trusted_only().untrusted_code_allowed());
assert!(ExecPolicy::allow_untrusted_code().untrusted_code_allowed());
}
#[test]
fn the_resolution_skip_reason_says_why_it_is_not_code_execution() {
let r = ExecPolicy::trusted_only().resolution_skip_reason("npm install");
assert!(r.starts_with("npm install "), "{r}");
assert!(r.contains("hosts the scanned repository names"), "{r}");
assert!(r.contains("PW_ALLOW_UNTRUSTED_CODE_EXEC"), "{r}");
}
#[test]
fn the_skip_reason_names_the_stage_and_the_escape_hatch() {
let r = ExecPolicy::trusted_only().skip_reason("clippy");
assert!(r.starts_with("clippy "), "{r}");
assert!(r.contains("PW_ALLOW_UNTRUSTED_CODE_EXEC"), "{r}");
}
}