Skip to content

Commit 7b4dbe0

Browse files
mikolalysenkoclaude
andcommitted
Strip credentials from the VEX product id taken from the git remote
For an origin on any host other than github.com, gitlab.com or bitbucket.org, VEX product auto-detection used the raw remote URL as the product id. A CI-style origin such as https://gitlab-ci-token:<TOKEN>@gitlab.example.com/g/r.git therefore wrote the token into the OpenVEX document, which is meant to be shared or committed. Drop the userinfo (URL and scp-style forms), query and fragment before returning the fallback id. The normalized hosts already skipped the userinfo. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 431b818 commit 7b4dbe0

1 file changed

Lines changed: 140 additions & 3 deletions

File tree

‎crates/socket-patch-core/src/vex/product.rs‎

Lines changed: 140 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
//! identifier when the repo IS the product. GitHub/GitLab/
66
//! Bitbucket URLs are normalized to
77
//! `pkg:<github|gitlab|bitbucket>/<owner>/<name>`; anything else
8-
//! is returned as the raw URL.
8+
//! is returned as the URL with any credentials (userinfo), query
9+
//! and fragment removed.
910
//! 2. `package.json` (npm) → `pkg:npm/<name>@<version>`
1011
//! 3. `pyproject.toml` (PyPI) → `pkg:pypi/<name>@<version>`
1112
//! 4. `Cargo.toml` (Cargo) → `pkg:cargo/<name>@<version>`
@@ -552,8 +553,18 @@ fn parse_git_config_value(raw: &str) -> String {
552553
/// * `https://github.com/owner/repo` → `pkg:github/owner/repo`
553554
/// * Same shapes for `gitlab.com` (→ `pkg:gitlab`) and `bitbucket.org`
554555
/// (→ `pkg:bitbucket`).
555-
/// * Anything else (self-hosted gitea, generic SSH, etc.) → URL as-is.
556+
/// * Anything else (self-hosted gitea, generic SSH, etc.) → the URL with
557+
/// its userinfo, query and fragment removed.
558+
///
559+
/// The identifier lands in a VEX document that is meant to be committed,
560+
/// so it must never carry credentials: a CI-style origin such as
561+
/// `https://gitlab-ci-token:<TOKEN>@gitlab.example.com/g/r.git` would
562+
/// otherwise publish the token. The query and fragment are dropped up
563+
/// front (they are not part of a repository's identity, and a token can
564+
/// ride there too), the userinfo on the fallback path
565+
/// ([`split_remote_host_path`] already skips it for the normalized hosts).
556566
fn remote_url_to_purl(url: &str) -> String {
567+
let url = strip_url_query_fragment(url);
557568
if let Some((host, path)) = split_remote_host_path(url) {
558569
// Trim slashes BEFORE stripping `.git`: a URL like
559570
// `https://github.com/owner/repo.git/` carries a trailing
@@ -578,7 +589,41 @@ fn remote_url_to_purl(url: &str) -> String {
578589
}
579590
}
580591
}
581-
url.to_string()
592+
strip_url_userinfo(url)
593+
}
594+
595+
/// Cut a URL at its first `?` or `#`. A trailing bare `?`/`#` goes too.
596+
fn strip_url_query_fragment(url: &str) -> &str {
597+
match url.find(['?', '#']) {
598+
Some(idx) => &url[..idx],
599+
None => url,
600+
}
601+
}
602+
603+
/// Remove a `user[:password]@` prefix from a remote URL.
604+
///
605+
/// * `scheme://[userinfo@]host[:port]/path` — the userinfo is everything
606+
/// up to the LAST `@` of the authority (the part before the first `/`
607+
/// after `://`), since an unencoded `@` in a password still precedes
608+
/// the host's.
609+
/// * scp-style `[user@]host:path` (git treats a `:` before any `/` as
610+
/// this form) — everything up to the last `@` before the first `/`,
611+
/// but only when a `:` follows it; otherwise the `@` belongs to the
612+
/// path (`host:repo@v1`) or the string is not a remote URL at all.
613+
fn strip_url_userinfo(url: &str) -> String {
614+
if let Some(scheme_end) = url.find("://") {
615+
let (scheme, rest) = url.split_at(scheme_end + 3);
616+
let authority_end = rest.find('/').unwrap_or(rest.len());
617+
return match rest[..authority_end].rfind('@') {
618+
Some(at) => format!("{scheme}{}", &rest[at + 1..]),
619+
None => url.to_string(),
620+
};
621+
}
622+
let prefix_end = url.find('/').unwrap_or(url.len());
623+
match url[..prefix_end].rfind('@') {
624+
Some(at) if url[at + 1..prefix_end].contains(':') => url[at + 1..].to_string(),
625+
_ => url.to_string(),
626+
}
582627
}
583628

584629
/// Pull `(host, path)` out of a git remote URL. Returns `None` for
@@ -812,6 +857,98 @@ mod tests {
812857
assert_eq!(remote_url_to_purl(raw), raw);
813858
}
814859

860+
/// A CI-style origin on a self-hosted forge carries a token in its
861+
/// userinfo. The raw-URL fallback must drop it: the product `@id`
862+
/// lands in a committed VEX document.
863+
#[test]
864+
fn remote_url_unknown_host_strips_credentials() {
865+
assert_eq!(
866+
remote_url_to_purl("https://gitlab-ci-token:glcbt-SECRET@gitlab.example.com/g/r.git"),
867+
"https://gitlab.example.com/g/r.git"
868+
);
869+
// Username only.
870+
assert_eq!(
871+
remote_url_to_purl("https://deploy@git.example.com/team/repo.git"),
872+
"https://git.example.com/team/repo.git"
873+
);
874+
// An unencoded `@` inside the password: the LAST `@` of the
875+
// authority ends the userinfo.
876+
assert_eq!(
877+
remote_url_to_purl("https://user:p@ss@git.example.com:8443/team/repo.git"),
878+
"https://git.example.com:8443/team/repo.git"
879+
);
880+
// ssh:// with a port, and git+https.
881+
assert_eq!(
882+
remote_url_to_purl("ssh://git@git.example.com:2222/team/repo.git"),
883+
"ssh://git.example.com:2222/team/repo.git"
884+
);
885+
assert_eq!(
886+
remote_url_to_purl("git+https://oauth2:tok@git.example.com/team/repo"),
887+
"git+https://git.example.com/team/repo"
888+
);
889+
}
890+
891+
/// The same token on a NORMALIZED host never reaches the output
892+
/// either (the purl drops the authority entirely), even when an `@`
893+
/// inside the password defeats `split_remote_host_path`'s
894+
/// first-`@` split.
895+
#[test]
896+
fn remote_url_normalized_host_never_leaks_credentials() {
897+
assert_eq!(
898+
remote_url_to_purl("https://x-access-token:ghs_SECRET@github.com/o/r.git"),
899+
"pkg:github/o/r"
900+
);
901+
let out = remote_url_to_purl("https://user:p@ss@github.com/o/r.git");
902+
assert!(!out.contains("p@ss") && !out.contains("user"), "{out}");
903+
}
904+
905+
/// scp-style remotes on unknown hosts lose their `user@` too; an `@`
906+
/// that belongs to the path (no `:` after it) is left alone.
907+
#[test]
908+
fn remote_url_unknown_host_scp_style_userinfo() {
909+
assert_eq!(
910+
remote_url_to_purl("git@git.example.com:team/repo.git"),
911+
"git.example.com:team/repo.git"
912+
);
913+
assert_eq!(
914+
remote_url_to_purl("deploy@git.example.com:team/repo.git"),
915+
"git.example.com:team/repo.git"
916+
);
917+
assert_eq!(remote_url_to_purl("host:repo@v1"), "host:repo@v1");
918+
// An `@` after the first `/` is path, not userinfo.
919+
assert_eq!(
920+
remote_url_to_purl("https://git.example.com/team/repo@v2"),
921+
"https://git.example.com/team/repo@v2"
922+
);
923+
assert_eq!(
924+
remote_url_to_purl("/srv/git/repo@v2.git"),
925+
"/srv/git/repo@v2.git"
926+
);
927+
}
928+
929+
/// Query strings and fragments are not part of a repository's
930+
/// identity and can carry tokens: dropped on every path, including
931+
/// the normalized hosts (where they used to leak into the purl name).
932+
#[test]
933+
fn remote_url_query_and_fragment_are_dropped() {
934+
assert_eq!(
935+
remote_url_to_purl("https://git.example.com/team/repo.git?private_token=SECRET#frag"),
936+
"https://git.example.com/team/repo.git"
937+
);
938+
assert_eq!(
939+
remote_url_to_purl("https://git.example.com/team/repo#readme"),
940+
"https://git.example.com/team/repo"
941+
);
942+
assert_eq!(
943+
remote_url_to_purl("https://github.com/o/r.git?token=SECRET"),
944+
"pkg:github/o/r"
945+
);
946+
assert_eq!(
947+
remote_url_to_purl("git@gitlab.com:foo/bar.git#main"),
948+
"pkg:gitlab/foo/bar"
949+
);
950+
}
951+
815952
#[test]
816953
fn remote_url_ssh_protocol_form() {
817954
assert_eq!(

0 commit comments

Comments
 (0)