Skip to content

Commit 2b07e75

Browse files
committed
reviseRootDir: skip default values, add validation
1. In case --root option is not provided, do nothing. 2. Instead of checking if root value is empty string, check it after filepath.Abs, and reject "/". Improve docstring while at it. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 899342b commit 2b07e75

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

utils.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"errors"
45
"fmt"
56
"os"
67
"path/filepath"
@@ -96,17 +97,25 @@ func revisePidFile(context *cli.Context) error {
9697
return context.Set("pid-file", pidFile)
9798
}
9899

99-
// reviseRootDir convert the root to absolute path
100+
// reviseRootDir ensures that the --root option argument,
101+
// if specified, is converted to an absolute and cleaned path,
102+
// and that this path is sane.
100103
func reviseRootDir(context *cli.Context) error {
101-
root := context.GlobalString("root")
102-
if root == "" {
104+
if !context.IsSet("root") {
103105
return nil
104106
}
105-
106-
root, err := filepath.Abs(root)
107+
root, err := filepath.Abs(context.GlobalString("root"))
107108
if err != nil {
108109
return err
109110
}
111+
if root == "/" {
112+
// This can happen if --root argument is
113+
// - "" (i.e. empty);
114+
// - "." (and the CWD is /);
115+
// - "../../.." (enough to get to /);
116+
// - "/" (the actual /).
117+
return errors.New("Option --root argument should not be set to /")
118+
}
110119

111120
return context.GlobalSet("root", root)
112121
}

0 commit comments

Comments
 (0)