Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(cli): drop git credentials before storing a remote url (#95917)

Aleksander Błaszkiewiczfcbcec2ef4d1
diff --git a/cli/.sampo/changesets/strip-git-credentials-from-release-metadata.md b/cli/.sampo/changesets/strip-git-credentials-from-release-metadata.md
new file mode 100644
index 000000000000..acf2fdaa79e3
--- /dev/null
+++ b/cli/.sampo/changesets/strip-git-credentials-from-release-metadata.md
@@ -0,0 +1,5 @@
+---
+cargo/posthog-cli: patch
+---
+
+Remove credentials from the git remote URL stored in release metadata. Rotate any token an earlier version stored.
diff --git a/cli/Cargo.lock b/cli/Cargo.lock
index c279bef4525b..0192d636356f 100644
--- a/cli/Cargo.lock
+++ b/cli/Cargo.lock
@@ -2133,6 +2133,7 @@ dependencies = [
"tracing",
"tracing-subscriber",
"tui-textarea",
+ "url",
"urlencoding",
"uuid",
"walkdir",
diff --git a/cli/Cargo.toml b/cli/Cargo.toml
index 92efde5b5b07..648bb2535a16 100644
--- a/cli/Cargo.toml
+++ b/cli/Cargo.toml
@@ -56,6 +56,7 @@ tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
posthog-rs = { version = "0.24.0", default-features = false, features = ["error-tracking"] }
sha2 = "0.10.9"
urlencoding = "2.1.3"
+url = "2"
rayon = "1.11.0"
similar = "2.7"
open = "5"
diff --git a/cli/src/utils/git.rs b/cli/src/utils/git.rs
index f89986edd2ec..1e6ddc0ccd62 100644
--- a/cli/src/utils/git.rs
+++ b/cli/src/utils/git.rs
@@ -3,6 +3,7 @@ use serde::{Deserialize, Serialize};
use std::fs;
use std::io::Read;
use std::path::{Path, PathBuf};
+use url::Url;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GitInfo {
@@ -206,10 +207,11 @@ fn get_remote_url_from_paths(paths: &GitRepositoryPaths) -> Option<String> {
let line = line.trim();
if line.starts_with("url = ") {
let url = line.trim_start_matches("url = ").trim();
- let normalized = if url.ends_with(".git") {
- url.to_string()
+ let sanitized = strip_credentials(url)?;
+ let normalized = if sanitized.ends_with(".git") {
+ sanitized
} else {
- format!("{url}.git")
+ format!("{sanitized}.git")
};
return Some(normalized);
}
@@ -219,6 +221,71 @@ fn get_remote_url_from_paths(paths: &GitRepositoryPaths) -> Option<String> {
None
}
+/// Drops every part of a URL that can carry a credential before it is stored anywhere: the
+/// userinfo component (`user[:pass]@`), the query, and the fragment. CI checkouts commonly
+/// write a remote URL with an embedded credential (e.g. `actions/checkout`'s
+/// `https://x-access-token:<token>@github.com/owner/repo.git`), and that credential must never
+/// reach release metadata.
+///
+/// Returns `None` for a URL whose credential sits where no parser can identify it, rather than
+/// store it. That covers a URL holding an `@` that does not parse, because userinfo must
+/// percent-encode `/`, `?` and `#`, and a URL holding an `@` in its path.
+fn strip_credentials(url: &str) -> Option<String> {
+ // A query or a fragment can hold a token (`?token=`, `#access_token=`) and a git remote
+ // needs neither, so both go before anything else looks at the URL.
+ let trimmed = match url.find(['?', '#']) {
+ Some(index) => &url[..index],
+ None => url,
+ };
+
+ // Dropping the delimiter can drop an `@` with it, which leaves nothing to tell a malformed
+ // credential (`https://user:token?x@host/owner/repo.git`) from a query that holds an `@`.
+ // What remains can be the credential itself, so the input is refused rather than stored.
+ if url.contains('@') && !trimmed.contains('@') {
+ return None;
+ }
+ let url = trimmed;
+
+ // SCP-like SSH remotes (`git@host:owner/repo.git`) have no `://` authority to parse, and
+ // the leading `git` is a fixed SSH username rather than a stored secret. A second `@` sits
+ // in the path, where the rule below applies.
+ if !url.contains("://") {
+ let path = url.split_once(':').map_or("", |(_, path)| path);
+ return if path.contains('@') {
+ None
+ } else {
+ Some(url.to_string())
+ };
+ }
+
+ let Ok(mut parsed) = Url::parse(url) else {
+ return if url.contains('@') {
+ None
+ } else {
+ Some(url.to_string())
+ };
+ };
+
+ // An `@` in the path is a credential written into the wrong position, most often
+ // `https://host/${TOKEN}@host/owner/repo.git` from a CI script that meant to write
+ // `https://${TOKEN}@host/owner/repo.git`. Nothing tells that token from a path segment.
+ if parsed.path().contains('@') {
+ return None;
+ }
+
+ // Return the input untouched when it holds no credential, so the parser never reshapes a
+ // URL that this function does not need to change.
+ if parsed.username().is_empty() && parsed.password().is_none() {
+ return Some(url.to_string());
+ }
+
+ // Both setters fail only for a URL that cannot have an authority, such as `mailto:`. A URL
+ // that parsed with userinfo always has one.
+ parsed.set_username("").ok()?;
+ parsed.set_password(None).ok()?;
+ Some(parsed.to_string())
+}
+
pub fn get_repo_name(git_dir: &Path) -> Option<String> {
get_repo_name_from_paths(&GitRepositoryPaths {
git_dir: git_dir.to_path_buf(),
@@ -229,7 +296,7 @@ pub fn get_repo_name(git_dir: &Path) -> Option<String> {
fn get_repo_name_from_paths(paths: &GitRepositoryPaths) -> Option<String> {
// Try grab it from the configured remote, otherwise just use the directory name
- for config_path in config_paths(&paths.git_dir, &paths.common_dir) {
+ 'configs: for config_path in config_paths(&paths.git_dir, &paths.common_dir) {
if !config_path.exists() {
continue;
}
@@ -242,11 +309,20 @@ fn get_repo_name_from_paths(paths: &GitRepositoryPaths) -> Option<String> {
for line in config_content.lines() {
let line = line.trim();
if line.starts_with("url = ") {
- let url = line.trim_start_matches("url = ");
- if let Some(repo_name) = url.split('/').next_back() {
+ let url = line.trim_start_matches("url = ").trim();
+ // A remote with no path puts the authority in the last segment, so the name
+ // is taken from the sanitized URL. Fall back to the directory name when the
+ // URL cannot be sanitized, rather than name the repository after a credential.
+ let Some(sanitized) = strip_credentials(url) else {
+ break 'configs;
+ };
+ if let Some(repo_name) = sanitized.split('/').next_back() {
let clean_name = repo_name.trim_end_matches(".git");
- return Some(clean_name.to_string());
+ if !clean_name.is_empty() {
+ return Some(clean_name.to_string());
+ }
}
+ break 'configs;
}
}
}
@@ -380,3 +456,177 @@ fn get_env_variable(name: &str) -> Option<String> {
_ => Some(env_variable),
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn strip_credentials_handles_remote_url_shapes() {
+ // `None` means the URL is refused, so no credential can reach release metadata.
+ let cases = [
+ (
+ "github actions checkout token",
+ "https://x-access-token:[email protected]/owner/repo.git",
+ Some("https://github.com/owner/repo.git"),
+ ),
+ (
+ "generic user and password",
+ "https://user:secret@host/owner/repo.git",
+ Some("https://host/owner/repo.git"),
+ ),
+ (
+ "token as the username",
+ "https://[email protected]/owner/repo.git",
+ Some("https://github.com/owner/repo.git"),
+ ),
+ (
+ "host and port are kept",
+ "https://user:[email protected]:8443/owner/repo.git",
+ Some("https://git.example.com:8443/owner/repo.git"),
+ ),
+ (
+ "the path survives the rewrite",
+ "https://x-access-token:[email protected]/owner/repo.git",
+ Some("https://github.com/owner/repo.git"),
+ ),
+ (
+ "a query is dropped, because it can hold a token",
+ "https://github.com/owner/repo.git?token=ghs_abc",
+ Some("https://github.com/owner/repo.git"),
+ ),
+ (
+ "a fragment is dropped, because it can hold a token",
+ "https://github.com/owner/repo.git#access_token=ghs_abc",
+ Some("https://github.com/owner/repo.git"),
+ ),
+ (
+ "a query is dropped from an scp-like ssh remote too",
+ "[email protected]:owner/repo.git?token=ghs_abc",
+ Some("[email protected]:owner/repo.git"),
+ ),
+ (
+ "url without a credential is unchanged",
+ "https://github.com/owner/repo.git",
+ Some("https://github.com/owner/repo.git"),
+ ),
+ (
+ "scp-like ssh remote is unchanged, because `git` is a fixed ssh username",
+ "[email protected]:owner/repo.git",
+ Some("[email protected]:owner/repo.git"),
+ ),
+ (
+ "a token in the path is refused, because nothing tells it from a path segment",
+ "https://github.com/[email protected]/owner/repo.git",
+ None,
+ ),
+ (
+ "any other `@` in the path is refused for the same reason",
+ "https://github.com/owner/[email protected]",
+ None,
+ ),
+ (
+ "a second `@` in an scp-like path is refused too",
+ "[email protected]:ghs_abc123@owner/repo.git",
+ None,
+ ),
+ (
+ "a `?` before the `@` is refused, because dropping the query drops the `@`",
+ "https://user:[email protected]/owner/repo.git",
+ None,
+ ),
+ (
+ "a `#` before the `@` is refused for the same reason",
+ "https://user:ghp_abc123#[email protected]/owner/repo.git",
+ None,
+ ),
+ (
+ "the truncated prefix is refused even when it parses as a host on its own",
+ "https://[email protected]/owner/repo.git",
+ None,
+ ),
+ (
+ "ssh url with a credential",
+ "ssh://user:secret@host/owner/repo.git",
+ Some("ssh://host/owner/repo.git"),
+ ),
+ (
+ "git protocol url is unchanged",
+ "git://github.com/owner/repo.git",
+ Some("git://github.com/owner/repo.git"),
+ ),
+ (
+ "unencoded `/` in a password hides the credential, so the url is refused",
+ "https://user:ab/[email protected]/owner/repo.git",
+ None,
+ ),
+ ];
+
+ for (name, url, expected) in cases {
+ assert_eq!(strip_credentials(url).as_deref(), expected, "case: {name}");
+ }
+ }
+
+ fn write_config(url: &str) -> (tempfile::TempDir, GitRepositoryPaths) {
+ let dir = tempfile::tempdir().unwrap();
+ let git_dir = dir.path().join(".git");
+ fs::create_dir_all(&git_dir).unwrap();
+ fs::write(
+ git_dir.join("config"),
+ format!("[remote \"origin\"]\n\turl = {url}\n"),
+ )
+ .unwrap();
+ let paths = GitRepositoryPaths {
+ git_dir: git_dir.clone(),
+ common_dir: git_dir,
+ worktree_dir: dir.path().to_path_buf(),
+ };
+ (dir, paths)
+ }
+
+ #[test]
+ fn get_remote_url_from_paths_strips_credential_from_config() {
+ let (_dir, paths) =
+ write_config("https://x-access-token:[email protected]/owner/repo.git");
+
+ assert_eq!(
+ get_remote_url_from_paths(&paths),
+ Some("https://github.com/owner/repo.git".to_string())
+ );
+
+ // The `.git` suffix is normalized after the query goes, so the suffix check sees the
+ // real end of the URL.
+ let (_dir, paths) =
+ write_config("https://x-access-token:[email protected]/owner/repo.git?ref=main");
+
+ assert_eq!(
+ get_remote_url_from_paths(&paths),
+ Some("https://github.com/owner/repo.git".to_string())
+ );
+ }
+
+ #[test]
+ fn get_repo_name_from_paths_never_returns_a_credential() {
+ let (_dir, paths) = write_config("https://user:[email protected]/owner/repo.git");
+ assert_eq!(get_repo_name_from_paths(&paths), Some("repo".to_string()));
+
+ // A truncated prefix parses as a host, which would otherwise name the repository
+ // after the credential.
+ let (dir, paths) = write_config("https://[email protected]/owner/repo.git");
+ let name = get_repo_name_from_paths(&paths).unwrap();
+ assert!(
+ !name.contains("ghp_abc123"),
+ "credential leaked into repo name: {name}"
+ );
+ assert_eq!(name, dir.path().file_name().unwrap().to_string_lossy());
+
+ // A remote with no path would otherwise name the repository after the authority.
+ let (dir, paths) = write_config("https://user:[email protected]");
+ let name = get_repo_name_from_paths(&paths).unwrap();
+ assert!(
+ !name.contains("ghp_abc123"),
+ "credential leaked into repo name: {name}"
+ );
+ assert_eq!(name, dir.path().file_name().unwrap().to_string_lossy());
+ }
+}