Repository: astral-sh/uv
File: crates/uv-pep508/src/verbatim_url.rs (Rust)

Code before:
        // This ends up being moderately annoying to detect, since the
        // above gets parsed into a "valid" WHATWG URL where the
        // host is `used` and the pathname is `/name:password@domain/a/b/c`
        // rather than causing a parse error.
        //
        // To detect it, we use a heuristic: if the password component
        // is missing but the path or fragment contain a `:` and `@`
        // in that order, then we assume the URL is ambiguous.
        let path_or_fragment_is_fishy = {
            let path = url.path();
            let fragment = url.fragment().unwrap_or("");

            path.contains(':') && path.contains('@')
                || fragment.contains(':') && fragment.contains('@')
        };

        if url.password().is_none() && path_or_fragment_is_fishy {
            // Our ambiguous URL probably has credentials in it,
            // so we don't want to blast it out in the error message.
            // We somewhat aggressively replace everything between
            // the scheme's ':' and the lastmost `@` with `***`.
            //
            // These unwraps are safe, since we've checked for both
            // characters above.
            let col_pos = given.find(':').unwrap();
            let at_pos = given.rfind('@').unwrap();

            let redacted_path = format!("{}***{}", &given[0..=col_pos], &given[at_pos..]);

            return Err(VerbatimUrlError::AmbiguousAuthority(redacted_path));
        }

        Ok(Self {
            url: DisplaySafeUrl::from(url),
            given: None,
        })
    }

    /// Convert a [`VerbatimUrl`] from a path or a URL.
    ///
    /// If no root directory is provided, relative paths are resolved against the current working
    /// directory.
    #[cfg(feature = "non-pep508-extensions")] // PEP 508 arguably only allows absolute file URLs.
    pub fn from_url_or_path(
        input: &str,
        root_dir: Option<&Path>,
    ) -> Result<Self, VerbatimUrlError> {
        let url = match split_scheme(input) {
            Some((scheme, ..)) => {
                match Scheme::parse(scheme) {
                    Some(_) => {

Review comment (security):
Noting: we do this over the original `given` input because we want to redact *everything* between the first `:` and the last `@`, not just the parts that happen to be in the path or fragment. We do this because it's hard to be confident that these sensitive values don't end up in other (non-path or fragment) components.

Code after:
        input: &str,
        root_dir: Option<&Path>,
    ) -> Result<Self, VerbatimUrlError> {
        let url = match split_scheme(input) {
            Some((scheme, ..)) => {
                match Scheme::parse(scheme) {
                    Some(_) => {
                        // Ex) `https://pypi.org/simple`
                        Self::parse_url(input)?
                    }
                    None => {
                        // Ex) `C:\Users\user\index`
                        if let Some(root_dir) = root_dir {
                            Self::from_path(input, root_dir)?
                        } else {
                            let absolute_path = std::path::absolute(input).map_err(|err| {
                                VerbatimUrlError::Absolute(input.to_string(), err)
                            })?;
                            Self::from_absolute_path(absolute_path)?
                        }
                    }
                }
            }
            None => {
                // Ex) `/Users/user/index`
                if let Some(root_dir) = root_dir {
                    Self::from_path(input, root_dir)?
                } else {
                    let absolute_path = std::path::absolute(input)
                        .map_err(|err| VerbatimUrlError::Absolute(input.to_string(), err))?;
                    Self::from_absolute_path(absolute_path)?
                }
            }
        };
        Ok(url.with_given(input))
    }

    /// Parse a URL from an absolute or relative path.
    #[cfg(feature = "non-pep508-extensions")] // PEP 508 arguably only allows absolute file URLs.
    pub fn from_path(
        path: impl AsRef<Path>,
        base_dir: impl AsRef<Path>,
    ) -> Result<Self, VerbatimUrlError> {
        debug_assert!(base_dir.as_ref().is_absolute(), "base dir must be absolute");
        let path = path.as_ref();

        // Convert the path to an absolute path, if necessary.
        let path = if path.is_absolute() {
            Cow::Borrowed(path)
        } else {
            Cow::Owned(base_dir.as_ref().join(path))