Skip to content

Commit 20ce96d

Browse files
authored
Merge c58fc85 into f66264f
2 parents f66264f + c58fc85 commit 20ce96d

File tree

2 files changed

+88
-2
lines changed

2 files changed

+88
-2
lines changed

registry-library/library/util.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func decompress(targetDir string, tarFile string, excludeFiles []string) error {
113113
continue
114114
}
115115

116-
target := path.Join(targetDir, filepath.Clean(header.Name))
116+
target := CleanFilepath(targetDir, header.Name)
117117
switch header.Typeflag {
118118
case tar.TypeDir:
119119
err = os.MkdirAll(target, os.FileMode(header.Mode))
@@ -122,7 +122,6 @@ func decompress(targetDir string, tarFile string, excludeFiles []string) error {
122122
return returnedErr
123123
}
124124
case tar.TypeReg:
125-
/* #nosec G304 -- target is produced using path.Join which cleans the dir path */
126125
w, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode))
127126
if err != nil {
128127
returnedErr = multierror.Append(returnedErr, err)
@@ -192,3 +191,10 @@ func getHTTPClient(options RegistryOptions) *http.Client {
192191
Timeout: overriddenTimeout,
193192
}
194193
}
194+
195+
// Cleans a child path to ensure that there is no escaping from the parent directory with the use of ../ escape methods
196+
// Ensures that the child path is always contained and absolutely pathed from the parent
197+
func CleanFilepath(parent string, child string)string{
198+
target := path.Join(parent, filepath.Clean("/"+child))
199+
return target
200+
}

registry-library/library/util_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package library
1818
import (
1919
"reflect"
2020
"testing"
21+
"strings"
2122
)
2223

2324
func TestValidateStackVersionTag(t *testing.T) {
@@ -130,3 +131,82 @@ func TestSplitVersionFromStack(t *testing.T) {
130131
})
131132
}
132133
}
134+
135+
func TestCleanFilepath(t *testing.T) {
136+
tests := []struct {
137+
name string
138+
parentPath string
139+
childPath string
140+
expectedPath string
141+
}{
142+
{
143+
name: "Absolute child path with leading slash",
144+
parentPath: ".",
145+
childPath: "/test/tmp",
146+
expectedPath: "test/tmp",
147+
},
148+
{
149+
name: "Absolute child path without leading slash",
150+
parentPath: ".",
151+
childPath: "test/tmp",
152+
expectedPath: "test/tmp",
153+
},
154+
{
155+
name: "Relative child path without leading slash",
156+
parentPath: ".",
157+
childPath: "../../../../test/tmp",
158+
expectedPath: "test/tmp",
159+
},
160+
{
161+
name: "Relative child path with leading slash",
162+
parentPath: ".",
163+
childPath: "/../../../../test/tmp",
164+
expectedPath: "test/tmp",
165+
},
166+
{
167+
name: "Absolute child path with leading slash and escape capabilities",
168+
parentPath: ".",
169+
childPath: "/home/../../../../test/tmp",
170+
expectedPath: "test/tmp",
171+
},
172+
{
173+
name: "Absolute child path with leading slash and escape capabilities (parent path not current dir)",
174+
parentPath: "newHome/dir",
175+
childPath: "/home/../../../../test/tmp",
176+
expectedPath: "newHome/dir/test/tmp",
177+
},
178+
{
179+
name: "Relative child path without leading slash and escape capabilities (parent path not current dir)",
180+
parentPath: "newHome/dir",
181+
childPath: "../home/../../../../test/tmp",
182+
expectedPath: "newHome/dir/test/tmp",
183+
},
184+
{
185+
name: "Blank child path",
186+
parentPath: "dir",
187+
childPath: "",
188+
expectedPath: "dir",
189+
},
190+
{
191+
name: "Child path only escape characters",
192+
parentPath: "dir",
193+
childPath: "../../../../../",
194+
expectedPath: "dir",
195+
},
196+
{
197+
name: "Single file as child path",
198+
parentPath: "dir",
199+
childPath: "test.txt",
200+
expectedPath: "dir/test.txt",
201+
},
202+
}
203+
204+
for _, test := range tests {
205+
t.Run(test.name, func(t *testing.T) {
206+
path:= CleanFilepath(test.parentPath, test.childPath)
207+
if !strings.EqualFold(test.expectedPath, path) {
208+
t.Errorf("Expected: %s, Got: %s", test.expectedPath, path)
209+
}
210+
})
211+
}
212+
}

0 commit comments

Comments
 (0)