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

fix 'pip install' for zips with bunk permissions #1757

Merged
merged 3 commits into from
Feb 20, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions crates/uv-client/src/cached_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,11 +344,17 @@ impl CachedClient {
{
Ok(data) => Some(data),
Err(err) => {
warn!(
"Broken cache policy entry at {}, removing: {err}",
cache_entry.path().display()
);
let _ = fs_err::tokio::remove_file(&cache_entry.path()).await;
// When we know the cache entry doesn't exist, then things are
// normal and we shouldn't emit a WARN.
if err.kind().is_file_not_exists() {
trace!("No cache entry exists for {}", cache_entry.path().display());
} else {
warn!(
"Broken cache policy entry at {}, removing: {err}",
cache_entry.path().display()
);
let _ = fs_err::tokio::remove_file(&cache_entry.path()).await;
}
None
}
}
Expand Down
9 changes: 9 additions & 0 deletions crates/uv-client/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,15 @@ pub enum ErrorKind {
}

impl ErrorKind {
/// Returns true if this error kind corresponds to an I/O "not found"
/// error.
pub(crate) fn is_file_not_exists(&self) -> bool {
let ErrorKind::Io(ref err) = *self else {
return false;
};
matches!(err.kind(), std::io::ErrorKind::NotFound)
}

pub(crate) fn from_middleware(err: reqwest_middleware::Error) -> Self {
if let reqwest_middleware::Error::Middleware(ref underlying) = err {
if let Some(err) = underlying.downcast_ref::<OfflineError>() {
Expand Down
5 changes: 3 additions & 2 deletions crates/uv-extract/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub async fn unzip<R: tokio::io::AsyncRead + Unpin>(
reader: R,
target: impl AsRef<Path>,
) -> Result<(), Error> {
let target = target.as_ref();
let mut reader = reader.compat();
let mut zip = async_zip::base::read::stream::ZipFileReader::new(&mut reader);

Expand All @@ -22,7 +23,7 @@ pub async fn unzip<R: tokio::io::AsyncRead + Unpin>(
while let Some(mut entry) = zip.next_with_entry().await? {
// Construct the (expected) path to the file on-disk.
let path = entry.reader().entry().filename().as_str()?;
let path = target.as_ref().join(path);
let path = target.join(path);
let is_dir = entry.reader().entry().dir()?;

// Either create the directory or write the file to disk.
Expand Down Expand Up @@ -81,7 +82,7 @@ pub async fn unzip<R: tokio::io::AsyncRead + Unpin>(
if has_any_executable_bit != 0 {
// Construct the (expected) path to the file on-disk.
let path = entry.filename().as_str()?;
let path = target.as_ref().join(path);
let path = target.join(path);

let permissions = fs_err::tokio::metadata(&path).await?.permissions();
fs_err::tokio::set_permissions(
Expand Down
36 changes: 36 additions & 0 deletions crates/uv/tests/pip_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1219,3 +1219,39 @@ fn install_sdist_resolution_lowest() -> Result<()> {

Ok(())
}

/// Tests that we can install a package from a zip file that has bunk
/// permissions.
///
/// See: <https://github.com/astral-sh/uv/issues/1453>
#[test]
fn direct_url_zip_file_bunk_permissions() -> Result<()> {
let context = TestContext::new("3.12");
let requirements_txt = context.temp_dir.child("requirements.txt");
requirements_txt.write_str(
"opensafely-pipeline @ https://github.com/opensafely-core/pipeline/archive/refs/tags/v2023.11.06.145820.zip",
)?;

uv_snapshot!(command(&context)
.arg("-r")
.arg("requirements.txt")
.arg("--strict"), @r###"
success: true
exit_code: 0
----- stdout -----

----- stderr -----
Resolved 6 packages in [TIME]
Downloaded 5 packages in [TIME]
Installed 6 packages in [TIME]
+ distro==1.8.0
+ opensafely-pipeline==2023.11.6.145820 (from https://github.com/opensafely-core/pipeline/archive/refs/tags/v2023.11.06.145820.zip)
+ pydantic==1.10.13
+ ruyaml==0.91.0
+ setuptools==68.2.2
+ typing-extensions==4.8.0
"###
);

Ok(())
}