forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.09.7 MiB37e985dTahinli2 hours ago363 commits

lfs_pointer.rs131 lines, 4.1 KiBHistoryRawDownload
1use crate::lfs::is_sha256_hex;
2
3const MAX_POINTER_BYTES: usize = 1024;
4
5const VERSION_LINE: &str = "version https://git-lfs.github.com/spec/v1";
6
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub struct Pointer {
9 pub oid: String,
10 pub size: u64,
11}
12
13pub fn parse(data: &[u8]) -> Option<Pointer> {
14 if data.len() > MAX_POINTER_BYTES {
15 return None;
16 }
17 let text = std::str::from_utf8(data).ok()?;
18 let mut lines = text.strip_suffix('\n').unwrap_or(text).split('\n');
19 if lines.next()? != VERSION_LINE {
20 return None;
21 }
22
23 let mut oid = None;
24 let mut size = None;
25 for line in lines {
26 let (key, value) = line.split_once(' ')?;
27 match key {
28 "oid" => {
29 let hex = value.strip_prefix("sha256:")?;
30 if oid.is_some() || !is_sha256_hex(hex) {
31 return None;
32 }
33 oid = Some(hex.to_owned());
34 }
35 "size" => {
36 if size.is_some() || value.is_empty() || !value.bytes().all(|b| b.is_ascii_digit())
37 {
38 return None;
39 }
40 size = Some(value.parse::<u64>().ok()?);
41 }
42 _ => {}
43 }
44 }
45 Some(Pointer {
46 oid: oid?,
47 size: size?,
48 })
49}
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54
55 const OID: &str = "4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393";
56
57 fn pointer(oid: &str, size: &str) -> String {
58 format!("{VERSION_LINE}\noid sha256:{oid}\nsize {size}\n")
59 }
60
61 #[test]
62 fn rejects_malformed_pointers() {
63 let no_size = format!("{VERSION_LINE}\noid sha256:{OID}\n");
64 let no_oid = format!("{VERSION_LINE}\nsize 1\n");
65 let twice = format!("{VERSION_LINE}\noid sha256:{OID}\noid sha256:{OID}\nsize 1\n");
66 let big = format!("{VERSION_LINE}\n{}", "x".repeat(MAX_POINTER_BYTES));
67 let cases: Vec<(&str, Vec<u8>)> = vec![
68 (
69 "other version url",
70 format!("version https://example.com/spec/v1\noid sha256:{OID}\nsize 1\n")
71 .into_bytes(),
72 ),
73 (
74 "version with trailing space",
75 format!("{VERSION_LINE} \noid sha256:{OID}\nsize 1\n").into_bytes(),
76 ),
77 (
78 "uppercase oid",
79 pointer(&OID.to_uppercase(), "1").into_bytes(),
80 ),
81 ("short oid", pointer(&OID[..63], "1").into_bytes()),
82 ("long oid", pointer(&format!("{OID}0"), "1").into_bytes()),
83 (
84 "md5 oid",
85 format!("{VERSION_LINE}\noid md5:{OID}\nsize 1\n").into_bytes(),
86 ),
87 ("empty size", pointer(OID, "").into_bytes()),
88 ("negative size", pointer(OID, "-1").into_bytes()),
89 ("fractional size", pointer(OID, "1.5").into_bytes()),
90 ("hex size", pointer(OID, "0x10").into_bytes()),
91 (
92 "size overflows u64",
93 pointer(OID, "99999999999999999999999").into_bytes(),
94 ),
95 ("no size line", no_size.into_bytes()),
96 ("no oid line", no_oid.into_bytes()),
97 ("duplicate oid line", twice.into_bytes()),
98 ("oversized", big.into_bytes()),
99 ("non-utf8", vec![0xff, 0xfe, 0x00]),
100 ("empty", Vec::new()),
101 ("plain text", b"hello\nworld\n".to_vec()),
102 ];
103 for (name, bytes) in cases {
104 assert_eq!(parse(&bytes), None, "case {name}");
105 }
106 }
107
108 #[test]
109 fn parses_a_standard_pointer() {
110 let got = parse(pointer(OID, "12345").as_bytes());
111 assert_eq!(
112 got,
113 Some(Pointer {
114 oid: OID.to_owned(),
115 size: 12345
116 })
117 );
118 }
119
120 #[test]
121 fn accepts_missing_trailing_newline_and_extra_keys() {
122 let text = format!("{VERSION_LINE}\next-0-x sha256:abc\noid sha256:{OID}\nsize 0");
123 assert_eq!(
124 parse(text.as_bytes()),
125 Some(Pointer {
126 oid: OID.to_owned(),
127 size: 0
128 })
129 );
130 }
131}