CI / Check (push) Skipped
CI / Agent integration (push) Skipped
CI / Detect Changes (push) Successful in 3s
CI / Deploy Docs (push) Successful in 1m8s
CI / Deploy MCP (push) Successful in 3m5s
CI / Deploy Dashboard (push) Successful in 5m45s
CI / Deploy Agent (push) Successful in 7m50s
416 lines
14 KiB
Rust
416 lines
14 KiB
Rust
use crate::common::TestServer;
|
|
use serde_json::json;
|
|
|
|
#[tokio::test]
|
|
async fn create_list_and_applicable_scans() {
|
|
let server = TestServer::start().await;
|
|
|
|
// Initially empty.
|
|
let resp = server.get("/api/v1/targets").await;
|
|
assert_eq!(resp.status(), 200);
|
|
let body: serde_json::Value = resp.json().await.unwrap();
|
|
assert_eq!(body["data"].as_array().unwrap().len(), 0);
|
|
|
|
// Create a web-app target with a git repo + a live URL.
|
|
let resp = server
|
|
.post(
|
|
"/api/v1/targets",
|
|
&json!({
|
|
"name": "acme-web",
|
|
"target_type": "web_app",
|
|
"artifacts": [
|
|
{ "kind": "git_repo", "source_ref": "https://git/acme.git", "branch": "main" },
|
|
{ "kind": "live_url", "source_ref": "https://acme.example.com" }
|
|
]
|
|
}),
|
|
)
|
|
.await;
|
|
assert_eq!(resp.status(), 200);
|
|
let body: serde_json::Value = resp.json().await.unwrap();
|
|
let id = body["data"]["_id"]["$oid"].as_str().unwrap().to_string();
|
|
assert!(!id.is_empty());
|
|
assert_eq!(body["data"]["artifacts"].as_array().unwrap().len(), 2);
|
|
|
|
// List returns it.
|
|
let resp = server.get("/api/v1/targets").await;
|
|
let body: serde_json::Value = resp.json().await.unwrap();
|
|
assert_eq!(body["data"].as_array().unwrap().len(), 1);
|
|
|
|
// Applicable scans: SAST present + DAST offered (live URL present), pentest supported.
|
|
let resp = server
|
|
.get(&format!("/api/v1/targets/{id}/applicable-scans"))
|
|
.await;
|
|
assert_eq!(resp.status(), 200);
|
|
let body: serde_json::Value = resp.json().await.unwrap();
|
|
let scans = body["data"]["scans"].as_array().unwrap();
|
|
let names: Vec<&str> = scans.iter().filter_map(|s| s["scan"].as_str()).collect();
|
|
assert!(names.contains(&"sast"));
|
|
assert!(names.contains(&"dast"));
|
|
assert_eq!(body["data"]["pentest_supported"], true);
|
|
|
|
server.cleanup().await;
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn detect_classifies_a_plc_target() {
|
|
let server = TestServer::start().await;
|
|
|
|
// A PLC project artifact is a strong kind-based signal.
|
|
let resp = server
|
|
.post(
|
|
"/api/v1/targets",
|
|
&json!({
|
|
"name": "line-controller",
|
|
"target_type": "backend_service", // deliberately wrong; detect should suggest PLC
|
|
"artifacts": [
|
|
{ "kind": "plc_project", "source_ref": "line.xml", "plc_format": "plcopen_xml" }
|
|
]
|
|
}),
|
|
)
|
|
.await;
|
|
let body: serde_json::Value = resp.json().await.unwrap();
|
|
let id = body["data"]["_id"]["$oid"].as_str().unwrap().to_string();
|
|
|
|
let resp = server
|
|
.post(&format!("/api/v1/targets/{id}/detect"), &json!({}))
|
|
.await;
|
|
assert_eq!(resp.status(), 200);
|
|
let body: serde_json::Value = resp.json().await.unwrap();
|
|
assert_eq!(body["data"]["classification"]["suggested"], "plc_sps");
|
|
|
|
server.cleanup().await;
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn add_artifact_and_delete_target() {
|
|
let server = TestServer::start().await;
|
|
|
|
let resp = server
|
|
.post(
|
|
"/api/v1/targets",
|
|
&json!({ "name": "svc", "target_type": "backend_service" }),
|
|
)
|
|
.await;
|
|
let body: serde_json::Value = resp.json().await.unwrap();
|
|
let id = body["data"]["_id"]["$oid"].as_str().unwrap().to_string();
|
|
|
|
// Attach a git repo.
|
|
let resp = server
|
|
.post(
|
|
&format!("/api/v1/targets/{id}/artifacts"),
|
|
&json!({ "kind": "git_repo", "source_ref": "https://git/svc.git" }),
|
|
)
|
|
.await;
|
|
assert_eq!(resp.status(), 200);
|
|
let body: serde_json::Value = resp.json().await.unwrap();
|
|
assert_eq!(body["data"]["artifacts"].as_array().unwrap().len(), 1);
|
|
|
|
// Delete it.
|
|
let resp = server.delete(&format!("/api/v1/targets/{id}")).await;
|
|
assert_eq!(resp.status(), 200);
|
|
let resp = server.get(&format!("/api/v1/targets/{id}")).await;
|
|
assert_eq!(resp.status(), 404);
|
|
|
|
server.cleanup().await;
|
|
}
|
|
|
|
/// Create an empty firmware target and return its id.
|
|
async fn firmware_target(server: &TestServer, name: &str) -> String {
|
|
let resp = server
|
|
.post(
|
|
"/api/v1/targets",
|
|
&json!({ "name": name, "target_type": "firmware_bare_metal", "artifacts": [] }),
|
|
)
|
|
.await;
|
|
assert_eq!(resp.status(), 200);
|
|
let body: serde_json::Value = resp.json().await.unwrap();
|
|
body["data"]["_id"]["$oid"].as_str().unwrap().to_string()
|
|
}
|
|
|
|
/// Files under the upload directory of `target_id`.
|
|
fn stored_uploads(target_id: &str) -> Vec<std::path::PathBuf> {
|
|
let dir = std::path::Path::new("/tmp/compliance-scanner-tests/artifacts")
|
|
.join("uploads")
|
|
.join(target_id);
|
|
std::fs::read_dir(dir)
|
|
.map(|entries| entries.filter_map(|e| e.ok().map(|e| e.path())).collect())
|
|
.unwrap_or_default()
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn an_upload_larger_than_the_json_limit_is_streamed_to_disk() {
|
|
let server = TestServer::start().await;
|
|
let id = firmware_target(&server, "fw-upload").await;
|
|
|
|
// 3 MiB: over axum's 2 MiB default, so it only works on the upload
|
|
// route's own limit, and it is written as it arrives.
|
|
let payload = vec![0xA5_u8; 3 * 1024 * 1024];
|
|
let form = reqwest::multipart::Form::new()
|
|
.text("kind", "firmware_image")
|
|
.part(
|
|
"file",
|
|
reqwest::multipart::Part::bytes(payload.clone()).file_name("fw.bin"),
|
|
);
|
|
let resp = server
|
|
.client
|
|
.post(format!(
|
|
"{}/api/v1/targets/{id}/artifacts/upload",
|
|
server.base_url
|
|
))
|
|
.multipart(form)
|
|
.send()
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(resp.status(), 200);
|
|
let body: serde_json::Value = resp.json().await.unwrap();
|
|
let artifacts = body["data"]["artifacts"].as_array().unwrap();
|
|
assert_eq!(artifacts.len(), 1);
|
|
assert_eq!(artifacts[0]["size_bytes"], payload.len() as u64);
|
|
|
|
let files = stored_uploads(&id);
|
|
assert_eq!(files.len(), 1);
|
|
assert_eq!(std::fs::read(&files[0]).unwrap(), payload);
|
|
|
|
server.cleanup().await;
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn an_ordinary_json_route_no_longer_accepts_a_huge_body() {
|
|
let server = TestServer::start().await;
|
|
// A 3 MiB description on a JSON route: it used to be buffered under a
|
|
// 512 MiB limit that applied to every route.
|
|
let resp = server
|
|
.post(
|
|
"/api/v1/targets",
|
|
&json!({
|
|
"name": "too-big",
|
|
"target_type": "web_app",
|
|
"description": "x".repeat(3 * 1024 * 1024),
|
|
"artifacts": []
|
|
}),
|
|
)
|
|
.await;
|
|
assert_eq!(resp.status(), 413);
|
|
server.cleanup().await;
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn a_rejected_upload_leaves_no_file_behind() {
|
|
let server = TestServer::start().await;
|
|
let id = firmware_target(&server, "fw-reject").await;
|
|
|
|
// No `kind` field: refused only after the file was already streamed.
|
|
let form = reqwest::multipart::Form::new().part(
|
|
"file",
|
|
reqwest::multipart::Part::bytes(vec![1_u8; 1024]).file_name("fw.bin"),
|
|
);
|
|
let resp = server
|
|
.client
|
|
.post(format!(
|
|
"{}/api/v1/targets/{id}/artifacts/upload",
|
|
server.base_url
|
|
))
|
|
.multipart(form)
|
|
.send()
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(resp.status(), 400);
|
|
assert!(
|
|
stored_uploads(&id).is_empty(),
|
|
"the partial upload must be deleted"
|
|
);
|
|
|
|
server.cleanup().await;
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn the_list_pages_with_a_total_after_the_project_filter() {
|
|
let server = TestServer::start().await;
|
|
let mut ids = Vec::new();
|
|
for name in ["page-a", "page-b", "page-c"] {
|
|
ids.push(firmware_target(&server, name).await);
|
|
}
|
|
// Projects are registry-owned, so put two targets in one directly.
|
|
let mongodb_uri = std::env::var("TEST_MONGODB_URI")
|
|
.unwrap_or_else(|_| "mongodb://root:example@localhost:27017/?authSource=admin".into());
|
|
let client = mongodb::Client::with_uri_str(&mongodb_uri).await.unwrap();
|
|
let targets = client
|
|
.database(&server.tenant_db_name())
|
|
.collection::<mongodb::bson::Document>("onboarded_targets");
|
|
for id in &ids[1..] {
|
|
let oid = mongodb::bson::oid::ObjectId::parse_str(id).unwrap();
|
|
let updated = targets
|
|
.update_one(
|
|
mongodb::bson::doc! { "_id": oid },
|
|
mongodb::bson::doc! { "$set": { "project_ids": ["p1"] } },
|
|
)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(updated.matched_count, 1);
|
|
}
|
|
|
|
let names = |body: &serde_json::Value| -> Vec<String> {
|
|
body["data"]
|
|
.as_array()
|
|
.unwrap()
|
|
.iter()
|
|
.map(|t| t["name"].as_str().unwrap().to_string())
|
|
.collect()
|
|
};
|
|
let page = |query: &'static str| {
|
|
let server = &server;
|
|
async move {
|
|
let resp = server.get(&format!("/api/v1/targets?{query}")).await;
|
|
assert_eq!(resp.status(), 200);
|
|
resp.json::<serde_json::Value>().await.unwrap()
|
|
}
|
|
};
|
|
|
|
// Creation order, two to a page, the total over every page.
|
|
let first = page("limit=2&page=1").await;
|
|
assert_eq!(names(&first), ["page-a", "page-b"]);
|
|
assert_eq!(first["total"], 3);
|
|
assert_eq!(first["page"], 1);
|
|
let second = page("limit=2&page=2").await;
|
|
assert_eq!(names(&second), ["page-c"]);
|
|
assert_eq!(second["total"], 3);
|
|
|
|
// The filter applies before paging: the total counts the project only.
|
|
let in_project = page("project=p1&limit=1&page=2").await;
|
|
assert_eq!(names(&in_project), ["page-c"]);
|
|
assert_eq!(in_project["total"], 2);
|
|
|
|
// A page past the end is empty but still knows the total.
|
|
let past = page("limit=2&page=9").await;
|
|
assert!(names(&past).is_empty());
|
|
assert_eq!(past["total"], 3);
|
|
|
|
server.cleanup().await;
|
|
}
|
|
|
|
/// Upload `bytes` as a supplier SBOM (#1025) named `name` to target `id`.
|
|
async fn upload_supplier_sbom(
|
|
server: &TestServer,
|
|
id: &str,
|
|
name: &str,
|
|
bytes: &[u8],
|
|
) -> reqwest::Response {
|
|
let form = reqwest::multipart::Form::new()
|
|
.text("kind", "supplier_sbom")
|
|
.part(
|
|
"file",
|
|
reqwest::multipart::Part::bytes(bytes.to_vec()).file_name(name.to_string()),
|
|
);
|
|
server
|
|
.client
|
|
.post(format!(
|
|
"{}/api/v1/targets/{id}/artifacts/upload",
|
|
server.base_url
|
|
))
|
|
.multipart(form)
|
|
.send()
|
|
.await
|
|
.unwrap()
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn a_supplier_sbom_is_kept_and_brings_its_scans() {
|
|
let server = TestServer::start().await;
|
|
let id = firmware_target(&server, "bought-in-pump").await;
|
|
let sbom = br#"{"bomFormat": "CycloneDX", "specVersion": "1.5", "components": [
|
|
{"type": "library", "name": "lodash", "version": "4.17.20", "purl": "pkg:npm/lodash@4.17.20"}
|
|
]}"#;
|
|
|
|
let resp = upload_supplier_sbom(&server, &id, "pump.cdx.json", sbom).await;
|
|
|
|
assert_eq!(resp.status(), 200);
|
|
let body: serde_json::Value = resp.json().await.unwrap();
|
|
let artifacts = body["data"]["artifacts"].as_array().unwrap();
|
|
assert_eq!(artifacts.len(), 1);
|
|
assert_eq!(artifacts[0]["kind"], "supplier_sbom");
|
|
assert_eq!(stored_uploads(&id).len(), 1);
|
|
// Its SBOM and CVE scans run, though the target has no code.
|
|
let resp = server
|
|
.get(&format!("/api/v1/targets/{id}/applicable-scans"))
|
|
.await;
|
|
let body: serde_json::Value = resp.json().await.unwrap();
|
|
let runs =
|
|
|scan: &str| {
|
|
body["data"]["scans"].as_array().unwrap().iter().any(|s| {
|
|
s["scan"] == scan && s["default_on"] == true && s["blocked_reason"].is_null()
|
|
})
|
|
};
|
|
assert!(runs("sbom") && runs("cve"), "{body}");
|
|
|
|
server.cleanup().await;
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn a_supplier_sbom_that_cannot_be_read_is_refused_with_the_reason() {
|
|
let server = TestServer::start().await;
|
|
let id = firmware_target(&server, "bought-in-valve").await;
|
|
|
|
for (name, bytes, reason) in [
|
|
(
|
|
"valve.csv",
|
|
&b"name,version\nzlib,1.2.11"[..],
|
|
"not a CycloneDX",
|
|
),
|
|
(
|
|
"valve.cdx.json",
|
|
&br#"{"bomFormat": "CycloneDX", "specVersion": "1.5"}"#[..],
|
|
"lists no components",
|
|
),
|
|
] {
|
|
let resp = upload_supplier_sbom(&server, &id, name, bytes).await;
|
|
assert_eq!(resp.status(), 422, "{name}");
|
|
let body: serde_json::Value = resp.json().await.unwrap();
|
|
assert!(
|
|
body["error"].as_str().is_some_and(|e| e.contains(reason)),
|
|
"{name}: {body}"
|
|
);
|
|
}
|
|
assert!(
|
|
stored_uploads(&id).is_empty(),
|
|
"a refused SBOM must be deleted"
|
|
);
|
|
let resp = server.get(&format!("/api/v1/targets/{id}")).await;
|
|
let body: serde_json::Value = resp.json().await.unwrap();
|
|
assert!(body["data"]["artifacts"].as_array().unwrap().is_empty());
|
|
|
|
server.cleanup().await;
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn a_new_supplier_sbom_replaces_the_one_before_it() {
|
|
let server = TestServer::start().await;
|
|
let id = firmware_target(&server, "bought-in-drive").await;
|
|
let version = |v: &str| {
|
|
format!(
|
|
r#"{{"bomFormat": "CycloneDX", "specVersion": "1.5", "components": [
|
|
{{"type": "library", "name": "zlib", "version": "{v}", "purl": "pkg:generic/zlib@{v}"}}
|
|
]}}"#
|
|
)
|
|
};
|
|
for (name, v) in [
|
|
("drive-1.cdx.json", "1.2.11"),
|
|
("drive-2.cdx.json", "1.3.1"),
|
|
] {
|
|
let resp = upload_supplier_sbom(&server, &id, name, version(v).as_bytes()).await;
|
|
assert_eq!(resp.status(), 200, "{name}");
|
|
}
|
|
|
|
let resp = server.get(&format!("/api/v1/targets/{id}")).await;
|
|
let body: serde_json::Value = resp.json().await.unwrap();
|
|
let artifacts = body["data"]["artifacts"].as_array().unwrap();
|
|
assert_eq!(artifacts.len(), 1, "{body}");
|
|
assert_eq!(artifacts[0]["source_ref"], "drive-2.cdx.json");
|
|
let files = stored_uploads(&id);
|
|
assert_eq!(files.len(), 1, "the replaced file must be gone: {files:?}");
|
|
assert!(std::fs::read_to_string(&files[0])
|
|
.unwrap()
|
|
.contains("1.3.1"));
|
|
|
|
server.cleanup().await;
|
|
}
|