Skip to content

Commit 5ccb39a

Browse files
committed
Make addAuthFromNetrc ignore ENOTDIR errors
The function already returns early if the specified 'netrc' configuration points to a non existing file, but currently returns an error if the OS reports ENOTDIR: This will happen if the $HOME directory of the specified user points to a file instead a directory - something eg. 'void linux' does for user 'nobody': ``` $ grep nobody /etc/passwd nobody:x:99:99:Unprivileged User:/dev/null:/bin/false ``` go-getter then attempts to open `/dev/null/.netrc` which fails with ENOTDIR - something that should just be threated the same way as a non existing file (in this case)
1 parent c12e42f commit 5ccb39a

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

netrc.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/url"
66
"os"
77
"runtime"
8+
"syscall"
89

910
"github.com/bgentry/go-netrc/netrc"
1011
"github.com/mitchellh/go-homedir"
@@ -38,7 +39,7 @@ func addAuthFromNetrc(u *url.URL) error {
3839
// If the file is not a file, then do nothing
3940
if fi, err := os.Stat(path); err != nil {
4041
// File doesn't exist, do nothing
41-
if os.IsNotExist(err) {
42+
if serr, ok := err.(*os.PathError); ok && (os.IsNotExist(serr.Err) || serr.Err == syscall.ENOTDIR) {
4243
return nil
4344
}
4445

netrc_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,43 @@ func TestAddAuthFromNetrc_hasUsername(t *testing.T) {
6161
t.Fatalf("Mismatch: %q != %q", actual, expected)
6262
}
6363
}
64+
65+
func TestAddAuthFromNetrc_isNotExist(t *testing.T) {
66+
defer tempEnv(t, "NETRC", "./testdata/netrc/_does_not_exist")()
67+
68+
u, err := url.Parse("http://example.com")
69+
if err != nil {
70+
t.Fatalf("err: %s", err)
71+
}
72+
73+
if err := addAuthFromNetrc(u); err != nil {
74+
t.Fatalf("err: %s", err)
75+
}
76+
77+
// no netrc, no change:
78+
expected := "http://example.com"
79+
actual := u.String()
80+
if expected != actual {
81+
t.Fatalf("Mismatch: %q != %q", actual, expected)
82+
}
83+
}
84+
85+
func TestAddAuthFromNetrc_isNotADirectory(t *testing.T) {
86+
defer tempEnv(t, "NETRC", "./testdata/netrc/basic/parent-not-a-dir")()
87+
88+
u, err := url.Parse("http://example.com")
89+
if err != nil {
90+
t.Fatalf("err: %s", err)
91+
}
92+
93+
if err := addAuthFromNetrc(u); err != nil {
94+
t.Fatalf("err: %s", err)
95+
}
96+
97+
// no netrc, no change:
98+
expected := "http://example.com"
99+
actual := u.String()
100+
if expected != actual {
101+
t.Fatalf("Mismatch: %q != %q", actual, expected)
102+
}
103+
}

0 commit comments

Comments
 (0)