cli/config: use filepath.IsLocal to validate config paths

Use filepath.Rel and filepath.IsLocal instead of comparing path prefixes
as strings when checking that a path stays within the config directory.

Also avoid calling Dir multiple times when constructing and validating
the path.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2026-09-02 02:50:28 +02:00
parent 64484430f4
commit 977d999382
+5 -4
View File
@@ -7,7 +7,6 @@ import (
"os/user"
"path/filepath"
"runtime"
"strings"
"sync"
"github.com/docker/cli/cli/config/configfile"
@@ -98,9 +97,11 @@ func SetDir(dir string) {
// Path returns the path to a file relative to the config dir
func Path(p ...string) (string, error) {
path := filepath.Join(append([]string{Dir()}, p...)...)
if !strings.HasPrefix(path, Dir()+string(filepath.Separator)) {
return "", fmt.Errorf("path %q is outside of root config directory %q", path, Dir())
root := Dir()
path := filepath.Join(append([]string{root}, p...)...)
if rel, err := filepath.Rel(root, path); err != nil || !filepath.IsLocal(rel) {
return "", fmt.Errorf("path %q is outside of root config directory %q", path, root)
}
return path, nil
}