Skip to content

Commit 10b01bc

Browse files
committed
bug fix
Signed-off-by: Jordan Dubrick <[email protected]>
1 parent 35722ec commit 10b01bc

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

registry-library/library/util.go

Lines changed: 8 additions & 1 deletion
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))
@@ -192,3 +192,10 @@ func getHTTPClient(options RegistryOptions) *http.Client {
192192
Timeout: overriddenTimeout,
193193
}
194194
}
195+
196+
// Cleans a child path to ensure that there is no escaping from the parent directory with the use of ../ escape methods
197+
// Ensures that the child path is always contained and absolutely pathed from the parent
198+
func CleanFilepath(parent string, child string)string{
199+
target := path.Join(parent, filepath.Clean("/"+child))
200+
return target
201+
}

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)