Skip to content
This repository was archived by the owner on Mar 27, 2024. It is now read-only.

Commit 7c42974

Browse files
author
Priya Wadhwa
committed
Added function to check filepath prefixes
1 parent a2c8cc9 commit 7c42974

File tree

7 files changed

+48
-7
lines changed

7 files changed

+48
-7
lines changed

pkg/util/cloud_prepper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (p CloudPrepper) GetFileSystem() (string, error) {
5959
return "", err
6060
}
6161

62-
return path, GetFileSystemFromReference(ref, p.ImageSource, path, []string{})
62+
return path, GetFileSystemFromReference(ref, p.ImageSource, path, nil)
6363
}
6464

6565
func (p CloudPrepper) GetConfig() (ConfigSchema, error) {

pkg/util/daemon_prepper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (p DaemonPrepper) GetFileSystem() (string, error) {
6464
if err != nil {
6565
return "", err
6666
}
67-
return path, GetFileSystemFromReference(ref, src, path, []string{})
67+
return path, GetFileSystemFromReference(ref, src, path, nil)
6868
}
6969

7070
func (p DaemonPrepper) GetConfig() (ConfigSchema, error) {

pkg/util/docker_utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func unpackDockerSave(tarPath string, target string) error {
115115
}
116116

117117
for _, layer := range layers {
118-
if err = UnTar(bytes.NewReader(layerMap[layer]), target, []string{}); err != nil {
118+
if err = UnTar(bytes.NewReader(layerMap[layer]), target, nil); err != nil {
119119
return fmt.Errorf("Could not unpack layer %s: %s", layer, err)
120120
}
121121
}

pkg/util/fs_utils.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,22 @@ func CheckSameFile(f1name, f2name string) (bool, error) {
160160
}
161161
return true, nil
162162
}
163+
164+
// HasFilepathPrefix checks if the given file path begins with prefix
165+
func HasFilepathPrefix(path, prefix string) bool {
166+
path = filepath.Clean(path)
167+
prefix = filepath.Clean(prefix)
168+
pathArray := strings.Split(path, "/")
169+
prefixArray := strings.Split(prefix, "/")
170+
171+
if len(pathArray) < len(prefixArray) {
172+
return false
173+
}
174+
for index := range prefixArray {
175+
if prefixArray[index] == pathArray[index] {
176+
continue
177+
}
178+
return false
179+
}
180+
return true
181+
}

pkg/util/tar_prepper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (p TarPrepper) GetConfig() (ConfigSchema, error) {
6161
return ConfigSchema{}, err
6262
}
6363
defer f.Close()
64-
if err := UnTar(f, tempDir, []string{}); err != nil {
64+
if err := UnTar(f, tempDir, nil); err != nil {
6565
return ConfigSchema{}, err
6666
}
6767

pkg/util/tar_utils.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@ package util
1818

1919
import (
2020
"archive/tar"
21+
"github.com/sirupsen/logrus"
2122
"io"
2223
"os"
2324
"path/filepath"
2425
"strings"
25-
26-
"github.com/sirupsen/logrus"
2726
)
2827

2928
func unpackTar(tr *tar.Reader, path string, whitelist []string) error {
@@ -137,7 +136,7 @@ func walkAndRemove(p string) error {
137136

138137
func checkWhitelist(target string, whitelist []string) bool {
139138
for _, w := range whitelist {
140-
if strings.HasPrefix(target, w) {
139+
if HasFilepathPrefix(target, w) {
141140
return true
142141
}
143142
}

util/fs_utils_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,26 @@ func TestCheckSameFile(t *testing.T) {
156156
}
157157
}
158158
}
159+
160+
func TestHasFilepathPrefix(t *testing.T) {
161+
type test struct {
162+
prefix string
163+
path string
164+
expectedBool bool
165+
}
166+
167+
var tests = []test{
168+
{"/foo", "/foo/bar", true},
169+
{"/foo/", "/foo/bar", true},
170+
{"/foo/bar", "/foo", false},
171+
{"/foo/bar", "/foo/", false},
172+
{"/foo", "/foobar", false},
173+
}
174+
175+
for _, test := range tests {
176+
hasPrefix := pkgutil.HasFilepathPrefix(test.path, test.prefix)
177+
if hasPrefix != test.expectedBool {
178+
t.Errorf("Got unexpected response: %v for prefix: %s and path: %s", hasPrefix, test.prefix, test.path)
179+
}
180+
}
181+
}

0 commit comments

Comments
 (0)