Skip to content

Commit 3db98f6

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

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-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 not 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 not relative")
560564
}
561565
}
562566

src/os/os_test.go

+60
Original file line numberDiff line numberDiff line change
@@ -2817,6 +2817,36 @@ 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+
oldval := Getenv("XDG_CACHE_HOME")
2827+
defer Setenv("XDG_CACHE_HOME", oldval)
2828+
2829+
wd, err := Getwd()
2830+
if err != nil {
2831+
t.Fatal(err)
2832+
}
2833+
Setenv("XDG_CACHE_HOME", wd)
2834+
2835+
dir, err := UserCacheDir()
2836+
if err != nil {
2837+
t.Fatal(err)
2838+
}
2839+
if dir != wd {
2840+
t.Fatalf("UserConfigDir returned %q; want the value of $XDG_CACHE_HOME %q", dir, wd)
2841+
}
2842+
2843+
Setenv("XDG_CACHE_HOME", "some-dir")
2844+
_, err = UserCacheDir()
2845+
if err == nil {
2846+
t.Fatal("UserConfigDir succeeded though $XDG_CACHE_HOME contains a relative path")
2847+
}
2848+
}
2849+
28202850
func TestUserConfigDir(t *testing.T) {
28212851
t.Parallel()
28222852

@@ -2841,6 +2871,36 @@ func TestUserConfigDir(t *testing.T) {
28412871
}
28422872
}
28432873

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

0 commit comments

Comments
 (0)