290 lines
9.8 KiB
Rust
290 lines
9.8 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;
|
|
}
|