From f598646623655dac80ee5bfafeeb50b406002a85 Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Wed, 15 Jul 2020 17:58:56 +0200 Subject: openpgp: Gracefully handle malformed notation names. --- openpgp/src/parse.rs | 19 ++++++++++++++++++- .../data/edge-cases/malformed-notation-name.pgp | Bin 0 -> 394 bytes 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 openpgp/tests/data/edge-cases/malformed-notation-name.pgp diff --git a/openpgp/src/parse.rs b/openpgp/src/parse.rs index 75e59da9..1944c5bd 100644 --- a/openpgp/src/parse.rs +++ b/openpgp/src/parse.rs @@ -1491,7 +1491,11 @@ impl Subpacket { SubpacketValue::NotationData( NotationData::new( std::str::from_utf8( - &php.parse_bytes("notation name", name_len)?)?, + &php.parse_bytes("notation name", name_len)?) + .map_err(|e| anyhow::Error::from( + Error::MalformedPacket( + format!("Malformed notation name: {}", e))) + )?, &php.parse_bytes("notation value", value_len)?, Some(flags.into()))) }, @@ -5538,4 +5542,17 @@ mod test { panic!("expected unknown packet, got: {:?}", packet); } } + + /// Malformed notation names must not cause hard parsing errors. + #[test] + fn malformed_notation_name() -> Result<()> { + let ppr = PacketParser::from_bytes( + crate::tests::file("edge-cases/malformed-notation-name.pgp"))?; + let packet = &ppr.unwrap().packet; + if let Packet::Unknown(_) = packet { + Ok(()) + } else { + panic!("expected unknown packet, got: {:?}", packet); + } + } } diff --git a/openpgp/tests/data/edge-cases/malformed-notation-name.pgp b/openpgp/tests/data/edge-cases/malformed-notation-name.pgp new file mode 100644 index 00000000..43c9701b Binary files /dev/null and b/openpgp/tests/data/edge-cases/malformed-notation-name.pgp differ -- cgit v1.2.3