Reject structurally impossible payloads before they can be signed or trusted: version, zero operation, ordering, raw-object consistency, signer coherence, and closure-root recomputation. This is the encoding-level content contract. Verification against a live Git object database lives in [`super::verify_binding_content`].
(&self)
| 314 | /// This is the encoding-level content contract. Verification against a |
| 315 | /// live Git object database lives in [`super::verify_binding_content`]. |
| 316 | pub fn validate(&self) -> Result<(), BindingEncodeError> { |
| 317 | if self.version != BINDING_VERSION { |
| 318 | return Err(BindingEncodeError::UnsupportedVersion { |
| 319 | version: self.version, |
| 320 | supported: BINDING_VERSION, |
| 321 | }); |
| 322 | } |
| 323 | if self.operation.as_bytes() == &[0u8; OperationId::SIZE] { |
| 324 | return Err(BindingEncodeError::MissingOperation); |
| 325 | } |
| 326 | // Object-format consistency: every OID must carry the width of the |
| 327 | // declared object format, so a SHA-1 binding can never smuggle a |
| 328 | // SHA-256 object id (or vice versa). |
| 329 | let oid_size = self.git_object_format.oid_size(); |
| 330 | for oid in std::iter::once(&self.git_commit) |
| 331 | .chain(std::iter::once(&self.git_tree)) |
| 332 | .chain(self.git_parents.iter()) |
| 333 | { |
| 334 | if oid.as_bytes().len() != oid_size { |
| 335 | return Err(BindingEncodeError::FormatMismatch { |
| 336 | format: format!("{:?}", self.git_object_format), |
| 337 | oid: oid.to_hex(), |
| 338 | }); |
| 339 | } |
| 340 | } |
| 341 | for (index, parent) in self.git_parents.iter().enumerate() { |
| 342 | if self.git_parents[index + 1..].contains(parent) { |
| 343 | return Err(BindingEncodeError::DuplicateParent { |
| 344 | oid: parent.to_hex(), |
| 345 | }); |
| 346 | } |
| 347 | } |
| 348 | for (index, change) in self.ordered_changes.iter().enumerate() { |
| 349 | if self.ordered_changes[index + 1..].contains(change) { |
| 350 | return Err(BindingEncodeError::DuplicateChange { |
| 351 | hash: change.to_base32(), |
| 352 | }); |
| 353 | } |
| 354 | } |
| 355 | if let Some(raw) = &self.raw_commit_object { |
| 356 | let digest = commit_object_digest(self.git_object_format, raw)?; |
| 357 | if digest != self.git_commit { |
| 358 | return Err(BindingEncodeError::RawObjectMismatch { |
| 359 | expected: self.git_commit.to_hex(), |
| 360 | found: digest.to_hex(), |
| 361 | }); |
| 362 | } |
| 363 | } |
| 364 | validate_view_hint(self.view_hint.as_deref())?; |
| 365 | self.validate_signer()?; |
| 366 | if self.closure_root != self.compute_closure_root() { |
| 367 | return Err(BindingEncodeError::ClosureRootMismatch); |
| 368 | } |
| 369 | Ok(()) |
| 370 | } |
| 371 | |
| 372 | fn validate_signer(&self) -> Result<(), BindingEncodeError> { |
| 373 | let public_key = PublicKey::from_bytes(&self.signer.verifying_key).map_err(|error| { |
no test coverage detected