exarch_core/security/
admitted.rs1use crate::Result;
10use crate::SecurityConfig;
11use crate::security::hardlink::HardlinkTarget;
12use crate::security::permissions::RawMode;
13use crate::security::permissions::SanitizedMode;
14use crate::security::permissions::sanitize_permissions;
15use crate::security::quota::EntryCount;
16use crate::security::quota::QuotaPermit;
17use crate::security::quota::QuotaTracker;
18use crate::security::zipbomb::Compressed;
19use crate::security::zipbomb::Uncompressed;
20use crate::security::zipbomb::validate_compression_ratio;
21use crate::types::SafePath;
22use crate::types::SafeSymlink;
23use crate::types::safe_path::ParentCheck;
24use crate::types::safe_path::Resolved;
25
26#[derive(Debug, Clone, Copy)]
28pub struct EntrySize {
29 uncompressed: Uncompressed,
30 compressed: Option<Compressed>,
31}
32
33impl EntrySize {
34 #[must_use]
36 pub const fn new(uncompressed: Uncompressed, compressed: Option<Compressed>) -> Self {
37 Self {
38 uncompressed,
39 compressed,
40 }
41 }
42
43 #[must_use]
45 pub const fn uncompressed(&self) -> Uncompressed {
46 self.uncompressed
47 }
48
49 #[must_use]
51 pub const fn compressed(&self) -> Option<Compressed> {
52 self.compressed
53 }
54
55 pub(crate) fn check_ratio(self, config: &SecurityConfig) -> Result<()> {
58 if let Some(compressed) = self.compressed {
59 validate_compression_ratio(compressed, self.uncompressed, config)?;
60 }
61 Ok(())
62 }
63}
64
65#[derive(Debug)]
68pub struct PendingFile<P: ParentCheck = Resolved> {
69 path: SafePath<P>,
70 size: EntrySize,
71 mode: Option<RawMode>,
72}
73
74impl<P: ParentCheck> PendingFile<P> {
75 pub(super) const fn new(path: SafePath<P>, size: EntrySize, mode: Option<RawMode>) -> Self {
76 Self { path, size, mode }
77 }
78
79 pub(crate) const fn path(&self) -> &SafePath<P> {
81 &self.path
82 }
83
84 pub(super) fn admit(
91 self,
92 quota: &mut QuotaTracker,
93 config: &SecurityConfig,
94 ) -> Result<ValidatedFile<P>> {
95 let permit = quota.reserve(self.size.uncompressed.0, config)?;
96 self.size.check_ratio(config)?;
97 Ok(ValidatedFile {
98 path: self.path,
99 permit,
100 mode: self.mode.map(|m| sanitize_permissions(m, config)),
101 })
102 }
103}
104
105#[derive(Debug)]
107pub struct ValidatedFile<P: ParentCheck = Resolved> {
108 path: SafePath<P>,
109 permit: QuotaPermit,
110 mode: Option<SanitizedMode>,
111}
112
113impl<P: ParentCheck> ValidatedFile<P> {
114 pub(crate) fn into_parts(self) -> (SafePath<P>, QuotaPermit, Option<SanitizedMode>) {
117 (self.path, self.permit, self.mode)
118 }
119}
120
121#[derive(Debug)]
124pub struct ValidatedSymlink<P: ParentCheck = Resolved> {
125 pub symlink: SafeSymlink<P>,
127}
128
129#[derive(Debug)]
131pub struct ValidatedHardlink<P: ParentCheck = Resolved> {
132 pub link: SafePath<P>,
134 pub target: HardlinkTarget,
136 pub counted: EntryCount,
138}
139
140#[cfg(test)]
141#[allow(clippy::unwrap_used)]
142mod tests {
143 use std::assert_matches;
144 use std::path::Path;
145
146 use tempfile::TempDir;
147
148 use super::*;
149 use crate::ArchiveError;
150 use crate::SecurityConfig;
151 use crate::security::fs_view::HostFs;
152 use crate::security::validator::ValidatorCore;
153 use crate::types::DestDir;
154
155 fn sizes(uncompressed: u64) -> EntrySize {
156 EntrySize::new(Uncompressed(uncompressed), None)
157 }
158
159 #[test]
160 fn a_file_carries_its_path_and_sanitized_mode() {
161 let temp = TempDir::new().unwrap();
162 let dest = DestDir::new(temp.path().to_path_buf()).unwrap();
163 let config = SecurityConfig::default();
164 let mut core = ValidatorCore::new(&config);
165 let ctx = core.context::<HostFs>();
166
167 let file = core
168 .validate_file_in(
169 Path::new("a.txt"),
170 sizes(3),
171 Some(RawMode::new(0o4755)),
172 &dest,
173 &HostFs,
174 &ctx,
175 )
176 .unwrap();
177
178 let (path, _permit, mode) = file.into_parts();
179 assert_eq!(path.as_path(), Path::new("a.txt"));
180 assert_eq!(mode.unwrap().as_u32() & 0o6000, 0, "setuid is stripped");
181 }
182
183 #[test]
184 fn a_refused_file_yields_no_proof() {
185 let temp = TempDir::new().unwrap();
186 let dest = DestDir::new(temp.path().to_path_buf()).unwrap();
187 let config = SecurityConfig::default();
188 let mut core = ValidatorCore::new(&config);
189 let ctx = core.context::<HostFs>();
190
191 assert_matches!(
192 core.validate_file_in(Path::new("../x"), sizes(1), None, &dest, &HostFs, &ctx),
193 Err(ArchiveError::PathTraversal { .. })
194 );
195 }
196
197 #[test]
198 fn a_directory_symlink_and_hardlink_carry_their_validated_parts() {
199 let temp = TempDir::new().unwrap();
200 let dest = DestDir::new(temp.path().to_path_buf()).unwrap();
201 let config = SecurityConfig::default()
202 .with_allow_symlinks(true)
203 .with_allow_hardlinks(true);
204 let mut core = ValidatorCore::new(&config);
205
206 let ctx = core.context::<HostFs>();
207 let directory = core
208 .validate_directory_in(Path::new("d"), sizes(0), &dest, &HostFs, &ctx)
209 .unwrap();
210 assert_eq!(directory.as_path(), Path::new("d"));
211
212 let ctx = core.context::<HostFs>();
213 let symlink = core
214 .validate_symlink_in(
215 Path::new("s"),
216 Path::new("d"),
217 sizes(1),
218 &dest,
219 &HostFs,
220 &ctx,
221 )
222 .unwrap();
223 assert_eq!(symlink.symlink.link().as_path(), Path::new("s"));
224 assert_eq!(symlink.symlink.target_path(), Path::new("d"));
225
226 let ctx = core.context::<HostFs>();
227 let hardlink = core
228 .validate_hardlink_in(
229 Path::new("h"),
230 Path::new("t"),
231 sizes(0),
232 &dest,
233 &HostFs,
234 &ctx,
235 )
236 .unwrap();
237 assert_eq!(hardlink.link.as_path(), Path::new("h"));
238 assert_eq!(hardlink.target.as_path(), Path::new("t"));
239 }
240}