Skip to content

Commit ece5c45

Browse files
committed
fix(storage): make overflow handling more robust
1 parent 20c4672 commit ece5c45

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

htsget-storage/src/c4gh/mod.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,14 @@ impl DeserializedHeader {
141141
data,
142142
))
143143
}
144-
145-
/// Decrypt the
146-
pub fn decrypt_stream() {}
147144
}
148145

149146
/// Convert an encrypted file position to an unencrypted position if the header length is known.
150147
pub fn to_unencrypted(encrypted_position: u64, header_length: u64) -> u64 {
148+
if encrypted_position < header_length + NONCE_SIZE {
149+
return 0;
150+
}
151+
151152
let number_data_blocks = encrypted_position / DATA_BLOCK_SIZE;
152153
let mut additional_bytes = number_data_blocks * (NONCE_SIZE + MAC_SIZE);
153154

@@ -161,6 +162,10 @@ pub fn to_unencrypted(encrypted_position: u64, header_length: u64) -> u64 {
161162

162163
/// Convert an encrypted file size to an unencrypted file size if the header length is known.
163164
pub fn to_unencrypted_file_size(encrypted_file_size: u64, header_length: u64) -> u64 {
165+
if encrypted_file_size < header_length + NONCE_SIZE + MAC_SIZE {
166+
return 0;
167+
}
168+
164169
to_unencrypted(encrypted_file_size, header_length) - MAC_SIZE
165170
}
166171

@@ -272,6 +277,26 @@ mod tests {
272277
assert_eq!(result, expected);
273278
}
274279

280+
#[test]
281+
fn test_to_unencrypted() {
282+
let result = to_unencrypted(124, 124);
283+
assert_eq!(result, 0);
284+
let result = to_unencrypted(124 + 12, 124);
285+
assert_eq!(result, 0);
286+
let result = to_unencrypted(124 + 12 + 12, 124);
287+
assert_eq!(result, 12);
288+
}
289+
290+
#[test]
291+
fn test_to_unencrypted_file_size() {
292+
let result = to_unencrypted_file_size(124, 124);
293+
assert_eq!(result, 0);
294+
let result = to_unencrypted_file_size(124 + 12 + 16, 124);
295+
assert_eq!(result, 0);
296+
let result = to_unencrypted_file_size(124 + 12 + 16 + 12, 124);
297+
assert_eq!(result, 12);
298+
}
299+
275300
#[test]
276301
fn test_unencrypted_clamp() {
277302
let pos = 0;

0 commit comments

Comments
 (0)