Skip to content

SCP-like URL to ssh conversion is lossy #2467

@weihanglo

Description

@weihanglo

Current behavior 😯

In SCP-like URL, the path [email protected]:rust-lang/cargo.git is home-relative. The path ought to be ~git/rust-lang/cargo.git. However, [email protected] converts [email protected]:rust-lang/cargo.git to ssh://[email protected]/rust-lang/cargo.git, which is lossy (not wrong). That SSH URL is absolute /rust-lang/cargo.git.

Expected behavior 🤔

I don't really know.

[email protected]:rust-lang/cargo.git -> ssh://[email protected]/~/rust-lang/cargo.git seems to be a more correct behavior. However, as I found in rust-lang/cargo#16740 (comment), GitHub doesn't recognize ssh://[email protected]/~/rust-lang/cargo.git (perhaps for security reason to prevent file system traversal?)

Git behavior

Not related to Git. Git doesn't do conversion.

Steps to reproduce 🕹

Run this cargo script cargo +nightly -Zscript script.rs

---cargo
package.edition = "2024"
dependencies.gix-url = "=0.35.2" 
---
fn main() {
    for url in [
        "[email protected]:rust-lang/cargo.git",
        "[email protected]:/rust-lang/cargo.git",
        "[email protected]:~/rust-lang/cargo.git",
        "[email protected]:~git/rust-lang/cargo.git",
        "[email protected]:/~/rust-lang/cargo.git",
        "[email protected]:/~git/rust-lang/cargo.git",
    ] {
        print(url);
        eprintln!();
    }
}

fn print(url: &str) {
    let scp = gix_url::parse(url.into()).unwrap();
    let canonical = scp.clone().serialize_alternate_form(false).to_bstring();
    eprintln!("SCP-like: {url}");
    eprintln!("path:     {}", std::str::from_utf8(&scp.path).unwrap());
    eprintln!("SSH URL:  {}", std::str::from_utf8(&canonical).unwrap());
}

Output:

SCP-like: [email protected]:rust-lang/cargo.git
path:     rust-lang/cargo.git
SSH URL:  ssh://[email protected]/rust-lang/cargo.git

SCP-like: [email protected]:/rust-lang/cargo.git
path:     /rust-lang/cargo.git
SSH URL:  ssh://[email protected]/rust-lang/cargo.git

SCP-like: [email protected]:~/rust-lang/cargo.git
path:     ~/rust-lang/cargo.git
SSH URL:  ssh://[email protected]/~/rust-lang/cargo.git

SCP-like: [email protected]:~git/rust-lang/cargo.git
path:     ~git/rust-lang/cargo.git
SSH URL:  ssh://[email protected]/~git/rust-lang/cargo.git

SCP-like: [email protected]:/~/rust-lang/cargo.git
path:     ~/rust-lang/cargo.git
SSH URL:  ssh://[email protected]/~/rust-lang/cargo.git

SCP-like: [email protected]:/~git/rust-lang/cargo.git
path:     ~git/rust-lang/cargo.git
SSH URL:  ssh://[email protected]/~git/rust-lang/cargo.git

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