127 lines
3.8 KiB
Rust
127 lines
3.8 KiB
Rust
//! Password-protected archives (#370): the upload marks them, the password is
|
|
//! checked against the file before it is stored, and 7z/rar are refused.
|
|
//! Drives the real API against a live Mongo (`TEST_MONGODB_URI`).
|
|
|
|
#![allow(clippy::expect_used, clippy::unwrap_used)]
|
|
|
|
mod common;
|
|
|
|
use common::TestServer;
|
|
|
|
/// Made with `zip -P pilot-2026`.
|
|
const ZIPCRYPTO: &[u8] = include_bytes!("fixtures/archives/zipcrypto.zip");
|
|
const SEVEN_ZIP: &[u8] = include_bytes!("fixtures/archives/sample.7z");
|
|
|
|
async fn create_target(server: &TestServer) -> String {
|
|
let created: serde_json::Value = server
|
|
.post(
|
|
"/api/v1/targets",
|
|
&serde_json::json!({ "name": "sensor", "target_type": "firmware_rtos" }),
|
|
)
|
|
.await
|
|
.json()
|
|
.await
|
|
.unwrap();
|
|
created["data"]["_id"]["$oid"].as_str().unwrap().to_string()
|
|
}
|
|
|
|
async fn upload(server: &TestServer, id: &str, name: &str, bytes: &[u8]) -> reqwest::Response {
|
|
let form = reqwest::multipart::Form::new()
|
|
.text("kind", "source_archive")
|
|
.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()
|
|
}
|
|
|
|
async fn set_password(server: &TestServer, id: &str, artifact: &str, password: &str) -> u16 {
|
|
server
|
|
.client
|
|
.put(format!(
|
|
"{}/api/v1/targets/{id}/artifacts/{artifact}/archive-password",
|
|
server.base_url
|
|
))
|
|
.json(&serde_json::json!({ "password": password }))
|
|
.send()
|
|
.await
|
|
.unwrap()
|
|
.status()
|
|
.as_u16()
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn an_encrypted_upload_is_marked_and_takes_only_its_password() {
|
|
let server = TestServer::start().await;
|
|
let id = create_target(&server).await;
|
|
|
|
// Arrange: the upload is kept, and marked as needing a password.
|
|
let target: serde_json::Value = upload(&server, &id, "firmware.zip", ZIPCRYPTO)
|
|
.await
|
|
.json()
|
|
.await
|
|
.unwrap();
|
|
let artifact = &target["data"]["artifacts"][0];
|
|
let artifact_id = artifact["id"].as_str().unwrap().to_string();
|
|
assert!(
|
|
artifact["detected"]
|
|
.as_array()
|
|
.unwrap()
|
|
.iter()
|
|
.any(|f| f["key"] == "archive_encrypted"),
|
|
"{artifact}"
|
|
);
|
|
|
|
// Act
|
|
let wrong = set_password(&server, &id, &artifact_id, "guess").await;
|
|
let right = set_password(&server, &id, &artifact_id, "pilot-2026").await;
|
|
|
|
// Assert: only the right one is stored, and it is never returned.
|
|
assert_eq!(wrong, 422);
|
|
assert_eq!(right, 204);
|
|
let stored: serde_json::Value = server
|
|
.get(&format!("/api/v1/targets/{id}"))
|
|
.await
|
|
.json()
|
|
.await
|
|
.unwrap();
|
|
let auth = &stored["data"]["artifacts"][0]["auth"];
|
|
assert_eq!(auth["method"], "archive_password");
|
|
assert!(auth["secret"].is_null(), "{auth}");
|
|
assert!(!stored.to_string().contains("pilot-2026"));
|
|
|
|
server.cleanup().await;
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn a_plain_upload_needs_no_password_and_7z_is_refused() {
|
|
let server = TestServer::start().await;
|
|
let id = create_target(&server).await;
|
|
|
|
let plain: serde_json::Value =
|
|
upload(&server, &id, "main.c", b"int main(void) { return 0; }\n")
|
|
.await
|
|
.json()
|
|
.await
|
|
.unwrap();
|
|
let artifact_id = plain["data"]["artifacts"][0]["id"]
|
|
.as_str()
|
|
.unwrap()
|
|
.to_string();
|
|
let seven = upload(&server, &id, "firmware.7z", SEVEN_ZIP).await;
|
|
|
|
assert_eq!(set_password(&server, &id, &artifact_id, "x").await, 409);
|
|
assert_eq!(seven.status().as_u16(), 415);
|
|
|
|
server.cleanup().await;
|
|
}
|