Skip to content

Commit

Permalink
remove os filter and unused arg
Browse files Browse the repository at this point in the history
  • Loading branch information
lkingland committed Feb 20, 2024
1 parent d09d5be commit 17c097d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 41 deletions.
4 changes: 2 additions & 2 deletions pkg/oci/containerize.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func newDataTarball(root, target string, ignored []string, verbose bool) error {

lnk := "" // if link, this will be used as the target
if info.Mode()&fs.ModeSymlink != 0 {
if lnk, err = validatedLinkTarget(root, path, info); err != nil {
if lnk, err = validatedLinkTarget(root, path); err != nil {
return err
}
}
Expand Down Expand Up @@ -193,7 +193,7 @@ func newDataTarball(root, target string, ignored []string, verbose bool) error {

// validatedLinkTarget returns the target of a given link or an error if
// that target is either absolute or outside the given project root.
func validatedLinkTarget(root, path string, info os.FileInfo) (tgt string, err error) {
func validatedLinkTarget(root, path string) (tgt string, err error) {
// tgt is the raw target of the link.
// This path is either absolute or relative to the link's location.
tgt, err = os.Readlink(path)
Expand Down
60 changes: 21 additions & 39 deletions pkg/oci/containerize_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package oci

import (
"os"
"path/filepath"
"runtime"
"testing"
Expand All @@ -13,53 +12,36 @@ import (
func Test_validatedLinkTarget(t *testing.T) {
root := "testdata/test-links"

allRuntimes := []string{} // optional list of runtimes a test applies to

testAppliesToCurrentRuntime := func(testRuntimes []string) bool {
if len(testRuntimes) == 0 {
return true // no filter defined.
}
for _, r := range testRuntimes {
if runtime.GOOS == r {
return true // filter defined, and current is defined.
}
}
return false // filter defined; current not in set.
// Windows-specific absolute link and link target values:
absoluteLink := "absoluteLink"
linkTarget := "./a.txt"
if runtime.GOOS == "windows" {
absoluteLink = "absoluteLinkWindows"
linkTarget = ".\\a.txt"
}

tests := []struct {
path string // path of the file within test project root
valid bool // If it should be considered valid
target string // optional test of the returned value (target)
name string // descriptive name of the test
runtimes []string // only apply this test to the given runtime(s)
path string // path of the file within test project root
valid bool // If it should be considered valid
target string // optional test of the returned value (target)
name string // descriptive name of the test
}{
{"absoluteLink", false, "", "disallow absolute-path links on linux", []string{"linux"}},
{"absoluteLinkWindows", false, "", "disallow absolute-path links on windows", []string{"windows"}},
{"a.lnk", true, "", "links to files within the root are allowed", allRuntimes},
{"...validName.lnk", true, "", "allow links with target of dot prefixed names", allRuntimes},
{"linkToRoot", true, "", "allow links to the project root", allRuntimes},
{"b/linkToRoot", true, "", "allow links to the project root from within subdir", allRuntimes},
{"b/linkToCurrentDir", true, "", "allow links to a subdirectory within the project", allRuntimes},
{"b/linkToRootsParent", false, "", "disallow links to the project's immediate parent", allRuntimes},
{"b/linkOutsideRootsParent", false, "", "disallow links outside project root and its parent", allRuntimes},
{"b/c/linkToParent", true, "", " allow links up, but within project", allRuntimes},
{"a.lnk", true, "./a.txt", "spot-check link target on linux", []string{"linux"}},
{"a.lnk", true, ".\\a.txt", "spot-check link target on windows ", []string{"windows"}},
{absoluteLink, false, "", "disallow absolute-path links on linux"},
{"a.lnk", true, linkTarget, "spot-check link target"},
{"a.lnk", true, "", "links to files within the root are allowed"},
{"...validName.lnk", true, "", "allow links with target of dot prefixed names"},
{"linkToRoot", true, "", "allow links to the project root"},
{"b/linkToRoot", true, "", "allow links to the project root from within subdir"},
{"b/linkToCurrentDir", true, "", "allow links to a subdirectory within the project"},
{"b/linkToRootsParent", false, "", "disallow links to the project's immediate parent"},
{"b/linkOutsideRootsParent", false, "", "disallow links outside project root and its parent"},
{"b/c/linkToParent", true, "", " allow links up, but within project"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if !testAppliesToCurrentRuntime(tt.runtimes) {
return // The test has a runtime filter defined
}

path := filepath.Join(root, tt.path)
info, err := os.Lstat(path) // filepath.Walk does not follow symlinks
if err != nil {
t.Fatal(err)
}
target, err := validatedLinkTarget(root, path, info)
target, err := validatedLinkTarget(root, path)

if err == nil != tt.valid {
t.Fatalf("expected validity '%v', got '%v'", tt.valid, err)
Expand Down

0 comments on commit 17c097d

Please sign in to comment.