From 082aea1ba65c5fc539ee20e15c23836a1c52732e Mon Sep 17 00:00:00 2001 From: Sahil Rihan Date: Tue, 26 Nov 2019 11:46:49 -0800 Subject: [PATCH] Allow symlinks that use absolute path within the memfs/tar. Fixes case where /home/rihan/bar/etc/alternatives/pager is symlinked to /bin/more --- lib/snapshot/mem_fs.go | 11 +++++------ lib/snapshot/mem_fs_test.go | 2 ++ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/snapshot/mem_fs.go b/lib/snapshot/mem_fs.go index 994e7b12..2045e5c2 100644 --- a/lib/snapshot/mem_fs.go +++ b/lib/snapshot/mem_fs.go @@ -577,12 +577,11 @@ func (fs *MemFS) untarOneItem(path string, header *tar.Header, r *tar.Reader) er if err != nil { return fmt.Errorf("read link %s: %s", linkTarget, err) } - - if filepath.IsAbs(linkTarget) { - linkTarget, err = pathutils.TrimRoot(linkTarget, fs.tree.src) - if err != nil { - return fmt.Errorf("trim link %s: %s", linkTarget, err) - } + if filepath.IsAbs(linkTarget) && strings.HasPrefix(linkTarget, fs.tree.src) { + linkTarget, err = pathutils.TrimRoot(linkTarget, fs.tree.src) + if err != nil { + return fmt.Errorf("trim link %s: %s", linkTarget, err) + } } } localHeader, err := tar.FileInfoHeader(localInfo, linkTarget) diff --git a/lib/snapshot/mem_fs_test.go b/lib/snapshot/mem_fs_test.go index cb0ce374..82e04cf1 100644 --- a/lib/snapshot/mem_fs_test.go +++ b/lib/snapshot/mem_fs_test.go @@ -68,6 +68,8 @@ func TestUntarFromPath(t *testing.T) { require.NoError(err) err = os.Mkdir(filepath.Join(tmpRoot, "mydir"), os.ModePerm) require.NoError(err) + err = os.Symlink(filepath.Join("/test1", "test1.txt"), filepath.Join(tmpRoot, "test1", "abs_symlink.txt")) + require.NoError(err) clk := clock.NewMock() fs, err := NewMemFS(clk, tmpRoot, pathutils.DefaultBlacklist)