Skip to content

Commit 8c874a4

Browse files
martinatsynqfmarp242
authored andcommitted
Warn about incorrect .pgpass permissions
Write warning to stderr instead of silently not using it. Writing to stderr is not super great, but this is also what libpq does and for now it's better than nothing. Also skip this check on Windows, which is also what libpq does. See fe-connect.c Fixes #940
1 parent 1445062 commit 8c874a4

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

conn.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"os"
1818
"os/user"
1919
"path/filepath"
20+
"runtime"
2021
"strconv"
2122
"strings"
2223
"sync"
@@ -229,6 +230,8 @@ func (cn *conn) handleDriverSettings(o values) (err error) {
229230
return boolSetting("binary_parameters", &cn.binaryParameters)
230231
}
231232

233+
// TODO: this should probably return errors instead of silently skipping it on
234+
// errors?
232235
func (cn *conn) handlePgpass(o values) {
233236
// if a password was supplied, do not process .pgpass
234237
if _, ok := o["password"]; ok {
@@ -250,14 +253,19 @@ func (cn *conn) handlePgpass(o values) {
250253
}
251254
filename = filepath.Join(userHome, ".pgpass")
252255
}
253-
fileinfo, err := os.Stat(filename)
254-
if err != nil {
255-
return
256-
}
257-
mode := fileinfo.Mode()
258-
if mode&(0x77) != 0 {
259-
// XXX should warn about incorrect .pgpass permissions as psql does
260-
return
256+
257+
// On Win32, the directory is protected, so we don't have to check the file.
258+
if runtime.GOOS != "windows" {
259+
fi, err := os.Stat(filename)
260+
if err != nil {
261+
return
262+
}
263+
if fi.Mode().Perm()&(0x77) != 0 {
264+
fmt.Fprintf(os.Stderr,
265+
"WARNING: password file %q has group or world access; permissions should be u=rw (0600) or less\n",
266+
filename)
267+
return
268+
}
261269
}
262270
file, err := os.Open(filename)
263271
if err != nil {

0 commit comments

Comments
 (0)