Skip to content

Commit c03f371

Browse files
committed
os: fix UserConfigDir and UserCacheDir don't check relative paths
1 parent 959b3fd commit c03f371

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

src/os/file.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -472,8 +472,8 @@ func TempDir() string {
472472
// On Windows, it returns %LocalAppData%.
473473
// On Plan 9, it returns $home/lib/cache.
474474
//
475-
// If the location cannot be determined (for example, $HOME is not defined),
476-
// then it will return an error.
475+
// If the location cannot be determined (for example, $HOME is not defined) or
476+
// the path in $XDG_CACHE_HOME is relative, then it will return an error.
477477
func UserCacheDir() (string, error) {
478478
var dir string
479479

@@ -506,6 +506,8 @@ func UserCacheDir() (string, error) {
506506
return "", errors.New("neither $XDG_CACHE_HOME nor $HOME are defined")
507507
}
508508
dir += "/.cache"
509+
} else if !filepathlite.IsAbs(dir) {
510+
return "", errors.New("path in $XDG_CACHE_HOME is relative")
509511
}
510512
}
511513

@@ -523,8 +525,8 @@ func UserCacheDir() (string, error) {
523525
// On Windows, it returns %AppData%.
524526
// On Plan 9, it returns $home/lib.
525527
//
526-
// If the location cannot be determined (for example, $HOME is not defined),
527-
// then it will return an error.
528+
// If the location cannot be determined (for example, $HOME is not defined) or
529+
// the path in $XDG_CONFIG_HOME is relative, then it will return an error.
528530
func UserConfigDir() (string, error) {
529531
var dir string
530532

@@ -557,6 +559,8 @@ func UserConfigDir() (string, error) {
557559
return "", errors.New("neither $XDG_CONFIG_HOME nor $HOME are defined")
558560
}
559561
dir += "/.config"
562+
} else if !filepathlite.IsAbs(dir) {
563+
return "", errors.New("path in $XDG_CONFIG_HOME is relative")
560564
}
561565
}
562566

src/os/os_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -2817,6 +2817,33 @@ func TestUserCacheDir(t *testing.T) {
28172817
}
28182818
}
28192819

2820+
func TestUserCacheDirXDGConfigDirEnvVar(t *testing.T) {
2821+
switch runtime.GOOS {
2822+
case "windows", "darwin", "plan9":
2823+
t.Skip("$XDG_CACHE_HOME is effective only on Unix systems")
2824+
}
2825+
2826+
wd, err := Getwd()
2827+
if err != nil {
2828+
t.Fatal(err)
2829+
}
2830+
t.Setenv("XDG_CACHE_HOME", wd)
2831+
2832+
dir, err := UserCacheDir()
2833+
if err != nil {
2834+
t.Fatal(err)
2835+
}
2836+
if dir != wd {
2837+
t.Fatalf("UserCacheDir returned %q; want the value of $XDG_CACHE_HOME %q", dir, wd)
2838+
}
2839+
2840+
t.Setenv("XDG_CACHE_HOME", "some-dir")
2841+
_, err = UserCacheDir()
2842+
if err == nil {
2843+
t.Fatal("UserCacheDir succeeded though $XDG_CACHE_HOME contains a relative path")
2844+
}
2845+
}
2846+
28202847
func TestUserConfigDir(t *testing.T) {
28212848
t.Parallel()
28222849

@@ -2841,6 +2868,33 @@ func TestUserConfigDir(t *testing.T) {
28412868
}
28422869
}
28432870

2871+
func TestUserConfigDirXDGConfigDirEnvVar(t *testing.T) {
2872+
switch runtime.GOOS {
2873+
case "windows", "darwin", "plan9":
2874+
t.Skip("$XDG_CONFIG_HOME is effective only on Unix systems")
2875+
}
2876+
2877+
wd, err := Getwd()
2878+
if err != nil {
2879+
t.Fatal(err)
2880+
}
2881+
t.Setenv("XDG_CONFIG_HOME", wd)
2882+
2883+
dir, err := UserConfigDir()
2884+
if err != nil {
2885+
t.Fatal(err)
2886+
}
2887+
if dir != wd {
2888+
t.Fatalf("UserConfigDir returned %q; want the value of $XDG_CONFIG_HOME %q", dir, wd)
2889+
}
2890+
2891+
t.Setenv("XDG_CONFIG_HOME", "some-dir")
2892+
_, err = UserConfigDir()
2893+
if err == nil {
2894+
t.Fatal("UserConfigDir succeeded though $XDG_CONFIG_HOME contains a relative path")
2895+
}
2896+
}
2897+
28442898
func TestUserHomeDir(t *testing.T) {
28452899
t.Parallel()
28462900

0 commit comments

Comments
 (0)