Skip to content

Commit

Permalink
Do not allow recursive decompression
Browse files Browse the repository at this point in the history
  • Loading branch information
link2xt committed Feb 19, 2024
1 parent 5357815 commit c268385
Showing 1 changed file with 24 additions and 2 deletions.
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();

Check failure on line 1013 in src/composed/message/types.rs

View workflow job for this annotation

GitHub Actions / Build and test (Windows) (windows-latest, nightly-2024-02-07, x86_64-pc-windows-gnu)

couldn't read src\composed\message\../../../tests/quine.out: The system cannot find the file specified. (os error 2)

Check failure on line 1013 in src/composed/message/types.rs

View workflow job for this annotation

GitHub Actions / Build and test (Windows) (windows-latest, beta, x86_64-pc-windows-gnu)

couldn't read src\composed\message\../../../tests/quine.out: The system cannot find the file specified. (os error 2)

Check failure on line 1013 in src/composed/message/types.rs

View workflow job for this annotation

GitHub Actions / Build and test (Nix) (ubuntu-latest, stable)

couldn't read src/composed/message/../../../tests/quine.out: No such file or directory (os error 2)
assert!(msg.get_content().is_err());
}
}

0 comments on commit c268385

Please sign in to comment.