Skip to content

Commit

Permalink
Make addAuthFromNetrc ignore ENOTDIR errors
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
adrian-bl committed Apr 22, 2023
1 parent c12e42f commit 5ccb39a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
3 changes: 2 additions & 1 deletion netrc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/url"
"os"
"runtime"
"syscall"

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

Expand Down
40 changes: 40 additions & 0 deletions netrc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,43 @@ func TestAddAuthFromNetrc_hasUsername(t *testing.T) {
t.Fatalf("Mismatch: %q != %q", actual, expected)
}
}

func TestAddAuthFromNetrc_isNotExist(t *testing.T) {
defer tempEnv(t, "NETRC", "./testdata/netrc/_does_not_exist")()

u, err := url.Parse("http://example.com")
if err != nil {
t.Fatalf("err: %s", err)
}

if err := addAuthFromNetrc(u); err != nil {
t.Fatalf("err: %s", err)
}

// no netrc, no change:
expected := "http://example.com"
actual := u.String()
if expected != actual {
t.Fatalf("Mismatch: %q != %q", actual, expected)
}
}

func TestAddAuthFromNetrc_isNotADirectory(t *testing.T) {
defer tempEnv(t, "NETRC", "./testdata/netrc/basic/parent-not-a-dir")()

u, err := url.Parse("http://example.com")
if err != nil {
t.Fatalf("err: %s", err)
}

if err := addAuthFromNetrc(u); err != nil {
t.Fatalf("err: %s", err)
}

// no netrc, no change:
expected := "http://example.com"
actual := u.String()
if expected != actual {
t.Fatalf("Mismatch: %q != %q", actual, expected)
}
}

0 comments on commit 5ccb39a

Please sign in to comment.