Fix Abs path validation on Windows (#124084)
* Windows: Consider slash-prefixed paths as absolute filepath.IsAbs does not consider "/" or "\" as absolute paths, even though files can be addressed as such. [1][2] Currently, there are some unit tests that are failing on Windows due to this reason. [1] https://learn.microsoft.com/en-us/dotnet/standard/io/file-path-formats#traditional-dos-paths [2] https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#fully-qualified-vs-relative-paths * Add test to verify IsAbs for windows Signed-off-by: Maksym Pavlenko <pavlenko.maksym@gmail.com> * Fix abs path validation on windows Signed-off-by: Maksym Pavlenko <pavlenko.maksym@gmail.com> * Skipp path clean check for podLogDir on windows Signed-off-by: Maksym Pavlenko <pavlenko.maksym@gmail.com> * Implement IsPathClean to validate path Signed-off-by: Maksym Pavlenko <pavlenko.maksym@gmail.com> * Add warn comment for IsAbs Signed-off-by: Maksym Pavlenko <pavlenko.maksym@gmail.com> --------- Signed-off-by: Maksym Pavlenko <pavlenko.maksym@gmail.com> Co-authored-by: Claudiu Belu <cbelu@cloudbasesolutions.com>
This commit is contained in:
@@ -19,6 +19,7 @@ package filesystem
|
||||
import (
|
||||
"net"
|
||||
"os"
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -84,3 +85,48 @@ func TestIsUnixDomainSocket(t *testing.T) {
|
||||
assert.Equal(t, result, test.expectSocket, "Unexpected result from IsUnixDomainSocket: %v for %s", result, test.label)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsCleanPath(t *testing.T) {
|
||||
type Case struct {
|
||||
path string
|
||||
expected bool
|
||||
}
|
||||
|
||||
// Credits https://github.com/kubernetes/kubernetes/pull/124084/files#r1557566941
|
||||
cases := []Case{
|
||||
{path: "/logs", expected: true},
|
||||
{path: "/var/lib/something", expected: true},
|
||||
{path: "var/lib/something", expected: true},
|
||||
{path: "var\\lib\\something", expected: true},
|
||||
{path: "/", expected: true},
|
||||
{path: ".", expected: true},
|
||||
{path: "/var/../something", expected: false},
|
||||
{path: "/var//lib/something", expected: false},
|
||||
{path: "/var/./lib/something", expected: false},
|
||||
}
|
||||
|
||||
// Additional cases applicable on Windows
|
||||
if runtime.GOOS == "windows" {
|
||||
cases = append(cases, []Case{
|
||||
{path: "\\", expected: true},
|
||||
{path: "C:/var/lib/something", expected: true},
|
||||
{path: "C:\\var\\lib\\something", expected: true},
|
||||
{path: "C:/", expected: true},
|
||||
{path: "C:\\", expected: true},
|
||||
{path: "C:/var//lib/something", expected: false},
|
||||
{path: "\\var\\\\lib\\something", expected: false},
|
||||
{path: "C:\\var\\\\lib\\something", expected: false},
|
||||
{path: "C:\\var\\..\\something", expected: false},
|
||||
{path: "\\var\\.\\lib\\something", expected: false},
|
||||
{path: "C:\\var\\.\\lib\\something", expected: false},
|
||||
}...)
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
actual := IsPathClean(tc.path)
|
||||
if actual != tc.expected {
|
||||
t.Errorf("actual: %t, expected: %t, for path: %s\n", actual, tc.expected, tc.path)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user