Decode a `changes.pack` blob under the transport budgets. Every gate runs **before** the corresponding allocation: the encoded size is checked before decompression begins and decompression is streaming with a hard ceiling, so a bomb is stopped without materializing.
(
bytes: &[u8],
limits: &BindingPackLimits,
)
| 212 | /// is checked before decompression begins and decompression is streaming |
| 213 | /// with a hard ceiling, so a bomb is stopped without materializing. |
| 214 | pub fn decode_changes_pack( |
| 215 | bytes: &[u8], |
| 216 | limits: &BindingPackLimits, |
| 217 | ) -> Result<Vec<ObjectRecord>, BindingPackError> { |
| 218 | // 1. Encoded size gate — before any allocation or decompression. |
| 219 | if bytes.len() > limits.max_pack_bytes { |
| 220 | return Err(BindingPackError::OversizedPack { |
| 221 | limit: limits.max_pack_bytes, |
| 222 | found: bytes.len(), |
| 223 | }); |
| 224 | } |
| 225 | |
| 226 | // 2. Streaming decompression with a hard output ceiling; 3. version gate. |
| 227 | let envelope = decode_with_limit::<ChangesPack>(bytes, limits.max_decompressed_bytes).map_err( |
| 228 | |error| match error { |
| 229 | atomic_objects::SyncError::TooLarge { limit } => { |
| 230 | BindingPackError::DecompressedTooLarge { limit } |
| 231 | } |
| 232 | other => BindingPackError::UnsupportedVersion { |
| 233 | reason: other.to_string(), |
| 234 | }, |
| 235 | }, |
| 236 | )?; |
| 237 | let ChangesPack::V1(v1) = envelope; |
| 238 | |
| 239 | // 4. Object-count gate. |
| 240 | if v1.records.len() > limits.max_objects { |
| 241 | return Err(BindingPackError::TooManyObjects { |
| 242 | limit: limits.max_objects, |
| 243 | found: v1.records.len(), |
| 244 | }); |
| 245 | } |
| 246 | |
| 247 | // 5-8. Per-record gates in quarantine order. |
| 248 | let mut seen: HashSet<&str> = HashSet::with_capacity(v1.records.len()); |
| 249 | for record in &v1.records { |
| 250 | if !matches!(record.family, ObjectFamily::Change) { |
| 251 | return Err(BindingPackError::Refused { |
| 252 | detail: format!( |
| 253 | "binding pack refused {:?} object {}: only V3 change files may enter Git", |
| 254 | record.family, record.key |
| 255 | ), |
| 256 | }); |
| 257 | } |
| 258 | if !is_blake3_key(&record.key) { |
| 259 | return Err(BindingPackError::InvalidKey { |
| 260 | key: record.key.clone(), |
| 261 | }); |
| 262 | } |
| 263 | if Hash::of(&record.bytes).to_hex() != record.key { |
| 264 | return Err(BindingPackError::HashMismatch { |
| 265 | key: record.key.clone(), |
| 266 | }); |
| 267 | } |
| 268 | if record.bytes.len() > limits.max_object_bytes { |
| 269 | return Err(BindingPackError::ObjectTooLarge { |
| 270 | key: record.key.clone(), |
| 271 | limit: limits.max_object_bytes, |