mirror of
https://github.com/docker/cli.git
synced 2026-09-27 01:40:31 -04:00
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:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user