mirror of
https://github.com/kemko/nomad.git
synced 2026-01-01 16:05:42 +03:00
[artifact] fix path within check on trimmed target (#26748)
When checking if the target path is within the root path, the target path is trimmed and then file information is fetched. If the trimmed path does not exist, then the full target path is not within the root. In the case of receiving a not exist error, simply return false.
This commit is contained in:
@@ -303,6 +303,10 @@ func isPathWithin(rootPath, toCheckPath string) (bool, error) {
|
||||
|
||||
checkStat, err := os.Stat(toCheckPath[0:len(rootPath)])
|
||||
if err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
return false, err
|
||||
}
|
||||
|
||||
|
||||
@@ -286,4 +286,15 @@ func TestUtil_isPathWithin(t *testing.T) {
|
||||
must.ErrorContains(t, err, "no such file or directory")
|
||||
must.False(t, result)
|
||||
})
|
||||
|
||||
t.Run("when path not within root but shorter", func(t *testing.T) {
|
||||
root, err := os.MkdirTemp(tdir, "testing-path-XXX")
|
||||
must.NoError(t, err)
|
||||
check, err := os.MkdirTemp(tdir, "testing-path-XXXX")
|
||||
must.NoError(t, err)
|
||||
result, err := isPathWithin(root, check)
|
||||
|
||||
must.NoError(t, err)
|
||||
must.False(t, result)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user