From 2b07e751b5f982ed05629cdcab2d9ab87a68abff Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Fri, 11 Feb 2022 13:18:56 -0800 Subject: [PATCH] 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 --- utils.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/utils.go b/utils.go index 32ab33e55..75752f183 100644 --- a/utils.go +++ b/utils.go @@ -1,6 +1,7 @@ package main import ( + "errors" "fmt" "os" "path/filepath" @@ -96,17 +97,25 @@ func revisePidFile(context *cli.Context) error { return context.Set("pid-file", pidFile) } -// reviseRootDir convert the root to absolute path +// reviseRootDir ensures that the --root option argument, +// if specified, is converted to an absolute and cleaned path, +// and that this path is sane. func reviseRootDir(context *cli.Context) error { - root := context.GlobalString("root") - if root == "" { + if !context.IsSet("root") { return nil } - - root, err := filepath.Abs(root) + root, err := filepath.Abs(context.GlobalString("root")) if err != nil { return err } + if root == "/" { + // This can happen if --root argument is + // - "" (i.e. empty); + // - "." (and the CWD is /); + // - "../../.." (enough to get to /); + // - "/" (the actual /). + return errors.New("Option --root argument should not be set to /") + } return context.GlobalSet("root", root) }