1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use std::{borrow::Cow, io::Read};
const ZSTD_PREFIX: [u8; 8] = [82, 188, 83, 118, 70, 219, 142, 5];
pub const CODE_BLOB_BOMB_LIMIT: usize = 50 * 1024 * 1024;
#[derive(Debug, Clone, PartialEq)]
pub enum Error {
PossibleBomb,
Invalid,
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match *self {
Error::PossibleBomb => write!(f, "Possible compression bomb encountered"),
Error::Invalid => write!(f, "Blob had invalid format"),
}
}
}
impl std::error::Error for Error {}
fn read_from_decoder(
decoder: impl Read,
blob_len: usize,
bomb_limit: usize,
) -> Result<Vec<u8>, Error> {
let mut decoder = decoder.take((bomb_limit + 1) as u64);
let mut buf = Vec::with_capacity(blob_len);
decoder.read_to_end(&mut buf).map_err(|_| Error::Invalid)?;
if buf.len() <= bomb_limit {
Ok(buf)
} else {
Err(Error::PossibleBomb)
}
}
#[cfg(not(target_os = "unknown"))]
fn decompress_zstd(blob: &[u8], bomb_limit: usize) -> Result<Vec<u8>, Error> {
let decoder = zstd::Decoder::new(blob).map_err(|_| Error::Invalid)?;
read_from_decoder(decoder, blob.len(), bomb_limit)
}
#[cfg(target_os = "unknown")]
fn decompress_zstd(mut blob: &[u8], bomb_limit: usize) -> Result<Vec<u8>, Error> {
let blob_len = blob.len();
let decoder =
ruzstd::streaming_decoder::StreamingDecoder::new(&mut blob).map_err(|_| Error::Invalid)?;
read_from_decoder(decoder, blob_len, bomb_limit)
}
pub fn decompress(blob: &[u8], bomb_limit: usize) -> Result<Cow<[u8]>, Error> {
if blob.starts_with(&ZSTD_PREFIX) {
decompress_zstd(&blob[ZSTD_PREFIX.len()..], bomb_limit).map(Into::into)
} else {
Ok(blob.into())
}
}
#[cfg(not(target_os = "unknown"))]
pub fn compress(blob: &[u8], bomb_limit: usize) -> Option<Vec<u8>> {
use std::io::Write;
if blob.len() > bomb_limit {
return None
}
let mut buf = ZSTD_PREFIX.to_vec();
{
let mut v = zstd::Encoder::new(&mut buf, 3).ok()?.auto_finish();
v.write_all(blob).ok()?;
}
Some(buf)
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Write;
const BOMB_LIMIT: usize = 10;
#[test]
fn refuse_to_encode_over_limit() {
let mut v = vec![0; BOMB_LIMIT + 1];
assert!(compress(&v, BOMB_LIMIT).is_none());
let _ = v.pop();
assert!(compress(&v, BOMB_LIMIT).is_some());
}
#[test]
fn compress_and_decompress() {
let v = vec![0; BOMB_LIMIT];
let compressed = compress(&v, BOMB_LIMIT).unwrap();
assert!(compressed.starts_with(&ZSTD_PREFIX));
assert_eq!(&decompress(&compressed, BOMB_LIMIT).unwrap()[..], &v[..])
}
#[test]
fn decompresses_only_when_magic() {
let v = vec![0; BOMB_LIMIT + 1];
assert_eq!(&decompress(&v, BOMB_LIMIT).unwrap()[..], &v[..]);
}
#[test]
fn possible_bomb_fails() {
let encoded_bigger_than_bomb = vec![0; BOMB_LIMIT + 1];
let mut buf = ZSTD_PREFIX.to_vec();
{
let mut v = zstd::Encoder::new(&mut buf, 3).unwrap().auto_finish();
v.write_all(&encoded_bigger_than_bomb[..]).unwrap();
}
assert_eq!(decompress(&buf[..], BOMB_LIMIT).err(), Some(Error::PossibleBomb));
}
}