tilo_actions/remote/
content_link.rs1pub const CONTENT_LINKS_UNAVAILABLE: &str = "links to content are not available here";
26
27pub const CONTENT_LINK_TTL_SECS: u64 = 15 * 60;
31
32pub const MAX_CONTENT_LINK_FILES: usize = 65_535;
34
35#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
37#[serde(deny_unknown_fields)]
38pub struct ContentLinkFile {
39 pub path: String,
42 pub hash: String,
45}
46
47fn is_hash(hash: &str) -> bool {
49 hash.len() == 64 && hash.bytes().all(|b| b.is_ascii_hexdigit())
50}
51
52#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
54#[serde(deny_unknown_fields)]
55pub struct ContentLinkAsk {
56 pub name: String,
59 pub files: Vec<ContentLinkFile>,
61 pub zip: bool,
63}
64
65impl ContentLinkAsk {
66 pub fn check(&self) -> Result<(), String> {
73 if self.name.trim().is_empty() || self.name.contains(['/', '\\', '\0']) {
74 return Err(format!("{:?} is no name to save under", self.name));
75 }
76 if !self.zip && self.files.len() != 1 {
77 return Err("a link to more than one file is a zip".into());
78 }
79 if self.files.len() > MAX_CONTENT_LINK_FILES {
80 return Err(format!(
81 "a link names at most {MAX_CONTENT_LINK_FILES} files"
82 ));
83 }
84 for file in &self.files {
85 let safe = !file.path.is_empty()
86 && !file.path.starts_with('/')
87 && !file.path.contains(['\\', '\0'])
88 && file
89 .path
90 .split('/')
91 .all(|part| !part.is_empty() && part != "." && part != "..");
92 if !safe {
93 return Err(format!("{:?} is no path in a zip", file.path));
94 }
95 if !is_hash(&file.hash) {
96 return Err(format!("{:?} is no content hash", file.hash));
97 }
98 }
99 Ok(())
100 }
101}
102
103#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
105#[serde(deny_unknown_fields)]
106pub struct ContentLink {
107 pub path: String,
109 pub expires_in_secs: u64,
111}
112
113#[must_use]
118pub fn viewable(name: &str) -> Option<&'static str> {
119 let (_, extension) = name.rsplit_once('.')?;
120 Some(match extension.to_ascii_lowercase().as_str() {
121 "mp4" | "m4v" => "video/mp4",
122 "mov" => "video/quicktime",
123 "webm" => "video/webm",
124 "ogv" => "video/ogg",
125 "mp3" => "audio/mpeg",
126 "m4a" => "audio/mp4",
127 "wav" => "audio/wav",
128 "ogg" | "oga" | "opus" => "audio/ogg",
129 "flac" => "audio/flac",
130 "png" => "image/png",
131 "jpg" | "jpeg" => "image/jpeg",
132 "gif" => "image/gif",
133 "webp" => "image/webp",
134 "avif" => "image/avif",
135 "txt" => "text/plain; charset=utf-8",
136 _ => return None,
137 })
138}
139
140#[cfg(test)]
141mod tests {
142
143 use super::*;
144
145 fn file(path: &str) -> ContentLinkFile {
146 ContentLinkFile {
147 path: path.into(),
148 hash: "ab".repeat(32),
149 }
150 }
151
152 #[test]
153 fn a_link_names_a_file_or_a_zip_of_safe_paths() {
154 let one = ContentLinkAsk {
155 name: "plan.md".into(),
156 files: vec![file("plan.md")],
157 zip: false,
158 };
159 assert!(one.check().is_ok());
160 let folder = ContentLinkAsk {
161 name: "Plans.zip".into(),
162 files: vec![file("Plans/a.md"), file("Plans/2026/b.csv")],
163 zip: true,
164 };
165 assert!(folder.check().is_ok());
166 let empty_folder = ContentLinkAsk {
167 files: Vec::new(),
168 ..folder.clone()
169 };
170 assert!(
171 empty_folder.check().is_ok(),
172 "an empty folder is an empty zip"
173 );
174
175 let two_unzipped = ContentLinkAsk {
176 zip: false,
177 ..folder.clone()
178 };
179 assert!(two_unzipped.check().is_err());
180 for path in ["../etc/passwd", "/abs", "a//b", "a/./b", "a\\b", ""] {
181 let bad = ContentLinkAsk {
182 files: vec![file(path)],
183 ..folder.clone()
184 };
185 assert!(bad.check().is_err(), "{path:?}");
186 }
187 let unnamed = ContentLinkAsk {
188 name: "a/b.zip".into(),
189 ..folder.clone()
190 };
191 assert!(unnamed.check().is_err());
192 let not_a_hash = ContentLinkAsk {
193 files: vec![ContentLinkFile {
194 path: "a".into(),
195 hash: "tilo.content:xyz".into(),
196 }],
197 ..folder
198 };
199 assert!(not_a_hash.check().is_err());
200 }
201}