-
Notifications
You must be signed in to change notification settings - Fork 129
Rely on os.Root for checking paths #2752
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ import ( | |
| "io" | ||
| "os" | ||
| "path/filepath" | ||
| "runtime" | ||
| "strings" | ||
| "testing" | ||
|
|
||
|
|
@@ -33,7 +34,7 @@ func TestLinkUpdateChecksum(t *testing.T) { | |
| require.NoError(t, copyDir(testDataSrc, filepath.Join(tempDir, "testdata"))) | ||
|
|
||
| // Set up paths within the temporary directory | ||
| basePath := filepath.Join(tempDir, "testdata/links") | ||
| basePath := filepath.Join(tempDir, "testdata", "links") | ||
|
|
||
| // Create an os.Root for secure file operations within tempDir | ||
| root, err := os.OpenRoot(tempDir) | ||
|
|
@@ -82,7 +83,7 @@ func TestListLinkedFiles(t *testing.T) { | |
| // Get current working directory to locate test data | ||
| wd, err := os.Getwd() | ||
| assert.NoError(t, err) | ||
| basePath := filepath.Join(wd, filepath.FromSlash("testdata/links")) | ||
| basePath := filepath.Join(wd, "testdata", "links") | ||
|
|
||
| // Find the repository root to create a secure os.Root context | ||
| root, err := FindRepositoryRoot() | ||
|
|
@@ -193,7 +194,7 @@ func TestUpdateLinkedFilesChecksums(t *testing.T) { | |
| require.NoError(t, copyDir(testDataSrc, filepath.Join(tempDir, "testdata"))) | ||
|
|
||
| // Set up paths within the temporary directory | ||
| basePath := filepath.Join(tempDir, "testdata/links") | ||
| basePath := filepath.Join(tempDir, "testdata", "links") | ||
|
|
||
| // Create an os.Root for secure file operations within tempDir | ||
| root, err := os.OpenRoot(tempDir) | ||
|
|
@@ -227,7 +228,7 @@ func TestLinkedFilesByPackageFrom(t *testing.T) { | |
| // Get current working directory to locate test data | ||
| wd, err := os.Getwd() | ||
| assert.NoError(t, err) | ||
| basePath := filepath.Join(wd, filepath.FromSlash("testdata/links")) | ||
| basePath := filepath.Join(wd, "testdata", "links") | ||
|
|
||
| // Find the repository root to create a secure os.Root context | ||
| root, err := FindRepositoryRoot() | ||
|
|
@@ -269,14 +270,14 @@ func TestIncludeLinkedFiles(t *testing.T) { | |
| // Get current working directory to locate test data | ||
| wd, err := os.Getwd() | ||
| assert.NoError(t, err) | ||
| testPkg := filepath.Join(wd, filepath.FromSlash("testdata")) | ||
| testPkg := filepath.Join(wd, "testdata") | ||
|
|
||
| // Create a temporary directory and copy test data to avoid modifying originals | ||
| tempDir := t.TempDir() | ||
| require.NoError(t, copyDir(testPkg, filepath.Join(tempDir, "testdata"))) | ||
|
|
||
| // Set up source and destination directories | ||
| fromDir := filepath.Join(tempDir, "testdata/testpackage") | ||
| fromDir := filepath.Join(tempDir, "testdata", "testpackage") | ||
| toDir := filepath.Join(tempDir, "dest") | ||
|
|
||
| // Create an os.Root for secure file operations within tempDir | ||
|
|
@@ -769,6 +770,11 @@ func TestLinksFS_ErrorConditions(t *testing.T) { | |
| err = os.WriteFile(invalidLinkFile, []byte(""), 0644) | ||
| require.NoError(t, err) | ||
|
|
||
| // Create link that escapes root | ||
| outOfRootLinkFile := filepath.Join(workDir, "escapesroot.txt.link") | ||
| err = os.WriteFile(outOfRootLinkFile, []byte("../../etc/passwd"), 06444) | ||
jsoriano marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| require.NoError(t, err) | ||
|
|
||
| // Setup LinksFS | ||
| root, err := os.OpenRoot(repoDir) | ||
| require.NoError(t, err) | ||
|
|
@@ -777,6 +783,11 @@ func TestLinksFS_ErrorConditions(t *testing.T) { | |
| lfs, err := NewLinksFS(root, workDir) | ||
| require.NoError(t, err) | ||
|
|
||
| notFoundErrorMsg := "no such file or directory" | ||
| if runtime.GOOS == "windows" { | ||
| notFoundErrorMsg = "The system cannot find the file specified" | ||
| } | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| fileName string | ||
|
|
@@ -785,13 +796,18 @@ func TestLinksFS_ErrorConditions(t *testing.T) { | |
| { | ||
| name: "broken link to non-existent file", | ||
| fileName: "broken.txt.link", | ||
| errorMsg: "escapes the repository root", | ||
| errorMsg: notFoundErrorMsg, | ||
| }, | ||
| { | ||
| name: "invalid link file format", | ||
| fileName: "invalid.txt.link", | ||
| errorMsg: "file is empty or first line is missing", | ||
| }, | ||
| { | ||
| name: "escapes root", | ||
| fileName: "escapesroot.txt.link", | ||
| errorMsg: "path escapes from parent", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
|
|
@@ -845,13 +861,13 @@ func TestLinksFS_WorkDirValidation(t *testing.T) { | |
| name: "invalid absolute workDir outside repo", | ||
| workDir: outsideDir, | ||
| expectError: true, | ||
| errorMsg: "is outside the repository root", | ||
| errorMsg: "path escapes from parent", | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Error from the stdlib is directly returned now, but we cannot check for it to convert it as it is not public (https://github.com/golang/go/blob/e5502e0959bb54ec70ca500e8d2b6f5ac5efbc53/src/os/file.go#L421). We could check the error message and replace it, but not sure if it is worth it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess it could be kept like this for now. |
||
| }, | ||
| { | ||
| name: "invalid relative workDir escaping repo", | ||
| workDir: "../outside", | ||
| expectError: true, | ||
| errorMsg: "is outside the repository root", | ||
| errorMsg: "path escapes from parent", | ||
| }, | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.