Skip to content

Commit

Permalink
archive/zip: ignore modified time if extended timestamp field exists
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderYastrebov committed Jul 26, 2023
1 parent 598958f commit b5faf48
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/archive/zip/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ func (w *Writer) CreateHeader(fh *FileHeader) (io.Writer, error) {
fh.ReaderVersion = zipVersion20

// If Modified is set, this takes precedence over MS-DOS timestamp fields.
if !fh.Modified.IsZero() {
if !fh.Modified.IsZero() && !hasExtraField(fh.Extra, extTimeExtraID) {
// Contrary to the FileHeader.SetModTime method, we intentionally
// do not convert to UTC, because we assume the user intends to encode
// the date using the specified timezone. A user may want this control
Expand Down Expand Up @@ -383,6 +383,20 @@ func (w *Writer) CreateHeader(fh *FileHeader) (io.Writer, error) {
return ow, nil
}

func hasExtraField(extra []byte, tag uint16) bool {
for buf := readBuf(extra); len(buf) >= 4; { // need at least tag and size
if fieldTag := buf.uint16(); fieldTag == tag {
return true
}
fieldSize := int(buf.uint16())
if len(buf) < fieldSize {
break
}
buf.sub(fieldSize)
}
return false
}

func writeHeader(w io.Writer, h *header) error {
const maxUint16 = 1<<16 - 1
if len(h.Name) > maxUint16 {
Expand Down
78 changes: 78 additions & 0 deletions src/archive/zip/zip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -826,3 +826,81 @@ func (zeros) Read(p []byte) (int, error) {
}
return len(p), nil
}

// Issue 61572
func TestWriterModifiedTime(t *testing.T) {
modified := time.Date(2023, 07, 27, 00, 41, 57, 0, timeZone(+1*time.Hour))
extendedTimestampField := []byte{
// tag 0x5455 size 5, extended timestamp, encodes modified
0x55, 0x54, 5, 0, 0x01, 0x45, 0xaf, 0xc1, 0x64,
}

for _, tc := range []struct {
name string
extra, want []byte
}{
{
name: "adds modified time to empty extra",
extra: nil,
want: extendedTimestampField,
},
{
name: "adds modified time if extended timestamp does not exist",
extra: []byte{
// tag 0x9999 size 5
0x99, 0x99, 5, 0, 0, 1, 2, 3, 4,
// tag 0xaaaa size 0
0xaa, 0xaa, 0, 0,
},
want: append([]byte{
0x99, 0x99, 5, 0, 0, 1, 2, 3, 4,
0xaa, 0xaa, 0, 0,
}, extendedTimestampField...),
},
{
name: "ignores modified time if extended timestamp exists",
extra: []byte{
// tag 0x9999 size 5
0x99, 0x99, 5, 0, 0, 1, 2, 3, 4,
// tag 0x5455 size 5, extended timestamp, encodes time.Date(2017, 10, 31, 21, 11, 57, 0, timeZone(-7*time.Hour))
0x55, 0x54, 5, 0, 0x01, 0x8d, 0x49, 0xf9, 0x59,
// tag 0xaaaa size 0
0xaa, 0xaa, 0, 0,
},
want: []byte{ // same as extra
0x99, 0x99, 5, 0, 0, 1, 2, 3, 4,
0x55, 0x54, 5, 0, 0x01, 0x8d, 0x49, 0xf9, 0x59,
0xaa, 0xaa, 0, 0,
},
},
} {
t.Run(tc.name, func(t *testing.T) {
h := &FileHeader{
Name: "issue61572.txt",
Modified: modified,
Extra: tc.extra,
}

var buf bytes.Buffer
w := NewWriter(&buf)
if _, err := w.CreateHeader(h); err != nil {
t.Fatalf("unexpected CreateHeader error: %v", err)
}
if err := w.Close(); err != nil {
t.Fatalf("unexpected Close error: %v", err)
}

in := buf.Bytes()
zf, err := NewReader(bytes.NewReader(in), int64(len(in)))
if err != nil {
t.Fatalf("unexpected NewReader error: %v", err)
}

if got := zf.File[0].FileHeader.Extra; !bytes.Equal(tc.want, got) {
t.Logf("want: % x", tc.want)
t.Logf(" got: % x", got)
t.Error("wrong header extra")
}
})
}
}

0 comments on commit b5faf48

Please sign in to comment.