data_notices.rs (7309B)
1 //! Exact source-owned redistribution material for every derived artifact. 2 use std::{fs, path::Path}; 3 4 use serde::Deserialize; 5 use sha2::{Digest, Sha256}; 6 7 use crate::{build_control::Mode, sdk_generation::fs::write_or_check}; 8 9 pub(crate) const CONTRACT: &str = "contracts/codegen/redistribution_materials.v1.json"; 10 pub(crate) const OUTPUT: &str = "crates/domain/NOTICE"; 11 const MATERIALS: [&str; 6] = [ 12 "crates/domain/data/currency_registry_v1.csv", 13 "crates/domain/data/currency_registry_v1.notice", 14 "crates/domain/data/currency_registry_v1.license", 15 "crates/domain/data/timezone_registry_v1.notice", 16 "crates/domain/data/timezone_registry_v1.license", 17 "crates/domain/data/timezone_registry_v1.copying", 18 ]; 19 20 #[derive(Deserialize)] 21 #[serde(deny_unknown_fields)] 22 struct Contract { 23 schema: String, 24 output: String, 25 max_input_bytes: usize, 26 max_output_bytes: usize, 27 source_distribution: String, 28 materials: Vec<Material>, 29 } 30 #[derive(Deserialize)] 31 #[serde(deny_unknown_fields)] 32 struct Material { 33 path: String, 34 sha256: String, 35 } 36 37 fn read(root: &Path, name: &str) -> Result<String, String> { 38 let path = root.join(name); 39 let metadata = 40 fs::symlink_metadata(&path).map_err(|_| "redistribution material is missing".to_owned())?; 41 if !metadata.is_file() || metadata.len() > 16_384 { 42 return Err("redistribution material must be a bounded regular file".to_owned()); 43 } 44 let value = 45 fs::read_to_string(path).map_err(|_| "redistribution material must be UTF8".to_owned())?; 46 if value.is_empty() || value.len() > 16_384 { 47 return Err("redistribution material size is invalid".to_owned()); 48 } 49 Ok(value) 50 } 51 52 /// Render original material verbatim; no timestamp, source path or host state. 53 pub(crate) fn render(root: &Path) -> Result<String, String> { 54 let contract: Contract = serde_json::from_str(&read(root, CONTRACT)?) 55 .map_err(|_| "invalid redistribution materials contract".to_owned())?; 56 if contract.schema != "radroots.redistribution_materials.v1" 57 || contract.output != OUTPUT 58 || contract.max_input_bytes != 16_384 59 || contract.max_output_bytes != 65_536 60 || contract.source_distribution.trim().is_empty() 61 || !contract 62 .materials 63 .iter() 64 .map(|v| v.path.as_str()) 65 .eq(MATERIALS) 66 { 67 return Err("redistribution material authority drifted".to_owned()); 68 } 69 let mut output = String::from( 70 "Radroots data redistribution material v1\n\nGenerated by cargo xtask generate data-notices --write.\nRetain this complete file with derived artifacts.\n\n", 71 ); 72 output.push_str(&contract.source_distribution); 73 output.push('\n'); 74 for material in contract.materials { 75 let text = read(root, &material.path)?; 76 let digest = format!("{:x}", Sha256::digest(text.as_bytes())); 77 if digest != material.sha256 { 78 return Err("redistribution material digest mismatch".to_owned()); 79 } 80 use std::fmt::Write as _; 81 write!( 82 output, 83 "\n===== {} / sha256:{} =====\n", 84 material.path, digest 85 ) 86 .map_err(|_| "cannot render redistribution material".to_owned())?; 87 output.push_str(&text); 88 if !text.ends_with('\n') { 89 output.push('\n'); 90 } 91 } 92 if output.len() > 65_536 { 93 return Err("redistribution material output exceeds its bound".to_owned()); 94 } 95 Ok(output) 96 } 97 98 pub(crate) fn run(flag: &str, root: &Path) -> Result<(), String> { 99 let mode = match flag { 100 "--write" => Mode::Write, 101 "--check" => Mode::Check, 102 _ => return Err("data-notices requires exactly --write or --check".to_owned()), 103 }; 104 write_or_check(&root.join(OUTPUT), &render(root)?, mode) 105 } 106 107 /// Copy the current complete material, requiring its owning generated source 108 /// to be fresh. Consumer artifact checks compare bytes, not merely metadata. 109 pub(crate) fn install(root: &Path, destination: &Path, mode: Mode) -> Result<String, String> { 110 let expected = render(root)?; 111 write_or_check(&root.join(OUTPUT), &expected, Mode::Check)?; 112 write_or_check(destination, &expected, mode)?; 113 Ok(format!("{:x}", Sha256::digest(expected.as_bytes()))) 114 } 115 116 #[cfg(test)] 117 mod tests { 118 use super::*; 119 120 fn fixture() -> tempfile::TempDir { 121 let root = tempfile::tempdir().unwrap(); 122 for name in [CONTRACT].into_iter().chain(MATERIALS) { 123 let destination = root.path().join(name); 124 fs::create_dir_all(destination.parent().unwrap()).unwrap(); 125 fs::copy(crate::workspace_root().join(name), destination).unwrap(); 126 } 127 root 128 } 129 130 #[test] 131 fn complete_exact_material_round_trips_and_rejects_stale_consumer_bytes() { 132 let root = fixture(); 133 run("--write", root.path()).unwrap(); 134 let output = fs::read_to_string(root.path().join(OUTPUT)).unwrap(); 135 for name in MATERIALS { 136 assert!(output.contains(&read(root.path(), name).unwrap())); 137 } 138 run("--check", root.path()).unwrap(); 139 run("--write", root.path()).unwrap(); 140 assert_eq!( 141 fs::read_to_string(root.path().join(OUTPUT)).unwrap(), 142 output 143 ); 144 let consumer = root.path().join("consumer/NOTICE"); 145 install(root.path(), &consumer, Mode::Write).unwrap(); 146 install(root.path(), &consumer, Mode::Check).unwrap(); 147 fs::write(&consumer, "truncated material\n").unwrap(); 148 assert!(install(root.path(), &consumer, Mode::Check).is_err()); 149 fs::write(root.path().join(OUTPUT), "stale source\n").unwrap(); 150 assert!(install(root.path(), &consumer, Mode::Write).is_err()); 151 } 152 153 #[test] 154 fn missing_changed_oversized_and_nonregular_material_never_generates() { 155 for name in MATERIALS { 156 let root = fixture(); 157 let path = root.path().join(name); 158 fs::remove_file(&path).unwrap(); 159 assert!(run("--write", root.path()).is_err()); 160 fs::write(&path, "changed\n").unwrap(); 161 assert!(run("--write", root.path()).is_err()); 162 fs::write(&path, vec![b'x'; 16_385]).unwrap(); 163 assert!(run("--write", root.path()).is_err()); 164 fs::remove_file(&path).unwrap(); 165 fs::create_dir(&path).unwrap(); 166 assert!(run("--write", root.path()).is_err()); 167 assert!(!root.path().join(OUTPUT).exists()); 168 } 169 } 170 171 #[test] 172 fn contract_cannot_select_extra_paths_or_silently_expand_limits() { 173 for field in [ 174 "output", 175 "materials", 176 "max_input_bytes", 177 "max_output_bytes", 178 "schema", 179 "extra", 180 ] { 181 let root = fixture(); 182 let path = root.path().join(CONTRACT); 183 let mut value: serde_json::Value = 184 serde_json::from_str(&fs::read_to_string(&path).unwrap()).unwrap(); 185 value[field] = serde_json::json!("unapproved"); 186 fs::write(&path, serde_json::to_vec(&value).unwrap()).unwrap(); 187 assert!(run("--write", root.path()).is_err(), "{field}"); 188 } 189 assert!(run("--unknown", fixture().path()).is_err()); 190 } 191 }