Skip to content

Commit fe426d1

Browse files
authored
fix: update tar traversal to respect current director entry (#225)
* fix: update tar traversal to respect current director entry --------- Signed-off-by: Christopher Phillips <[email protected]>
1 parent cf0e754 commit fe426d1

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

pkg/file/tarutil.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ func (v tarVisitor) visit(entry TarFileEntry) error {
147147
target := filepath.Join(v.destination, entry.Header.Name)
148148

149149
// we should not allow for any destination path to be outside of where we are unarchiving to
150-
if !strings.HasPrefix(target, v.destination+string(os.PathSeparator)) {
150+
// "." is a special case that we allow (it is the root of the unarchived content)
151+
withinDir := v.destination + string(os.PathSeparator)
152+
if !strings.HasPrefix(target, withinDir) && entry.Header.Name != "." {
151153
return fmt.Errorf("potential path traversal attack with entry: %q", entry.Header.Name)
152154
}
153155

@@ -157,6 +159,10 @@ func (v tarVisitor) visit(entry TarFileEntry) error {
157159
log.WithFields("path", entry.Header.Name).Trace("skipping symlink/link entry in image tar")
158160

159161
case tar.TypeDir:
162+
// we don't need to do anything for directories, they are created as needed
163+
if entry.Header.Name == "." {
164+
return nil
165+
}
160166
if _, err := v.fs.Stat(target); err != nil {
161167
if err := v.fs.MkdirAll(target, 0755); err != nil {
162168
return err

pkg/file/tarutil_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,20 @@ func Test_tarVisitor_visit(t *testing.T) {
279279
},
280280
wantErr: require.Error,
281281
},
282+
{
283+
name: "local . index is not a traversal error and should skip",
284+
entry: TarFileEntry{
285+
Sequence: 0,
286+
Header: tar.Header{
287+
Typeflag: tar.TypeDir,
288+
Name: ".",
289+
Linkname: "",
290+
Size: 2,
291+
},
292+
Reader: strings.NewReader("hi"),
293+
},
294+
assertFs: []func(t testing.TB, fs afero.Fs){},
295+
},
282296
{
283297
name: "regular file with possible path traversal errors out (same prefix)",
284298
entry: TarFileEntry{

0 commit comments

Comments
 (0)