use super::*;
use crate::manifest::parse_manifest_for_test;
use crate::test_support::{load_golden, zip_with, B64, MIME_TEXT};
use crate::uris;
const MIME_WRONG: &str = "text/plain";
fn manifest_wrap(package_version: Option<&str>, body: &str) -> String {
let ver = match package_version {
Some(v) => format!(" manifest:version=\"{v}\""),
None => String::new(),
};
format!(
r#"<?xml version="1.0" encoding="UTF-8"?>
<manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0" xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0"{ver}>
{body}
</manifest:manifest>
"#
)
}
struct PwOpts<'a> {
path: &'a str,
media_type: &'a str,
size: Option<i64>,
cipher: &'a str,
checksum: bool,
start_key: Option<&'a str>,
kdf: &'a str,
iteration_count: Option<i32>,
key_size: Option<i32>,
argon2: Option<(i32, i32, i32)>,
kdf_before_algorithm: bool,
}
impl Default for PwOpts<'_> {
fn default() -> Self {
Self {
path: "content.xml",
media_type: "text/xml",
size: Some(100),
cipher: uris::AES256_URL,
checksum: true,
start_key: Some(uris::SHA1_NAME),
kdf: uris::PBKDF2_NAME,
iteration_count: Some(1024),
key_size: Some(32),
argon2: None,
kdf_before_algorithm: false,
}
}
}
fn file_entry(opts: PwOpts<'_>) -> String {
let size = match opts.size {
Some(n) => format!(" manifest:size=\"{n}\""),
None => String::new(),
};
let checksum = if opts.checksum {
format!(
" manifest:checksum-type=\"{}\" manifest:checksum=\"{B64}\"",
uris::SHA1_1K_NAME
)
} else {
String::new()
};
let algorithm = format!(
r#" <manifest:algorithm manifest:algorithm-name="{}" manifest:initialisation-vector="{B64}"/>"#,
opts.cipher
);
let start_key = match opts.start_key {
Some(name) => format!(
r#" <manifest:start-key-generation manifest:start-key-generation-name="{name}"/>"#
),
None => String::new(),
};
let key_size = match opts.key_size {
Some(n) => format!(" manifest:key-size=\"{n}\""),
None => String::new(),
};
let kdf = if let Some((t, m, p)) = opts.argon2 {
format!(
r#" <manifest:key-derivation manifest:key-derivation-name="{kdf}" manifest:salt="{B64}" manifest:argon2-iterations="{t}" manifest:argon2-memory="{m}" manifest:argon2-lanes="{p}"{key_size}/>"#,
kdf = opts.kdf,
)
} else {
let count = match opts.iteration_count {
Some(n) => format!(" manifest:iteration-count=\"{n}\""),
None => String::new(),
};
format!(
r#" <manifest:key-derivation manifest:key-derivation-name="{kdf}" manifest:salt="{B64}"{count}{key_size}/>"#,
kdf = opts.kdf,
)
};
let children = if opts.kdf_before_algorithm {
format!("{kdf}\n{start_key}\n{algorithm}")
} else {
format!("{algorithm}\n{start_key}\n{kdf}")
};
format!(
r#" <manifest:file-entry manifest:full-path="{path}" manifest:media-type="{mt}"{size}>
<manifest:encryption-data{checksum}>
{children}
</manifest:encryption-data>
</manifest:file-entry>
"#,
path = opts.path,
mt = opts.media_type,
)
}
fn root_row(version: &str, media_type: &str) -> String {
format!(
r#" <manifest:file-entry manifest:full-path="/" manifest:version="{version}" manifest:media-type="{media_type}"/>
"#
)
}
fn plain_row(path: &str, media_type: &str) -> String {
format!(
r#" <manifest:file-entry manifest:full-path="{path}" manifest:media-type="{media_type}"/>
"#
)
}
fn classify_pkg(manifest: &str, files: &[(&str, &[u8])]) -> Classification {
let mut all = vec![("META-INF/manifest.xml", manifest.as_bytes())];
all.extend_from_slice(files);
classify(&zip_with(&all)).expect("constructed package should classify")
}
const UNENCRYPTED_MANIFEST: &str = r#"<?xml version="1.0" encoding="UTF-8"?>
<manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0" manifest:version="1.3">
<manifest:file-entry manifest:full-path="/" manifest:version="1.3" manifest:media-type="application/vnd.oasis.opendocument.text"/>
<manifest:file-entry manifest:full-path="content.xml" manifest:media-type="text/xml"/>
<manifest:file-entry manifest:full-path="styles.xml" manifest:media-type="text/xml"/>
<manifest:file-entry manifest:full-path="meta.xml" manifest:media-type="text/xml"/>
<manifest:file-entry manifest:full-path="settings.xml" manifest:media-type="text/xml"/>
</manifest:manifest>
"#;
const CONTENT_XML: &str = r#"<?xml version="1.0" encoding="UTF-8"?>
<office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" office:version="1.3">
<office:body>
<office:text>
<text:p>Hello</text:p>
</office:text>
</office:body>
</office:document-content>
"#;
const STYLES_XML: &str = r#"<?xml version="1.0" encoding="UTF-8"?>
<office:document-styles xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" office:version="1.3"/>
"#;
const META_XML: &str = r#"<?xml version="1.0" encoding="UTF-8"?>
<office:document-meta xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" office:version="1.3"/>
"#;
const SETTINGS_XML: &str = r#"<?xml version="1.0" encoding="UTF-8"?>
<office:document-settings xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" office:version="1.3"/>
"#;
fn unencrypted_odt() -> Vec<u8> {
zip_with(&[
("mimetype", MIME_TEXT.as_bytes()),
("META-INF/manifest.xml", UNENCRYPTED_MANIFEST.as_bytes()),
("content.xml", CONTENT_XML.as_bytes()),
("styles.xml", STYLES_XML.as_bytes()),
("meta.xml", META_XML.as_bytes()),
("settings.xml", SETTINGS_XML.as_bytes()),
])
}
fn pgp_two_row_manifest() -> String {
format!(
r#"<?xml version="1.0" encoding="UTF-8"?>
<manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0" xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0" manifest:version="1.3">
<manifest:file-entry manifest:full-path="content.xml" manifest:media-type="text/xml" manifest:size="100">
<loext:encrypted-key>
<loext:encryption-method loext:PGPAlgorithm="http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p"/>
<loext:KeyInfo>
<loext:PGPData>
<loext:PGPKeyID>{B64}</loext:PGPKeyID>
<loext:PGPKeyPacket>{B64}</loext:PGPKeyPacket>
</loext:PGPData>
</loext:KeyInfo>
<loext:CipherData>
<loext:CipherValue>{B64}</loext:CipherValue>
</loext:CipherData>
</loext:encrypted-key>
<manifest:encryption-data>
<manifest:algorithm manifest:algorithm-name="http://www.w3.org/2009/xmlenc11#aes256-gcm" manifest:initialisation-vector="{B64}"/>
<manifest:key-derivation manifest:key-derivation-name="PGP"/>
</manifest:encryption-data>
</manifest:file-entry>
<manifest:file-entry manifest:full-path="styles.xml" manifest:media-type="text/xml" manifest:size="50">
<manifest:encryption-data>
<manifest:algorithm manifest:algorithm-name="http://www.w3.org/2009/xmlenc11#aes256-gcm" manifest:initialisation-vector="{B64}"/>
<manifest:key-derivation manifest:key-derivation-name="PGP"/>
</manifest:encryption-data>
</manifest:file-entry>
</manifest:manifest>
"#
)
}
fn pgp_two_row_zip() -> Vec<u8> {
let manifest = pgp_two_row_manifest();
zip_with(&[
("mimetype", MIME_TEXT.as_bytes()),
("META-INF/manifest.xml", manifest.as_bytes()),
("content.xml", b"encrypted-content"),
("styles.xml", b"encrypted-styles"),
])
}
#[test]
fn classify_real_unencrypted_odt_is_plain() {
let class = classify(&load_golden("lo-unencrypted.odt")).expect("real unencrypted");
assert_eq!(class.mode, Mode::Plain);
assert!(!class.package_encrypted);
assert!(class.common.is_none());
assert!(class.encrypted_entries.is_empty());
assert!(!class.zip_has_encrypted_package);
assert_eq!(class.odf_version.as_deref(), Some("1.4"));
assert_eq!(class.media_type.as_deref(), Some(MIME_TEXT));
assert!(!class.has_unexpected_streams);
assert!(!class.odf12_fatal);
}
#[test]
fn classify_unencrypted_odt_is_plain() {
let bytes = unencrypted_odt();
let class = classify(&bytes).expect("unencrypted ODT should classify");
assert_eq!(class.mode, Mode::Plain);
assert!(!class.package_encrypted);
assert!(class.encrypted_entries.is_empty());
assert!(class.common.is_none());
assert!(!class.zip_has_encrypted_package);
assert!(!class.has_unexpected_streams);
assert!(!class.odf12_fatal);
}
#[test]
fn pgp_two_row_key_info_leaks_to_styles() {
let xml = pgp_two_row_manifest();
let bags = parse_manifest_for_test(&xml);
assert_eq!(bags.len(), 2, "two file-entry bags");
assert!(bags[0].key_info.is_some(), "first entry carries KeyInfo");
assert!(
bags[1].key_info.is_none(),
"second entry has no KeyInfo of its own"
);
assert_eq!(bags[0].full_path, "content.xml");
assert_eq!(bags[1].full_path, "styles.xml");
assert_eq!(bags[0].kdf, Some(KdfId::PgpRsaOaepMgf1p));
assert_eq!(bags[1].kdf, Some(KdfId::PgpRsaOaepMgf1p));
let class = classify(&pgp_two_row_zip()).expect("constructed PGP zip should classify");
assert_eq!(class.encrypted_entries.len(), 2);
assert!(
class
.encrypted_entries
.iter()
.any(|e| e.path == "styles.xml" && matches!(e.kdf, Kdf::PgpRsaOaepMgf1p)),
"styles.xml satisfies the PGP predicate because of sticky key_info"
);
assert!(class
.encrypted_entries
.iter()
.any(|e| e.path == "content.xml" && matches!(e.kdf, Kdf::PgpRsaOaepMgf1p)));
assert_eq!(class.mode, Mode::PerEntry);
assert!(class.package_encrypted);
assert_eq!(
class.common.as_ref().map(|e| e.path.as_str()),
Some("content.xml")
);
}
enum S2Expect {
Plain,
PerEntryLatch { check: fn(&EntryEncryption) },
}
fn s2_min_zip() -> [(&'static str, &'static [u8]); 2] {
[("mimetype", MIME_TEXT.as_bytes()), ("content.xml", b"x")]
}
#[test]
fn s2_accept_predicate_fixtures() {
struct Row {
name: &'static str,
body: String,
expect: S2Expect,
pre: Option<fn(&str)>,
}
let rows = [
Row {
name: "missing manifest:size",
body: format!(
"{}{}",
root_row("1.2", MIME_TEXT),
file_entry(PwOpts {
size: None,
..PwOpts::default()
})
),
expect: S2Expect::Plain,
pre: None,
},
Row {
name: "GCM without checksum",
body: format!(
"{}{}",
root_row("1.2", MIME_TEXT),
file_entry(PwOpts {
cipher: uris::AESGCM256_URL,
checksum: false,
..PwOpts::default()
})
),
expect: S2Expect::PerEntryLatch {
check: |e| {
assert_eq!(e.cipher, Cipher::AesGcmW3c);
assert_eq!(e.checksum, Checksum::None);
},
},
pre: None,
},
Row {
name: "CBC without checksum",
body: format!(
"{}{}",
root_row("1.2", MIME_TEXT),
file_entry(PwOpts {
checksum: false,
..PwOpts::default()
})
),
expect: S2Expect::Plain,
pre: None,
},
Row {
name: "Argon2 t=0",
body: format!(
"{}{}",
root_row("1.2", MIME_TEXT),
file_entry(PwOpts {
cipher: uris::AESGCM256_URL,
checksum: false,
kdf: uris::ARGON2ID_URL,
argon2: Some((0, 65536, 4)),
iteration_count: None,
..PwOpts::default()
})
),
expect: S2Expect::Plain,
pre: Some(|xml| {
let bags = parse_manifest_for_test(xml);
let content = bags
.iter()
.find(|b| b.full_path == "content.xml")
.expect("content row");
assert_eq!(content.kdf, Some(KdfId::Argon2id));
assert!(content.argon2_args.is_none());
}),
},
Row {
name: "missing start-key",
body: format!(
"{}{}",
root_row("1.2", MIME_TEXT),
file_entry(PwOpts {
start_key: None,
..PwOpts::default()
})
),
expect: S2Expect::PerEntryLatch {
check: |e| assert_eq!(e.start_key, StartKeyAlg::Sha1),
},
pre: None,
},
Row {
name: "unknown cipher URI",
body: format!(
"{}{}",
root_row("1.2", MIME_TEXT),
file_entry(PwOpts {
cipher: "not-a-cipher",
..PwOpts::default()
})
),
expect: S2Expect::Plain,
pre: None,
},
Row {
name: "KDF before algorithm, aes256-cbc, no key-size",
body: format!(
"{}{}",
root_row("1.2", MIME_TEXT),
file_entry(PwOpts {
cipher: uris::AES256_URL,
key_size: None,
kdf_before_algorithm: true,
..PwOpts::default()
})
),
expect: S2Expect::PerEntryLatch {
check: |e| {
assert_eq!(e.cipher, Cipher::AesCbcW3c);
assert_eq!(e.derived_key_len, 16);
assert_eq!(e.size, 100);
assert_eq!(e.iv, vec![1, 2, 3, 4]);
},
},
pre: None,
},
Row {
name: "algorithm before KDF, aes256-cbc, no key-size",
body: format!(
"{}{}",
root_row("1.2", MIME_TEXT),
file_entry(PwOpts {
cipher: uris::AES256_URL,
key_size: None,
..PwOpts::default()
})
),
expect: S2Expect::PerEntryLatch {
check: |e| assert_eq!(e.derived_key_len, 32),
},
pre: None,
},
Row {
name: "algorithm before KDF, aes192-gcm, no key-size",
body: format!(
"{}{}",
root_row("1.2", MIME_TEXT),
file_entry(PwOpts {
cipher: uris::AESGCM192_URL,
checksum: false,
key_size: None,
..PwOpts::default()
})
),
expect: S2Expect::PerEntryLatch {
check: |e| {
assert_eq!(e.cipher, Cipher::AesGcmW3c);
assert_eq!(e.derived_key_len, 24);
},
},
pre: None,
},
Row {
name: "key-size 256 is sal_Int32 not u8",
body: format!(
"{}{}",
root_row("1.2", MIME_TEXT),
file_entry(PwOpts {
key_size: Some(256),
..PwOpts::default()
})
),
expect: S2Expect::PerEntryLatch {
check: |e| assert_eq!(e.derived_key_len, 256),
},
pre: None,
},
Row {
name: "argon2 iterations +3",
body: format!(
"{}{}",
root_row("1.2", MIME_TEXT),
file_entry(PwOpts {
cipher: uris::AESGCM256_URL,
checksum: false,
kdf: uris::ARGON2ID_URL,
argon2: Some((3, 65536, 4)),
iteration_count: None,
..PwOpts::default()
})
.replace(
"manifest:argon2-iterations=\"3\"",
"manifest:argon2-iterations=\"+3\""
)
),
expect: S2Expect::PerEntryLatch {
check: |e| match &e.kdf {
Kdf::Argon2id { t, m, p, .. } => {
assert_eq!((*t, *m, *p), (3, 65536, 4));
}
other => panic!("{other:?}"),
},
},
pre: None,
},
Row {
name: "missing iteration-count, PBKDF2",
body: format!(
"{}{}",
root_row("1.2", MIME_TEXT),
file_entry(PwOpts {
iteration_count: None,
..PwOpts::default()
})
),
expect: S2Expect::PerEntryLatch {
check: |e| match &e.kdf {
Kdf::Pbkdf2 { iterations, .. } => assert_eq!(*iterations, 0),
other => panic!("expected PBKDF2 with 0 iterations, got {other:?}"),
},
},
pre: None,
},
Row {
name: "slash file-entry applies root version and media-type",
body: format!(
"{}{}",
root_row("1.2", MIME_TEXT),
file_entry(PwOpts::default())
),
expect: S2Expect::PerEntryLatch { check: |_| {} },
pre: None,
},
];
for row in rows {
let manifest = manifest_wrap(Some("1.2"), &row.body);
if let Some(pre) = row.pre {
pre(&manifest);
}
let class = classify_pkg(&manifest, &s2_min_zip());
match row.expect {
S2Expect::Plain => {
assert_eq!(class.mode, Mode::Plain, "{}", row.name);
assert!(!class.package_encrypted, "{}", row.name);
assert!(class.encrypted_entries.is_empty(), "{}", row.name);
}
S2Expect::PerEntryLatch { check } => {
assert_eq!(class.mode, Mode::PerEntry, "{}", row.name);
assert!(class.package_encrypted, "{}", row.name);
let entry = class.common.as_ref().expect(row.name);
check(entry);
if row.name == "slash file-entry applies root version and media-type" {
assert_eq!(class.odf_version.as_deref(), Some("1.2"));
assert_eq!(class.media_type.as_deref(), Some(MIME_TEXT));
}
}
}
}
}
#[test]
fn s2_pictures_folder_resolves_without_zip_dir_entry() {
let body = format!(
"{}{}{}",
root_row("1.2", MIME_TEXT),
plain_row("Pictures/", "application/vnd.oasis.opendocument.text"),
file_entry(PwOpts::default())
);
let bytes = zip_with(&[
("mimetype", MIME_TEXT.as_bytes()),
(
"META-INF/manifest.xml",
manifest_wrap(Some("1.2"), &body).as_bytes(),
),
("content.xml", b"x"),
("Pictures/photo.png", b"png"),
]);
let names: Vec<String> = {
let mut archive = zip::ZipArchive::new(Cursor::new(&bytes)).unwrap();
(0..archive.len())
.map(|i| archive.by_index(i).unwrap().name().unwrap().to_string())
.collect()
};
assert!(
!names.iter().any(|n| n == "Pictures/" || n == "Pictures"),
"fixture must not include an explicit Pictures/ zip directory entry"
);
let class = classify(&bytes).expect("Pictures/ row must resolve");
assert_eq!(class.mode, Mode::PerEntry);
assert_eq!(class.odf_version.as_deref(), Some("1.2"));
assert!(
class.has_unexpected_streams,
"Pictures/photo.png is in the zip but not the manifest"
);
}
#[test]
fn s2_first_wins_latch_encrypted_package_before_content() {
let body = format!(
"{}{}{}",
file_entry(PwOpts {
path: "encrypted-package",
media_type: MIME_TEXT,
..PwOpts::default()
}),
file_entry(PwOpts::default()),
root_row("1.2", MIME_TEXT)
);
let class = classify_pkg(
&manifest_wrap(Some("1.2"), &body),
&[
("mimetype", MIME_TEXT.as_bytes()),
("encrypted-package", b"inner"),
("content.xml", b"x"),
],
);
assert_eq!(class.mode, Mode::Wholesome);
assert_eq!(
class.common.as_ref().map(|e| e.path.as_str()),
Some("encrypted-package")
);
}
#[test]
fn s2_first_wins_latch_content_before_encrypted_package() {
let body = format!(
"{}{}",
file_entry(PwOpts::default()),
file_entry(PwOpts {
path: "encrypted-package",
media_type: MIME_TEXT,
..PwOpts::default()
})
);
let class = classify_pkg(
&manifest_wrap(Some("1.2"), &body),
&[
("mimetype", MIME_TEXT.as_bytes()),
("encrypted-package", b"inner"),
("content.xml", b"x"),
],
);
assert_eq!(class.mode, Mode::Wholesome);
assert_eq!(
class.common.as_ref().map(|e| e.path.as_str()),
Some("content.xml")
);
}
fn wholesome_body(include_slash: bool) -> String {
let mut body = String::new();
if include_slash {
body.push_str(&root_row("1.3", MIME_TEXT));
}
body.push_str(&file_entry(PwOpts {
path: "encrypted-package",
media_type: MIME_TEXT,
cipher: uris::AESGCM256_URL,
checksum: false,
kdf: uris::ARGON2ID_URL,
argon2: Some((3, 65536, 4)),
iteration_count: None,
start_key: Some(uris::SHA256_URL),
..PwOpts::default()
}));
body
}
#[test]
fn s3_zip_member_and_complete_bag_is_wholesome() {
let class = classify_pkg(
&manifest_wrap(Some("1.3"), &wholesome_body(false)),
&[
("mimetype", MIME_TEXT.as_bytes()),
("encrypted-package", b"inner"),
],
);
assert_eq!(class.mode, Mode::Wholesome);
assert!(class.package_encrypted);
assert!(class.zip_has_encrypted_package);
assert_eq!(
class.common.as_ref().map(|e| e.path.as_str()),
Some("encrypted-package")
);
}
#[test]
fn s3_xml_only_encrypted_package_is_not_wholesome() {
let class = classify_pkg(
&manifest_wrap(Some("1.3"), &wholesome_body(false)),
&[("mimetype", MIME_TEXT.as_bytes())],
);
assert_ne!(class.mode, Mode::Wholesome);
assert!(!class.zip_has_encrypted_package);
assert!(!class.package_encrypted);
assert!(class.encrypted_entries.is_empty());
}
#[test]
fn s3_package_version_copied_onto_first_entry_when_slash_omitted() {
let xml = manifest_wrap(Some("1.3"), &wholesome_body(false));
let bags = parse_manifest_for_test(&xml);
assert_eq!(bags[0].full_path, "encrypted-package");
assert_eq!(bags[0].version.as_deref(), Some("1.3"));
let class = classify_pkg(
&xml,
&[
("mimetype", MIME_TEXT.as_bytes()),
("encrypted-package", b"inner"),
],
);
assert_eq!(class.odf_version.as_deref(), Some("1.3"));
}
#[test]
fn s3_mimetype_fallback_only_with_vnd_prefix() {
let xml = manifest_wrap(Some("1.3"), &wholesome_body(false));
let present = classify_pkg(
&xml,
&[
("mimetype", MIME_TEXT.as_bytes()),
("encrypted-package", b"inner"),
],
);
assert_eq!(present.odf_version.as_deref(), Some("1.3"));
assert_eq!(present.media_type.as_deref(), Some(MIME_TEXT));
let missing = classify_pkg(&xml, &[("encrypted-package", b"inner")]);
assert!(missing.odf_version.is_none());
assert!(missing.media_type.is_none());
let wrong = classify_pkg(
&xml,
&[
("mimetype", MIME_WRONG.as_bytes()),
("encrypted-package", b"inner"),
],
);
assert!(wrong.odf_version.is_none());
assert!(wrong.media_type.is_none());
}
#[test]
fn s4_extra_root_stream_version_12_is_fatal() {
let body = format!(
"{}{}{}",
root_row("1.2", MIME_TEXT),
file_entry(PwOpts::default()),
plain_row("content.xml", "text/xml")
);
let class = classify_pkg(
&manifest_wrap(Some("1.2"), &body),
&[
("mimetype", MIME_TEXT.as_bytes()),
("content.xml", b"x"),
("extra.bin", b"nope"),
],
);
assert!(class.has_unexpected_streams);
assert!(class.odf12_fatal);
}
#[test]
fn s4_extra_root_stream_pre_12_is_not_fatal() {
let body = format!(
"{}{}",
root_row("1.1", MIME_TEXT),
file_entry(PwOpts::default())
);
let class = classify_pkg(
&manifest_wrap(Some("1.1"), &body),
&[
("mimetype", MIME_TEXT.as_bytes()),
("content.xml", b"x"),
("extra.bin", b"nope"),
],
);
assert!(class.has_unexpected_streams);
assert!(!class.odf12_fatal);
}
#[test]
fn s4_incomplete_encrypted_package_member_still_uses_wholesome_allow_list() {
let body = file_entry(PwOpts {
path: "encrypted-package",
media_type: MIME_TEXT,
size: None,
cipher: uris::AESGCM256_URL,
checksum: false,
kdf: uris::ARGON2ID_URL,
argon2: Some((3, 65536, 4)),
iteration_count: None,
..PwOpts::default()
});
let extra_listed = format!(
"{}{}",
body,
plain_row("extra.bin", "application/octet-stream")
);
let class = classify_pkg(
&manifest_wrap(Some("1.3"), &extra_listed),
&[
("mimetype", MIME_TEXT.as_bytes()),
("encrypted-package", b"inner"),
("extra.bin", b"listed"),
],
);
assert_ne!(class.mode, Mode::Wholesome);
assert!(class.zip_has_encrypted_package);
assert!(
class.has_unexpected_streams,
"wholesome allow-list flags extra.bin even when listed"
);
}
#[test]
fn s4_xml_only_encrypted_package_uses_non_wholesome_scan() {
let body = format!(
"{}{}",
file_entry(PwOpts {
path: "encrypted-package",
media_type: MIME_TEXT,
cipher: uris::AESGCM256_URL,
checksum: false,
kdf: uris::ARGON2ID_URL,
argon2: Some((3, 65536, 4)),
iteration_count: None,
..PwOpts::default()
}),
plain_row("extra.bin", "application/octet-stream")
);
let class = classify_pkg(
&manifest_wrap(Some("1.3"), &body),
&[("mimetype", MIME_TEXT.as_bytes()), ("extra.bin", b"listed")],
);
assert!(!class.zip_has_encrypted_package);
assert_ne!(class.mode, Mode::Wholesome);
assert!(
!class.has_unexpected_streams,
"non-wholesome scan accepts a listed extra.bin"
);
}
#[test]
fn s4_nested_mimetype_is_not_exempt() {
let body = format!(
"{}{}",
root_row("1.2", MIME_TEXT),
file_entry(PwOpts::default())
);
let class = classify_pkg(
&manifest_wrap(Some("1.2"), &body),
&[
("mimetype", MIME_TEXT.as_bytes()),
("content.xml", b"x"),
("foo/mimetype", b"nested"),
],
);
assert!(class.has_unexpected_streams);
assert!(class.odf12_fatal);
}
fn loext_encrypted_key(wrap_uri: &str) -> String {
format!(
r#" <loext:encrypted-key>
<loext:encryption-method loext:PGPAlgorithm="{wrap_uri}"/>
<loext:KeyInfo>
<loext:PGPData>
<loext:PGPKeyID>{B64}</loext:PGPKeyID>
<loext:PGPKeyPacket>{B64}</loext:PGPKeyPacket>
</loext:PGPData>
</loext:KeyInfo>
<loext:CipherData>
<loext:CipherValue>{B64}</loext:CipherValue>
</loext:CipherData>
</loext:encrypted-key>
"#
)
}
fn manifest_encrypted_key(wrap_uri: &str) -> String {
format!(
r#" <manifest:encrypted-key>
<manifest:encryption-method manifest:PGPAlgorithm="{wrap_uri}"/>
<manifest:keyinfo>
<manifest:PGPData>
<manifest:PGPKeyID>{B64}</manifest:PGPKeyID>
<manifest:PGPKeyPacket>{B64}</manifest:PGPKeyPacket>
</manifest:PGPData>
</manifest:keyinfo>
<manifest:CipherData>
<manifest:CipherValue>{B64}</manifest:CipherValue>
</manifest:CipherData>
</manifest:encrypted-key>
"#
)
}
fn pgp_row(path: &str, size: i64, key_size: Option<i32>, cipher: &str) -> String {
let key_size = match key_size {
Some(n) => format!(" manifest:key-size=\"{n}\""),
None => String::new(),
};
format!(
r#" <manifest:file-entry manifest:full-path="{path}" manifest:media-type="text/xml" manifest:size="{size}">
<manifest:encryption-data>
<manifest:algorithm manifest:algorithm-name="{cipher}" manifest:initialisation-vector="{B64}"/>
<manifest:key-derivation manifest:key-derivation-name="PGP"{key_size}/>
</manifest:encryption-data>
</manifest:file-entry>
"#
)
}
#[test]
fn s5_loext_tree_classifies_as_pgp() {
let xml = manifest_wrap(
Some("1.3"),
&format!(
r#" <manifest:file-entry manifest:full-path="content.xml" manifest:media-type="text/xml" manifest:size="100">
{}
<manifest:encryption-data>
<manifest:algorithm manifest:algorithm-name="{}" manifest:initialisation-vector="{B64}"/>
<manifest:key-derivation manifest:key-derivation-name="PGP"/>
</manifest:encryption-data>
</manifest:file-entry>
"#,
loext_encrypted_key(uris::PGP_WRAP_URI),
uris::AESGCM256_URL
),
);
let class = classify_pkg(
&xml,
&[("mimetype", MIME_TEXT.as_bytes()), ("content.xml", b"x")],
);
assert_eq!(class.mode, Mode::PerEntry);
assert!(matches!(
class.common.as_ref().map(|e| &e.kdf),
Some(Kdf::PgpRsaOaepMgf1p)
));
}
#[test]
fn s5_manifest_tree_classifies_as_pgp_without_version_gate() {
let xml = manifest_wrap(
Some("1.2"),
&format!(
"{}{}",
manifest_encrypted_key(uris::PGP_WRAP_URI),
pgp_row("content.xml", 100, None, uris::AESGCM256_URL)
),
);
let class = classify_pkg(
&xml,
&[("mimetype", MIME_TEXT.as_bytes()), ("content.xml", b"x")],
);
assert_eq!(class.mode, Mode::PerEntry);
assert!(matches!(
class.common.as_ref().map(|e| &e.kdf),
Some(Kdf::PgpRsaOaepMgf1p)
));
}
#[test]
fn s5_wrong_wrap_uri_discards_key_and_suppresses_keyinfo() {
let xml = manifest_wrap(
Some("1.3"),
&format!(
r#" <manifest:file-entry manifest:full-path="content.xml" manifest:media-type="text/xml" manifest:size="100">
{}
<manifest:encryption-data>
<manifest:algorithm manifest:algorithm-name="{}" manifest:initialisation-vector="{B64}"/>
<manifest:key-derivation manifest:key-derivation-name="PGP"/>
</manifest:encryption-data>
</manifest:file-entry>
"#,
loext_encrypted_key("http://www.w3.org/2001/04/xmlenc#rsa-1_5"),
uris::AESGCM256_URL
),
);
let bags = parse_manifest_for_test(&xml);
assert!(
bags[0].key_info.is_none(),
"malformed wrap suppresses package KeyInfo"
);
assert_ne!(bags[0].kdf, Some(KdfId::PgpRsaOaepMgf1p));
let class = classify_pkg(
&xml,
&[("mimetype", MIME_TEXT.as_bytes()), ("content.xml", b"x")],
);
assert!(class.encrypted_entries.is_empty());
assert!(!class.package_encrypted);
}
#[test]
fn s5_pgp_derived_key_len_ignores_lying_key_size() {
let xml = manifest_wrap(
Some("1.3"),
&format!(
"{}{}",
manifest_encrypted_key(uris::PGP_WRAP_URI),
pgp_row("content.xml", 100, Some(16), uris::AESGCM256_URL)
),
);
let gcm = classify_pkg(
&xml,
&[("mimetype", MIME_TEXT.as_bytes()), ("content.xml", b"x")],
);
assert_eq!(gcm.common.as_ref().unwrap().derived_key_len, 32);
assert_eq!(gcm.common.as_ref().unwrap().cipher, Cipher::AesGcmW3c);
let cbc_row = format!(
r#" <manifest:file-entry manifest:full-path="content.xml" manifest:media-type="text/xml" manifest:size="100">
<manifest:encryption-data manifest:checksum-type="{}" manifest:checksum="{B64}">
<manifest:algorithm manifest:algorithm-name="{}" manifest:initialisation-vector="{B64}"/>
<manifest:key-derivation manifest:key-derivation-name="PGP" manifest:key-size="16"/>
</manifest:encryption-data>
</manifest:file-entry>
"#,
uris::SHA1_1K_NAME,
uris::AES256_URL
);
let xml = manifest_wrap(
Some("1.3"),
&format!("{}{}", manifest_encrypted_key(uris::PGP_WRAP_URI), cbc_row),
);
let cbc = classify_pkg(
&xml,
&[("mimetype", MIME_TEXT.as_bytes()), ("content.xml", b"x")],
);
assert_eq!(cbc.common.as_ref().unwrap().derived_key_len, 32);
assert_eq!(cbc.common.as_ref().unwrap().cipher, Cipher::AesCbcW3c);
}
#[test]
fn s6_wholesome_gcm_argon2() {
let class = classify(&load_golden("lo-wholesome-gcm-argon2.odt")).expect("golden");
assert_eq!(class.mode, Mode::Wholesome);
assert!(class.package_encrypted);
assert!(class.zip_has_encrypted_package);
let common = class.common.as_ref().expect("latch");
assert_eq!(common.path, "encrypted-package");
assert_eq!(common.cipher, Cipher::AesGcmW3c);
assert!(matches!(
common.kdf,
Kdf::Argon2id {
t: 3,
m: 65536,
p: 4,
..
}
));
assert_eq!(common.start_key, StartKeyAlg::Sha256);
assert_eq!(common.checksum, Checksum::None);
assert_eq!(common.derived_key_len, 32);
assert_eq!(class.odf_version.as_deref(), Some("1.4"));
assert!(!class.has_unexpected_streams);
assert!(!class.odf12_fatal);
assert_eq!(class.encrypted_entries.len(), 1);
assert_eq!(common.size, 6977);
assert!(!common.iv.is_empty());
}
#[test]
fn s6_legacy_aes_cbc() {
let class = classify(&load_golden("lo-legacy-aes-cbc.odt")).expect("golden");
assert_eq!(class.mode, Mode::PerEntry);
assert!(class.package_encrypted);
assert!(!class.zip_has_encrypted_package);
let common = class.common.as_ref().expect("latch");
assert_eq!(common.path, "content.xml");
assert_eq!(common.cipher, Cipher::AesCbcW3c);
assert!(matches!(
common.kdf,
Kdf::Pbkdf2 {
iterations: 100000,
..
}
));
assert_eq!(common.start_key, StartKeyAlg::Sha256);
assert!(matches!(common.checksum, Checksum::Sha256_1K(_)));
assert_eq!(common.derived_key_len, 32);
assert_eq!(class.odf_version.as_deref(), Some("1.2"));
assert!(!class.has_unexpected_streams);
assert!(!class.odf12_fatal);
assert!(class.encrypted_entries.len() >= 2);
assert_eq!(common.size, 4251);
assert!(!common.iv.is_empty());
}
#[test]
fn s6_blowfish_pbkdf2() {
let class = classify(&load_golden("aoo-blowfish-pbkdf2.odt")).expect("golden");
assert_eq!(class.mode, Mode::PerEntry);
assert!(class.package_encrypted);
assert!(!class.zip_has_encrypted_package);
let common = class.common.as_ref().expect("latch");
assert_eq!(common.path, "content.xml");
assert_eq!(common.cipher, Cipher::BlowfishCfb8);
assert!(matches!(
common.kdf,
Kdf::Pbkdf2 {
iterations: 100000,
..
}
));
assert_eq!(common.start_key, StartKeyAlg::Sha1);
assert!(matches!(common.checksum, Checksum::Sha1_1K(_)));
assert_eq!(common.derived_key_len, 16);
assert!(!class.has_unexpected_streams);
assert!(!class.odf12_fatal);
assert!(class.odf_version.is_none() || class.odf_version.as_deref() == Some(""));
assert!(class.encrypted_entries.len() >= 2);
assert!(!common.iv.is_empty());
assert!(common.size > 0);
}
fn classify_err(bytes: &[u8]) -> DetectError {
classify(bytes).expect_err("expected classify to refuse")
}
#[test]
fn hostile_media_type_cannot_flood_the_diagnostic() {
let padded = format!("{MIME_TEXT}{}", "A".repeat(512 * 1024));
let xml = manifest_wrap(Some("1.2"), &root_row("1.2", &padded));
let bytes = zip_with(&[
("mimetype", MIME_TEXT.as_bytes()),
("META-INF/manifest.xml", xml.as_bytes()),
("content.xml", b"x"),
]);
let err = classify_err(&bytes);
assert!(matches!(err, DetectError::Inconsistent(_)));
let msg = err.to_string();
assert!(
msg.len() < 512,
"diagnostic grew with attacker input: {} chars",
msg.len()
);
assert!(
!msg.contains(&"A".repeat(DIAGNOSTIC_ELISION + 1)),
"more than the elision bound of the hostile value survived"
);
assert!(
msg.contains("bytes elided]"),
"elision should say it happened, and how much was cut: {msg}"
);
}
#[test]
fn elision_cuts_on_a_character_boundary() {
let straddling = "ä".repeat(DIAGNOSTIC_ELISION);
let xml = manifest_wrap(Some("1.2"), &root_row("1.2", &straddling));
let bytes = zip_with(&[
("mimetype", MIME_TEXT.as_bytes()),
("META-INF/manifest.xml", xml.as_bytes()),
("content.xml", b"x"),
]);
let msg = classify_err(&bytes).to_string();
assert!(msg.contains("bytes elided]"), "{msg}");
}
#[test]
fn not_zip_is_detect_error() {
assert!(matches!(classify_err(b"not a zip"), DetectError::NotZip));
}
#[test]
fn data_before_the_zip_is_not_zip_as_in_libreoffice() {
for name in ["lo-wholesome-gcm-argon2.odt", "lo-legacy-aes-cbc.odt"] {
let golden = load_golden(name);
assert!(classify(&golden).unwrap().package_encrypted, "{name}");
let mut prefixed = b"#!/bin/sh\nexit 0\n".to_vec();
prefixed.extend_from_slice(&golden);
assert!(
zip::ZipArchive::new(std::io::Cursor::new(&prefixed)).is_ok(),
"{name}: Detect must still open it, or this tests nothing"
);
assert!(
matches!(classify_err(&prefixed), DetectError::NotZip),
"{name}"
);
}
}
#[test]
fn missing_manifest_is_detect_error() {
let bytes = zip_with(&[("mimetype", MIME_TEXT.as_bytes())]);
assert!(matches!(classify_err(&bytes), DetectError::MissingManifest));
}
#[test]
fn invalid_zip_entry_name_is_refused() {
let bytes = zip_with(&[
("mimetype", MIME_TEXT.as_bytes()),
("META-INF/manifest.xml", UNENCRYPTED_MANIFEST.as_bytes()),
("a/../content.xml", b"x"),
]);
assert!(matches!(classify_err(&bytes), DetectError::Zip(_)));
}
#[test]
fn s3_slash_first_and_versionless_gets_package_version() {
let slash = format!(
r#" <manifest:file-entry manifest:full-path="/" manifest:media-type="{MIME_TEXT}"/>
"#
);
let body = format!(
"{}{}{}",
slash,
file_entry(PwOpts::default()),
plain_row("content.xml", "text/xml")
);
let class = classify_pkg(
&manifest_wrap(Some("1.2"), &body),
&[
("mimetype", MIME_TEXT.as_bytes()),
("content.xml", b"x"),
("extra.bin", b"nope"),
],
);
assert_eq!(class.odf_version.as_deref(), Some("1.2"));
assert!(class.has_unexpected_streams);
assert!(class.odf12_fatal);
}
#[test]
fn s4_extra_root_stream_empty_root_version_is_not_fatal() {
let xml = manifest_wrap(Some("1.3"), &wholesome_body(false));
let class = classify_pkg(
&xml,
&[("encrypted-package", b"inner"), ("extra.bin", b"nope")],
);
assert!(class.odf_version.is_none());
assert!(class.has_unexpected_streams);
assert!(!class.odf12_fatal);
}
#[test]
fn s5_malformed_key_poisons_only_first_entry() {
let poisoned = format!(
r#" <manifest:file-entry manifest:full-path="styles.xml" manifest:media-type="text/xml" manifest:size="100">
{}
<manifest:encryption-data>
<manifest:algorithm manifest:algorithm-name="{}" manifest:initialisation-vector="{B64}"/>
<manifest:key-derivation manifest:key-derivation-name="PGP"/>
</manifest:encryption-data>
</manifest:file-entry>
"#,
loext_encrypted_key("http://www.w3.org/2001/04/xmlenc#rsa-1_5"),
uris::AESGCM256_URL
);
let body = format!("{}{}", poisoned, file_entry(PwOpts::default()));
let class = classify_pkg(
&manifest_wrap(Some("1.3"), &body),
&[
("mimetype", MIME_TEXT.as_bytes()),
("styles.xml", b"s"),
("content.xml", b"x"),
],
);
assert!(class.package_encrypted);
assert_eq!(
class.common.as_ref().map(|e| e.path.as_str()),
Some("content.xml")
);
}
#[test]
fn s5_pgp_start_key_clamps_to_sha256() {
let xml = manifest_wrap(
Some("1.3"),
&format!(
r#" <manifest:file-entry manifest:full-path="content.xml" manifest:media-type="text/xml" manifest:size="100">
{}
<manifest:encryption-data>
<manifest:algorithm manifest:algorithm-name="{}" manifest:initialisation-vector="{B64}"/>
<manifest:start-key-generation manifest:start-key-generation-name="{}"/>
<manifest:key-derivation manifest:key-derivation-name="PGP"/>
</manifest:encryption-data>
</manifest:file-entry>
"#,
loext_encrypted_key(uris::PGP_WRAP_URI),
uris::AESGCM256_URL,
uris::SHA1_NAME
),
);
let class = classify_pkg(
&xml,
&[("mimetype", MIME_TEXT.as_bytes()), ("content.xml", b"x")],
);
assert_eq!(
class.common.as_ref().map(|e| e.start_key),
Some(StartKeyAlg::Sha256)
);
}
#[test]
fn derived_key_size_resets_per_encryption_data() {
let first = file_entry(PwOpts {
key_size: Some(32),
..PwOpts::default()
});
let second = file_entry(PwOpts {
path: "styles.xml",
key_size: None,
kdf_before_algorithm: true,
..PwOpts::default()
});
let class = classify_pkg(
&manifest_wrap(Some("1.2"), &format!("{}{}", first, second)),
&[
("mimetype", MIME_TEXT.as_bytes()),
("content.xml", b"x"),
("styles.xml", b"s"),
],
);
let styles = class
.encrypted_entries
.iter()
.find(|e| e.path == "styles.xml")
.expect("styles");
assert_eq!(styles.derived_key_len, 16);
}
#[test]
fn typo_root_element_still_imports_file_entries() {
let body = file_entry(PwOpts::default());
let xml = format!(
r#"<?xml version="1.0" encoding="UTF-8"?>
<manifest:manifest-typo xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0">
{body}
</manifest:manifest-typo>
"#
);
let class = classify_pkg(
&xml,
&[("mimetype", MIME_TEXT.as_bytes()), ("content.xml", b"x")],
);
assert_eq!(class.mode, Mode::PerEntry);
assert!(class.package_encrypted);
}
#[test]
fn truncated_manifest_is_plain_not_partial() {
let body = file_entry(PwOpts::default());
let xml = manifest_wrap(Some("1.3"), &body);
let truncated = &xml[..xml.len().saturating_sub(20)];
let class = classify_pkg(
truncated,
&[("mimetype", MIME_TEXT.as_bytes()), ("content.xml", b"x")],
);
assert_eq!(class.mode, Mode::Plain);
assert!(!class.package_encrypted);
}
#[test]
fn nested_encrypted_package_row_is_not_wholesome() {
let body = format!(
"{}{}",
file_entry(PwOpts {
path: "encrypted-package",
media_type: "text/plain",
size: None,
cipher: uris::AESGCM256_URL,
checksum: false,
kdf: uris::ARGON2ID_URL,
argon2: Some((3, 65536, 4)),
iteration_count: None,
..PwOpts::default()
}),
file_entry(PwOpts {
path: "Object 1/encrypted-package",
media_type: MIME_TEXT,
cipher: uris::AESGCM256_URL,
checksum: false,
kdf: uris::ARGON2ID_URL,
argon2: Some((3, 65536, 4)),
iteration_count: None,
..PwOpts::default()
})
);
let class = classify_pkg(
&manifest_wrap(Some("1.3"), &body),
&[
("mimetype", MIME_TEXT.as_bytes()),
("encrypted-package", b"inner"),
("Object 1/encrypted-package", b"nested"),
],
);
assert_ne!(class.mode, Mode::Wholesome);
assert!(class.zip_has_encrypted_package);
}
#[test]
fn later_bare_slash_row_clears_root_version() {
let body = format!(
"{}{}{}",
root_row("1.2", MIME_TEXT),
r#" <manifest:file-entry manifest:full-path="/"/>
"#,
file_entry(PwOpts::default())
);
let class = classify_pkg(
&manifest_wrap(Some("1.2"), &body),
&[("content.xml", b"x"), ("stray.txt", b"x")],
);
assert!(class.has_unexpected_streams);
assert!(!class.odf12_fatal);
assert!(class.odf_version.is_none() || class.odf_version.as_deref() == Some(""));
}
#[test]
fn double_slash_member_resolves_and_latches() {
let body = file_entry(PwOpts {
path: "a//content.xml",
..PwOpts::default()
});
let class = classify_pkg(
&manifest_wrap(Some("1.2"), &body),
&[("mimetype", MIME_TEXT.as_bytes()), ("a//content.xml", b"x")],
);
assert!(class.package_encrypted);
assert_eq!(class.mode, Mode::PerEntry);
}
#[test]
fn second_encryption_data_rereads_checksum() {
let xml = manifest_wrap(
Some("1.2"),
&format!(
r#" <manifest:file-entry manifest:full-path="content.xml" manifest:media-type="text/xml" manifest:size="100">
<manifest:encryption-data manifest:checksum-type="{}" manifest:checksum="{B64}">
<manifest:algorithm manifest:algorithm-name="{}" manifest:initialisation-vector="{B64}"/>
<manifest:key-derivation manifest:key-derivation-name="PBKDF2" manifest:salt="{B64}" manifest:iteration-count="1" manifest:key-size="32"/>
</manifest:encryption-data>
<manifest:encryption-data manifest:checksum-type="bogus" manifest:checksum="QUJDRA==">
</manifest:encryption-data>
</manifest:file-entry>
"#,
uris::SHA1_1K_NAME,
uris::AES256_URL
),
);
let class = classify_pkg(
&xml,
&[("mimetype", MIME_TEXT.as_bytes()), ("content.xml", b"x")],
);
match class.common.as_ref().map(|e| &e.checksum) {
Some(Checksum::Sha1_1K(d)) => assert_eq!(d, b"ABCD"),
other => panic!("{other:?}"),
}
}
#[test]
fn deep_subtree_does_not_drop_later_entries() {
let mut deep = String::from(
" <manifest:file-entry manifest:full-path=\"junk\" manifest:media-type=\"text/xml\" \
xmlns:m2=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\">\n",
);
for _ in 0..40 {
deep.push_str("<m2:foo>");
}
for _ in 0..40 {
deep.push_str("</m2:foo>");
}
deep.push_str(" </manifest:file-entry>\n");
let body = format!(
"{}{}{}",
root_row("1.2", MIME_TEXT),
deep,
file_entry(PwOpts::default())
);
let class = classify_pkg(
&manifest_wrap(Some("1.2"), &body),
&[
("mimetype", MIME_TEXT.as_bytes()),
("content.xml", b"x"),
("junk", b"x"),
],
);
assert_eq!(class.mode, Mode::PerEntry);
assert!(class.package_encrypted);
assert_eq!(class.encrypted_entries.len(), 1);
assert_eq!(class.odf_version.as_deref(), Some("1.2"));
}
#[test]
fn leading_slash_stream_row_is_not_applied() {
let body = format!(
"{}{}",
root_row("1.2", MIME_TEXT),
file_entry(PwOpts {
path: "/content.xml",
..PwOpts::default()
})
);
let class = classify_pkg(
&manifest_wrap(Some("1.2"), &body),
&[("mimetype", MIME_TEXT.as_bytes()), ("content.xml", b"x")],
);
assert_eq!(class.mode, Mode::Plain);
assert!(!class.package_encrypted);
assert!(class.encrypted_entries.is_empty());
}
#[test]
fn pictures_folder_row_poisons_nested_content_xml_onto_root() {
let body = format!(
r#" <manifest:file-entry manifest:full-path="Pictures/" manifest:version="1.2" manifest:media-type="image/"/>
{}"#,
file_entry(PwOpts {
path: "Pictures/content.xml",
..PwOpts::default()
})
);
let class = classify_pkg(
&manifest_wrap(None, &body),
&[
("content.xml", b"plain-root"),
("Pictures/album/photo.png", b"png"),
],
);
assert_eq!(class.mode, Mode::PerEntry);
assert!(class.package_encrypted);
assert_eq!(
class.common.as_ref().map(|e| e.path.as_str()),
Some("content.xml")
);
assert_eq!(class.encrypted_entries.len(), 1);
assert_eq!(class.encrypted_entries[0].path, "content.xml");
assert!(class.odf_version.is_none());
assert!(class.media_type.is_none());
}
#[test]
fn nested_row_without_a_folder_row_does_not_latch() {
let class = classify_pkg(
&manifest_wrap(
None,
&file_entry(PwOpts {
path: "Pictures/content.xml",
..PwOpts::default()
}),
),
&[
("content.xml", b"plain-root"),
("Pictures/album/photo.png", b"png"),
],
);
assert_eq!(class.mode, Mode::Plain);
assert!(!class.package_encrypted);
assert!(class.common.is_none());
assert!(class.encrypted_entries.is_empty());
}
#[test]
fn pictures_photo_insert_then_folder_row_does_not_poison() {
let body = format!(
r#" <manifest:file-entry manifest:full-path="Pictures/" manifest:version="1.2" manifest:media-type="image/"/>
{}"#,
file_entry(PwOpts {
path: "Pictures/content.xml",
..PwOpts::default()
})
);
let class = classify_pkg(
&manifest_wrap(None, &body),
&[
("content.xml", b"plain-root"),
("Pictures/photo.png", b"png"),
],
);
assert_eq!(class.mode, Mode::Plain);
assert!(!class.package_encrypted);
assert!(class.common.is_none());
assert!(class.encrypted_entries.is_empty());
assert!(class.odf_version.is_none());
assert!(class.media_type.is_none());
}
#[test]
fn every_detect_error_variant_is_accounted_for_in_the_cli_exit_map() {
fn accounted_for(e: &DetectError) {
match e {
DetectError::NotZip => (),
DetectError::MissingManifest => (),
DetectError::Zip(_) => (),
DetectError::Inconsistent(_) => (),
DetectError::Io(_) => (),
}
}
accounted_for(&DetectError::NotZip);
}
fn two_row_wholesome(first_is_modern: bool) -> Vec<u8> {
let (a_cipher, a_kdf, b_cipher, b_kdf) = if first_is_modern {
(uris::AESGCM256_URL, "argon2", uris::AES256_URL, "pbkdf2")
} else {
(uris::AES256_URL, "pbkdf2", uris::AESGCM256_URL, "argon2")
};
let row = |path: &str, media: &str, cipher: &str, kdf: &str| {
let (kdf_attrs, checksum) = if kdf == "argon2" {
(
format!(
r#"manifest:key-derivation-name="{}" manifest:salt="{B64}" manifest:argon2-iterations="3" manifest:argon2-memory="65536" manifest:argon2-lanes="4" manifest:key-size="32""#,
uris::ARGON2ID_URL
),
String::new(),
)
} else {
(
format!(
r#"manifest:key-derivation-name="{}" manifest:salt="{B64}" manifest:iteration-count="1024" manifest:key-size="32""#,
uris::PBKDF2_NAME
),
format!(
r#" manifest:checksum-type="{}" manifest:checksum="{B64}""#,
uris::SHA1_1K_NAME
),
)
};
format!(
r#" <manifest:file-entry manifest:full-path="{path}" manifest:media-type="{media}" manifest:size="64">
<manifest:encryption-data{checksum}>
<manifest:algorithm manifest:algorithm-name="{cipher}" manifest:initialisation-vector="{B64}"/>
<manifest:start-key-generation manifest:start-key-generation-name="{}"/>
<manifest:key-derivation {kdf_attrs}/>
</manifest:encryption-data>
</manifest:file-entry>
"#,
uris::SHA256_URL
)
};
let manifest = manifest_wrap(
Some("1.4"),
&format!(
"{}{}",
row("content.xml", "text/xml", a_cipher, a_kdf),
row("encrypted-package", MIME_TEXT, b_cipher, b_kdf),
),
);
zip_with(&[
("mimetype", MIME_TEXT.as_bytes()),
("META-INF/manifest.xml", manifest.as_bytes()),
("content.xml", b"x"),
("encrypted-package", b"y"),
])
}
#[test]
fn common_can_report_a_profile_the_payload_does_not_have() {
let pkg = two_row_wholesome(true);
let class = classify(&pkg).expect("constructed package classifies");
assert_eq!(class.mode, Mode::Wholesome);
assert!(class.package_encrypted);
let latch = class
.common
.as_ref()
.expect("wholesome carries a latch row");
assert_eq!(latch.path, "content.xml");
assert_eq!(latch.cipher, Cipher::AesGcmW3c);
let payload = class
.wholesome_row()
.expect("Wholesome implies a payload row");
assert_eq!(payload.path, "encrypted-package");
assert_eq!(payload.cipher, Cipher::AesCbcW3c);
assert_ne!(
latch.cipher, payload.cipher,
"fixture must actually exercise the divergence, or this test proves nothing"
);
}
#[test]
fn the_divergence_runs_both_directions() {
let class = classify(&two_row_wholesome(false)).expect("classifies");
assert_eq!(
class.common.as_ref().expect("latch").cipher,
Cipher::AesCbcW3c
);
assert_eq!(
class.wholesome_row().expect("payload").cipher,
Cipher::AesGcmW3c
);
}
#[test]
fn wholesome_row_is_none_for_plain_and_per_entry() {
let plain = classify(&load_golden("lo-unencrypted.odt")).expect("plain");
assert_eq!(plain.mode, Mode::Plain);
assert!(plain.wholesome_row().is_none());
let per_entry = classify(&load_golden("lo-legacy-aes-cbc.odt")).expect("per-entry");
assert_eq!(per_entry.mode, Mode::PerEntry);
assert!(
per_entry.wholesome_row().is_none(),
"per-entry has no single payload row -- decrypt acts on every entry"
);
assert!(per_entry.common.is_some(), "but it still has a latch row");
}
#[test]
fn latch_and_payload_agree_on_a_real_libreoffice_package() {
let class = classify(&load_golden("lo-wholesome-gcm-argon2.odt")).expect("real wholesome");
let latch = class.common.as_ref().expect("latch");
let payload = class.wholesome_row().expect("payload");
assert_eq!(latch.path, payload.path);
assert_eq!(latch.cipher, payload.cipher);
}
fn non_encrypted_flag(body: &str, files: &[(&str, &[u8])]) -> (bool, bool) {
let manifest = manifest_wrap(Some("1.3"), body);
let class = classify_pkg(&manifest, files);
(class.package_encrypted, class.has_non_encrypted_entries)
}
#[test]
fn a_plaintext_stream_beside_an_encrypted_one_sets_the_flag() {
let body = root_row("1.3", MIME_TEXT)
+ &file_entry(PwOpts::default())
+ &plain_row("styles.xml", "text/xml");
let files: &[(&str, &[u8])] = &[("content.xml", b"x"), ("styles.xml", b"y")];
assert_eq!(non_encrypted_flag(&body, files), (true, true));
}
#[test]
fn every_listed_stream_encrypted_leaves_the_flag_clear() {
let body = root_row("1.3", MIME_TEXT)
+ &file_entry(PwOpts::default())
+ &file_entry(PwOpts {
path: "styles.xml",
..PwOpts::default()
})
+ &plain_row(
"Configurations2/",
"application/vnd.sun.xml.ui.configuration",
);
let files: &[(&str, &[u8])] = &[
("mimetype", MIME_TEXT.as_bytes()),
("content.xml", b"x"),
("styles.xml", b"y"),
("Configurations2/accelerator/current.xml", b""),
];
assert_eq!(non_encrypted_flag(&body, files), (true, false));
}
#[test]
fn a_row_naming_no_member_does_not_count() {
let body = root_row("1.3", MIME_TEXT)
+ &file_entry(PwOpts::default())
+ &plain_row("missing.xml", "text/xml");
assert_eq!(
non_encrypted_flag(&body, &[("content.xml", b"x")]),
(true, false)
);
}
#[test]
fn an_incomplete_encryption_row_counts_as_non_encrypted() {
let body = root_row("1.3", MIME_TEXT)
+ &file_entry(PwOpts::default())
+ &file_entry(PwOpts {
path: "styles.xml",
checksum: false,
..PwOpts::default()
});
let files: &[(&str, &[u8])] = &[("content.xml", b"x"), ("styles.xml", b"y")];
assert_eq!(non_encrypted_flag(&body, files), (true, true));
}
#[test]
fn a_plaintext_package_sets_the_flag_without_the_latch() {
let class = classify(&unencrypted_odt()).expect("classifies");
assert!(!class.package_encrypted);
assert!(class.has_non_encrypted_entries);
}
#[test]
fn no_encrypted_libreoffice_golden_has_non_encrypted_entries() {
let dir = crate::test_support::goldens_dir();
let mut checked = 0;
for entry in std::fs::read_dir(&dir).expect("goldens dir") {
let path = entry.expect("dir entry").path();
if path.extension().is_none_or(|x| x != "odt") {
continue;
}
let name = path.file_name().unwrap().to_string_lossy().into_owned();
let class = classify(&load_golden(&name)).expect(&name);
if class.package_encrypted {
assert!(!class.has_non_encrypted_entries, "{name}");
checked += 1;
}
}
assert!(checked > 0, "no encrypted golden found under {dir:?}");
}
fn every_fixture() -> Vec<std::path::PathBuf> {
let goldens = crate::test_support::goldens_dir();
let mut out = Vec::new();
for dir in ["goldens", "ladder", "artifacts"] {
let dir = goldens.with_file_name(dir);
let before = out.len();
for entry in std::fs::read_dir(&dir).expect("fixture dir") {
let path = entry.expect("dir entry").path();
if path.extension().is_some_and(|x| x == "odt") {
out.push(path);
}
}
assert!(out.len() > before, "no .odt under {dir:?}");
}
out
}
#[test]
fn classify_reader_agrees_with_classify_on_every_fixture() {
for path in every_fixture() {
let bytes = std::fs::read(&path).unwrap();
let from_file = classify_reader(std::fs::File::open(&path).unwrap());
match (classify(&bytes), from_file) {
(Ok(a), Ok(b)) => assert_eq!(a, b, "{path:?}"),
(a, b) => panic!("{path:?}: slice {a:?}, reader {b:?}"),
}
}
}
#[derive(Debug)]
struct Cancelled;
impl std::fmt::Display for Cancelled {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("cancelled by the caller")
}
}
impl std::error::Error for Cancelled {}
struct FailingReader<'a> {
inner: Cursor<&'a [u8]>,
calls: usize,
fail_at: usize,
}
impl FailingReader<'_> {
fn tick(&mut self) -> std::io::Result<()> {
self.calls += 1;
if self.calls >= self.fail_at {
return Err(std::io::Error::other(Cancelled));
}
Ok(())
}
}
impl std::io::Read for FailingReader<'_> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
self.tick()?;
self.inner.read(buf)
}
}
impl std::io::Seek for FailingReader<'_> {
fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
self.tick()?;
self.inner.seek(pos)
}
}
#[test]
fn a_failing_reader_is_reported_as_io_at_every_call() {
let bytes = load_golden("lo-legacy-aes-cbc.odt");
let mut fail_at = 1;
loop {
let reader = FailingReader {
inner: Cursor::new(&bytes),
calls: 0,
fail_at,
};
match classify_reader(reader) {
Err(DetectError::Io(err)) => {
let inner = err.get_ref().expect("the caller's error, not a stand-in");
assert!(inner.is::<Cancelled>(), "call {fail_at}: {err}");
}
Ok(class) => {
assert_eq!(class, classify(&bytes).unwrap());
assert!(fail_at > 1, "succeeded although the first call failed");
break;
}
Err(other) => panic!("call {fail_at}: expected Io, got {other:?}"),
}
fail_at += 1;
}
}
#[test]
fn a_truncated_slice_is_not_an_io_error() {
let bytes = load_golden("lo-legacy-aes-cbc.odt");
for cut in [0, 1, 22, bytes.len() / 2, bytes.len() - 1] {
let err = classify(&bytes[..cut]).unwrap_err();
assert!(!matches!(err, DetectError::Io(_)), "cut {cut}: {err:?}");
}
}