[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:
Chris Roberts
2025-09-11 08:59:18 -07:00
committed by GitHub
parent 8eb72b2868
commit 8b51acf259
2 changed files with 15 additions and 0 deletions

View File

@@ -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
}

View File

@@ -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)
})
}