Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit 8bc81fd

Browse files
author
priyawadhwa
authored
Merge pull request #293 from priyawadhwa/fedora
Get absolute path of file before checking whitelist
2 parents 10efecb + d8ae561 commit 8bc81fd

4 files changed

Lines changed: 53 additions & 13 deletions

File tree

integration/dockerfiles/Dockerfile_test_multistage

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ COPY --from=0 $foopath context/b* /foo/
88
FROM second
99
COPY --from=base /context/foo /new/foo
1010

11+
# This base image contains symlinks with relative paths to whitelisted directories
12+
# We need to test they're extracted correctly
13+
FROM fedora@sha256:c4cc32b09c6ae3f1353e7e33a8dda93dc41676b923d6d89afa996b421cc5aa48
14+
1115
FROM base
1216
ARG file
1317
COPY --from=second /foo ${file}

pkg/snapshot/snapshot.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ func (s *Snapshotter) snapshotFiles(f io.Writer, files []string) (bool, error) {
9595
if val, ok := snapshottedFiles[file]; ok && val {
9696
continue
9797
}
98-
if util.CheckWhitelist(file) && !isBuildFile(file) {
98+
whitelisted, err := util.CheckWhitelist(file)
99+
if err != nil {
100+
return false, err
101+
}
102+
if whitelisted && !isBuildFile(file) {
99103
logrus.Infof("Not adding %s to layer, as it's whitelisted", file)
100104
continue
101105
}
@@ -168,7 +172,11 @@ func (s *Snapshotter) snapShotFS(f io.Writer) (bool, error) {
168172

169173
// Now create the tar.
170174
for path, info := range memFs {
171-
if util.CheckWhitelist(path) {
175+
whitelisted, err := util.CheckWhitelist(path)
176+
if err != nil {
177+
return false, err
178+
}
179+
if whitelisted {
172180
logrus.Debugf("Not adding %s to layer, as it's whitelisted", path)
173181
continue
174182
}

pkg/util/fs_util.go

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,20 @@ func GetFSFromImage(root string, img v1.Image) error {
9090
logrus.Infof("Not adding %s because it was added by a prior layer", path)
9191
continue
9292
}
93-
94-
if CheckWhitelist(path) && !checkWhitelistRoot(root) {
93+
whitelisted, err := CheckWhitelist(path)
94+
if err != nil {
95+
return err
96+
}
97+
if whitelisted && !checkWhitelistRoot(root) {
9598
logrus.Infof("Not adding %s because it is whitelisted", path)
9699
continue
97100
}
98101
if hdr.Typeflag == tar.TypeSymlink {
99-
if CheckWhitelist(hdr.Linkname) {
102+
whitelisted, err := CheckWhitelist(hdr.Linkname)
103+
if err != nil {
104+
return err
105+
}
106+
if whitelisted {
100107
logrus.Debugf("skipping symlink from %s to %s because %s is whitelisted", hdr.Linkname, path, hdr.Linkname)
101108
continue
102109
}
@@ -115,7 +122,11 @@ func GetFSFromImage(root string, img v1.Image) error {
115122
func DeleteFilesystem() error {
116123
logrus.Info("Deleting filesystem...")
117124
err := filepath.Walk(constants.RootDir, func(path string, info os.FileInfo, err error) error {
118-
if CheckWhitelist(path) || ChildDirInWhitelist(path, constants.RootDir) {
125+
whitelisted, err := CheckWhitelist(path)
126+
if err != nil {
127+
return err
128+
}
129+
if whitelisted || ChildDirInWhitelist(path, constants.RootDir) {
119130
logrus.Debugf("Not deleting %s, as it's whitelisted", path)
120131
return nil
121132
}
@@ -247,13 +258,18 @@ func checkWhiteouts(path string, whiteouts map[string]struct{}) bool {
247258
return false
248259
}
249260

250-
func CheckWhitelist(path string) bool {
261+
func CheckWhitelist(path string) (bool, error) {
262+
abs, err := filepath.Abs(path)
263+
if err != nil {
264+
logrus.Infof("unable to get absolute path for %s", path)
265+
return false, err
266+
}
251267
for _, wl := range whitelist {
252-
if HasFilepathPrefix(path, wl) {
253-
return true
268+
if HasFilepathPrefix(abs, wl) {
269+
return true, nil
254270
}
255271
}
256-
return false
272+
return false, nil
257273
}
258274

259275
func checkWhitelistRoot(root string) bool {
@@ -313,7 +329,11 @@ func RelativeFiles(fp string, root string) ([]string, error) {
313329
fullPath := filepath.Join(root, fp)
314330
logrus.Debugf("Getting files and contents at root %s", fullPath)
315331
err := filepath.Walk(fullPath, func(path string, info os.FileInfo, err error) error {
316-
if CheckWhitelist(path) && !HasFilepathPrefix(path, root) {
332+
whitelisted, err := CheckWhitelist(path)
333+
if err != nil {
334+
return err
335+
}
336+
if whitelisted && !HasFilepathPrefix(path, root) {
317337
return nil
318338
}
319339
if err != nil {
@@ -334,7 +354,11 @@ func Files(root string) ([]string, error) {
334354
var files []string
335355
logrus.Debugf("Getting files and contents at root %s", root)
336356
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
337-
if CheckWhitelist(path) {
357+
whitelisted, err := CheckWhitelist(path)
358+
if err != nil {
359+
return err
360+
}
361+
if whitelisted {
338362
return nil
339363
}
340364
files = append(files, path)

pkg/util/fs_util_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,11 @@ func Test_CheckWhitelist(t *testing.T) {
266266
whitelist = original
267267
}()
268268
whitelist = tt.args.whitelist
269-
if got := CheckWhitelist(tt.args.path); got != tt.want {
269+
got, err := CheckWhitelist(tt.args.path)
270+
if err != nil {
271+
t.Fatalf("error checking whitelist: %v", err)
272+
}
273+
if got != tt.want {
270274
t.Errorf("CheckWhitelist() = %v, want %v", got, tt.want)
271275
}
272276
})

0 commit comments

Comments
 (0)