Skip to main content

capsid_protocol/
copy.rs

1use std::time::Duration;
2
3use serde::{Deserialize, Serialize};
4
5use crate::{AttemptId, ContentDigest, Error, RequestId, Result, encoding, path::is_absolute};
6
7pub const MAX_ACTIVE: usize = 2;
8
9pub const DEADLINE: Duration = Duration::from_secs(300);
10
11pub const MAX_HISTORY: usize = 256;
12
13pub const MAX_PATH: usize = crate::path::MAX_BYTES;
14
15pub const MAX_ENTRIES: usize = 4096;
16
17pub const MAX_BYTES: u64 = 256 * 1024 * 1024;
18
19#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
20#[serde(deny_unknown_fields)]
21pub struct Request {
22    pub version: u32,
23
24    pub attempt_id: AttemptId,
25
26    pub request_id: RequestId,
27
28    pub action: Action,
29}
30
31#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
32#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
33pub enum Action {
34    Download {
35        #[serde(with = "encoding")]
36        path: Vec<u8>,
37    },
38
39    Upload {
40        #[serde(with = "encoding")]
41        path: Vec<u8>,
42
43        identity: ContentDigest,
44
45        durable: bool,
46    },
47}
48
49impl Action {
50    pub fn path(&self) -> &[u8] {
51        match self {
52            Self::Download { path } | Self::Upload { path, .. } => path,
53        }
54    }
55
56    pub fn validate(&self) -> Result<()> {
57        let path = self.path();
58        if !is_absolute(path) || path.split(|byte| *byte == b'/').any(|part| part == b"..") {
59            return Err(Error::Invalid(
60                "copy requires an absolute guest path without parent traversal",
61            ));
62        }
63        Ok(())
64    }
65}
66
67#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
68#[serde(deny_unknown_fields)]
69pub struct Receipt {
70    pub request_id: RequestId,
71
72    pub identity: ContentDigest,
73
74    pub entries: usize,
75
76    pub bytes: u64,
77}
78
79#[derive(Debug, Serialize, Deserialize)]
80#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
81pub enum Response {
82    Ready,
83
84    Complete { receipt: Receipt },
85
86    Rejected { message: String },
87
88    Uncertain { message: String },
89}
90
91#[derive(Debug, Serialize, Deserialize)]
92#[serde(deny_unknown_fields)]
93pub struct Ack {
94    pub request_id: RequestId,
95}
96
97#[cfg(test)]
98mod tests {
99    use super::*;
100
101    #[test]
102    fn test_actions_reject_parent_traversal_nulls_and_relative_paths() {
103        for path in [
104            b"relative".as_slice(),
105            b"/../escape",
106            b"/one/../two",
107            b"/bad\0name",
108        ] {
109            assert!(
110                Action::Download {
111                    path: path.to_vec()
112                }
113                .validate()
114                .is_err()
115            );
116        }
117        assert!(
118            Action::Download {
119                path: b"/raw-\xff".to_vec()
120            }
121            .validate()
122            .is_ok()
123        );
124    }
125
126    #[test]
127    fn test_upload_identities_must_be_sha256_digests() {
128        for identity in ["A".repeat(64), "z".repeat(64), "a".repeat(63)] {
129            let request = serde_json::json!({"kind":"upload","path":"Lw==","identity":format!("sha256:{identity}"),"durable":true});
130            assert!(serde_json::from_value::<Action>(request).is_err());
131        }
132        let request = serde_json::json!({"kind":"upload","path":"Lw==","identity":format!("sha256:{}", "a".repeat(64)),"durable":true});
133        assert!(serde_json::from_value::<Action>(request).is_ok());
134    }
135}