Skip to content

Fix SSH agent socket path tilde expansion#329

Merged
datlechin merged 3 commits intomainfrom
fix/ssh-agent-tilde-expansion
Mar 15, 2026
Merged

Fix SSH agent socket path tilde expansion#329
datlechin merged 3 commits intomainfrom
fix/ssh-agent-tilde-expansion

Conversation

@datlechin
Copy link
Copy Markdown
Collaborator

@datlechin datlechin commented Mar 15, 2026

Summary

  • Fix SSH agent connections failing with rc=-42 when socket paths contain ~ (e.g., 1Password's ~/Library/Group Containers/2BUA8C4S2C.com.1password/t/agent.sock)
  • setenv() doesn't expand ~ like shell commands do — now applying SSHPathUtilities.expandTilde() before passing to setenv("SSH_AUTH_SOCK", ...)
  • Extract duplicated expandPath() from PublicKeyAuthenticator and SSHConfigParser into shared SSHPathUtilities utility

Test plan

  • Connect with SSH Agent + 1Password preset — should succeed (previously rc=-42)
  • Connect with SSH Agent + custom ~/.ssh/agent.sock path — should expand correctly
  • Connect with SSH Agent + system default (SSH_AUTH_SOCK) — should still work (no expansion needed)
  • Connect with SSH private key using ~/.ssh/id_rsa — should still work (uses shared utility)
  • Run SSHConfigurationTests — 4 new tilde expansion tests pass

Closes #322

Summary by CodeRabbit

  • New Features

    • Consistent tilde (~) expansion for SSH socket and key paths so home-directory shortcuts resolve correctly across authentication flows.
  • Bug Fixes

    • Restores SSH agent connectivity when socket paths contain "~" (e.g., external agents).
  • Tests

    • Added unit tests covering tilde expansion for various path formats.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 15, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: f8a1ab96-9c21-4575-b312-58509b9b023a

📥 Commits

Reviewing files that changed from the base of the PR and between 85fd896 and 663c176.

📒 Files selected for processing (2)
  • CHANGELOG.md
  • TablePro/Core/SSH/Auth/PublicKeyAuthenticator.swift

📝 Walkthrough

Walkthrough

Centralizes SSH path expansion into SSHPathUtilities.expandTilde(_:) and updates three SSH-related sources to use it. Adds tests covering tilde expansion and updates the changelog entry.

Changes

Cohort / File(s) Summary
New Utility Module
TablePro/Core/SSH/SSHPathUtilities.swift
Adds SSHPathUtilities with static func expandTilde(_:) to expand ~ and ~/ to the current user's home directory.
SSH Authenticators & Parser
TablePro/Core/SSH/Auth/AgentAuthenticator.swift, TablePro/Core/SSH/Auth/PublicKeyAuthenticator.swift, TablePro/Core/SSH/SSHConfigParser.swift
Replaces local tilde-expansion logic with calls to SSHPathUtilities.expandTilde(_:); removes now-unused private helper(s).
Tests
TableProTests/Core/SSH/SSHConfigurationTests.swift
Adds four tests validating tilde expansion for subpaths, bare ~, absolute paths, and empty string.
Changelog
CHANGELOG.md
Notes fix for SSH agent connections failing when socket path contains a tilde.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐇 I bounced along a tilde trail,

home unrolled where paths went pale.
One little util, tidy and bright,
now guides the agent through the night. ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 36.36% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and concisely summarizes the main change: fixing SSH agent socket path tilde expansion, which is the primary objective of the changeset.
Linked Issues check ✅ Passed The pull request fully addresses issue #322 by expanding tilde-containing SSH agent socket paths before use, fixing the rc=-42 connection failures reported for 1Password SSH agent.
Out of Scope Changes check ✅ Passed All changes directly support the tilde expansion fix: new SSHPathUtilities utility, refactored path expansion logic, updated AgentAuthenticator/PublicKeyAuthenticator/SSHConfigParser, tests, and CHANGELOG entry.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/ssh-agent-tilde-expansion
📝 Coding Plan
  • Generate coding plan for human review comments

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
TablePro/Core/SSH/SSHPathUtilities.swift (1)

11-22: Consider handling ~username syntax for completeness.

The implementation correctly handles ~ and ~/path, which covers the primary use cases for SSH socket paths. However, SSH config also supports ~username/path syntax for other users' home directories.

If this edge case isn't needed for the current use cases (1Password agent socket, private keys), this is fine to defer. Otherwise, NSString.expandingTildeInPath handles all tilde forms natively.

Alternative using NSString
 static func expandTilde(_ path: String) -> String {
-    if path.hasPrefix("~/") {
-        return FileManager.default.homeDirectoryForCurrentUser
-            .appendingPathComponent(String(path.dropFirst(2)))
-            .path(percentEncoded: false)
-    }
-    if path == "~" {
-        return FileManager.default.homeDirectoryForCurrentUser
-            .path(percentEncoded: false)
-    }
-    return path
+    return (path as NSString).expandingTildeInPath
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@TablePro/Core/SSH/SSHPathUtilities.swift` around lines 11 - 22, The
expandTilde(_:) function only handles "~" and "~/..." but not "~username/...";
replace or augment its logic by using NSString(string:
path).expandingTildeInPath (or manually detect "~" followed by non-slash and
call getpwnam to resolve other users) so that SSHPathUtilities.expandTilde
correctly expands "~username" and "~username/..." as well as the existing cases;
update the function to return the expanded path from
NSString.expandingTildeInPath (or the resolved home directory plus remaining
path) while preserving current behavior for "~" and "~/...".
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@TablePro/Core/SSH/SSHPathUtilities.swift`:
- Line 8: The enum declaration for SSHPathUtilities lacks explicit access
control; update the declaration of enum SSHPathUtilities to include the
appropriate access level (e.g., internal or public depending on cross-module
usage) so the type follows the project's coding guidelines requiring explicit
access control.

---

Nitpick comments:
In `@TablePro/Core/SSH/SSHPathUtilities.swift`:
- Around line 11-22: The expandTilde(_:) function only handles "~" and "~/..."
but not "~username/..."; replace or augment its logic by using NSString(string:
path).expandingTildeInPath (or manually detect "~" followed by non-slash and
call getpwnam to resolve other users) so that SSHPathUtilities.expandTilde
correctly expands "~username" and "~username/..." as well as the existing cases;
update the function to return the expanded path from
NSString.expandingTildeInPath (or the resolved home directory plus remaining
path) while preserving current behavior for "~" and "~/...".

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: de85690e-0561-44b9-9866-d985242bb054

📥 Commits

Reviewing files that changed from the base of the PR and between d2f88c1 and 85fd896.

📒 Files selected for processing (5)
  • TablePro/Core/SSH/Auth/AgentAuthenticator.swift
  • TablePro/Core/SSH/Auth/PublicKeyAuthenticator.swift
  • TablePro/Core/SSH/SSHConfigParser.swift
  • TablePro/Core/SSH/SSHPathUtilities.swift
  • TableProTests/Core/SSH/SSHConfigurationTests.swift


import Foundation

enum SSHPathUtilities {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major

Add explicit access control to the enum declaration.

The coding guidelines require explicit access control on types. Since this utility is used across multiple modules within the SSH subsystem, consider adding internal (or public if needed externally).

Proposed fix
-enum SSHPathUtilities {
+internal enum SSHPathUtilities {

As per coding guidelines: "Always specify access control explicitly (private, internal, public) on extensions and types."

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
enum SSHPathUtilities {
internal enum SSHPathUtilities {
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@TablePro/Core/SSH/SSHPathUtilities.swift` at line 8, The enum declaration for
SSHPathUtilities lacks explicit access control; update the declaration of enum
SSHPathUtilities to include the appropriate access level (e.g., internal or
public depending on cross-module usage) so the type follows the project's coding
guidelines requiring explicit access control.

@datlechin datlechin merged commit 49f3d7b into main Mar 15, 2026
3 checks passed
@datlechin datlechin deleted the fix/ssh-agent-tilde-expansion branch March 15, 2026 10:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: ssh agent tunnel connection fails to connect

1 participant