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

Accept either strings or structs for hosts #6763

Merged
merged 1 commit into from
Aug 28, 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
21 changes: 19 additions & 2 deletions crates/uv-configuration/src/trusted_host.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::str::FromStr;

use url::Url;

/// A trusted host, which could be a host or a host-port pair.
Expand Down Expand Up @@ -31,13 +33,28 @@ impl TrustedHost {
}
}

#[derive(serde::Deserialize)]
#[serde(untagged)]
enum TrustHostWire {
String(String),
Struct {
scheme: Option<String>,
host: String,
port: Option<u16>,
},
}

impl<'de> serde::de::Deserialize<'de> for TrustedHost {
fn deserialize<D>(deserializer: D) -> Result<TrustedHost, D::Error>
where
D: serde::de::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
s.parse().map_err(serde::de::Error::custom)
let helper = TrustHostWire::deserialize(deserializer)?;

match helper {
TrustHostWire::String(s) => TrustedHost::from_str(&s).map_err(serde::de::Error::custom),
TrustHostWire::Struct { scheme, host, port } => Ok(TrustedHost { scheme, host, port }),
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion crates/uv/tests/show_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3419,7 +3419,7 @@ fn allow_insecure_host() -> anyhow::Result<()> {

let config = context.temp_dir.child("uv.toml");
config.write_str(indoc::indoc! {r#"
allow-insecure-host = ["google.com"]
allow-insecure-host = ["google.com", { host = "example.com" }]
"#})?;

let requirements_in = context.temp_dir.child("requirements.in");
Expand Down Expand Up @@ -3495,6 +3495,11 @@ fn allow_insecure_host() -> anyhow::Result<()> {
host: "google.com",
port: None,
},
TrustedHost {
scheme: None,
host: "example.com",
port: None,
},
],
no_build_isolation: false,
no_build_isolation_package: [],
Expand Down
Loading