Files
pruefwerk/compliance-core/src/scan_matrix.rs
T
Sharang ParnerkarandClaude Opus 5.5 a50fdf44e8
CI / Check (push) Skipped
CI / Detect Changes (pull_request) Skipped
CI / Deploy Agent (pull_request) Skipped
CI / Deploy Dashboard (pull_request) Skipped
CI / Deploy Docs (pull_request) Skipped
CI / Deploy MCP (pull_request) Skipped
CI / Check (pull_request) Failing after 2m11s
feat(yocto): recipes are checked with oelint-adv and our own secure-by-default rules
Recipe lint over a Yocto layer tree, without a build:

- oelint-adv 9.11.2 (BSD-2-Clause, pinned in the agent image) runs with a
  rule file holding only its security rules (bad image features, missing
  checksums, AUTOREV, git tags, network tasks, INSANE_SKIP), with an empty
  environment, OELINT_SKIP_CONFIG and a 10-minute timeout. Each layer is read
  with the oldest release its LAYERSERIES_COMPAT names. A run that does not
  complete is recorded as the skipped stage recipe_lint.
- Our own rules (yocto-config): passwords in EXTRA_USERS_PARAMS (value
  hidden), http:// in SRC_URI, PermitRootLogin/PasswordAuthentication yes in a
  shipped sshd_config, certificates shipped with a recipe (info), and a distro
  or local.conf.sample without security_flags.inc (following require/include).
- Upstream layers (Poky, OpenEmbedded, BSP vendors, by BBFILE_COLLECTIONS) are
  not linted and are named as a skipped stage.
- Findings carry the type recipe_lint, the CRA Annex I requirement, and bind
  by rule id to cra-ai-1, cra-ai-8 and cra-ai-6 in the control map; .bb files
  count as the language bitbake, so those controls can be assessed.
- The scan matrix turns recipe_lint on; the dashboard lists the type in the
  findings filter and names both tools; docs list the rules; the nightly
  fixture gains an insecure image, recipes, distro and sshd_config.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p
2026-10-09 17:25:54 +02:00

791 lines
31 KiB
Rust

//! The scan-applicability matrix.
//!
//! Which scans are possible for a target is a function of its [`TargetType`] and
//! which [`ArtifactKind`]s are actually present: SAST needs code, DAST needs a
//! running URL, firmware-static analysis needs a firmware image, and so on. This
//! module encodes that as a table — one rule set per target type — and resolves
//! it against a concrete [`OnboardedTarget`] into a list of [`ScanOption`]s the
//! onboarding wizard and the scan pipeline both consume.
//!
//! A rule may also be marked *unavailable*: the scan belongs to the target type,
//! but no pipeline stage runs it yet (unpacking firmware images, mobile packages,
//! container images). Those are never offered, and when the target does carry the
//! artifact they would consume, the run records a skipped stage naming the reason
//! — so a finished scan never implies something was examined that was not.
use crate::models::{ArtifactKind, OnboardedTarget, ScanType, TargetType};
/// Why a firmware *image* is not analysed: the firmware path today is the source
/// tree (Firmwerk build-plan analysis).
const UNPACKING_IMAGES: &str =
"firmware images are not unpacked or analysed yet: scan the firmware sources \
instead";
const SBOM_FROM_IMAGE: &str =
"no SBOM is extracted from a firmware image yet: it comes from the firmware \
sources";
const CVE_FROM_IMAGE: &str =
"CVE matching needs an SBOM, and none is extracted from a firmware image yet";
const CONTAINER_IMAGES: &str =
"container images are not scanned yet: only the sources that build them are";
/// Why a Yocto image has no SBOM or CVE match yet: that needs the build's
/// artefacts (tier 2), not the layer tree.
const YOCTO_IMAGE_SBOM: &str = "a Yocto image SBOM needs build artefacts (SPDX/manifest), planned";
const YOCTO_IMAGE_CVE: &str =
"CVE matching for a Yocto image needs build artefacts (SPDX/manifest), planned";
/// Why the firmware SBOM stage passes over a Yocto layer tree: Firmwerk forms
/// no build plan from BitBake recipes. The agent records it with this text.
pub const YOCTO_BUILD_PLANNED: &str =
"Yocto build analysis is planned (tier 2): a layer tree is not a firmware build";
/// The recipe inventory of a Yocto layer tree (SBOM and CVEs from its BitBake
/// recipes): `Some(reason)` while no stage runs it, `None` once one does.
const RECIPE_INVENTORY: Option<&str> =
Some("the recipe inventory (SBOM and CVEs from the BitBake recipes) is planned");
/// Recipe lint (oelint-adv and our own rules) over a Yocto layer tree, likewise.
const RECIPE_LINT: Option<&str> = None;
/// What an artifact a scan needs in order to run.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ArtifactRequirement {
/// Source code — a git repo or a source archive.
Code,
/// A reachable running instance (any live URL / endpoint, scheme-agnostic —
/// e.g. the ICS probe works off the host:port of a modbus:// or http:// ref).
RunningUrl,
/// A reachable **web** endpoint — a live URL with an http(s) scheme. DAST is
/// an HTTP crawler, so a modbus:// / opc.tcp:// endpoint does not satisfy it.
HttpUrl,
/// A firmware image / binary blob.
Firmware,
/// A PLC project (PLCopen XML or Structured Text).
Plc,
/// A mobile package (APK / AAB / IPA).
Mobile,
/// A container image.
Container,
/// No specific artifact required.
Any,
}
/// A static rule: this scan applies to a target type, needs this artifact, and
/// defaults on/off. The rationale explains the entry to the user.
#[derive(Debug, Clone, Copy)]
pub struct ScanRule {
/// The scan this rule governs.
pub scan: ScanType,
/// Whether the scan is on by default (only when its artifact is present).
pub default_on: bool,
/// Human-readable explanation of what the scan does here.
pub rationale: &'static str,
/// The artifact the scan consumes.
pub requires: ArtifactRequirement,
/// Set when no pipeline stage runs this scan yet: what covers the ground
/// instead. Such a scan is never offered, and a target that carries its
/// artifact records it as a skipped stage, so "scanned" never means
/// "nothing looked at this".
pub unavailable: Option<&'static str>,
}
impl ScanRule {
/// A rule for a scan the matrix knows about but no pipeline stage runs yet.
/// `reason` is what a user is told instead, and what the scan run records.
const fn unavailable(
scan: ScanType,
rationale: &'static str,
requires: ArtifactRequirement,
reason: &'static str,
) -> Self {
Self {
unavailable: Some(reason),
scan,
default_on: false,
rationale,
requires,
}
}
/// A rule for a scan the pipeline runs.
const fn new(
scan: ScanType,
default_on: bool,
rationale: &'static str,
requires: ArtifactRequirement,
) -> Self {
Self {
unavailable: None,
scan,
default_on,
rationale,
requires,
}
}
}
/// A resolved scan choice for a specific target: a rule intersected with the
/// artifacts actually present. `blocked_reason` is `Some` when the required
/// artifact is missing; `unavailable` when no pipeline stage runs the scan yet.
#[derive(Debug, Clone)]
pub struct ScanOption {
/// The scan.
pub scan: ScanType,
/// Whether to pre-select the scan (false when blocked or unavailable).
pub default_on: bool,
/// Why the scan is offered.
pub rationale: String,
/// The artifact kind the scan needs, if any specific one.
pub required_artifact: Option<ArtifactKind>,
/// Set when the required artifact is absent, explaining the block.
pub blocked_reason: Option<String>,
/// Set when no pipeline stage runs this scan yet, explaining what covers the
/// ground instead. Such a scan is never offered; when the target does carry
/// its artifact, the scan run records it as a skipped stage.
pub unavailable: Option<&'static str>,
}
impl ScanOption {
/// The reason this scan cannot run, for the user: not implemented outranks a
/// missing artifact, since attaching one would not help.
pub fn unrunnable_reason(&self) -> Option<String> {
self.unavailable
.map(str::to_string)
.or_else(|| self.blocked_reason.clone())
}
/// The scan is not implemented **and** the target carries the artifact it
/// would consume: the run records this, so an unscanned artifact is visible.
pub fn unavailable_with_artifact(&self) -> Option<&'static str> {
self.unavailable.filter(|_| self.blocked_reason.is_none())
}
}
/// The SAST umbrella: every static-analysis sub-scan that runs over source code.
fn sast_umbrella() -> Vec<ScanRule> {
use ArtifactRequirement::Code;
vec![
ScanRule::new(
ScanType::Sast,
true,
"Static analysis (Semgrep) over source",
Code,
),
ScanRule::new(
ScanType::Sbom,
true,
"Software bill of materials from source",
Code,
),
ScanRule::new(
ScanType::Cve,
true,
"Match dependencies against known CVEs",
Code,
),
ScanRule::new(
ScanType::SecretDetection,
true,
"Scan source for committed secrets",
Code,
),
ScanRule::new(ScanType::Lint, true, "Language linters over source", Code),
ScanRule::new(
ScanType::Gdpr,
true,
"GDPR data-handling pattern checks",
Code,
),
ScanRule::new(
ScanType::OAuth,
true,
"OAuth misconfiguration patterns",
Code,
),
ScanRule::new(
ScanType::Graph,
true,
"Build the code graph for impact analysis",
Code,
),
ScanRule::new(
ScanType::CodeReview,
false,
"LLM code review over changed source",
Code,
),
]
}
/// The rule set for a target type. Scans that are never applicable to a type are
/// simply absent (e.g. DAST is not listed for a PLC target).
pub fn rules_for(target_type: TargetType) -> Vec<ScanRule> {
let mut rules = target_rules(target_type);
// Any target may carry a container image, and nothing scans one; say so
// rather than accepting the artifact in silence.
rules.push(ScanRule::unavailable(
ScanType::ContainerScan,
"Scan the container image for OS and package vulnerabilities",
ArtifactRequirement::Container,
CONTAINER_IMAGES,
));
rules
}
/// The rules specific to a target type (see [`rules_for`], which adds the rules
/// that apply to every target).
fn target_rules(target_type: TargetType) -> Vec<ScanRule> {
use ArtifactRequirement::{Code, Firmware, HttpUrl, Mobile, Plc, RunningUrl};
match target_type {
TargetType::WebApp | TargetType::BackendService => {
let mut r = sast_umbrella();
r.push(ScanRule::new(
ScanType::Dast,
true,
"Dynamic scan of the running endpoint",
HttpUrl,
));
r
}
TargetType::DesktopApp => sast_umbrella(),
TargetType::AndroidApp | TargetType::IosApp => {
let mut r = sast_umbrella();
r.push(ScanRule::unavailable(
ScanType::MobileStatic,
"Static analysis of the mobile package (manifest, permissions, libs)",
Mobile,
"mobile packages are not analysed yet: only the app's source code is scanned",
));
r
}
TargetType::FirmwareBareMetal | TargetType::FirmwareRtos => {
let mut r = sast_umbrella();
r.push(ScanRule::unavailable(
ScanType::FirmwareStatic,
"Unpack and statically analyze the firmware image",
Firmware,
UNPACKING_IMAGES,
));
r.push(ScanRule::unavailable(
ScanType::Sbom,
"SBOM from the firmware image",
Firmware,
SBOM_FROM_IMAGE,
));
r.push(ScanRule::unavailable(
ScanType::Cve,
"Match firmware components against known CVEs",
Firmware,
CVE_FROM_IMAGE,
));
r
}
TargetType::EmbeddedLinuxYocto => {
let mut r = sast_umbrella();
// Tier 1: the layer tree itself. Each turns on by setting its
// constant to `None`.
r.push(ScanRule {
unavailable: RECIPE_INVENTORY,
..ScanRule::new(
ScanType::RecipeInventory,
true,
"Recipe inventory (SBOM and CVEs) from the layers' BitBake recipes",
Code,
)
});
r.push(ScanRule {
unavailable: RECIPE_LINT,
..ScanRule::new(
ScanType::RecipeLint,
true,
"Security lint of the BitBake recipes and image configuration",
Code,
)
});
r.push(ScanRule::unavailable(
ScanType::FirmwareStatic,
"EMBA / binwalk static analysis of the image",
Firmware,
UNPACKING_IMAGES,
));
r.push(ScanRule::unavailable(
ScanType::Sbom,
"SBOM from the image's build artefacts",
Firmware,
YOCTO_IMAGE_SBOM,
));
r.push(ScanRule::unavailable(
ScanType::Cve,
"Match image components against known CVEs",
Firmware,
YOCTO_IMAGE_CVE,
));
r.push(ScanRule::new(
ScanType::Dast,
false,
"Dynamic scan of exposed network services (if any)",
HttpUrl,
));
r
}
TargetType::PlcSps => {
// A PLC/SPS device is a composite: the control application *and* the
// device it runs on (firmware/OS + reachable runtime services). The
// control-logic scan runs on the PLC project; the firmware and DAST
// scans light up only when a firmware image / running endpoint is
// attached (e.g. a CODESYS runtime on a Yocto image with WebVisu).
// Firmware-image SBOM/CVE *execution* is shared with the firmware
// families and tracked in #151/#128; DAST over a WebVisu/OPC-UA
// endpoint uses the existing DAST path.
vec![
ScanRule::new(
ScanType::PlcControlLogic,
true,
"Control-logic security rules over the PLC program",
Plc,
),
// Device-level scans are offered but opt-in (default-off): they
// apply only when a firmware image is attached, and firmware-image
// SBOM/CVE *execution* is shared with the firmware families and
// still landing (#151/#128), so they must not silently auto-run.
ScanRule::unavailable(
ScanType::FirmwareStatic,
"Static analysis of the device firmware image (OS + runtime)",
Firmware,
UNPACKING_IMAGES,
),
ScanRule::unavailable(
ScanType::Sbom,
"SBOM from the device firmware image (OS packages + CODESYS runtime)",
Firmware,
SBOM_FROM_IMAGE,
),
ScanRule::unavailable(
ScanType::Cve,
"Match device firmware components against known CVEs",
Firmware,
CVE_FROM_IMAGE,
),
ScanRule::new(
ScanType::Dast,
false,
"Dynamic scan of the running device (WebVisu / exposed services)",
HttpUrl,
),
ScanRule::new(
ScanType::IcsProbe,
false,
"Probe the running device over industrial protocols (Modbus/TCP, …)",
RunningUrl,
),
]
}
}
}
/// Whether an active penetration test is applicable to this target type.
///
/// Pentest runs as its own session (not a [`ScanType`] scan) and needs a
/// reachable running target, so it is offered only for the network-reachable
/// families. Not for Embedded Linux / Yocto until an image can be booted in an
/// emulation sandbox: a pentest of the customer's device is out of scope.
pub fn supports_pentest(target_type: TargetType) -> bool {
matches!(
target_type,
TargetType::WebApp
| TargetType::BackendService
| TargetType::AndroidApp
| TargetType::IosApp
// A PLC/SPS device exposes reachable runtime services (WebVisu, OPC UA,
// the CODESYS programming protocol), so an active pentest applies.
| TargetType::PlcSps
)
}
/// The representative artifact kind a requirement is satisfied by.
fn representative_kind(req: ArtifactRequirement) -> Option<ArtifactKind> {
match req {
ArtifactRequirement::Code => Some(ArtifactKind::GitRepo),
ArtifactRequirement::RunningUrl | ArtifactRequirement::HttpUrl => {
Some(ArtifactKind::LiveUrl)
}
ArtifactRequirement::Firmware => Some(ArtifactKind::FirmwareImage),
ArtifactRequirement::Plc => Some(ArtifactKind::PlcProject),
ArtifactRequirement::Mobile => Some(ArtifactKind::MobilePackage),
ArtifactRequirement::Container => Some(ArtifactKind::ContainerImage),
ArtifactRequirement::Any => None,
}
}
/// Whether a live-URL reference is an http(s) web endpoint (vs. an industrial
/// endpoint like `modbus://` / `opc.tcp://`, which DAST cannot crawl).
fn is_http_url(source_ref: &str) -> bool {
let s = source_ref.trim();
s.starts_with("http://") || s.starts_with("https://")
}
/// Whether the target carries an artifact that satisfies the requirement.
fn requirement_satisfied(req: ArtifactRequirement, target: &OnboardedTarget) -> bool {
match req {
ArtifactRequirement::Code => target.code_artifact().is_some(),
ArtifactRequirement::RunningUrl => target.has(ArtifactKind::LiveUrl),
ArtifactRequirement::HttpUrl => target
.artifacts
.iter()
.any(|a| a.kind == ArtifactKind::LiveUrl && is_http_url(&a.source_ref)),
ArtifactRequirement::Firmware => target.has(ArtifactKind::FirmwareImage),
// A PLC project artifact, or a code artifact (git repo / source archive)
// holding the control logic as PLCopen XML / ST exports — the common way
// CODESYS projects are version-controlled.
ArtifactRequirement::Plc => {
target.has(ArtifactKind::PlcProject) || target.code_artifact().is_some()
}
ArtifactRequirement::Mobile => target.has(ArtifactKind::MobilePackage),
ArtifactRequirement::Container => target.has(ArtifactKind::ContainerImage),
ArtifactRequirement::Any => true,
}
}
/// Resolve the matrix for a concrete target into the scans it can run, marking
/// any whose required artifact is missing as blocked.
pub fn applicable_scans(target: &OnboardedTarget) -> Vec<ScanOption> {
rules_for(target.target_type)
.into_iter()
.map(|rule| {
let satisfied = requirement_satisfied(rule.requires, target);
let required_artifact = representative_kind(rule.requires);
let blocked_reason = if satisfied {
None
} else if rule.requires == ArtifactRequirement::HttpUrl {
// A live URL may be present but non-HTTP (e.g. modbus://): be
// specific so the user knows DAST needs a web endpoint.
Some("no http(s) live URL — DAST needs a web endpoint".to_string())
} else {
Some(match required_artifact {
Some(kind) => format!("no {kind} artifact provided"),
None => "required artifact missing".to_string(),
})
};
ScanOption {
scan: rule.scan,
default_on: rule.default_on && satisfied && rule.unavailable.is_none(),
rationale: rule.rationale.to_string(),
required_artifact,
blocked_reason,
unavailable: rule.unavailable,
}
})
.collect()
}
#[cfg(test)]
#[allow(clippy::expect_used, clippy::unwrap_used)]
mod tests {
use super::*;
use crate::models::{Artifact, PlcFormat};
fn target_with(target_type: TargetType, artifacts: Vec<Artifact>) -> OnboardedTarget {
let mut t = OnboardedTarget::new("t".to_string(), target_type);
t.artifacts = artifacts;
t
}
fn option(opts: &[ScanOption], scan: ScanType) -> Option<&ScanOption> {
opts.iter().find(|o| o.scan == scan)
}
#[test]
fn webapp_with_code_and_url_offers_sast_and_dast() {
let t = target_with(
TargetType::WebApp,
vec![
Artifact::git_repo("u", "main"),
Artifact::live_url("http://x"),
],
);
let opts = applicable_scans(&t);
let sast = option(&opts, ScanType::Sast).expect("sast offered");
assert!(sast.default_on && sast.blocked_reason.is_none());
let dast = option(&opts, ScanType::Dast).expect("dast offered");
assert!(dast.default_on && dast.blocked_reason.is_none());
}
#[test]
fn webapp_without_url_blocks_dast() {
let t = target_with(TargetType::WebApp, vec![Artifact::git_repo("u", "main")]);
let opts = applicable_scans(&t);
let dast = option(&opts, ScanType::Dast).expect("dast listed");
assert!(!dast.default_on);
assert!(dast.blocked_reason.is_some());
assert_eq!(dast.required_artifact, Some(ArtifactKind::LiveUrl));
}
#[test]
fn a_firmware_image_is_listed_as_unanalysed_never_offered() {
let t = target_with(
TargetType::FirmwareBareMetal,
vec![Artifact::firmware_image("fw.bin")],
);
let opts = applicable_scans(&t);
// Nothing unpacks an image yet, so it is never pre-selected or offered,
// and the reason says what to do instead. SBOM and CVE are listed twice
// (once off the sources, once off the image); this is the image rule.
let image_option = |scan: ScanType| {
opts.iter().find(|o| {
o.scan == scan && o.required_artifact == Some(ArtifactKind::FirmwareImage)
})
};
for (scan, reason) in [
(ScanType::FirmwareStatic, UNPACKING_IMAGES),
(ScanType::Sbom, SBOM_FROM_IMAGE),
(ScanType::Cve, CVE_FROM_IMAGE),
] {
let opt = image_option(scan).expect("listed for a firmware target");
assert!(!opt.default_on, "{scan} must not be pre-selected");
assert_eq!(opt.unrunnable_reason().as_deref(), Some(reason));
// The artifact is attached, so the run records it as passed over.
assert_eq!(opt.unavailable_with_artifact(), Some(reason));
}
assert!(option(&opts, ScanType::Dast).is_none());
}
#[test]
fn an_unavailable_scan_without_its_artifact_is_not_recorded() {
// No image attached: there is nothing the run passed over, so it has
// nothing to record — only the reason shown in the wizard.
let t = target_with(
TargetType::FirmwareBareMetal,
vec![Artifact::git_repo("https://git/fw", "main")],
);
let opts = applicable_scans(&t);
let fw = option(&opts, ScanType::FirmwareStatic).expect("listed");
assert!(fw.unavailable_with_artifact().is_none());
assert!(fw.unrunnable_reason().is_some());
}
#[test]
fn a_container_image_is_listed_as_unscanned_for_any_target() {
let t = target_with(
TargetType::WebApp,
vec![Artifact::container_image("registry/app:1.2.3")],
);
let opts = applicable_scans(&t);
let container = option(&opts, ScanType::ContainerScan).expect("container scan listed");
assert!(!container.default_on);
assert_eq!(
container.unavailable_with_artifact(),
Some(CONTAINER_IMAGES)
);
}
#[test]
fn a_mobile_package_is_listed_as_unanalysed() {
let t = target_with(
TargetType::AndroidApp,
vec![Artifact::mobile_package("app.apk")],
);
let opts = applicable_scans(&t);
let mobile = option(&opts, ScanType::MobileStatic).expect("mobile static listed");
assert!(!mobile.default_on);
assert!(mobile
.unavailable_with_artifact()
.is_some_and(|r| r.contains("mobile packages")));
// Its source code is still scanned.
assert!(option(&opts, ScanType::Sast).is_some());
}
#[test]
fn plc_control_logic_is_default_on_and_device_scans_block_without_artifacts() {
// A PLC project alone: control-logic runs; the device-level scans are
// offered but blocked until a firmware image / running endpoint is added.
let t = target_with(
TargetType::PlcSps,
vec![Artifact::plc_project("p.xml", PlcFormat::PlcopenXml)],
);
let opts = applicable_scans(&t);
let plc = option(&opts, ScanType::PlcControlLogic).expect("control-logic offered");
assert!(plc.default_on && plc.blocked_reason.is_none());
for scan in [ScanType::FirmwareStatic, ScanType::Sbom, ScanType::Cve] {
let o = option(&opts, scan).expect("device scan offered");
assert!(
!o.default_on,
"{scan} must not pre-select without a firmware image"
);
assert!(o.blocked_reason.is_some());
}
let dast = option(&opts, ScanType::Dast).expect("dast offered");
assert!(!dast.default_on);
assert!(dast.blocked_reason.is_some());
}
#[test]
fn plc_control_logic_is_satisfied_by_a_git_repo() {
// A CODESYS project version-controlled in git (PLCopen XML / ST exports),
// no uploaded PlcProject artifact.
let t = target_with(TargetType::PlcSps, vec![Artifact::git_repo("u", "main")]);
let opts = applicable_scans(&t);
let plc = option(&opts, ScanType::PlcControlLogic).expect("control-logic offered");
assert!(
plc.default_on && plc.blocked_reason.is_none(),
"a git repo should satisfy PLC control-logic"
);
}
#[test]
fn plc_composite_lights_up_device_scans_with_firmware_and_url() {
// A CODESYS-on-Yocto device: PLC project + firmware image + WebVisu URL.
let t = target_with(
TargetType::PlcSps,
vec![
Artifact::plc_project("p.xml", PlcFormat::PlcopenXml),
Artifact::firmware_image("device.img"),
Artifact::live_url("http://plc.local/webvisu"),
],
);
let opts = applicable_scans(&t);
for scan in [
ScanType::PlcControlLogic,
ScanType::FirmwareStatic,
ScanType::Sbom,
ScanType::Cve,
] {
let o = option(&opts, scan).expect("scan offered");
assert!(o.blocked_reason.is_none(), "{scan} should be unblocked");
}
// Control-logic auto-runs; the device-level scans are unblocked but opt-in
// (default-off) until firmware-image execution lands (#151/#128).
assert!(option(&opts, ScanType::PlcControlLogic).unwrap().default_on);
assert!(!option(&opts, ScanType::Sbom).unwrap().default_on);
assert!(!option(&opts, ScanType::Dast).unwrap().default_on);
assert!(option(&opts, ScanType::Dast)
.unwrap()
.blocked_reason
.is_none());
}
#[test]
fn plc_with_modbus_url_offers_ics_probe_but_blocks_dast() {
// A soft-PLC reachable only over Modbus/TCP (no WebVisu). The ICS probe
// is applicable (it works off host:port), but DAST — an HTTP crawler —
// must be blocked so it isn't offered/run against a non-web endpoint.
let t = target_with(
TargetType::PlcSps,
vec![Artifact::live_url("modbus://plc-sim:502")],
);
let opts = applicable_scans(&t);
let ics = option(&opts, ScanType::IcsProbe).expect("ics probe offered");
assert!(
ics.blocked_reason.is_none(),
"ICS probe should be unblocked for a modbus:// endpoint"
);
assert!(!ics.default_on, "ICS probe stays opt-in (default-off)");
let dast = option(&opts, ScanType::Dast).expect("dast listed");
assert!(
dast.blocked_reason.is_some(),
"DAST must be blocked without an http(s) endpoint"
);
assert!(!dast.default_on);
}
#[test]
fn plc_with_http_webvisu_offers_both_dast_and_ics_probe() {
// A PLC exposing a WebVisu over HTTP: both DAST (web) and the ICS probe
// (OT ports on the same host) are applicable.
let t = target_with(
TargetType::PlcSps,
vec![Artifact::live_url("http://plc.local/webvisu")],
);
let opts = applicable_scans(&t);
assert!(option(&opts, ScanType::Dast)
.expect("dast offered")
.blocked_reason
.is_none());
assert!(option(&opts, ScanType::IcsProbe)
.expect("ics probe offered")
.blocked_reason
.is_none());
}
#[test]
fn pentest_support_matches_reachable_families() {
assert!(supports_pentest(TargetType::WebApp));
assert!(supports_pentest(TargetType::BackendService));
assert!(!supports_pentest(TargetType::EmbeddedLinuxYocto));
// A PLC/SPS device is network-reachable (WebVisu / OPC UA / 11740).
assert!(supports_pentest(TargetType::PlcSps));
assert!(!supports_pentest(TargetType::FirmwareBareMetal));
assert!(!supports_pentest(TargetType::DesktopApp));
}
#[test]
fn a_yocto_layer_tree_runs_recipe_lint_and_lists_the_planned_inventory() {
let t = target_with(
TargetType::EmbeddedLinuxYocto,
vec![Artifact::git_repo("https://git/meta-pump", "main")],
);
let opts = applicable_scans(&t);
let inventory = option(&opts, ScanType::RecipeInventory).expect("listed");
assert!(
!inventory.default_on,
"the inventory must not be pre-selected yet"
);
// The layer tree is there, so the run records the stage as not done
// instead of passing over it.
assert_eq!(inventory.unavailable_with_artifact(), RECIPE_INVENTORY);
// Recipe lint runs over the layers.
let lint = option(&opts, ScanType::RecipeLint).expect("listed");
assert!(lint.default_on && lint.unavailable.is_none() && lint.blocked_reason.is_none());
// The source scans (secrets, malware, the source SBOM) still run.
let sast = option(&opts, ScanType::Sast).expect("sast");
assert!(sast.default_on && sast.unavailable.is_none());
// DAST stays opt-in and needs a web address.
let dast = option(&opts, ScanType::Dast).expect("dast listed");
assert!(!dast.default_on && dast.blocked_reason.is_some());
}
#[test]
fn a_yocto_image_has_no_sbom_until_build_artefacts_can_be_imported() {
let t = target_with(
TargetType::EmbeddedLinuxYocto,
vec![
Artifact::firmware_image("core-image.wic"),
Artifact::live_url("https://device.local"),
],
);
let opts = applicable_scans(&t);
let image_option = |scan: ScanType| {
opts.iter().find(|o| {
o.scan == scan && o.required_artifact == Some(ArtifactKind::FirmwareImage)
})
};
for (scan, reason) in [
(ScanType::Sbom, YOCTO_IMAGE_SBOM),
(ScanType::Cve, YOCTO_IMAGE_CVE),
] {
let opt = image_option(scan).expect("image rule listed");
assert!(!opt.default_on);
assert_eq!(opt.unavailable_with_artifact(), Some(reason));
assert!(reason.contains("build artefacts (SPDX/manifest), planned"));
}
// With a web address DAST is unblocked but still opt-in; no pentest
// until an image boots in an emulation sandbox.
let dast = option(&opts, ScanType::Dast).expect("dast listed");
assert!(dast.blocked_reason.is_none() && !dast.default_on);
assert!(!supports_pentest(t.target_type));
}
#[test]
fn every_target_type_has_at_least_one_rule() {
for tt in [
TargetType::WebApp,
TargetType::BackendService,
TargetType::DesktopApp,
TargetType::AndroidApp,
TargetType::IosApp,
TargetType::FirmwareBareMetal,
TargetType::FirmwareRtos,
TargetType::EmbeddedLinuxYocto,
TargetType::PlcSps,
] {
assert!(!rules_for(tt).is_empty(), "{tt} has no rules");
}
}
}