Skip to content

Commit cb201ac

Browse files
committed
fix(codec): enforce canonical descriptor and metadata validation
1 parent 2a52269 commit cb201ac

3 files changed

Lines changed: 300 additions & 15 deletions

File tree

‎src/descriptor.rs‎

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ impl ServingDescriptor {
5757

5858
/// Encode to canonical bytes.
5959
pub fn encode(&self) -> CodecResult<Vec<u8>> {
60+
validate_instance_uuid(&self.instance_uuid)?;
6061
validate_scope(&self.scope)?;
6162
let mut out = Vec::with_capacity(BASE_LENGTH + self.scope.len());
6263
out.extend_from_slice(b"MSD2");
@@ -116,6 +117,7 @@ impl ServingDescriptor {
116117
}
117118
let mut instance_uuid = [0u8; 16];
118119
instance_uuid.copy_from_slice(&buf[8..24]);
120+
validate_instance_uuid(&instance_uuid)?;
119121
let mut namespace_view_id = [0u8; 32];
120122
namespace_view_id.copy_from_slice(&buf[24..56]);
121123
let scope_bytes = &buf[58..58 + scope_len];
@@ -136,35 +138,48 @@ impl ServingDescriptor {
136138

137139
/// Canonical Mega absolute UTF-8 path: starts with `/`, no trailing slash
138140
/// (except the root itself), no empty/`.`/`..` components, no NUL,
139-
/// each component at most 256 bytes (spec 02 §3; note this component cap is
140-
/// wider than the 255-byte MTP2 name cap of spec 05 §2).
141+
/// at most 4096 UTF-8 bytes and 256 components, each at most 255 bytes
142+
/// (spec 02 §3 and spec 14 §4).
141143
pub fn validate_scope(scope: &str) -> CodecResult<()> {
142144
if scope.is_empty() || !scope.starts_with('/') {
143145
return Err(CodecError::BadName("scope must be absolute"));
144146
}
145147
if scope.contains('\0') {
146148
return Err(CodecError::BadName("NUL in scope"));
147149
}
150+
if scope.len() > 4096 {
151+
return Err(CodecError::BadLength("scope over 4096 UTF-8 bytes"));
152+
}
148153
if scope.len() > 1 && scope.ends_with('/') {
149154
return Err(CodecError::BadName("trailing slash"));
150155
}
151156
if scope == "/" {
152157
return Ok(());
153158
}
154-
for comp in scope[1..].split('/') {
159+
for (index, comp) in scope[1..].split('/').enumerate() {
160+
if index >= 256 {
161+
return Err(CodecError::BadLength("scope over 256 components"));
162+
}
155163
if comp.is_empty() {
156164
return Err(CodecError::BadName("empty path component"));
157165
}
158166
if comp == "." || comp == ".." {
159167
return Err(CodecError::BadName("dot component"));
160168
}
161-
if comp.len() > 256 {
162-
return Err(CodecError::BadName("path component over 256 bytes"));
169+
if comp.len() > 255 {
170+
return Err(CodecError::BadName("path component over 255 bytes"));
163171
}
164172
}
165173
Ok(())
166174
}
167175

176+
fn validate_instance_uuid(uuid: &[u8; 16]) -> CodecResult<()> {
177+
if *uuid == [0; 16] {
178+
return Err(CodecError::BadConstant("instance UUID must be non-nil"));
179+
}
180+
Ok(())
181+
}
182+
168183
#[cfg(test)]
169184
mod tests {
170185
use super::*;
@@ -244,9 +259,54 @@ mod tests {
244259
#[test]
245260
fn scope_component_length_limit() {
246261
let mut d = sample();
247-
d.scope = format!("/{}", "a".repeat(256));
262+
d.scope = format!("/{}", "a".repeat(255));
248263
assert!(d.encode().is_ok());
249-
d.scope = format!("/{}", "a".repeat(257));
264+
d.scope = format!("/{}", "a".repeat(256));
265+
assert!(d.encode().is_err());
266+
}
267+
268+
#[test]
269+
fn scope_limits_count_utf8_bytes_and_components() {
270+
let mut d = sample();
271+
d.scope = format!("/{}/a", vec!["a".repeat(255); 15].join("/"));
272+
d.scope.push_str(&"b".repeat(254));
273+
assert_eq!(d.scope.len(), 4096);
274+
assert_eq!(ServingDescriptor::decode(&d.encode().unwrap()).unwrap(), d);
275+
let oversized = format!("{}/c", &d.scope[..d.scope.len() - 1]);
276+
assert_eq!(oversized.len(), 4097);
277+
assert!(validate_scope(&oversized).is_err());
278+
279+
d.scope = format!("/{}", vec!["a"; 256].join("/"));
280+
assert_eq!(ServingDescriptor::decode(&d.encode().unwrap()).unwrap(), d);
281+
d.scope.push_str("/a");
282+
assert!(d.encode().is_err());
283+
284+
assert!(validate_scope(&format!("/{}", "é".repeat(127))).is_ok());
285+
assert!(validate_scope(&format!("/{}", "é".repeat(128))).is_err());
286+
// A missing total-length bound previously truncated the u16 field.
287+
assert!(validate_scope(&format!("/{}", vec!["a"; 40_000].join("/"))).is_err());
288+
}
289+
290+
#[test]
291+
fn decoder_rejects_over_limit_scope_and_nil_instance() {
292+
let bytes = sample().encode().unwrap();
293+
let fields = &bytes[59..];
294+
for scope in [
295+
format!("/{}", "a".repeat(256)),
296+
format!("/{}", vec!["a"; 257].join("/")),
297+
format!("/{}", vec!["a".repeat(255); 17].join("/")),
298+
] {
299+
let mut forged = bytes[..56].to_vec();
300+
write_u16(&mut forged, scope.len() as u16);
301+
forged.extend_from_slice(scope.as_bytes());
302+
forged.extend_from_slice(fields);
303+
assert!(ServingDescriptor::decode(&forged).is_err());
304+
}
305+
let mut nil = bytes;
306+
nil[8..24].fill(0);
307+
assert!(ServingDescriptor::decode(&nil).is_err());
308+
let mut d = sample();
309+
d.instance_uuid = [0; 16];
250310
assert!(d.encode().is_err());
251311
}
252312
}

0 commit comments

Comments
 (0)