Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not rewrite ~ #2496

Merged
merged 2 commits into from
Dec 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions pkg/fsutil/fsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io"
"math/rand"
"os"
"os/user"
"path/filepath"
"regexp"
"strings"
Expand All @@ -26,20 +25,16 @@ func CleanFilename(in string) string {

// CleanPath resolves common aliases in a path and cleans it as much as possible.
func CleanPath(path string) string {
// http://stackoverflow.com/questions/17609732/expand-tilde-to-home-directory
// TODO(GH-2083): We should consider if we really want to rewrite ~
// Only replace ~ if GOPASS_HOMEDIR is set. In that case we do expect any reference
// to the users homedir to be replaced by the value of GOPASS_HOMEDIR. This is mainly
// for testing and experiments. In all other cases we do want to leave ~ as-is.
if len(path) > 1 && path[:2] == "~/" {
usr, _ := user.Current()
dir := usr.HomeDir

if hd := os.Getenv("GOPASS_HOMEDIR"); hd != "" {
dir = hd
return filepath.Clean(hd + path[2:])
}

path = strings.Replace(path, "~/", dir+"/", 1)
}

if p, err := filepath.Abs(path); err == nil {
if p, err := filepath.Abs(path); err == nil && !strings.HasPrefix(path, "~") {
return p
}

Expand Down
23 changes: 12 additions & 11 deletions pkg/fsutil/fsutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"crypto/rand"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"runtime"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -34,21 +35,21 @@ func TestCleanPath(t *testing.T) {
"/home/user/../bob/.password-store": "/home/bob/.password-store",
"/home/user//.password-store": "/home/user/.password-store",
tempdir + "/foo.gpg": tempdir + "/foo.gpg",
}

usr, err := user.Current()
if err == nil {
hd := usr.HomeDir
if gph := os.Getenv("GOPASS_HOMEDIR"); gph != "" {
hd = gph
}

m["~/.password-store"] = hd + "/.password-store"
"~/.password-store": "~/.password-store",
}

for in, out := range m {
got := CleanPath(in)

if strings.HasPrefix(out, "~") {
// skip these tests on windows
if runtime.GOOS == "windows" {
continue
}
assert.Equal(t, out, got)

continue
}
// filepath.Abs turns /home/bob into C:\home\bob on Windows
absOut, err := filepath.Abs(out)
assert.NoError(t, err)
Expand Down