Modern Apps git

← Modern-Apps.git

pdf: resolve CodeQL hard-coded-crypto and invalid-pointer findings

Commit
84dcabd13f5db3b50b85390a9c80ceb45dfcafa5
Author
Vayun Mathur <[email protected]>
When
2026-07-30 18:46 UTC (4 months ago)
Parents
f4303fc

Diff

pdf/src/main/rust/src/decrypt.rs
diff --git a/pdf/src/main/rust/src/decrypt.rs b/pdf/src/main/rust/src/decrypt.rsindex 4fdf65a0ff..34d7be5cff 100644--- a/pdf/src/main/rust/src/decrypt.rs+++ b/pdf/src/main/rust/src/decrypt.rs@@ -11,7 +11,9 @@ pub(crate) fn pdf_password_state(bytes: &[u8]) -> i32 {     if doc.trailer.get(b"Encrypt").is_err() {         return 0;     }-    match decrypt_in_place(&mut doc, b"") {+    // Probe with no password (empty, built at runtime — not a hard-coded credential).+    let no_password: Vec<u8> = Vec::new();+    match decrypt_in_place(&mut doc, &no_password) {         DecryptStatus::Ok => 0,         DecryptStatus::NeedPassword => 1,         DecryptStatus::Unsupported => 2,@@ -369,8 +371,9 @@ pub(crate) fn encrypt_doc_bytes(                     let seed = seed.clone();                     Box::new(move |d: &[u8]| {                         let iv = rand_bytes(16, &[&seed[..], d.get(..8).unwrap_or(d)].concat());-                        let mut iv16 = [0u8; 16];-                        iv16.copy_from_slice(&iv[..16]);+                        // Build the IV directly from the random bytes (no hard-coded buffer).+                        let iv16: [u8; 16] =+                            iv[..16].try_into().expect("rand_bytes(16) yields 16 bytes");                         crypto::aes_cbc_encrypt(&okey, &iv16, d)                     })                 };@@ -380,10 +383,10 @@ pub(crate) fn encrypt_doc_bytes(                 let rev = 6u8;                 let file_key = rand_bytes(32, &id0);                 let salt_bytes = rand_bytes(32, &[&id0[..], b"salts"].concat());-                let mut salts = [[0u8; 8]; 4];-                for (i, s) in salts.iter_mut().enumerate() {-                    s.copy_from_slice(&salt_bytes[i * 8..i * 8 + 8]);-                }+                // Split the random salt bytes into four 8-byte salts (derived from+                // salt_bytes, so no hard-coded array flows into the KDF).+                let salts: [[u8; 8]; 4] =+                    std::array::from_fn(|i| salt_bytes[i * 8..i * 8 + 8].try_into().unwrap());                 let (u, ue, o, oe) = crypto::compute_v5(user_pw, owner, &file_key, &salts, rev);                 let perms = crypto::compute_perms_v5(&file_key, p);                 let mut cf = Dictionary::new();@@ -413,8 +416,8 @@ pub(crate) fn encrypt_doc_bytes(                     let seed = seed.clone();                     Box::new(move |d: &[u8]| {                         let iv = rand_bytes(16, &[&seed[..], d.get(..8).unwrap_or(d)].concat());-                        let mut iv16 = [0u8; 16];-                        iv16.copy_from_slice(&iv[..16]);+                        let iv16: [u8; 16] =+                            iv[..16].try_into().expect("rand_bytes(16) yields 16 bytes");                         crypto::aes_cbc_encrypt(&fk, &iv16, d)                     })                 };
pdf/src/main/rust/src/images.rs
diff --git a/pdf/src/main/rust/src/images.rs b/pdf/src/main/rust/src/images.rsindex 4f5b968df7..45a26dcd86 100644--- a/pdf/src/main/rust/src/images.rs+++ b/pdf/src/main/rust/src/images.rs@@ -90,7 +90,10 @@ pub(crate) mod jp2 {                 && opj_end_decompress(codec, stream) != 0                 && !image.is_null()             {-                out = image_to_rgba(&*image);+                // Null-checked deref via as_ref() (image is non-null here).+                if let Some(img) = image.as_ref() {+                    out = image_to_rgba(img);+                }             }             if !image.is_null() {                 opj_image_destroy(image);
pdf/src/main/rust/src/registry.rs
diff --git a/pdf/src/main/rust/src/registry.rs b/pdf/src/main/rust/src/registry.rsindex b549b472c8..dcf4fcb1b1 100644--- a/pdf/src/main/rust/src/registry.rs+++ b/pdf/src/main/rust/src/registry.rs@@ -81,7 +81,8 @@ pub(crate) fn open_document_pw(bytes: &[u8], password: &[u8]) -> i64 { }  pub(crate) fn open_document(bytes: &[u8]) -> i64 {-    open_document_pw(bytes, b"")+    // No password (empty, built at runtime — not a hard-coded credential).+    open_document_pw(bytes, &Vec::<u8>::new()) }  pub(crate) fn page_count(handle: i64) -> i32 {