vela_protocol/objects/
repository_inputs.rs1use std::collections::BTreeSet;
4
5use serde::{Deserialize, Serialize};
6use unicode_normalization::UnicodeNormalization;
7
8pub const RETAINED_OBJECT_MANIFEST_SCHEMA: &str = "vela.retained-object-manifest.v1";
9
10#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
11#[serde(rename_all = "lowercase")]
12pub enum GitObjectFormat {
13 Sha1,
14 Sha256,
15}
16
17#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
18#[serde(deny_unknown_fields)]
19pub struct RetainedObjectEntryV1 {
20 pub path: String,
21 pub git_mode: String,
22 pub size: u64,
23 pub sha256: String,
25}
26
27impl RetainedObjectEntryV1 {
28 pub fn validate(&self) -> Result<(), String> {
29 validate_repository_path(&self.path)?;
30 if !matches!(self.git_mode.as_str(), "100644" | "100755") {
31 return Err(
32 "retained object git_mode must be tracked regular-file mode 100644 or 100755"
33 .to_string(),
34 );
35 }
36 if self.sha256.len() != 64
37 || !self
38 .sha256
39 .bytes()
40 .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
41 {
42 return Err("retained object sha256 must be 64 lowercase hex characters".into());
43 }
44 Ok(())
45 }
46}
47
48#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
51#[serde(transparent)]
52pub struct RetainedObjectManifestV1(pub Vec<RetainedObjectEntryV1>);
53
54impl RetainedObjectManifestV1 {
55 pub fn validate(&self) -> Result<(), String> {
56 let mut previous_path: Option<&str> = None;
57 let mut portable_collision_keys = BTreeSet::new();
58 for entry in &self.0 {
59 entry.validate()?;
60 if let Some(previous) = previous_path {
61 if entry.path == previous {
62 return Err(format!("duplicate retained object path {:?}", entry.path));
63 }
64 if entry.path.as_str() < previous {
65 return Err("retained object entries must be sorted by path".into());
66 }
67 }
68 previous_path = Some(&entry.path);
69
70 let collision_key = entry
71 .path
72 .nfc()
73 .flat_map(char::to_lowercase)
74 .collect::<String>();
75 if !portable_collision_keys.insert(collision_key) {
76 return Err(format!(
77 "retained object path {:?} has a portable case-fold collision",
78 entry.path
79 ));
80 }
81 }
82 Ok(())
83 }
84}
85
86fn validate_repository_path(path: &str) -> Result<(), String> {
87 if path.trim().is_empty() {
88 return Err("retained object path must not be empty".into());
89 }
90 if path != path.nfc().collect::<String>() {
91 return Err("retained object path must already be Unicode NFC".into());
92 }
93 if path.chars().any(char::is_control) {
94 return Err("retained object path contains a forbidden control character".into());
95 }
96 if path.starts_with('/') || path.ends_with('/') || path.contains('\\') {
97 return Err("retained object path must be a normalized relative repository path".into());
98 }
99 if path
100 .split('/')
101 .any(|component| component.is_empty() || matches!(component, "." | ".."))
102 {
103 return Err("retained object path contains an empty, dot, or traversal segment".into());
104 }
105 Ok(())
106}
107
108#[cfg(test)]
109mod tests {
110 use super::*;
111
112 #[test]
113 fn retained_manifest_is_sorted_and_portable() {
114 let entry = |path: &str, digest: char| RetainedObjectEntryV1 {
115 path: path.into(),
116 git_mode: "100644".into(),
117 size: 1,
118 sha256: digest.to_string().repeat(64),
119 };
120 assert!(
121 RetainedObjectManifestV1(vec![entry("a", '1'), entry("b", '2')])
122 .validate()
123 .is_ok()
124 );
125 assert!(
126 RetainedObjectManifestV1(vec![entry("A", '1'), entry("a", '2')])
127 .validate()
128 .is_err()
129 );
130 }
131}