Skip to content

Commit 75fa797

Browse files
authored
fix(age): expand ~/ in age.ssh-key-path (gopasspw#3474)
age.ssh-key-path (and the --age-ssh-key-path flag) was read raw and passed to fsutil.IsDir/IsFile, which do not expand a leading ~/. A custom path like ~/custom-keys was therefore silently ignored; only the default ~/.ssh discovery ran. Expand it via fsutil.ExpandHomedir in New, the single constructor all callers flow through, matching how storage and mount paths already handle ~. Adds a unit test and a behavioral regression test; both fail on the un-fixed code. Assisted-by: Claude-Code:GLM-5.2 Signed-off-by: Zexin Yuan <git@yzx9.xyz>
1 parent c1cb9c1 commit 75fa797

4 files changed

Lines changed: 84 additions & 1 deletion

File tree

‎docs/config.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ This is a list of available options:
8686
| ------------------------------- | -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------- |
8787
| `age.agent-enabled` | `bool` | Enable the persistent Agent for caching of age identities. This will remove the need to repeatedly enter the passphrase. EXPERIMENTAL. |
8888
| `age.agent-timeout` | `int` | Automatically lock the agent after this many seconds of inactivity. | `0` |
89-
| `age.ssh-key-path` | `string` | Path to a custom SSH key file or directory containing SSH keys. If set, this will be used to load SSH identities for age. If not set, the default SSH directory is used. | `~/.ssh` |
89+
| `age.ssh-key-path` | `string` | Path to a custom SSH key file or directory containing SSH keys. A leading `~/` is expanded to the home directory. If set, this will be used to load SSH identities for age. If not set, the default SSH directory is used. | `~/.ssh` |
9090
| `age.usekeychain` | `bool` | Use the OS keychain to cache age passphrases. | `false` |
9191
| `audit.concurrency` | `int` | Number of concurrent audit workers. | `` |
9292
| `audit.hibp-dump-file` | `string` | Specify a HIBPv2 Dump file (sorted) if you want `audit` to check password hashes against this file. | `None` |

‎internal/backend/crypto/age/age.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/gopasspw/gopass/pkg/appdir"
1818
"github.com/gopasspw/gopass/pkg/ctxutil"
1919
"github.com/gopasspw/gopass/pkg/debug"
20+
"github.com/gopasspw/gopass/pkg/fsutil"
2021
)
2122

2223
const (
@@ -54,6 +55,9 @@ func New(ctx context.Context, sshKeyPath string) (*Age, error) {
5455
return nil, err
5556
}
5657

58+
// Expand ~/ so a custom path set via age.ssh-key-path works without shell expansion.
59+
sshKeyPath = fsutil.ExpandHomedir(sshKeyPath)
60+
5761
a := &Age{
5862
ghCache: ghc,
5963
recpCache: rc,

‎internal/backend/crypto/age/age_test.go‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package age
22

33
import (
4+
"path/filepath"
45
"testing"
56

67
"github.com/gopasspw/gopass/pkg/debug"
@@ -15,6 +16,31 @@ func TestNew(t *testing.T) {
1516
assert.NotNil(t, a)
1617
}
1718

19+
func TestNewExpandsSshKeyPathTilde(t *testing.T) {
20+
td := t.TempDir()
21+
t.Setenv("GOPASS_HOMEDIR", td)
22+
23+
// A leading ~/ is expanded to the home dir (appdir.UserHome honors GOPASS_HOMEDIR).
24+
a, err := New(t.Context(), "~/custom-ssh")
25+
require.NoError(t, err)
26+
assert.Equal(t, filepath.Join(td, "custom-ssh"), a.sshKeyPath)
27+
28+
// Absolute paths are left untouched by fsutil.ExpandHomedir.
29+
a2, err := New(t.Context(), "/opt/keys")
30+
require.NoError(t, err)
31+
assert.Equal(t, "/opt/keys", a2.sshKeyPath)
32+
33+
// ~user/ and a bare ~ are NOT expanded: ExpandHomedir only matches the ~/ prefix.
34+
a3, err := New(t.Context(), "~alice/keys")
35+
require.NoError(t, err)
36+
assert.Equal(t, "~alice/keys", a3.sshKeyPath)
37+
38+
// Empty stays empty (the len>1 guard inside ExpandHomedir).
39+
a4, err := New(t.Context(), "")
40+
require.NoError(t, err)
41+
assert.Empty(t, a4.sshKeyPath)
42+
}
43+
1844
func TestInitialized(t *testing.T) {
1945
ctx := t.Context()
2046
a, err := New(ctx, "")
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package age
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
// TestGetSSHIdentitiesCustomSshKeyPathTilde is a regression test for the bug
13+
// where a custom age.ssh-key-path with a leading ~/ was silently ignored:
14+
// fsutil.IsDir("~/custom-ssh") is false (no shell expansion), so the custom
15+
// path was skipped and only the default ~/.ssh discovery ran.
16+
//
17+
// Setup: GOPASS_HOMEDIR points at a temp dir, the custom dir lives at
18+
// <td>/custom-ssh, and there is NO <td>/.ssh and no GOPASS_SSH_DIR. So the only
19+
// way getSSHIdentities returns non-error is if the ~/custom-ssh path is
20+
// expanded to <td>/custom-ssh and recognized as a directory.
21+
//
22+
// Before the fix this returned ErrNoSSHDir; after the fix the dir is found and
23+
// searched (no error).
24+
func TestGetSSHIdentitiesCustomSshKeyPathTilde(t *testing.T) {
25+
td := t.TempDir()
26+
t.Setenv("GOPASS_HOMEDIR", td)
27+
t.Setenv("GOPASS_SSH_DIR", "")
28+
29+
require.NoError(t, os.MkdirAll(filepath.Join(td, "custom-ssh"), 0o700))
30+
31+
// The package-level sshCache is never reset elsewhere; clear it so this test
32+
// observes a fresh filesystem read, and clear it again afterward so a
33+
// populated cache cannot leak to later tests. Safe because no test in this
34+
// package touches sshCache in parallel. (same-package access to the var.)
35+
sshCacheMu.Lock()
36+
sshCache = nil
37+
sshCacheMu.Unlock()
38+
t.Cleanup(func() {
39+
sshCacheMu.Lock()
40+
sshCache = nil
41+
sshCacheMu.Unlock()
42+
})
43+
44+
a, err := New(t.Context(), "~/custom-ssh")
45+
require.NoError(t, err)
46+
47+
// Before the fix this returned ErrNoSSHDir (literal ~ is not a dir). After
48+
// the fix the expanded dir is found and searched: assert no error and that a
49+
// (possibly empty) identities map is returned.
50+
ids, err := a.getSSHIdentities(t.Context())
51+
require.NoError(t, err)
52+
assert.NotNil(t, ids)
53+
}

0 commit comments

Comments
 (0)
⌂ 换网址