Skip to content

Commit b187a51

Browse files
zsolzanieb
authored andcommitted
Honor certificate overrides when running remote scripts (#20767)
#20418 moved certificate overrides out of `BaseClientBuilder` and applied them to the main HTTP client. The separate client used by `uv run https://...` and GitHub Gists was left unconfigured, allowing remote scripts to fall back to the default trust store instead of respecting `SSL_CERT_FILE` and `SSL_CERT_DIR`. Apply the environment certificate overrides directly to the remote-script client before downloading. Preserve the default trust store when neither override is set, but fail closed when an explicitly configured certificate source is missing or invalid. Follow-up to the [discussion on #20418](#20418 (comment)).
1 parent 6e79013 commit b187a51

6 files changed

Lines changed: 80 additions & 48 deletions

File tree

‎crates/uv-test/src/lib.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ pub const INSTA_FILTERS: &[(&str, &str)] = &[
114114
),
115115
// Trim end-of-line whitespaces, to allow removing them on save.
116116
(r"([^\s])[ \t]+(\r?\n)", "$1$2"),
117+
// Certificate overrides and their contents depend on the host environment.
118+
(
119+
r"(?ms)^([ \t]*custom_certificates: )(?:None|Some\(\n.*?^[ \t]*\),\n[ \t]*\)),",
120+
"${1}[CERTIFICATES],",
121+
),
117122
// Filter SSL certificate loading debug messages (environment-dependent)
118123
(r"DEBUG Loaded \d+ certificate\(s\) from [^\n]+\n", ""),
119124
];

‎crates/uv/src/commands/project/run.rs‎

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ use uv_types::SourceTreeEditablePolicy;
4949
use uv_warnings::warn_user;
5050
use uv_workspace::{DiscoveryOptions, VirtualProject, WorkspaceCache, WorkspaceErrorKind};
5151

52+
use crate::base_client_builder;
5253
use crate::child::run_to_completion;
5354

5455
/// GitHub Gist API response structure
@@ -1472,19 +1473,8 @@ impl ParsedRunCommand {
14721473
Ok((script, run_command))
14731474
}
14741475
Self::PendingRemote(remote_command) => {
1475-
let settings = GlobalSettings::resolve(global_args, filesystem, environment)?;
1476-
let client_builder = BaseClientBuilder::new(
1477-
settings.network_settings.connectivity,
1478-
settings.network_settings.system_certs,
1479-
settings.network_settings.allow_insecure_host,
1480-
settings.preview,
1481-
settings.network_settings.read_timeout,
1482-
settings.network_settings.connect_timeout,
1483-
settings.network_settings.retries,
1484-
)
1485-
.http_proxy(settings.network_settings.http_proxy)
1486-
.https_proxy(settings.network_settings.https_proxy)
1487-
.no_proxy(settings.network_settings.no_proxy);
1476+
let settings = GlobalSettings::resolve(global_args, filesystem, environment, None)?;
1477+
let client_builder = base_client_builder(&settings);
14881478

14891479
let (url, downloaded_script, args) =
14901480
remote_command.download(&client_builder).await?;

‎crates/uv/src/lib.rs‎

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use uv_cli::{
3232
PythonNamespace, SelfCommand, SelfNamespace, ToolCommand, ToolNamespace, TopLevelArgs,
3333
WorkspaceCommand, WorkspaceNamespace, compat::CompatArgs, options::ArgumentError,
3434
};
35-
use uv_client::{BaseClientBuilder, Certificates};
35+
use uv_client::BaseClientBuilder;
3636
use uv_configuration::min_stack_size;
3737
use uv_flags::EnvironmentFlags;
3838
use uv_fs::{CWD, Simplified, normalize_path};
@@ -68,6 +68,29 @@ mod logging;
6868
pub(crate) mod printer;
6969
pub(crate) mod settings;
7070

71+
/// Construct the shared HTTP client builder from the resolved global settings.
72+
pub(crate) fn base_client_builder<'a>(globals: &GlobalSettings) -> BaseClientBuilder<'a> {
73+
let client_builder = BaseClientBuilder::new(
74+
globals.network_settings.connectivity,
75+
globals.network_settings.system_certs,
76+
globals.network_settings.allow_insecure_host.clone(),
77+
globals.preview,
78+
globals.network_settings.read_timeout,
79+
globals.network_settings.connect_timeout,
80+
globals.network_settings.retries,
81+
)
82+
.cache_read_concurrency(globals.concurrency.cache_reads)
83+
.http_proxy(globals.network_settings.http_proxy.clone())
84+
.https_proxy(globals.network_settings.https_proxy.clone())
85+
.no_proxy(globals.network_settings.no_proxy.clone());
86+
87+
if let Some(certificates) = &globals.network_settings.custom_certificates {
88+
client_builder.custom_certificates(certificates.clone())
89+
} else {
90+
client_builder
91+
}
92+
}
93+
7194
/// Whether to initialize process-global state.
7295
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
7396
#[doc(hidden)]
@@ -461,11 +484,17 @@ pub async fn run(cli: Cli, global_initialization: GlobalInitialization) -> Resul
461484
.map(FilesystemOptions::from)
462485
.combine(filesystem);
463486

487+
let custom_certificate_file = match &*cli.command {
488+
Commands::Pip(PipNamespace { cert, .. }) => cert.as_deref(),
489+
_ => None,
490+
};
491+
464492
// Resolve the global settings.
465493
let globals = GlobalSettings::resolve(
466494
&cli.top_level.global_args,
467495
filesystem.as_ref(),
468496
&environment,
497+
custom_certificate_file,
469498
)?;
470499

471500
if global_initialization.needs_initialization() {
@@ -607,37 +636,8 @@ pub async fn run(cli: Cli, global_initialization: GlobalInitialization) -> Resul
607636
WorkspaceCache::default()
608637
};
609638

610-
// Configure custom CA certificates from `--cert` or from the environment (`SSL_CERT_FILE` and
611-
// `SSL_CERT_DIR`).
612-
// Like pip, an explicit certificate bundle overrides the environment certificate sources.
613-
let custom_certificates = if let Commands::Pip(PipNamespace {
614-
cert: Some(cert), ..
615-
}) = &*cli.command
616-
{
617-
Some(Certificates::from_file(cert)?)
618-
} else {
619-
Certificates::from_env()
620-
};
621-
622639
// Configure the global network settings.
623-
let client_builder = BaseClientBuilder::new(
624-
globals.network_settings.connectivity,
625-
globals.network_settings.system_certs,
626-
globals.network_settings.allow_insecure_host.clone(),
627-
globals.preview,
628-
globals.network_settings.read_timeout,
629-
globals.network_settings.connect_timeout,
630-
globals.network_settings.retries,
631-
)
632-
.cache_read_concurrency(globals.concurrency.cache_reads)
633-
.http_proxy(globals.network_settings.http_proxy.clone())
634-
.https_proxy(globals.network_settings.https_proxy.clone())
635-
.no_proxy(globals.network_settings.no_proxy.clone());
636-
let client_builder = if let Some(certificates) = custom_certificates {
637-
client_builder.custom_certificates(certificates)
638-
} else {
639-
client_builder
640-
};
640+
let client_builder = base_client_builder(&globals);
641641

642642
match *cli.command {
643643
Commands::Auth(AuthNamespace {

‎crates/uv/src/settings.rs‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::env::VarError;
22
use std::fmt;
33
use std::num::NonZeroUsize;
4-
use std::path::PathBuf;
4+
use std::path::{Path, PathBuf};
55
use std::process;
66
use std::str::FromStr;
77
use std::time::Duration;
@@ -32,7 +32,7 @@ use uv_cli::{
3232
resolver_installer_options, resolver_options,
3333
},
3434
};
35-
use uv_client::Connectivity;
35+
use uv_client::{Certificates, Connectivity};
3636
use uv_configuration::{
3737
BuildIsolation, BuildOptions, Concurrency, DependencyGroups, DevMode, DryRun, EditableMode,
3838
EnvFile, ExcludeDependency, ExportFormat, ExtrasSpecification, GitLfsSetting, HashCheckingMode,
@@ -98,8 +98,10 @@ impl GlobalSettings {
9898
args: &GlobalArgs,
9999
workspace: Option<&FilesystemOptions>,
100100
environment: &EnvironmentOptions,
101+
custom_certificate_file: Option<&Path>,
101102
) -> anyhow::Result<Self> {
102-
let network_settings = NetworkSettings::resolve(args, workspace, environment)?;
103+
let network_settings =
104+
NetworkSettings::resolve(args, workspace, environment, custom_certificate_file)?;
103105
let python_preference = resolve_python_preference(args, workspace, environment)?;
104106
let color = resolve_color(args);
105107
Ok(Self {
@@ -273,6 +275,7 @@ pub(crate) struct NetworkSettings {
273275
pub(super) connectivity: Connectivity,
274276
pub(super) offline: Flag,
275277
pub(super) system_certs: bool,
278+
pub(super) custom_certificates: Option<Certificates>,
276279
pub(super) http_proxy: Option<ProxyUrl>,
277280
pub(super) https_proxy: Option<ProxyUrl>,
278281
pub(super) no_proxy: Option<Vec<String>>,
@@ -288,6 +291,7 @@ impl NetworkSettings {
288291
args: &GlobalArgs,
289292
workspace: Option<&FilesystemOptions>,
290293
environment: &EnvironmentOptions,
294+
custom_certificate_file: Option<&Path>,
291295
) -> anyhow::Result<Self> {
292296
// Resolve offline flag from CLI, environment variable, and workspace config.
293297
// Precedence: CLI > Env var > Workspace config > default (false).
@@ -388,10 +392,16 @@ impl NetworkSettings {
388392
let https_proxy = workspace.and_then(|workspace| workspace.globals.https_proxy.clone());
389393
let no_proxy = workspace.and_then(|workspace| workspace.globals.no_proxy.clone());
390394

395+
let custom_certificates = custom_certificate_file
396+
.map(Certificates::from_file)
397+
.transpose()?
398+
.or_else(Certificates::from_env);
399+
391400
Ok(Self {
392401
connectivity,
393402
offline,
394403
system_certs,
404+
custom_certificates,
395405
http_proxy,
396406
https_proxy,
397407
no_proxy,

‎crates/uv/tests/project/run.rs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4641,6 +4641,27 @@ fn run_remote_pep723_script() {
46414641
");
46424642
}
46434643

4644+
#[test]
4645+
fn run_remote_pep723_script_with_nonexistent_ssl_cert_file() {
4646+
let context = uv_test::test_context!("3.12");
4647+
4648+
uv_snapshot!(context.filters(), context.run()
4649+
.arg("https://raw.githubusercontent.com/astral-sh/uv/df45b9ac2584824309ff29a6a09421055ad730f6/scripts/uv-run-remote-script-test.py")
4650+
.arg(EnvVars::CI)
4651+
.env(EnvVars::SSL_CERT_FILE, context.temp_dir.join("missing.pem"))
4652+
.env(EnvVars::UV_HTTP_RETRIES, "0")
4653+
.env_remove(EnvVars::SSL_CERT_DIR)
4654+
.env_remove(EnvVars::UV_NATIVE_TLS)
4655+
.env_remove(EnvVars::UV_SYSTEM_CERTS), @"
4656+
exit_code: 2 (failure)
4657+
----- stderr -----
4658+
warning: Invalid `SSL_CERT_FILE`. Path does not exist: [TEMP_DIR]/missing.pem. No default certificates will be trusted.
4659+
error: error sending request for url (https://raw.githubusercontent.com/astral-sh/uv/df45b9ac2584824309ff29a6a09421055ad730f6/scripts/uv-run-remote-script-test.py)
4660+
Caused by: client error (Connect)
4661+
Caused by: invalid peer certificate: UnknownIssuer
4662+
");
4663+
}
4664+
46444665
#[test]
46454666
fn run_remote_requirements_offline_redacts_credentials() -> Result<()> {
46464667
let context = uv_test::test_context!("3.12");

‎crates/uv/tests/sync/show_settings.rs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ fn pip_compile_baseline() {
4949
connectivity: Online,
5050
offline: Disabled,
5151
system_certs: false,
52+
custom_certificates: [CERTIFICATES],
5253
http_proxy: None,
5354
https_proxy: None,
5455
no_proxy: None,
@@ -247,6 +248,7 @@ fn publish_resolved_settings() -> anyhow::Result<()> {
247248
connectivity: Online,
248249
offline: Disabled,
249250
system_certs: false,
251+
custom_certificates: [CERTIFICATES],
250252
http_proxy: None,
251253
https_proxy: None,
252254
no_proxy: None,
@@ -413,6 +415,7 @@ fn pip_install_baseline() {
413415
connectivity: Online,
414416
offline: Disabled,
415417
system_certs: false,
418+
custom_certificates: [CERTIFICATES],
416419
http_proxy: None,
417420
https_proxy: None,
418421
no_proxy: None,
@@ -594,6 +597,7 @@ fn lock_baseline() {
594597
connectivity: Online,
595598
offline: Disabled,
596599
system_certs: false,
600+
custom_certificates: [CERTIFICATES],
597601
http_proxy: None,
598602
https_proxy: None,
599603
no_proxy: None,
@@ -714,6 +718,7 @@ fn version_baseline() {
714718
connectivity: Online,
715719
offline: Disabled,
716720
system_certs: false,
721+
custom_certificates: [CERTIFICATES],
717722
http_proxy: None,
718723
https_proxy: None,
719724
no_proxy: None,
@@ -849,6 +854,7 @@ fn tool_install_baseline() {
849854
connectivity: Online,
850855
offline: Disabled,
851856
system_certs: false,
857+
custom_certificates: [CERTIFICATES],
852858
http_proxy: None,
853859
https_proxy: None,
854860
no_proxy: None,
@@ -4307,9 +4313,9 @@ fn system_certs_config_aliases() -> anyhow::Result<()> {
43074313
offline: Disabled,
43084314
- system_certs: false,
43094315
+ system_certs: true,
4316+
custom_certificates: [CERTIFICATES],
43104317
http_proxy: None,
43114318
https_proxy: None,
4312-
no_proxy: None,
43134319
...
43144320
"
43154321
);

0 commit comments

Comments
 (0)