Skip to content

from_file_path does not canoncalize paths leading to inconsistencies with Url::parse #1086

@rtyler

Description

@rtyler
  • Note that this crate implements the URL Standard not RFC 1738 or RFC 3986

Describe the bug

The use of from_file_path() will produce a different Url when the path has .. in the path segments compared to Url::parse. What this means in essence is that the only way to ensure that a file path is "canonicalized" is to

let u = Url::from_file_path("/home/tyler/../../dev/null");
let u =  Url::parse(fu.as_str())?

Reproduction

use url::Url;

fn main() {
    let fu = Url::parse("file:///home/tyler/../../dev/null").expect("oops");
    let u = Url::from_file_path("/home/tyler/../../dev/null")
        .expect("Failed to parse file path");
    assert_eq!(fu, u, "The Url objects themselves are different");
}
 Compiling playground v0.0.1 (/playground)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.81s
     Running `target/debug/playground`

thread 'main' (13) panicked at src/main.rs:7:5:
assertion `left == right` failed: The Url objects themselves are different
  left: Url { scheme: "file", cannot_be_a_base: false, username: "", password: None, host: None, port: None, path: "/dev/null", query: None, fragment: None }
 right: Url { scheme: "file", cannot_be_a_base: false, username: "", password: None, host: None, port: None, path: "/home/tyler/../../dev/null", query: None, fragment: None }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions