File tree 2 files changed +21
-1
lines changed
2 files changed +21
-1
lines changed Original file line number Diff line number Diff line change @@ -147,7 +147,9 @@ func (v tarVisitor) visit(entry TarFileEntry) error {
147
147
target := filepath .Join (v .destination , entry .Header .Name )
148
148
149
149
// 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 != "." {
151
153
return fmt .Errorf ("potential path traversal attack with entry: %q" , entry .Header .Name )
152
154
}
153
155
@@ -157,6 +159,10 @@ func (v tarVisitor) visit(entry TarFileEntry) error {
157
159
log .WithFields ("path" , entry .Header .Name ).Trace ("skipping symlink/link entry in image tar" )
158
160
159
161
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
+ }
160
166
if _ , err := v .fs .Stat (target ); err != nil {
161
167
if err := v .fs .MkdirAll (target , 0755 ); err != nil {
162
168
return err
Original file line number Diff line number Diff line change @@ -279,6 +279,20 @@ func Test_tarVisitor_visit(t *testing.T) {
279
279
},
280
280
wantErr : require .Error ,
281
281
},
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
+ },
282
296
{
283
297
name : "regular file with possible path traversal errors out (same prefix)" ,
284
298
entry : TarFileEntry {
You can’t perform that action at this time.
0 commit comments