Skip to content

Commit 9937fa2

Browse files
Check remote wheel metadata prefetch ranges (#21919)
`wheel_metadata_from_remote_zip` can panic in debug builds while computing a prefetch range from a malformed ZIP64 size, before the ZIP reader validates the entry. Use checked arithmetic and omit an unrepresentable optional prefetch, leaving the existing entry-range validation and normal ranges unchanged. Follows up on #18605. Co-authored-by: Zanie Blue <[email protected]>
1 parent 0f03963 commit 9937fa2

2 files changed

Lines changed: 84 additions & 13 deletions

File tree

‎crates/uv-client/src/remote_metadata.rs‎

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,22 +78,26 @@ pub(crate) async fn wheel_metadata_from_remote_zip(
7878
.map_err(|err| ErrorKind::Metadata(debug_name.to_string(), err))?;
7979

8080
let offset = metadata_entry.header_offset();
81-
let size = metadata_entry.compressed_size()
82-
+ 30 // Header size in bytes
83-
+ metadata_entry.filename().as_bytes().len() as u64;
8481

85-
// The zip archive uses as BufReader which reads in chunks of 8192. To ensure we prefetch
86-
// enough data we round the size up to the nearest multiple of the buffer size.
82+
// The ZIP reader uses a `BufReader`, which reads in chunks of 8192. Round the
83+
// prefetch size up to the nearest multiple of the buffer size.
8784
let buffer_size = 8192;
88-
let size = size.div_ceil(buffer_size) * buffer_size;
85+
let end = metadata_entry
86+
.compressed_size()
87+
.checked_add(30) // Header size in bytes
88+
.and_then(|size| size.checked_add(metadata_entry.filename().as_bytes().len() as u64))
89+
.and_then(|size| size.checked_next_multiple_of(buffer_size))
90+
.and_then(|size| offset.checked_add(size));
8991

90-
// Fetch the bytes from the zip archive that contain the requested file.
91-
reader
92-
.inner_mut()
93-
.get_mut()
94-
.get_mut()
95-
.prefetch(offset..offset + size)
96-
.await;
92+
// Prefetching is optional. The ZIP reader validates the entry range when it is opened.
93+
if let Some(end) = end {
94+
reader
95+
.inner_mut()
96+
.get_mut()
97+
.get_mut()
98+
.prefetch(offset..end)
99+
.await;
100+
}
97101

98102
// Read the contents of the METADATA file
99103
let mut contents = String::new();

‎crates/uv-client/tests/it/remote_metadata.rs‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ use std::path::Path;
22
use std::str::FromStr;
33

44
use anyhow::{Context, Result};
5+
use async_zip::base::read::mem::ZipFileReader;
56
use async_zip::base::write::ZipFileWriter;
7+
use async_zip::error::ZipError;
68
use async_zip::{Compression, ZipEntryBuilder};
79
use reqwest::header::{
810
ACCEPT_RANGES, AUTHORIZATION, CONTENT_LENGTH, CONTENT_RANGE, HeaderName, LOCATION, RANGE,
@@ -433,6 +435,71 @@ async fn remote_metadata_bounded_ranges() -> Result<()> {
433435
assert_wheel_metadata_readable(&server).await
434436
}
435437

438+
#[tokio::test]
439+
async fn remote_metadata_rejects_overflowing_zip64_size() -> Result<()> {
440+
let metadata_path = "ok-1.0.0.dist-info/METADATA";
441+
let mut writer = ZipFileWriter::new(Vec::new());
442+
writer
443+
.write_entry_whole(
444+
ZipEntryBuilder::new(metadata_path.into(), Compression::Stored),
445+
b"Metadata-Version: 2.1\nName: ok\nVersion: 1.0.0\n",
446+
)
447+
.await?;
448+
let mut wheel = writer.close().await?;
449+
450+
// Add a ZIP64 compressed size to the central directory, leaving the real local header intact.
451+
let end = wheel.len() - 22;
452+
assert_eq!(&wheel[end..end + 4], b"PK\x05\x06");
453+
let directory = u32::from_le_bytes(wheel[end + 16..end + 20].try_into()?) as usize;
454+
let directory_size = u32::from_le_bytes(wheel[end + 12..end + 16].try_into()?);
455+
let extra = directory + 46 + metadata_path.len();
456+
assert_eq!(&wheel[directory..directory + 4], b"PK\x01\x02");
457+
assert_eq!(&wheel[directory + 30..directory + 32], &[0, 0]);
458+
assert_eq!(&wheel[directory + 46..extra], metadata_path.as_bytes());
459+
wheel[directory + 20..directory + 24].copy_from_slice(&u32::MAX.to_le_bytes());
460+
wheel[directory + 30..directory + 32].copy_from_slice(&12u16.to_le_bytes());
461+
wheel.splice(
462+
extra..extra,
463+
[1, 0, 8, 0].into_iter().chain(u64::MAX.to_le_bytes()),
464+
);
465+
let end = end + 12;
466+
wheel[end + 12..end + 16].copy_from_slice(&(directory_size + 12).to_le_bytes());
467+
468+
let archive = ZipFileReader::new(wheel.clone()).await?;
469+
assert_eq!(archive.file().entries().len(), 1);
470+
assert_eq!(archive.file().entries()[0].compressed_size(), u64::MAX);
471+
472+
let server = MockServer::start().await;
473+
Mock::given(method("HEAD"))
474+
.and(path("/artifact"))
475+
.respond_with(
476+
ResponseTemplate::new(200)
477+
.insert_header(ACCEPT_RANGES, "bytes")
478+
.insert_header(CONTENT_LENGTH, wheel.len().to_string()),
479+
)
480+
.expect(1)
481+
.mount(&server)
482+
.await;
483+
Mock::given(method("GET"))
484+
.and(path("/artifact"))
485+
.and(header_exists(RANGE.as_str()))
486+
.respond_with(move |request: &Request| wheel_range_response(request, &wheel))
487+
.expect(1)
488+
.mount(&server)
489+
.await;
490+
491+
let error = assert_wheel_metadata_readable(&server)
492+
.await
493+
.expect_err("the overflowing entry size should be rejected");
494+
let Some(ZipError::InvalidEntryDataRange) = error
495+
.chain()
496+
.find_map(|cause| cause.downcast_ref::<ZipError>())
497+
else {
498+
return Err(error.context("expected an invalid ZIP entry data range"));
499+
};
500+
Ok(())
501+
}
502+
436503
/// A redirect target may reject range requests while allowing a full download. The range request
437504
/// should not reach the target; metadata should be read after retrying the source without a `Range`
438505
/// header.

0 commit comments

Comments
 (0)