Avoid reading files in the environment bin that are not entrypoints (#14830)

Closes https://github.com/astral-sh/uv/issues/14829

I tested this against the given Dockerfile.
This commit is contained in:
Zanie Blue 2025-07-22 14:11:15 -05:00 committed by GitHub
parent fe17b753b3
commit 02cc49296b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 36 additions and 2 deletions

View File

@ -1754,12 +1754,46 @@ fn copy_entrypoint(
previous_executable: &Path, previous_executable: &Path,
python_executable: &Path, python_executable: &Path,
) -> Result<(), CopyEntrypointError> { ) -> Result<(), CopyEntrypointError> {
use std::io::Write; use std::io::{Seek, Write};
use std::os::unix::fs::PermissionsExt; use std::os::unix::fs::PermissionsExt;
use fs_err::os::unix::fs::OpenOptionsExt; use fs_err::os::unix::fs::OpenOptionsExt;
let contents = fs_err::read_to_string(source)?; let mut file = fs_err::File::open(source)?;
let mut buffer = [0u8; 2];
if file.read_exact(&mut buffer).is_err() {
// File is too small to have a shebang
trace!(
"Skipping copy of entrypoint `{}`: file is too small to contain a shebang",
source.user_display()
);
return Ok(());
}
// Check if it starts with `#!` to avoid reading binary files and such into memory
if &buffer != b"#!" {
trace!(
"Skipping copy of entrypoint `{}`: does not start with #!",
source.user_display()
);
return Ok(());
}
let mut contents = String::new();
file.seek(std::io::SeekFrom::Start(0))?;
match file.read_to_string(&mut contents) {
Ok(_) => {}
Err(err) if err.kind() == std::io::ErrorKind::InvalidData => {
// If the file is not valid UTF-8, we skip it in case it was a binary file with `#!` at
// the start (which seems pretty niche, but being defensive here seems safe)
trace!(
"Skipping copy of entrypoint `{}`: is not valid UTF-8",
source.user_display()
);
return Ok(());
}
Err(err) => return Err(err.into()),
}
let Some(contents) = contents let Some(contents) = contents
// Check for a relative path or relocatable shebang // Check for a relative path or relocatable shebang