Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

Keep legacy project approvals within SSH port boundaries (#4606)

Maximilian Roos89e604084808
diff --git a/src/config/user/project_match.rs b/src/config/user/project_match.rs
index 6ad74ec0d..2dbe8ebae 100644
--- a/src/config/user/project_match.rs
+++ b/src/config/user/project_match.rs
@@ -35,10 +35,11 @@
//! The release after v0.80.0 changed the identifier of some remotes: a
//! non-default SSH port joined the host, hosts were lowercased, and
//! `git+ssh://` URLs began to parse. So that keys saved under the old
-//! identifier keep applying, a [`ProjectId`] carries it too where it differs
-//! (see [`legacy_project_identifier`]). Keys matching it count as least
-//! specific: [`matching_keys`] returns them before every key matching the
-//! current identifier, ordered among themselves by the usual rules. Settings
+//! identifier keep applying, a [`ProjectId`] carries it where it differs and
+//! still identifies the same server (see [`legacy_project_identifier`]).
+//! Keys matching it count as least specific: [`matching_keys`] returns them
+//! before every key matching the current identifier, ordered among themselves
+//! by the usual rules. Settings
//! under a current key override settings under a legacy key, and approvals
//! under either key apply, so approving a new command (recorded under the
//! current identifier) keeps the earlier approvals in force. The per-project
@@ -279,14 +280,14 @@ mod tests {
fn test_legacy_identifier_keys_rank_below_current_ones() {
let projects = BTreeMap::from([
("*".to_string(), "all"),
- ("git.example/*".to_string(), "legacy-host"),
- ("git.example/owner/repo".to_string(), "legacy-exact"),
- ("git.example:2222/*".to_string(), "host"),
- ("git.example:2222/owner/repo".to_string(), "exact"),
+ ("Git.example/*".to_string(), "legacy-host"),
+ ("Git.example/owner/repo".to_string(), "legacy-exact"),
+ ("git.example/*".to_string(), "host"),
+ ("git.example/owner/repo".to_string(), "exact"),
]);
let project = ProjectId::with_legacy(
- "git.example:2222/owner/repo".to_string(),
- Some("git.example/owner/repo".to_string()),
+ "git.example/owner/repo".to_string(),
+ Some("Git.example/owner/repo".to_string()),
);
// `*` matches both identifiers and ranks with the current ones.
assert_eq!(
diff --git a/src/git/repository/remotes.rs b/src/git/repository/remotes.rs
index e66461efd..176863144 100644
--- a/src/git/repository/remotes.rs
+++ b/src/git/repository/remotes.rs
@@ -407,7 +407,7 @@ impl Repository {
///
/// This identifier keys approved commands and `[projects."…"]` settings.
/// For a remote whose identifier changed after v0.80.0, it also carries
- /// the old one, so keys saved under it keep applying.
+ /// the old one where it still identifies the same server.
///
/// Result is cached in the repository's shared cache (same for all clones).
pub fn project_identifier(&self) -> anyhow::Result<ProjectId> {
diff --git a/src/git/url.rs b/src/git/url.rs
index 936e061b8..5dece332c 100644
--- a/src/git/url.rs
+++ b/src/git/url.rs
@@ -460,7 +460,8 @@ impl GitRemoteUrl {
/// The project identifier Worktrunk v0.80.0 and earlier derived from a remote
/// URL, frozen so `approvals.toml` and `[projects."…"]` keys written under it
-/// keep applying (read by [`ProjectId`](crate::config::ProjectId)).
+/// keep applying where they identify the same server
+/// (read by [`ProjectId`](crate::config::ProjectId)).
///
/// The release after v0.80.0 moved remote parsing to `gix-url`, which changed
/// the identifier for three kinds of remote. Saved keys use the old spelling:
@@ -470,13 +471,17 @@ impl GitRemoteUrl {
/// - `git+ssh://` and `ssh+git://`: these didn't parse, so the identifier was
/// the raw URL minus `.git`, as for any other unparsed URL.
///
-/// Returns `None` for an `ssh://` URL with an IPv6 host, which the old parser
-/// truncated at its first `:` (`[2001/o/r`). That identifier was shared by
-/// every IPv6 server with the same leading group, the collision the `gix-url`
-/// parser fixed. The SCP form kept the rest of the address in the path, so
-/// its identifier stayed distinct and still counts.
+/// For `ssh://` URLs, returns `None` when parsing fails or the old identifier
+/// lost server identity: non-default ports were dropped, and IPv6 hosts were
+/// truncated at their first `:` (`[2001/o/r`). The SCP form kept the rest of
+/// the address in the path, so its identifier stayed distinct and still counts.
pub(crate) fn legacy_project_identifier(url: &str) -> Option<String> {
let trimmed = url.trim();
+ if trimmed.starts_with("ssh://")
+ && GitRemoteUrl::parse(trimmed).is_none_or(|remote| remote.ssh_port.is_some())
+ {
+ return None;
+ }
let parse = || -> Option<(&str, String, String)> {
let (host, path) = if let Some(rest) = trimmed
.strip_prefix("https://")
@@ -889,15 +894,19 @@ mod tests {
/// The v0.80.0 identifier, for remotes whose identifier changed and for
/// ordinary ones where it didn't. Values are what the v0.80.0 binary's
- /// `wt config show` printed, except the truncated `ssh://` IPv6 host
- /// (`[2001/o/r`), which gets none.
+ /// `wt config show` printed, except ambiguous port-less and truncated
+ /// IPv6 identifiers, which get none.
#[test]
fn test_legacy_project_identifier() {
let cases = [
+ ("ssh://[email protected]:2222/o/r.git", None),
+ ("ssh://[email protected]:2222/o/r.git", None),
+ ("ssh://[email protected]:65536/o/r.git", None),
(
- "ssh://[email protected]:2222/o/r.git",
- Some("host.example/o/r"),
+ "ssh://[email protected]:22/o/r.git",
+ Some("Host.example/o/r"),
),
+ ("ssh://[email protected]/o/r.git", Some("host.example/o/r")),
("[email protected]:o/r.git", Some("GitHub.com/o/r")),
("https://GitHub.com/O/R.git", Some("GitHub.com/O/R")),
(
diff --git a/tests/integration_tests/repository.rs b/tests/integration_tests/repository.rs
index ad3ae6075..48a817bf7 100644
--- a/tests/integration_tests/repository.rs
+++ b/tests/integration_tests/repository.rs
@@ -384,8 +384,7 @@ fn test_project_identifier_ipv6_hosts_are_distinct() {
/// The identifier selects both saved approvals and `[projects]` settings. The
/// ordinary SSH and HTTPS forms of one project share one entry. A remote on a
-/// non-default SSH port has its own identifier, but entries keyed on the
-/// port-less identifier v0.80.0 gave it still apply, below its own.
+/// non-default SSH port must not inherit entries for the default port.
#[test]
fn test_project_identifier_selects_approvals_and_project_settings() {
use worktrunk::config::{Approvals, UserConfig};
@@ -396,7 +395,7 @@ fn test_project_identifier_selects_approvals_and_project_settings() {
project_identifier_for_remote("ssh://[email protected]:22/team/project.git");
let https = project_identifier_for_remote("https://gitlab.example.com/team/project.git");
assert_eq!(ported, "gitlab.example.com:2222/team/project");
- assert_eq!(ported.legacy(), Some("gitlab.example.com/team/project"));
+ assert_eq!(ported.legacy(), None);
assert_eq!(default_port, "gitlab.example.com/team/project");
assert_eq!(default_port.legacy(), None);
assert_eq!(https, default_port);
@@ -413,7 +412,7 @@ approved-commands = ["npm test"]
.unwrap();
assert!(approvals.is_command_approved(&https, "npm install"));
assert!(!approvals.is_command_approved(&https, "npm test"));
- assert!(approvals.is_command_approved(&ported, "npm install"));
+ assert!(!approvals.is_command_approved(&ported, "npm install"));
assert!(approvals.is_command_approved(&ported, "npm test"));
let config: UserConfig = toml::from_str(
@@ -426,7 +425,7 @@ worktree-path = "default-port"
)
.unwrap();
assert_eq!(config.worktree_path_for_project(&https), "default-port");
- assert_eq!(config.worktree_path_for_project(&ported), "default-port");
+ assert_eq!(config.worktree_path_for_project(&ported), "global");
let config: UserConfig = toml::from_str(
r#"
@@ -446,10 +445,6 @@ worktree-path = "port-2222"
/// `wt switch --create` runs the approved hook, in the worktree path the
/// legacy entry names.
#[rstest::rstest]
-#[case::ssh_port(
- "ssh://[email protected]:2222/team/app.git",
- "gitlab.example.com/team/app"
-)]
#[case::uppercase_host("[email protected]:team/app.git", "GitHub.com/team/app")]
#[case::git_plus_ssh(
"git+ssh://[email protected]/team/app.git",
@@ -485,6 +480,44 @@ fn test_switch_honors_entries_under_legacy_identifier(#[case] url: &str, #[case]
assert!(worktree.join("hook-ran.txt").exists());
}
+/// A port-less approval cannot authorize an alias on a different SSH server.
+#[test]
+fn test_project_alias_approval_respects_ssh_port() {
+ let mut repo = TestRepo::with_initial_commit();
+ repo.setup_remote("main");
+ repo.git_command()
+ .args([
+ "remote",
+ "set-url",
+ "origin",
+ "ssh://[email protected]:2222/o/r.git",
+ ])
+ .run()
+ .unwrap();
+ repo.write_project_config(
+ r#"[aliases]
+probe = "echo ran > alias-ran.txt"
+"#,
+ );
+ for (key, allowed) in [
+ ("host.example/o/r", false),
+ ("host.example/*", false),
+ ("host.example:2222/o/r", true),
+ ] {
+ repo.write_test_approvals(&format!(
+ "[projects.'{key}']\napproved-commands = [\"echo ran > alias-ran.txt\"]\n"
+ ));
+ let output = repo.wt_command().arg("probe").output().unwrap();
+ assert_eq!(
+ output.status.success(),
+ allowed,
+ "key: {key}, stderr: {}",
+ String::from_utf8_lossy(&output.stderr)
+ );
+ assert_eq!(repo.root_path().join("alias-ran.txt").exists(), allowed);
+ }
+}
+
#[test]
fn test_project_identifier_no_remote_fallback() {
let repo = TestRepo::with_initial_commit();