mirror of
https://github.com/storytold/deckcraft.git
synced 2026-11-17 15:44:44 +00:00
#792: read password-protected decks with craft-cfb and drop the local PNG encoder
DeckCraft's pptx crate carried a copy of GridCraft's CFB directory walk to tell an encrypted package from other compound files. It now asks the shared craft-cfb (has_entry_named, same UTF-16 fallback for damaged directories, directory entries capped at 4096 as before). The placeholder for missing picture media keeps its exact bytes, written as a constant instead of through a hand-rolled CRC/Adler/PNG encoder. Part of storytold/craft-libs#778 and storytold/craft-libs#792.
This commit is contained in:
1 parent
596ec54b59
commit
5f58c3c208
5 files changed
+47
-142
No files matched your search
Generated
+6
@@ -853,6 +853,11 @@ dependencies = [
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-cfb"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=ea24422ae9859dc5da398d0462bc87f8e02e0d10#ea24422ae9859dc5da398d0462bc87f8e02e0d10"
|
||||
|
||||
[[package]]
|
||||
name = "craft-codepage"
|
||||
version = "0.1.0"
|
||||
@@ -1301,6 +1306,7 @@ dependencies = [
|
||||
name = "deckcraft-pptx"
|
||||
version = "0.19.0"
|
||||
dependencies = [
|
||||
"craft-cfb",
|
||||
"craft-opc",
|
||||
"craft-xml",
|
||||
"deckcraft-color",
|
||||
|
||||
@@ -13,6 +13,8 @@ repository = "https://github.com/storytold/deckcraft"
|
||||
[workspace.dependencies]
|
||||
# OPC part-name primitives; relationship admission stays app-local.
|
||||
craft-opc = { git = "https://github.com/storytold/craft-libs", rev = "ea24422ae9859dc5da398d0462bc87f8e02e0d10", version = "0.1.0" }
|
||||
# Compound File Binary directory reads (password-protected deck detection).
|
||||
craft-cfb = { git = "https://github.com/storytold/craft-libs", rev = "ea24422ae9859dc5da398d0462bc87f8e02e0d10", version = "0.1.0" }
|
||||
# XML writer primitives; parser admission and OOXML policy stay app-local.
|
||||
craft-xml = { git = "https://github.com/storytold/craft-libs", rev = "ea24422ae9859dc5da398d0462bc87f8e02e0d10", version = "0.1.0" }
|
||||
# internal
|
||||
|
||||
@@ -11,6 +11,7 @@ workspace = true
|
||||
[dependencies]
|
||||
craft-opc.workspace = true
|
||||
craft-xml.workspace = true
|
||||
craft-cfb.workspace = true
|
||||
deckcraft-model = { workspace = true }
|
||||
deckcraft-geom = { workspace = true }
|
||||
deckcraft-color = { workspace = true }
|
||||
|
||||
+16
-94
@@ -1,103 +1,24 @@
|
||||
//! Just enough of the Compound File Binary (CFB/OLE2) container to tell a password-protected
|
||||
//! presentation apart: PowerPoint wraps an encrypted `.pptx` in a CFB holding `EncryptionInfo`
|
||||
//! and `EncryptedPackage` streams (ECMA-376 / [MS-OFFCRYPTO]) instead of a zip. Every read is
|
||||
//! bounds-checked and every loop bounded. Copied from GridCraft's `crates/xlsx/src/cfb.rs`.
|
||||
|
||||
/// The CFB signature at the start of the file.
|
||||
const SIGNATURE: [u8; 8] = [0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1];
|
||||
|
||||
const END_OF_CHAIN: u32 = 0xFFFF_FFFE;
|
||||
/// Sector ids at or above this are markers (free, end of chain, FAT/DIFAT sector), not sectors.
|
||||
const MAX_REG_SECT: u32 = 0xFFFF_FFFA;
|
||||
/// Directory sectors followed at most (4096 entries with 512-byte sectors).
|
||||
const MAX_DIR_SECTORS: usize = 1024;
|
||||
/// DIFAT sectors followed at most.
|
||||
const MAX_DIFAT_SECTORS: usize = 1024;
|
||||
//! Password-protected presentations. PowerPoint wraps an encrypted `.pptx` in a Compound File
|
||||
//! Binary (CFB/OLE2) container holding `EncryptionInfo` and `EncryptedPackage` streams
|
||||
//! (ECMA-376 / [MS-OFFCRYPTO]) instead of a zip. The container's directory is read by the shared
|
||||
//! `craft-cfb` crate, which bounds every chain and falls back to a UTF-16 name search when a
|
||||
//! damaged file's directory cannot be followed.
|
||||
|
||||
/// Stream names that mark an encrypted (password-protected) OOXML package.
|
||||
const ENCRYPTED_STREAMS: [&str; 2] = ["EncryptedPackage", "EncryptionInfo"];
|
||||
|
||||
fn u16_at(b: &[u8], at: usize) -> Option<u16> {
|
||||
let s = b.get(at..at.checked_add(2)?)?;
|
||||
Some(u16::from_le_bytes([*s.first()?, *s.get(1)?]))
|
||||
/// Directory entries read at most: 1024 directory sectors of 512 bytes, as before the move to
|
||||
/// `craft-cfb`, so a hostile directory chain stays cheap.
|
||||
const MAX_DIR_ENTRIES: usize = 4096;
|
||||
|
||||
fn limits() -> craft_cfb::Limits {
|
||||
craft_cfb::Limits { max_dir_entries: MAX_DIR_ENTRIES, ..craft_cfb::Limits::default() }
|
||||
}
|
||||
|
||||
fn u32_at(b: &[u8], at: usize) -> Option<u32> {
|
||||
let s = b.get(at..at.checked_add(4)?)?;
|
||||
Some(u32::from_le_bytes([*s.first()?, *s.get(1)?, *s.get(2)?, *s.get(3)?]))
|
||||
}
|
||||
|
||||
/// Whether `bytes` is a compound file holding an encryption stream. Walks the directory through
|
||||
/// the FAT; when the structure can't be followed (truncated or malformed), falls back to scanning
|
||||
/// the bytes for the UTF-16LE stream names.
|
||||
/// Whether `bytes` is a compound file holding an encryption stream.
|
||||
pub(crate) fn is_encrypted(bytes: &[u8]) -> bool {
|
||||
if !bytes.starts_with(&SIGNATURE) {
|
||||
return false;
|
||||
}
|
||||
match directory_names(bytes) {
|
||||
Some(names) => names.iter().any(|n| ENCRYPTED_STREAMS.iter().any(|e| n.eq_ignore_ascii_case(e))),
|
||||
None => ENCRYPTED_STREAMS.iter().any(|e| contains_utf16(bytes, e)),
|
||||
}
|
||||
}
|
||||
|
||||
fn contains_utf16(hay: &[u8], needle: &str) -> bool {
|
||||
let n: Vec<u8> = needle.encode_utf16().flat_map(u16::to_le_bytes).collect();
|
||||
!n.is_empty() && hay.windows(n.len()).any(|w| w == n.as_slice())
|
||||
}
|
||||
|
||||
/// Names of every directory entry, or `None` when the directory can't be read.
|
||||
fn directory_names(b: &[u8]) -> Option<Vec<String>> {
|
||||
if !b.starts_with(&SIGNATURE) {
|
||||
return None;
|
||||
}
|
||||
let shift = u16_at(b, 30)?;
|
||||
if shift != 9 && shift != 12 {
|
||||
return None;
|
||||
}
|
||||
let size = 1usize << shift;
|
||||
let per = size / 4;
|
||||
let sector = |id: u32| -> Option<&[u8]> {
|
||||
let start = (id as usize).checked_add(1)?.checked_mul(size)?;
|
||||
b.get(start..start.checked_add(size)?)
|
||||
};
|
||||
// The DIFAT: the FAT's sector ids, 109 in the header then chained DIFAT sectors.
|
||||
let mut difat: Vec<u32> = (0..109).filter_map(|i| u32_at(b, 76 + i * 4)).filter(|&s| s < MAX_REG_SECT).collect();
|
||||
let mut next = u32_at(b, 68)?;
|
||||
for _ in 0..MAX_DIFAT_SECTORS {
|
||||
if next >= MAX_REG_SECT {
|
||||
break;
|
||||
}
|
||||
let s = sector(next)?;
|
||||
difat.extend((0..per - 1).filter_map(|i| u32_at(s, i * 4)).filter(|&x| x < MAX_REG_SECT));
|
||||
next = u32_at(s, (per - 1) * 4)?;
|
||||
}
|
||||
let fat_next = |id: u32| -> Option<u32> {
|
||||
let idx = id as usize;
|
||||
let fat_sector = *difat.get(idx / per)?;
|
||||
u32_at(sector(fat_sector)?, (idx % per) * 4)
|
||||
};
|
||||
let mut names = Vec::new();
|
||||
let mut id = u32_at(b, 48)?;
|
||||
for _ in 0..MAX_DIR_SECTORS {
|
||||
if id == END_OF_CHAIN {
|
||||
return Some(names);
|
||||
}
|
||||
if id >= MAX_REG_SECT {
|
||||
return None;
|
||||
}
|
||||
let s = sector(id)?;
|
||||
for e in s.as_chunks::<128>().0 {
|
||||
let len = usize::from(u16_at(e, 64)?);
|
||||
if !(2..=64).contains(&len) {
|
||||
continue;
|
||||
}
|
||||
let units: Vec<u16> = (0..(len - 2) / 2).filter_map(|i| u16_at(e, i * 2)).collect();
|
||||
names.push(String::from_utf16_lossy(&units));
|
||||
}
|
||||
id = fat_next(id)?;
|
||||
}
|
||||
// A directory longer than the bound (or a cyclic chain): report what was read.
|
||||
Some(names)
|
||||
let limits = limits();
|
||||
ENCRYPTED_STREAMS.iter().any(|name| craft_cfb::has_entry_named(bytes, name, &limits))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -105,6 +26,7 @@ mod tests {
|
||||
#![allow(clippy::unwrap_used)]
|
||||
|
||||
use super::*;
|
||||
use craft_cfb::{ENDOFCHAIN as END_OF_CHAIN, SIGNATURE};
|
||||
|
||||
/// A minimal valid compound file (512-byte sectors: FAT at sector 0, directory at sector 1)
|
||||
/// whose directory holds a root entry plus the given stream names.
|
||||
@@ -146,7 +68,7 @@ mod tests {
|
||||
#[test]
|
||||
fn reads_directory_names() {
|
||||
let b = build(&["EncryptionInfo", "EncryptedPackage"]);
|
||||
assert_eq!(directory_names(&b), Some(vec!["Root Entry".into(), "EncryptionInfo".into(), "EncryptedPackage".into()]));
|
||||
assert_eq!(craft_cfb::directory_names(&b, &limits()), Some(vec!["Root Entry".into(), "EncryptionInfo".into(), "EncryptedPackage".into()]));
|
||||
assert!(is_encrypted(&b));
|
||||
// A PowerPoint 97-2003 .ppt is a compound file too, without the encryption streams.
|
||||
assert!(!is_encrypted(&build(&["PowerPoint Document", "Current User"])));
|
||||
|
||||
+22
-48
@@ -1,59 +1,33 @@
|
||||
//! A minimal PNG encoder for the placeholder image written when a picture's media is missing.
|
||||
//! The placeholder image written when a picture's media is missing: a 1×1 fully transparent RGBA
|
||||
//! PNG, kept as its fixed bytes (signature, IHDR, one stored-deflate IDAT scanline, IEND) so the
|
||||
//! app carries no PNG encoder of its own.
|
||||
|
||||
fn crc32(data: &[u8]) -> u32 {
|
||||
let mut c = 0xFFFF_FFFFu32;
|
||||
for b in data {
|
||||
c ^= *b as u32;
|
||||
for _ in 0..8 {
|
||||
c = if c & 1 != 0 { 0xEDB8_8320 ^ (c >> 1) } else { c >> 1 };
|
||||
}
|
||||
}
|
||||
!c
|
||||
}
|
||||
|
||||
fn adler32(data: &[u8]) -> u32 {
|
||||
let (mut a, mut b) = (1u32, 0u32);
|
||||
for x in data {
|
||||
a = (a + *x as u32) % 65521;
|
||||
b = (b + a) % 65521;
|
||||
}
|
||||
(b << 16) | a
|
||||
}
|
||||
|
||||
fn chunk(out: &mut Vec<u8>, kind: &[u8; 4], data: &[u8]) {
|
||||
out.extend_from_slice(&(data.len() as u32).to_be_bytes());
|
||||
let mut c = Vec::with_capacity(data.len() + 4);
|
||||
c.extend_from_slice(kind);
|
||||
c.extend_from_slice(data);
|
||||
out.extend_from_slice(&c);
|
||||
out.extend_from_slice(&crc32(&c).to_be_bytes());
|
||||
}
|
||||
/// The PNG file, byte for byte what the former local encoder produced.
|
||||
const TRANSPARENT_1X1: [u8; 73] = [
|
||||
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
|
||||
0x08, 0x06, 0x00, 0x00, 0x00, 0x1F, 0x15, 0xC4, 0x89, 0x00, 0x00, 0x00, 0x10, 0x49, 0x44, 0x41, 0x54, 0x78, 0x01, 0x01, 0x05, 0x00, 0xFA, 0xFF,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x01, 0x64, 0x78, 0x95, 0x38, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60,
|
||||
0x82,
|
||||
];
|
||||
|
||||
/// A 1×1 fully transparent RGBA PNG.
|
||||
pub fn transparent_1x1() -> Vec<u8> {
|
||||
let mut out = b"\x89PNG\r\n\x1a\n".to_vec();
|
||||
let mut ihdr = vec![];
|
||||
ihdr.extend_from_slice(&1u32.to_be_bytes());
|
||||
ihdr.extend_from_slice(&1u32.to_be_bytes());
|
||||
ihdr.extend_from_slice(&[8, 6, 0, 0, 0]);
|
||||
chunk(&mut out, b"IHDR", &ihdr);
|
||||
// One scanline: filter byte + RGBA, stored (uncompressed) deflate block.
|
||||
let raw = [0u8, 0, 0, 0, 0];
|
||||
let mut z = vec![0x78, 0x01, 0x01];
|
||||
z.extend_from_slice(&(raw.len() as u16).to_le_bytes());
|
||||
z.extend_from_slice(&(!(raw.len() as u16)).to_le_bytes());
|
||||
z.extend_from_slice(&raw);
|
||||
z.extend_from_slice(&adler32(&raw).to_be_bytes());
|
||||
chunk(&mut out, b"IDAT", &z);
|
||||
chunk(&mut out, b"IEND", &[]);
|
||||
out
|
||||
TRANSPARENT_1X1.to_vec()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#![allow(clippy::unwrap_used)]
|
||||
|
||||
#[test]
|
||||
fn crc_known() {
|
||||
assert_eq!(super::crc32(b"IEND"), 0xAE42_6082);
|
||||
assert!(super::transparent_1x1().starts_with(b"\x89PNG"));
|
||||
fn placeholder_is_a_transparent_1x1_png() {
|
||||
let png = super::transparent_1x1();
|
||||
assert!(png.starts_with(b"\x89PNG\r\n\x1a\n"));
|
||||
// IHDR: 1×1, 8-bit RGBA, no interlace.
|
||||
assert_eq!(&png[12..16], b"IHDR");
|
||||
assert_eq!(&png[16..29], &[0, 0, 0, 1, 0, 0, 0, 1, 8, 6, 0, 0, 0]);
|
||||
assert!(png.ends_with(b"IEND\xAE\x42\x60\x82"));
|
||||
// The stored scanline: filter byte 0, then one pixel with zero alpha.
|
||||
assert_eq!(&png[49..54], &[0, 0, 0, 0, 0]);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user