Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

Reject signed and empty arcs in try_oid (#5581)

NetterSilverfdaa502f279e
diff --git a/crates/sign/src/der.rs b/crates/sign/src/der.rs
index 6248c1e9e..0cac252c3 100644
--- a/crates/sign/src/der.rs
+++ b/crates/sign/src/der.rs
@@ -465,7 +465,12 @@ pub fn oid(dotted: &str) -> Vec<u8> {
/// Encode a dotted OID supplied by a caller. Unlike [`oid`], this validates every arc and
/// never panics on malformed input.
pub fn try_oid(dotted: &str) -> Result<Vec<u8>, SignError> {
- let mut arcs = dotted.split('.').map(|a| a.parse::<u64>().map_err(|_| bad("invalid OID arc")));
+ let mut arcs = dotted.split('.').map(|a| {
+ if a.is_empty() || !a.bytes().all(|c| c.is_ascii_digit()) {
+ return Err(bad("invalid OID arc"));
+ }
+ a.parse::<u64>().map_err(|_| bad("invalid OID arc"))
+ });
let first = arcs.next().ok_or_else(|| bad("empty OID"))??;
let second = arcs.next().ok_or_else(|| bad("OID needs two arcs"))??;
if first > 2 || (first < 2 && second >= 40) {
@@ -549,6 +554,14 @@ pub fn algorithm(oid_s: &str, params: Option<&[u8]>) -> Vec<u8> {
mod tests {
use super::*;
+ #[test]
+ fn try_oid_rejects_signs_and_empty_arcs() {
+ for bad in ["+1.2.840", "1.+2", "1.2.-3", "1..2"] {
+ assert!(try_oid(bad).is_err(), "accepted {bad}");
+ }
+ assert_eq!(try_oid("1.2.840.113549").unwrap(), oid("1.2.840.113549"));
+ }
+
#[test]
fn round_trips() {
let o = oid("1.2.840.113549.1.7.2");