Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not allow recursive decompression #290

Merged
merged 1 commit into from Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/composed/message/types.rs
Expand Up @@ -611,16 +611,29 @@ impl Message {
}

/// Returns the underlying content and `None` if the message is encrypted.
///
/// Decompresses up to one layer of compressed data.
pub fn get_content(&self) -> Result<Option<Vec<u8>>> {
self.get_content_internal(true)
}

/// Returns the underlying content and `None` if the message is encrypted.
///
/// If `decompress` is true, may decompress a compressed message.
fn get_content_internal(&self, decompress: bool) -> Result<Option<Vec<u8>>> {
match self {
Message::Literal(ref data) => Ok(Some(data.data().to_vec())),
Message::Signed { message, .. } => Ok(message
.as_ref()
.and_then(|m| m.get_literal())
.map(|l| l.data().to_vec())),
Message::Compressed(data) => {
let msg = Message::from_bytes(data.decompress()?)?;
msg.get_content()
if decompress {
let msg = Message::from_bytes(data.decompress()?)?;
msg.get_content_internal(false)
} else {
bail!("Recursive decompression not allowed");
}
}
Message::Encrypted { .. } => Ok(None),
}
Expand Down Expand Up @@ -991,4 +1004,13 @@ mod tests {
// verify the signature with alice's signing subkey
signed_msg.verify(&verify).expect("signature seems bad");
}

/// Tests that decompressing compression quine does not result in stack overflow.
/// quine.out comes from <https://mumble.net/~campbell/misc/pgp-quine/>
/// See <https://mumble.net/~campbell/2013/10/08/compression> for details.
#[test]
fn test_compression_quine() {
let msg = Message::from_bytes(&include_bytes!("../../../tests/quine.out")[..]).unwrap();
assert!(msg.get_content().is_err());
}
}
Binary file added tests/quine.out
Binary file not shown.