| 1 | package xlsx |
| 2 | |
| 3 | import ( |
| 4 | "archive/zip" |
| 5 | "bytes" |
| 6 | "encoding/xml" |
| 7 | "errors" |
| 8 | "io" |
| 9 | "path" |
| 10 | "strings" |
| 11 | ) |
| 12 | |
| 13 | // The scan's own bounds. excelize's openLimits bound what excelize |
| 14 | // decompresses; these bound what this package's second reader does, |
| 15 | // since a bound on one reader says nothing about the other. |
| 16 | const ( |
| 17 | maxZipEntries = 10_000 |
| 18 | maxPartBytes = 8 << 20 // _rels/.rels, the workbook and its rels |
| 19 | maxSheetBytes = 256 << 20 |
| 20 | maxXMLDepth = 64 |
| 21 | maxCachedBytes = 1 << 20 // one <v>; Excel caps a cell at 32,767 characters |
| 22 | ) |
| 23 | |
| 24 | const ( |
| 25 | relOfficeDocument = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" |
| 26 | relWorksheet = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" |
| 27 | relNamespace = "http://schemas.openxmlformats.org/officeDocument/2006/relationships" |
| 28 | ) |
| 29 | |
| 30 | // errScanInconsistent is a workbook this package's reader and |
| 31 | // excelize's disagree about. Either could be the one that is right; |
| 32 | // neither is trusted to decide alone, so the import stops. |
| 33 | var errScanInconsistent = errors.New("xlsx: workbook parts disagree") |
| 34 | |
| 35 | // errScanLimit is a part past one of the bounds above. |
| 36 | var errScanLimit = errors.New("xlsx: workbook part exceeds a limit") |
| 37 | |
| 38 | // zipParts indexes a workbook's entries by name the way excelize does: |
| 39 | // backslashes read as slashes, and a later duplicate replaces an |
| 40 | // earlier one. |
| 41 | type zipParts map[string]*zip.File |
| 42 | |
| 43 | func openParts(data []byte) (zipParts, error) { |
| 44 | zr, err := zip.NewReader(bytes.NewReader(data), int64(len(data))) |
| 45 | if err != nil { |
| 46 | return nil, err |
| 47 | } |
| 48 | if len(zr.File) > maxZipEntries { |
| 49 | return nil, errScanLimit |
| 50 | } |
| 51 | parts := make(zipParts, len(zr.File)) |
| 52 | for _, f := range zr.File { |
| 53 | parts[strings.ReplaceAll(f.Name, `\`, "/")] = f |
| 54 | } |
| 55 | return parts, nil |
| 56 | } |
| 57 | |
| 58 | // open returns a reader over one part that fails, rather than |
| 59 | // truncating, once it has produced more than limit bytes. |
| 60 | func (p zipParts) open(name string, limit int64) (io.ReadCloser, error) { |
| 61 | f, ok := p[name] |
| 62 | if !ok { |
| 63 | return nil, errScanInconsistent |
| 64 | } |
| 65 | rc, err := f.Open() |
| 66 | if err != nil { |
| 67 | return nil, err |
| 68 | } |
| 69 | return &capReader{rc: rc, left: limit}, nil |
| 70 | } |
| 71 | |
| 72 | // count reports how many entries sit under prefix: charts and images, |
| 73 | // which this import never reads, only counts. |
| 74 | func (p zipParts) count(prefix string) int { |
| 75 | n := 0 |
| 76 | for name := range p { |
| 77 | if strings.HasPrefix(name, prefix) && !strings.HasSuffix(name, "/") { |
| 78 | n++ |
| 79 | } |
| 80 | } |
| 81 | return n |
| 82 | } |
| 83 | |
| 84 | type capReader struct { |
| 85 | rc io.ReadCloser |
| 86 | left int64 |
| 87 | } |
| 88 | |
| 89 | func (c *capReader) Read(b []byte) (int, error) { |
| 90 | if c.left < 0 { |
| 91 | return 0, errScanLimit |
| 92 | } |
| 93 | if int64(len(b)) > c.left+1 { |
| 94 | b = b[:c.left+1] |
| 95 | } |
| 96 | n, err := c.rc.Read(b) |
| 97 | c.left -= int64(n) |
| 98 | if c.left < 0 { |
| 99 | return n, errScanLimit |
| 100 | } |
| 101 | return n, err |
| 102 | } |
| 103 | |
| 104 | func (c *capReader) Close() error { return c.rc.Close() } |
| 105 | |
| 106 | // firstWorksheetPath resolves the first <sheet> of the workbook to its |
| 107 | // part, by the same route excelize takes: _rels/.rels names the |
| 108 | // workbook, the workbook's own rels name each sheet's target, and a |
| 109 | // target resolves against the workbook's directory unless it is |
| 110 | // absolute. It also returns that sheet's name, compared in memory with |
| 111 | // excelize's first sheet and never written anywhere. |
| 112 | func firstWorksheetPath(p zipParts) (sheetPath, name string, err error) { |
| 113 | rootRels, err := readRels(p, "_rels/.rels") |
| 114 | if err != nil { |
| 115 | return "", "", err |
| 116 | } |
| 117 | var wbPath string |
| 118 | for _, r := range rootRels { |
| 119 | if r.Type == relOfficeDocument { |
| 120 | wbPath = strings.TrimPrefix(r.Target, "/") |
| 121 | break |
| 122 | } |
| 123 | } |
| 124 | if wbPath == "" { |
| 125 | return "", "", errScanInconsistent |
| 126 | } |
| 127 | sheetID, name, err := firstSheetRelID(p, wbPath) |
| 128 | if err != nil { |
| 129 | return "", "", err |
| 130 | } |
| 131 | dir := path.Dir(wbPath) |
| 132 | relsPath := strings.TrimPrefix(path.Join(dir, "_rels", path.Base(wbPath)+".rels"), "./") |
| 133 | rels, err := readRels(p, relsPath) |
| 134 | if err != nil { |
| 135 | return "", "", err |
| 136 | } |
| 137 | for _, r := range rels { |
| 138 | if r.ID != sheetID { |
| 139 | continue |
| 140 | } |
| 141 | if r.Type != relWorksheet { |
| 142 | return "", "", errScanInconsistent // a chartsheet or dialog first |
| 143 | } |
| 144 | target := strings.ReplaceAll(r.Target, `\`, "/") |
| 145 | if strings.HasPrefix(target, "/") { |
| 146 | return strings.TrimPrefix(path.Clean(target), "/"), name, nil |
| 147 | } |
| 148 | return strings.TrimPrefix(path.Clean(dir+"/"+target), "/"), name, nil |
| 149 | } |
| 150 | return "", "", errScanInconsistent |
| 151 | } |
| 152 | |
| 153 | type relationship struct { |
| 154 | ID string `xml:"Id,attr"` |
| 155 | Type string `xml:"Type,attr"` |
| 156 | Target string `xml:"Target,attr"` |
| 157 | } |
| 158 | |
| 159 | func readRels(p zipParts, name string) ([]relationship, error) { |
| 160 | var doc struct { |
| 161 | Rels []relationship `xml:"Relationship"` |
| 162 | } |
| 163 | if err := decodePart(p, name, &doc); err != nil { |
| 164 | return nil, err |
| 165 | } |
| 166 | return doc.Rels, nil |
| 167 | } |
| 168 | |
| 169 | func firstSheetRelID(p zipParts, wbPath string) (id, name string, err error) { |
| 170 | var doc struct { |
| 171 | Sheets []struct { |
| 172 | Name string `xml:"name,attr"` |
| 173 | Attrs []xml.Attr `xml:",any,attr"` |
| 174 | } `xml:"sheets>sheet"` |
| 175 | } |
| 176 | if err := decodePart(p, wbPath, &doc); err != nil { |
| 177 | return "", "", err |
| 178 | } |
| 179 | if len(doc.Sheets) == 0 { |
| 180 | return "", "", errScanInconsistent |
| 181 | } |
| 182 | for _, a := range doc.Sheets[0].Attrs { |
| 183 | if a.Name.Space == relNamespace && a.Name.Local == "id" { |
| 184 | return a.Value, doc.Sheets[0].Name, nil |
| 185 | } |
| 186 | } |
| 187 | return "", "", errScanInconsistent |
| 188 | } |
| 189 | |
| 190 | // decodePart unmarshals one small part, bounded in bytes and depth. |
| 191 | func decodePart(p zipParts, name string, into any) error { |
| 192 | rc, err := p.open(name, maxPartBytes) |
| 193 | if err != nil { |
| 194 | return err |
| 195 | } |
| 196 | defer rc.Close() |
| 197 | body, err := io.ReadAll(rc) |
| 198 | if err != nil { |
| 199 | return err |
| 200 | } |
| 201 | if err := checkDepth(body); err != nil { |
| 202 | return err |
| 203 | } |
| 204 | return xml.Unmarshal(body, into) |
| 205 | } |
| 206 | |
| 207 | // checkDepth refuses XML nested deeper than maxXMLDepth before |
| 208 | // encoding/xml, which has no depth limit of its own, unmarshals it. |
| 209 | func checkDepth(body []byte) error { |
| 210 | d := xml.NewDecoder(bytes.NewReader(body)) |
| 211 | depth := 0 |
| 212 | for { |
| 213 | tok, err := d.RawToken() |
| 214 | if err == io.EOF { |
| 215 | return nil |
| 216 | } |
| 217 | if err != nil { |
| 218 | return err |
| 219 | } |
| 220 | switch tok.(type) { |
| 221 | case xml.StartElement: |
| 222 | if depth++; depth > maxXMLDepth { |
| 223 | return errScanLimit |
| 224 | } |
| 225 | case xml.EndElement: |
| 226 | depth-- |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | |