fix(domain): shape check sees positional parents, multicast never an own address
This commit is contained in:
1 parent
257a29b09b
commit
1ec9e1bea6
6 files changed
+273
-8
No files matched your search
@@ -115,6 +115,10 @@ fn own_address(host: &str) -> bool {
|
||||
let Ok(ip) = host.parse::<std::net::IpAddr>() else {
|
||||
return false;
|
||||
};
|
||||
// A multicast group binds and connects like an own address on Linux: never unicast.
|
||||
if ip.is_multicast() {
|
||||
return false;
|
||||
}
|
||||
let Ok(socket) = std::net::UdpSocket::bind((ip, 0)) else {
|
||||
return false;
|
||||
};
|
||||
@@ -254,7 +258,14 @@ mod tests {
|
||||
#[test]
|
||||
fn only_an_own_unicast_address_counts_as_the_dev_address() {
|
||||
assert!(own_address("127.0.0.1"));
|
||||
for refused in ["255.255.255.255", "203.0.113.7", "not-an-ip", ""] {
|
||||
for refused in [
|
||||
"255.255.255.255",
|
||||
"224.0.0.1",
|
||||
"239.255.255.250",
|
||||
"203.0.113.7",
|
||||
"not-an-ip",
|
||||
"",
|
||||
] {
|
||||
assert!(!own_address(refused), "{refused}");
|
||||
}
|
||||
let (unicast, broadcast) = host_addresses();
|
||||
|
||||
@@ -121,6 +121,10 @@ fn snapshot_matches(snapshot: &SheetSnapshot, report: &Report) -> bool {
|
||||
/// `ReportArchiveEnvelopeSchema.parse`: the shape of a whole envelope (the snapshot's shape
|
||||
/// only, no caps nor integrity), as the session keeps it.
|
||||
pub fn parse_envelope(data: &Value) -> Result<ReportArchiveEnvelope, serde_json::Error> {
|
||||
// A positional envelope would carry its report past the check unseen.
|
||||
if !data.is_object() {
|
||||
return Err(serde::de::Error::custom("envelope: expected an object"));
|
||||
}
|
||||
let report = data.get("report").unwrap_or(&Value::Null);
|
||||
check_v3_shape(report).map_err(serde::de::Error::custom)?;
|
||||
ReportArchiveEnvelope::deserialize(data)
|
||||
|
||||
@@ -51,8 +51,13 @@ pub fn version_of(data: &Value) -> Option<f64> {
|
||||
|
||||
/// The shapes Zod refused and serde's derives accept (owner go 2026-10-24, RUST.md §11.18):
|
||||
/// `sheet` and `practitioner` written as positional arrays, a `severity` written as a one-key
|
||||
/// map. Checked on the raw value before the typed read; nothing else is widened or narrowed.
|
||||
/// map. Checked on the raw value before the typed read. The report and each observation must
|
||||
/// be objects themselves, or a positional parent would carry those fields past the check
|
||||
/// unseen; nothing else is widened or narrowed.
|
||||
pub fn check_v3_shape(data: &Value) -> Result<(), String> {
|
||||
if !data.is_object() {
|
||||
return Err("report: expected an object".to_owned());
|
||||
}
|
||||
for key in ["sheet", "practitioner"] {
|
||||
if data.get(key).is_some_and(|value| !value.is_object()) {
|
||||
return Err(format!("{key}: expected an object"));
|
||||
@@ -60,11 +65,14 @@ pub fn check_v3_shape(data: &Value) -> Result<(), String> {
|
||||
}
|
||||
let observations = data.get("observations").and_then(Value::as_array);
|
||||
for (index, observation) in observations.into_iter().flatten().enumerate() {
|
||||
let severity = observation.get("severity");
|
||||
if severity.is_some_and(|value| !value.is_string()) {
|
||||
return Err(format!(
|
||||
"observations[{index}].severity: expected a string or null"
|
||||
));
|
||||
if !observation.is_object() {
|
||||
return Err(format!("observations[{index}]: expected an object"));
|
||||
}
|
||||
if observation
|
||||
.get("severity")
|
||||
.is_some_and(|value| !value.is_string())
|
||||
{
|
||||
return Err(format!("observations[{index}].severity: expected a string"));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
|
||||
Reference in new issue
Block a user