Skip to content

Commit

Permalink
Do not allow to read a header with a future revision
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin Doer committed Sep 27, 2024
1 parent 0f8ad4b commit c348c8f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
4 changes: 4 additions & 0 deletions nuts-container/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ pub enum HeaderError {
#[error("invalid header revision, expected {0} but got {1}")]
InvalidRevision(u32, u32),

/// Unknown header revision (from the future)
#[error("unknown header revision {0}")]
UnknownRevision(u32),

/// Invalid header, could not validate magic
#[error("invalid header")]
InvalidHeader,
Expand Down
4 changes: 2 additions & 2 deletions nuts-container/src/header/revision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#[cfg(test)]
mod tests;

use crate::buffer::{Buffer, BufferError, BufferMut};
use crate::buffer::{Buffer, BufferMut};
use crate::cipher::Cipher;
use crate::header::HeaderError;
use crate::kdf::Kdf;
Expand Down Expand Up @@ -104,7 +104,7 @@ impl Revision {
match b {
0 => Data::get_from_buffer(buf).map(Revision::Rev0),
1 => Data::get_from_buffer(buf).map(Revision::Rev1),
_ => Err(BufferError::InvalidIndex("Revision".to_string(), b).into()),
_ => Err(HeaderError::UnknownRevision(b)),
}
}

Expand Down
6 changes: 2 additions & 4 deletions nuts-container/src/header/revision/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.

use crate::buffer::BufferError;
use crate::cipher::Cipher;
use crate::header::revision::{Data, Revision};
use crate::header::HeaderError;
Expand Down Expand Up @@ -79,9 +78,8 @@ fn de_inval_revision() {
buf[10] = 2;

let err = Revision::get_from_buffer(&mut &buf[..]).unwrap_err();
assert!(matches!(err, HeaderError::Buffer(ref cause)
if matches!(cause, BufferError::InvalidIndex(str, idx)
if str == "Revision" && *idx == 2)));

assert!(matches!(err, HeaderError::UnknownRevision(rev) if rev == 2));
}

#[test]
Expand Down

0 comments on commit c348c8f

Please sign in to comment.