326 lines
14 KiB
Rust
326 lines
14 KiB
Rust
//! Additive bounded passive metadata; the strict original Inventory stays intact.
|
|
use chrono::{DateTime, Utc};
|
|
use serde::Deserialize;
|
|
use serde_json::Value;
|
|
#[derive(Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
struct Observation {
|
|
scope: String,
|
|
source_id: String,
|
|
observed_at: DateTime<Utc>,
|
|
quality: String,
|
|
completeness: String,
|
|
truncated: bool,
|
|
error_code: Option<String>,
|
|
data: Value,
|
|
}
|
|
pub fn validate(bytes: &[u8], now: DateTime<Utc>) -> Result<Value, &'static str> {
|
|
validate_for_scope(bytes, now, "linux.base")
|
|
}
|
|
/// Snapshot scope is supplied by the authenticated ingestion path, never inferred
|
|
/// from an arbitrary observation's label. Windows does not manufacture inodes.
|
|
pub fn validate_for_scope(
|
|
bytes: &[u8],
|
|
now: DateTime<Utc>,
|
|
scope: &str,
|
|
) -> Result<Value, &'static str> {
|
|
if scope == "agent.update" {
|
|
if bytes.len() > 65536 {
|
|
return Err("AGENT_UPDATE_INVENTORY_SIZE");
|
|
}
|
|
#[derive(Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
struct UpdateInventory {
|
|
agent_update: crate::agent_update::AgentUpdateObservation,
|
|
}
|
|
let update: UpdateInventory =
|
|
serde_json::from_slice(bytes).map_err(|_| "INVALID_AGENT_UPDATE_OBSERVATION")?;
|
|
if !update.agent_update.validate()
|
|
|| update.agent_update.observed_at - now > chrono::Duration::seconds(45)
|
|
|| now - update.agent_update.observed_at > chrono::Duration::hours(72)
|
|
{
|
|
return Err("INVALID_AGENT_UPDATE_OBSERVATION");
|
|
}
|
|
return serde_json::from_slice(bytes).map_err(|_| "INVALID_AGENT_UPDATE_OBSERVATION");
|
|
}
|
|
let prefix = match scope {
|
|
"linux.base" => "linux.",
|
|
"windows.base" => "windows.",
|
|
_ => return Err("UNSUPPORTED_INVENTORY_SCOPE"),
|
|
};
|
|
if bytes.len() > 512 * 1024 {
|
|
return Err("INVENTORY_SIZE");
|
|
}
|
|
let value: Value = serde_json::from_slice(bytes).map_err(|_| "INVALID_INVENTORY_JSON")?;
|
|
let mut base = value.as_object().cloned().ok_or("INVENTORY_OBJECT")?;
|
|
if let Some(update) = base.remove("agent_update") {
|
|
// Decode the original bytes as well: Value alone would discard duplicate
|
|
// fields before the strict new metadata DTO could reject them.
|
|
#[derive(Deserialize)]
|
|
struct UpdateField {
|
|
agent_update: crate::agent_update::AgentUpdateObservation,
|
|
}
|
|
let typed: UpdateField =
|
|
serde_json::from_slice(bytes).map_err(|_| "INVALID_AGENT_UPDATE_OBSERVATION")?;
|
|
if serde_json::to_value(&typed.agent_update)
|
|
.map_err(|_| "INVALID_AGENT_UPDATE_OBSERVATION")?
|
|
!= update
|
|
|| !typed.agent_update.validate()
|
|
|| typed.agent_update.platform
|
|
!= if scope == "linux.base" {
|
|
crate::agent_update::Platform::LinuxX64
|
|
} else {
|
|
crate::agent_update::Platform::WindowsX64
|
|
}
|
|
|| typed.agent_update.observed_at - now > chrono::Duration::seconds(45)
|
|
|| now - typed.agent_update.observed_at > chrono::Duration::hours(72)
|
|
{
|
|
return Err("INVALID_AGENT_UPDATE_OBSERVATION");
|
|
}
|
|
}
|
|
let observations = base.remove("observations");
|
|
let started = base.remove("collection_started_at");
|
|
let completed = base.remove("collection_completed_at");
|
|
if scope == "windows.base" {
|
|
#[derive(Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
struct WindowsInventory {
|
|
os: String,
|
|
hostname: String,
|
|
architecture: String,
|
|
root_total_bytes: u64,
|
|
root_available_bytes: u64,
|
|
root_available_inodes: Option<u64>,
|
|
}
|
|
let inventory: WindowsInventory =
|
|
serde_json::from_value(Value::Object(base)).map_err(|_| "INVALID_BASE_INVENTORY")?;
|
|
if !inventory.os.starts_with("Windows")
|
|
|| inventory.os.len() > 4096
|
|
|| inventory.hostname.is_empty()
|
|
|| inventory.hostname.len() > 255
|
|
|| inventory.architecture != "x86_64"
|
|
|| inventory.root_total_bytes == 0
|
|
|| inventory.root_available_bytes > inventory.root_total_bytes
|
|
|| inventory.root_available_inodes.is_some()
|
|
{
|
|
return Err("INVALID_WINDOWS_BASE_INVENTORY");
|
|
}
|
|
} else {
|
|
let inventory: crate::Inventory =
|
|
serde_json::from_value(Value::Object(base)).map_err(|_| "INVALID_BASE_INVENTORY")?;
|
|
if !inventory.validate() {
|
|
return Err("INVALID_BASE_INVENTORY");
|
|
}
|
|
}
|
|
if let Some(observations) = observations {
|
|
let started: DateTime<Utc> =
|
|
serde_json::from_value(started.ok_or("COLLECTION_WINDOW_REQUIRED")?)
|
|
.map_err(|_| "INVALID_COLLECTION_TIME")?;
|
|
let completed: DateTime<Utc> =
|
|
serde_json::from_value(completed.ok_or("COLLECTION_WINDOW_REQUIRED")?)
|
|
.map_err(|_| "INVALID_COLLECTION_TIME")?;
|
|
if started > completed
|
|
|| (completed - started).num_seconds() > 3600
|
|
|| (now - started).num_seconds() > 72 * 3600
|
|
|| (completed - now).num_seconds() > 45
|
|
{
|
|
return Err("INVALID_COLLECTION_WINDOW");
|
|
}
|
|
let observations = observations.as_array().ok_or("OBSERVATION_ARRAY")?;
|
|
if observations.len() > 32 {
|
|
return Err("OBSERVATION_COUNT");
|
|
}
|
|
let mut scopes = std::collections::BTreeSet::new();
|
|
for item in observations {
|
|
if serde_json::to_vec(item)
|
|
.map_err(|_| "INVALID_OBSERVATION")?
|
|
.len()
|
|
> 32768
|
|
{
|
|
return Err("OBSERVATION_SIZE");
|
|
}
|
|
let o: Observation =
|
|
serde_json::from_value(item.clone()).map_err(|_| "INVALID_OBSERVATION")?;
|
|
// Old durable observations without this additive field remain
|
|
// acknowledgeable. A present host-level flag must stay a boolean.
|
|
let invalid_host_reboot = matches!(
|
|
o.scope.as_str(),
|
|
"linux.apt.planning" | "linux.apt.recovery-planning"
|
|
) && o
|
|
.data
|
|
.get("host_reboot_required")
|
|
.is_some_and(|v| !v.is_boolean());
|
|
if !o.scope.starts_with(prefix)
|
|
|| o.scope.len() > 64
|
|
|| !(o
|
|
.scope
|
|
.bytes()
|
|
.all(|c| c.is_ascii_lowercase() || b"._".contains(&c))
|
|
|| (scope == "linux.base" && o.scope == "linux.apt.recovery-planning"))
|
|
|| !scopes.insert(o.scope)
|
|
{
|
|
return Err("INVALID_OBSERVATION_SCOPE");
|
|
}
|
|
if o.source_id.is_empty()
|
|
|| o.source_id.len() > 255
|
|
|| o.source_id.contains('\0')
|
|
|| o.observed_at < started - chrono::Duration::seconds(1)
|
|
|| o.observed_at > completed + chrono::Duration::seconds(1)
|
|
|| !matches!(
|
|
o.quality.as_str(),
|
|
"valid" | "unknown" | "unsupported" | "permission_denied" | "error"
|
|
)
|
|
|| !matches!(o.completeness.as_str(), "complete" | "partial" | "unknown")
|
|
|| (o.truncated && o.completeness == "complete")
|
|
|| o.error_code.as_ref().is_some_and(|s| s.len() > 128)
|
|
|| (o.quality == "valid" && o.data.is_null())
|
|
|| invalid_host_reboot
|
|
{
|
|
return Err("INVALID_OBSERVATION_METADATA");
|
|
}
|
|
}
|
|
} else if started.is_some() || completed.is_some() {
|
|
return Err("OBSERVATIONS_REQUIRED");
|
|
}
|
|
Ok(value)
|
|
}
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use serde_json::json;
|
|
fn base() -> Value {
|
|
json!({"os":"Linux","hostname":"fixture","architecture":"x86_64","root_total_bytes":2,"root_available_bytes":1,"root_available_inodes":1})
|
|
}
|
|
#[test]
|
|
fn additive_metadata_and_bounds() {
|
|
let now = Utc::now();
|
|
let mut v = base();
|
|
assert!(validate(&serde_json::to_vec(&v).unwrap(), now).is_ok());
|
|
v["observations"] = json!([{"scope":"linux.smart","source_id":"linux.smart","observed_at":now,"quality":"unsupported","completeness":"unknown","truncated":false,"error_code":"NOT_SUPPORTED","data":null}]);
|
|
v["collection_started_at"] = json!(now);
|
|
v["collection_completed_at"] = json!(now);
|
|
assert_eq!(validate(&serde_json::to_vec(&v).unwrap(), now).unwrap(), v);
|
|
v["observations"][0]["data"] = json!("x".repeat(32768));
|
|
assert_eq!(
|
|
validate(&serde_json::to_vec(&v).unwrap(), now).unwrap_err(),
|
|
"OBSERVATION_SIZE"
|
|
);
|
|
let mut unknown = base();
|
|
unknown["tenant_id"] = json!("forged");
|
|
assert!(validate(&serde_json::to_vec(&unknown).unwrap(), now).is_err());
|
|
}
|
|
#[test]
|
|
fn windows_scope_does_not_weaken_linux_or_accept_platform_spoofing() {
|
|
let now = Utc::now();
|
|
let mut v = base();
|
|
v["os"] = json!("Windows 11 Pro");
|
|
v["root_available_inodes"] = Value::Null;
|
|
let bytes = serde_json::to_vec(&v).unwrap();
|
|
assert!(validate(&bytes, now).is_err());
|
|
assert!(validate_for_scope(&bytes, now, "windows.base").is_ok());
|
|
assert!(validate_for_scope(&bytes, now, "arbitrary.base").is_err());
|
|
v["root_available_inodes"] = json!(0);
|
|
assert!(validate_for_scope(&serde_json::to_vec(&v).unwrap(), now, "windows.base").is_err());
|
|
v["root_available_inodes"] = Value::Null;
|
|
v["collection_started_at"] = json!(now);
|
|
v["collection_completed_at"] = json!(now);
|
|
v["observations"] = json!([{"scope":"linux.os","source_id":"windows.native","observed_at":now,"quality":"valid","completeness":"partial","truncated":false,"error_code":null,"data":{}}]);
|
|
assert_eq!(
|
|
validate_for_scope(&serde_json::to_vec(&v).unwrap(), now, "windows.base").unwrap_err(),
|
|
"INVALID_OBSERVATION_SCOPE"
|
|
);
|
|
v["observations"][0]["scope"] = json!("windows.os");
|
|
assert!(validate_for_scope(&serde_json::to_vec(&v).unwrap(), now, "windows.base").is_ok());
|
|
}
|
|
#[test]
|
|
fn exact_linux_recovery_scope_does_not_admit_other_hyphens_or_windows() {
|
|
let now = Utc::now();
|
|
let mut v = base();
|
|
v["collection_started_at"] = json!(now);
|
|
v["collection_completed_at"] = json!(now);
|
|
v["observations"] = json!([{"scope":"linux.apt.recovery-planning","source_id":"root-helper:apt_recovery_planning_v1","observed_at":now,"quality":"valid","completeness":"complete","truncated":false,"error_code":null,"data":{"native_recovery":{"schema_version":1}}}]);
|
|
let original = serde_json::to_vec(&v).unwrap();
|
|
assert_eq!(validate(&original, now).unwrap(), v);
|
|
v["observations"][0]["data"]["host_reboot_required"] = json!(true);
|
|
assert!(validate(&serde_json::to_vec(&v).unwrap(), now).is_ok());
|
|
v["observations"][0]["data"]["host_reboot_required"] = json!("true");
|
|
assert_eq!(
|
|
validate(&serde_json::to_vec(&v).unwrap(), now).unwrap_err(),
|
|
"INVALID_OBSERVATION_METADATA"
|
|
);
|
|
v = serde_json::from_slice(&original).unwrap();
|
|
for rejected in [
|
|
"linux.apt.other-planning",
|
|
"linux.apt.recovery-planning-extra",
|
|
"linux.other-scope",
|
|
] {
|
|
v["observations"][0]["scope"] = json!(rejected);
|
|
assert_eq!(
|
|
validate(&serde_json::to_vec(&v).unwrap(), now).unwrap_err(),
|
|
"INVALID_OBSERVATION_SCOPE"
|
|
);
|
|
}
|
|
v["os"] = json!("Windows 11 Pro");
|
|
v["root_available_inodes"] = Value::Null;
|
|
for rejected in [
|
|
"linux.apt.recovery-planning",
|
|
"windows.apt.recovery-planning",
|
|
] {
|
|
v["observations"][0]["scope"] = json!(rejected);
|
|
assert_eq!(
|
|
validate_for_scope(&serde_json::to_vec(&v).unwrap(), now, "windows.base")
|
|
.unwrap_err(),
|
|
"INVALID_OBSERVATION_SCOPE"
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod agent_update_tests {
|
|
use super::*;
|
|
use crate::agent_update::{AgentUpdateObservation, Platform};
|
|
#[test]
|
|
fn update_scope_is_separate_bounded_strict_and_never_restamps_base_inventory() {
|
|
let now = Utc::now();
|
|
let observation = AgentUpdateObservation::not_configured(Platform::LinuxX64, now);
|
|
let value = serde_json::json!({"agent_update":observation});
|
|
let bytes = serde_json::to_vec(&value).unwrap();
|
|
assert_eq!(
|
|
validate_for_scope(&bytes, now, "agent.update").unwrap(),
|
|
value
|
|
);
|
|
assert!(validate_for_scope(&bytes, now, "linux.base").is_err());
|
|
let object = serde_json::to_string(&observation).unwrap();
|
|
let duplicate = format!("{{\"agent_update\":{object},\"agent_update\":{object}}}");
|
|
assert!(validate_for_scope(duplicate.as_bytes(), now, "agent.update").is_err());
|
|
let mut false_valid = value.clone();
|
|
false_valid["agent_update"]["quality"] = serde_json::json!("valid");
|
|
assert!(
|
|
validate_for_scope(
|
|
&serde_json::to_vec(&false_valid).unwrap(),
|
|
now,
|
|
"agent.update"
|
|
)
|
|
.is_err()
|
|
);
|
|
let mut mixed = value;
|
|
mixed["hostname"] = serde_json::json!("pretend-new-base");
|
|
assert!(
|
|
validate_for_scope(&serde_json::to_vec(&mixed).unwrap(), now, "agent.update").is_err()
|
|
);
|
|
let future = AgentUpdateObservation::not_configured(
|
|
Platform::LinuxX64,
|
|
now + chrono::Duration::seconds(46),
|
|
);
|
|
assert!(
|
|
validate_for_scope(
|
|
&serde_json::to_vec(&serde_json::json!({"agent_update":future})).unwrap(),
|
|
now,
|
|
"agent.update"
|
|
)
|
|
.is_err()
|
|
);
|
|
}
|
|
}
|