Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 48 additions & 16 deletions src/uapi16/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,8 +471,15 @@ impl FileObject {
/// [`ContentsObject::check_values`], and whose fields agree with each
/// other. Where one of them does not, nothing is filled in for the
/// object either, as a reader refuses its record and a writer refuses
/// the object.
/// the object. Where there are more than [`MAX_CONTENTS`] of them,
/// nothing is filled in at all, neither for the object nor for any of
/// them.
pub(crate) fn fill_in_from_contents(&mut self) {
// A reader refuses the record of an object with more than
// MAX_CONTENTS contents objects, and nothing follows from them.
if self.contents.len() > MAX_CONTENTS {
return;
}
// The object is in no record, and what is wrong with it is not
// shown, so the number of the record does not matter.
let subject = Subject::new(self, ROOT_RECORD);
Expand Down Expand Up @@ -1862,19 +1869,11 @@ impl<'a> Part<'a> for ContentsElement {
match field {
Field::File => {
if let Some(file) = decoder.string(field, value) {
match Name::new(file.as_ref()) {
Ok(file) => {
contents.source = ContentsSource::File(file);
places += 1;
}
Err(reason) => decoder.note_error(Problem::InvalidContents, field, || {
format!(
"{}: contents file reference: {}: '{}'",
subject,
reason,
file.escape_debug()
)
}),
let name = Name::new(file.as_ref())
.map_err(|reason| Error::invalid_file_reference(subject, reason, &file));
if let Some(name) = decoder.note_err(name) {
contents.source = ContentsSource::File(name);
places += 1;
}
}
}
Expand Down Expand Up @@ -3101,8 +3100,9 @@ mod tests {
}
}
// A contents object with a value a reader refuses: a URL that is
// neither http nor https, the encoding that stands for none and a
// name that is not one of an HTTP header field.
// neither http nor https, the encoding that stands for none, a name
// that is not one of an HTTP header field, and the name of the root
// as the file.
let ftp = || ContentsObject::new(ContentsSource::Url("ftp://example.com/x".into()));
let https = || ContentsObject::new(ContentsSource::Url("https://example.com/x".into()));
for (record, contents, field) in [
Expand Down Expand Up @@ -3132,6 +3132,14 @@ mod tests {
},
Field::HttpHeaders,
),
(
r#"{"name":"x","contents":[{"file":"","originalSize":5}]}"#,
ContentsObject {
original_size: Some(5),
..ContentsObject::new(ContentsSource::File(Name::ROOT))
},
Field::File,
),
] {
let e = refused(record);
assert_eq!(
Expand All @@ -3145,6 +3153,30 @@ mod tests {
assert!(object.filled_in().is_empty(), "{}", record);
assert_eq!(object.contents(), [contents], "{}", record);
}
// As many contents objects as a reader takes, and one more, for
// which nothing is filled in at all.
let one = ContentsObject {
original_size: Some(5),
..file()
};
let many = |n: usize| {
let record = vec![r#"{"file":"a","originalSize":5}"#; n].join(",");
(
format!(r#"{{"name":"x","contents":[{}]}}"#, record),
set(None, None, vec![one.clone(); n]),
)
};
let (record, object) = many(MAX_CONTENTS);
assert_eq!(object.size(), Some(5));
assert_eq!(object, self::object(&record));
let (record, object) = many(MAX_CONTENTS + 1);
assert_eq!(
refused(&record).to_string(),
"manifest object 1: more than 256 contents entries"
);
assert_eq!((object.size(), object.sha256()), (None, None));
assert!(object.filled_in().is_empty());
assert!(object.contents().iter().all(|contents| *contents == one));
}

#[test]
Expand Down
17 changes: 16 additions & 1 deletion src/uapi16/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::io;
use crate::decode::Subject;
use crate::object::FileObject;
use crate::strict::{ParseError, ParseErrorKind};
use crate::{MAX_CONTENTS, MAX_RECORD_SIZE};
use crate::{NameProblem, MAX_CONTENTS, MAX_RECORD_SIZE};

/// What is wrong with a manifest.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -637,6 +637,21 @@ impl Error {
.at(Field::Offset)
}

/// A contents object of the file object `subject` names whose `file`
/// is `file`, which is no name of a file for the reason `reason`.
pub(crate) fn invalid_file_reference(subject: Subject<'_>, reason: NameProblem, file: &str) -> Error {
Error::manifest(
Problem::InvalidContents,
format!(
"{}: contents file reference: {}: '{}'",
subject,
reason,
file.escape_debug()
),
)
.at(Field::File)
}

/// `object`, which `subject` names, states the flag of the field
/// `field`, which it cannot have, see [`FileObject::unpermitted_flag`].
pub(crate) fn unpermitted_flag(object: &FileObject, field: Field, subject: Subject<'_>) -> Error {
Expand Down
3 changes: 2 additions & 1 deletion src/uapi16/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1826,7 +1826,8 @@ impl FileObject {
/// reader filled in that no longer follows is dropped, and what the
/// object states stays. A writer refuses an object whose fields
/// contradict each other or that has such a value, and more than
/// [`MAX_CONTENTS`] contents objects.
/// [`MAX_CONTENTS`] contents objects, for which nothing is filled in at
/// all.
///
/// [`MAX_CONTENTS`]: crate::MAX_CONTENTS
pub fn set_contents(&mut self, contents: Vec<ContentsObject>) {
Expand Down
47 changes: 44 additions & 3 deletions src/uapi16/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use crate::object::{
};
use crate::sequence::SequenceCheck;
use crate::{
Features, Name, Order, Sha256, MAX_CONTENTS, MAX_RECORD_SIZE, MEDIA_TYPE, RECORD_END, RECORD_SEPARATOR,
SIGNATURE_MEDIA_TYPE, STRING_RULES, TRAILER_MEDIA_TYPE, USER_NAMESPACE,
Features, Name, NameProblem, Order, Sha256, MAX_CONTENTS, MAX_RECORD_SIZE, MEDIA_TYPE, RECORD_END,
RECORD_SEPARATOR, SIGNATURE_MEDIA_TYPE, STRING_RULES, TRAILER_MEDIA_TYPE, USER_NAMESPACE,
};

/// The most room for a record that an [`Encoder`] keeps from one record to
Expand Down Expand Up @@ -729,6 +729,12 @@ impl ContentsObject {
if let Some(encoding) = &self.encoding {
ContentsObject::check_encoding(encoding, subject)?;
}
// Every name is one a reader takes for a file, but the root's: it is
// empty.
if let Some(file) = self.file().filter(|file| file.is_root()) {
let reason = NameProblem::Empty;
return Err(Error::invalid_file_reference(subject, reason, file.as_str()));
}
// A reader checks the header fields in the order they are written.
if !self.http_headers.is_empty() {
ContentsObject::check_headers(self.sorted_headers(), subject)?;
Expand Down Expand Up @@ -1785,7 +1791,7 @@ mod tests {
..GptPartition::default()
}))
}
let cases: [(Give, Problem, Field); 19] = [
let cases: [(Give, Problem, Field); 20] = [
(|o| o.inode.uid = Some(u32::MAX), Problem::FieldOutOfRange, Field::Uid),
(|o| o.inode.gid = Some(u32::MAX), Problem::FieldOutOfRange, Field::Gid),
// -1 as a 16-bit ID is no ID either.
Expand Down Expand Up @@ -1856,6 +1862,11 @@ mod tests {
Problem::InvalidContents,
Field::Contents,
),
(
|o| o.contents = vec![ContentsObject::new(ContentsSource::File(Name::ROOT))],
Problem::InvalidContents,
Field::File,
),
];
let (root, _) = recording_all();
for (give, problem, field) in cases {
Expand All @@ -1870,6 +1881,36 @@ mod tests {
object
);
}
// A contents object can't refer to the root. Name::ROOT, the default
// name and the parent of a name at the top are all the name of the
// root. A reader refuses the record a writer would write for such an
// object with the same error, only without the name of the object,
// which it doesn't know yet.
for file in [Name::ROOT, Name::default(), name("a").parent().unwrap()] {
let mut object = FileObject::new(name("x"));
object.contents = vec![ContentsObject::new(ContentsSource::File(file))];
let mut writer = Writer::begin(Vec::new(), &root).unwrap();
let e = writer.file(&object).unwrap_err();
assert_eq!(
(e.kind(), e.field(), e.to_string()),
(
ErrorKind::Manifest(Problem::InvalidContents),
Some(Field::File),
"manifest object 1 (x): contents file reference: name is empty: ''".to_string()
)
);
}
let (_, root_text) = recording_all();
let record = r#"{"contents":[{"file":""}],"name":"x"}"#;
let e = read_whole(&sequence(&[&root_text, record, TRAILER])).unwrap_err();
assert_eq!(
(e.kind(), e.field(), e.to_string()),
(
ErrorKind::Manifest(Problem::InvalidContents),
Some(Field::File),
"manifest object 1: contents file reference: name is empty: ''".to_string()
)
);
// A field of a feature the manifest does not record, also one of
// those the specification added last.
let mut object = FileObject::new(name("x"));
Expand Down