Skip to content

Commit 4e5ff19

Browse files
committed
fix(rust/signed-doc): add content type validation
1 parent 3017ece commit 4e5ff19

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

rust/signed_doc/src/content.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,22 @@ impl Content {
4141
///
4242
/// # Errors
4343
/// Returns an error if content is not correctly encoded
44-
#[allow(clippy::unnecessary_wraps)]
4544
pub(crate) fn from_decoded(
4645
data: Vec<u8>, content_type: ContentType, content_encoding: Option<ContentEncoding>,
4746
) -> anyhow::Result<Self> {
4847
// TODO add content_type verification
48+
match content_type {
49+
ContentType::Json => {
50+
if let Err(e) = serde_json::from_slice::<serde_json::Value>(&data) {
51+
anyhow::bail!("Invalid {content_type} content: {e}")
52+
}
53+
},
54+
ContentType::Cbor => {
55+
if let Err(e) = minicbor::decode::<minicbor::data::Token>(&data) {
56+
anyhow::bail!("Invalid {content_type} content: {e}")
57+
}
58+
},
59+
}
4960
Ok(Self {
5061
data,
5162
content_type,

0 commit comments

Comments
 (0)