]> Cypherpunks repositories - gostls13.git/commitdiff
archive/zip: accept bogus trailing zeros in extras
authorBrad Fitzpatrick <bradfitz@golang.org>
Mon, 4 Aug 2014 23:12:55 +0000 (16:12 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Mon, 4 Aug 2014 23:12:55 +0000 (16:12 -0700)
Popular tools both add incorrect trailing zeroes to the zip
extras, and popular tools accept trailing zeros. We seemed to
be the only ones being strict here. Stop being strict. :(

Fixes #8186

LGTM=ruiu, adg, dave
R=adg, ruiu, dave
CC=frohrweck, golang-codereviews
https://golang.org/cl/117550044

src/pkg/archive/zip/reader.go
src/pkg/archive/zip/reader_test.go

index 80ee03006f09691740f8d8ccd4c6b217ab718120..8136b840d455f4bcf0911d1c0b7020822a49faba 100644 (file)
@@ -267,8 +267,13 @@ func readDirectoryHeader(f *File, r io.Reader) error {
                        b = b[size:]
                }
                // Should have consumed the whole header.
-               if len(b) != 0 {
-                       return ErrFormat
+               // But popular zip & JAR creation tools are broken and
+               // may pad extra zeros at the end, so accept those
+               // too. See golang.org/issue/8186.
+               for _, v := range b {
+                       if v != 0 {
+                               return ErrFormat
+                       }
                }
        }
        return nil
index 5652f3a50074b6b2ebef61a87c729dca1fe77a8c..29d0652dcc16ed795841f9449aecb4d1a0f0caa3 100644 (file)
@@ -13,6 +13,7 @@ import (
        "os"
        "path/filepath"
        "regexp"
+       "strings"
        "testing"
        "time"
 )
@@ -508,3 +509,25 @@ func returnRecursiveZip() (r io.ReaderAt, size int64) {
        b := rZipBytes()
        return bytes.NewReader(b), int64(len(b))
 }
+
+func TestIssue8186(t *testing.T) {
+       // Directory headers & data found in the TOC of a JAR file.
+       dirEnts := []string{
+               "PK\x01\x02\n\x00\n\x00\x00\b\x00\x004\x9d3?\xaa\x1b\x06\xf0\x81\x02\x00\x00\x81\x02\x00\x00-\x00\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00res/drawable-xhdpi-v4/ic_actionbar_accept.png\xfe\xca\x00\x00\x00",
+               "PK\x01\x02\n\x00\n\x00\x00\b\x00\x004\x9d3?\x90K\x89\xc7t\n\x00\x00t\n\x00\x00\x0e\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xd1\x02\x00\x00resources.arsc\x00\x00\x00",
+               "PK\x01\x02\x14\x00\x14\x00\b\b\b\x004\x9d3?\xff$\x18\xed3\x03\x00\x00\xb4\b\x00\x00\x13\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00t\r\x00\x00AndroidManifest.xml",
+               "PK\x01\x02\x14\x00\x14\x00\b\b\b\x004\x9d3?\x14\xc5K\xab\x192\x02\x00\xc8\xcd\x04\x00\v\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xe8\x10\x00\x00classes.dex",
+               "PK\x01\x02\x14\x00\x14\x00\b\b\b\x004\x9d3?E\x96\nD\xac\x01\x00\x00P\x03\x00\x00&\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00:C\x02\x00res/layout/actionbar_set_wallpaper.xml",
+               "PK\x01\x02\x14\x00\x14\x00\b\b\b\x004\x9d3?Ļ\x14\xe3\xd8\x01\x00\x00\xd8\x03\x00\x00 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00:E\x02\x00res/layout/wallpaper_cropper.xml",
+               "PK\x01\x02\x14\x00\x14\x00\b\b\b\x004\x9d3?}\xc1\x15\x9eZ\x01\x00\x00!\x02\x00\x00\x14\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00`G\x02\x00META-INF/MANIFEST.MF",
+               "PK\x01\x02\x14\x00\x14\x00\b\b\b\x004\x9d3?\xe6\x98Ьo\x01\x00\x00\x84\x02\x00\x00\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xfcH\x02\x00META-INF/CERT.SF",
+               "PK\x01\x02\x14\x00\x14\x00\b\b\b\x004\x9d3?\xbfP\x96b\x86\x04\x00\x00\xb2\x06\x00\x00\x11\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa9J\x02\x00META-INF/CERT.RSA",
+       }
+       for i, s := range dirEnts {
+               var f File
+               err := readDirectoryHeader(&f, strings.NewReader(s))
+               if err != nil {
+                       t.Errorf("error reading #%d: %v", i, err)
+               }
+       }
+}