Check if the system-target path supplied is absolute

docker.Untar
(https://github.com/mudler/luet/blob/master/vendor/github.com/docker/docker/pkg/archive/archive.go#L942) requires absolute paths.
We didn't do any input validation before, assuming the path passed by
were absolute since they were coming from YAML configuration files, now
that this is not the truth anymore we need to sanitize the input.

With this change we check if the given path is absolute or relative, if
it's relative we calculate the absolute path and use it in place.
This commit is contained in:
Ettore Di Giacinto
2021-08-03 16:17:55 +02:00
parent 75906c4198
commit 9d1594c036
13 changed files with 27 additions and 11 deletions

View File

@@ -45,7 +45,7 @@ var cleanupCmd = &cobra.Command{
LuetCfg.System.DatabaseEngine = engine LuetCfg.System.DatabaseEngine = engine
LuetCfg.System.DatabasePath = dbpath LuetCfg.System.DatabasePath = dbpath
LuetCfg.System.Rootfs = rootfs LuetCfg.System.SetRootFS(rootfs)
// Check if cache dir exists // Check if cache dir exists
if fileHelper.Exists(LuetCfg.GetSystem().GetSystemPkgsCacheDirPath()) { if fileHelper.Exists(LuetCfg.GetSystem().GetSystemPkgsCacheDirPath()) {

View File

@@ -58,7 +58,7 @@ For reference, inspect a "metadata.yaml" file generated while running "luet buil
LuetCfg.System.DatabaseEngine = engine LuetCfg.System.DatabaseEngine = engine
LuetCfg.System.DatabasePath = dbpath LuetCfg.System.DatabasePath = dbpath
LuetCfg.System.Rootfs = rootfs LuetCfg.System.SetRootFS(rootfs)
systemDB := LuetCfg.GetSystemDB() systemDB := LuetCfg.GetSystemDB()

View File

@@ -51,7 +51,7 @@ To return also files:
LuetCfg.System.DatabaseEngine = engine LuetCfg.System.DatabaseEngine = engine
LuetCfg.System.DatabasePath = dbpath LuetCfg.System.DatabasePath = dbpath
LuetCfg.System.Rootfs = rootfs LuetCfg.System.SetRootFS(rootfs)
systemDB := LuetCfg.GetSystemDB() systemDB := LuetCfg.GetSystemDB()

View File

@@ -48,7 +48,7 @@ This commands takes multiple packages as arguments and prunes their entries from
LuetCfg.System.DatabaseEngine = engine LuetCfg.System.DatabaseEngine = engine
LuetCfg.System.DatabasePath = dbpath LuetCfg.System.DatabasePath = dbpath
LuetCfg.System.Rootfs = rootfs LuetCfg.System.SetRootFS(rootfs)
systemDB := LuetCfg.GetSystemDB() systemDB := LuetCfg.GetSystemDB()

View File

@@ -87,7 +87,7 @@ To force install a package:
LuetCfg.System.DatabaseEngine = engine LuetCfg.System.DatabaseEngine = engine
LuetCfg.System.DatabasePath = dbpath LuetCfg.System.DatabasePath = dbpath
LuetCfg.System.Rootfs = rootfs LuetCfg.System.SetRootFS(rootfs)
LuetCfg.GetSolverOptions().Type = stype LuetCfg.GetSolverOptions().Type = stype
LuetCfg.GetSolverOptions().LearnRate = float32(rate) LuetCfg.GetSolverOptions().LearnRate = float32(rate)

View File

@@ -45,7 +45,7 @@ It scans the target file system, and if finds a match with a package available i
LuetCfg.System.DatabaseEngine = engine LuetCfg.System.DatabaseEngine = engine
LuetCfg.System.DatabasePath = dbpath LuetCfg.System.DatabasePath = dbpath
LuetCfg.System.Rootfs = rootfs LuetCfg.System.SetRootFS(rootfs)
// This shouldn't be necessary, but we need to unmarshal the repositories to a concrete struct, thus we need to port them back to the Repositories type // This shouldn't be necessary, but we need to unmarshal the repositories to a concrete struct, thus we need to port them back to the Repositories type
repos := installer.Repositories{} repos := installer.Repositories{}

View File

@@ -66,7 +66,7 @@ var reinstallCmd = &cobra.Command{
LuetCfg.System.DatabaseEngine = engine LuetCfg.System.DatabaseEngine = engine
LuetCfg.System.DatabasePath = dbpath LuetCfg.System.DatabasePath = dbpath
LuetCfg.System.Rootfs = rootfs LuetCfg.System.SetRootFS(rootfs)
for _, a := range args { for _, a := range args {
pack, err := helpers.ParsePackageStr(a) pack, err := helpers.ParsePackageStr(a)

View File

@@ -70,7 +70,7 @@ var replaceCmd = &cobra.Command{
LuetCfg.System.DatabaseEngine = engine LuetCfg.System.DatabaseEngine = engine
LuetCfg.System.DatabasePath = dbpath LuetCfg.System.DatabasePath = dbpath
LuetCfg.System.Rootfs = rootfs LuetCfg.System.SetRootFS(rootfs)
for _, a := range args { for _, a := range args {
pack, err := helpers.ParsePackageStr(a) pack, err := helpers.ParsePackageStr(a)

View File

@@ -340,7 +340,7 @@ Search can also return results in the terminal in different ways: as terminal ou
LuetCfg.System.DatabaseEngine = engine LuetCfg.System.DatabaseEngine = engine
LuetCfg.System.DatabasePath = dbpath LuetCfg.System.DatabasePath = dbpath
LuetCfg.System.Rootfs = rootfs LuetCfg.System.SetRootFS(rootfs)
out, _ := cmd.Flags().GetString("output") out, _ := cmd.Flags().GetString("output")
if out != "terminal" { if out != "terminal" {
LuetCfg.GetLogging().SetLogLevel("error") LuetCfg.GetLogging().SetLogLevel("error")

View File

@@ -71,7 +71,7 @@ var uninstallCmd = &cobra.Command{
LuetCfg.System.DatabaseEngine = engine LuetCfg.System.DatabaseEngine = engine
LuetCfg.System.DatabasePath = dbpath LuetCfg.System.DatabasePath = dbpath
LuetCfg.System.Rootfs = rootfs LuetCfg.System.SetRootFS(rootfs)
LuetCfg.ConfigProtectSkip = !keepProtected LuetCfg.ConfigProtectSkip = !keepProtected
LuetCfg.GetSolverOptions().Type = stype LuetCfg.GetSolverOptions().Type = stype

View File

@@ -70,7 +70,7 @@ var upgradeCmd = &cobra.Command{
LuetCfg.System.DatabaseEngine = engine LuetCfg.System.DatabaseEngine = engine
LuetCfg.System.DatabasePath = dbpath LuetCfg.System.DatabasePath = dbpath
LuetCfg.System.Rootfs = rootfs LuetCfg.System.SetRootFS(rootfs)
LuetCfg.GetSolverOptions().Type = stype LuetCfg.GetSolverOptions().Type = stype
LuetCfg.GetSolverOptions().LearnRate = float32(rate) LuetCfg.GetSolverOptions().LearnRate = float32(rate)
LuetCfg.GetSolverOptions().Discount = float32(discount) LuetCfg.GetSolverOptions().Discount = float32(discount)

View File

@@ -453,6 +453,9 @@ func (a *PackageArtifact) GetProtectFiles() []string {
// Unpack Untar and decompress (TODO) to the given path // Unpack Untar and decompress (TODO) to the given path
func (a *PackageArtifact) Unpack(dst string, keepPerms bool) error { func (a *PackageArtifact) Unpack(dst string, keepPerms bool) error {
if !strings.HasPrefix(dst, "/") {
return errors.New("destination must be an absolute path")
}
// Create // Create
protectedFiles := a.GetProtectFiles() protectedFiles := a.GetProtectFiles()

View File

@@ -107,6 +107,19 @@ type LuetSystemConfig struct {
TmpDirBase string `yaml:"tmpdir_base" mapstructure:"tmpdir_base"` TmpDirBase string `yaml:"tmpdir_base" mapstructure:"tmpdir_base"`
} }
func (s *LuetSystemConfig) SetRootFS(path string) error {
pathToSet := path
if !filepath.IsAbs(path) {
abs, err := filepath.Abs(path)
if err != nil {
return err
}
pathToSet = abs
}
s.Rootfs = pathToSet
return nil
}
func (sc *LuetSystemConfig) GetRepoDatabaseDirPath(name string) string { func (sc *LuetSystemConfig) GetRepoDatabaseDirPath(name string) string {
dbpath := filepath.Join(sc.Rootfs, sc.DatabasePath) dbpath := filepath.Join(sc.Rootfs, sc.DatabasePath)
dbpath = filepath.Join(dbpath, "repos/"+name) dbpath = filepath.Join(dbpath, "repos/"+name)