Skip to content

Commit ae2113b

Browse files
authored
This fixes GHSA-5h23-36rv-pm65 panic on negative style index (#2367)
- Update unit tests
1 parent 0f7445b commit ae2113b

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

styles.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,17 +1171,17 @@ var (
11711171
"fill": func(xf xlsxXf, s *xlsxStyleSheet) bool {
11721172
return (xf.ApplyFill == nil || (xf.ApplyFill != nil && *xf.ApplyFill)) &&
11731173
xf.FillID != nil && s.Fills != nil &&
1174-
*xf.FillID < len(s.Fills.Fill)
1174+
0 <= *xf.FillID && *xf.FillID < len(s.Fills.Fill)
11751175
},
11761176
"border": func(xf xlsxXf, s *xlsxStyleSheet) bool {
11771177
return (xf.ApplyBorder == nil || (xf.ApplyBorder != nil && *xf.ApplyBorder)) &&
11781178
xf.BorderID != nil && s.Borders != nil &&
1179-
*xf.BorderID < len(s.Borders.Border)
1179+
0 <= *xf.BorderID && *xf.BorderID < len(s.Borders.Border)
11801180
},
11811181
"font": func(xf xlsxXf, s *xlsxStyleSheet) bool {
11821182
return (xf.ApplyFont == nil || (xf.ApplyFont != nil && *xf.ApplyFont)) &&
11831183
xf.FontID != nil && s.Fonts != nil &&
1184-
*xf.FontID < len(s.Fonts.Font)
1184+
0 <= *xf.FontID && *xf.FontID < len(s.Fonts.Font)
11851185
},
11861186
"alignment": func(xf xlsxXf, s *xlsxStyleSheet) bool {
11871187
return xf.ApplyAlignment == nil || (xf.ApplyAlignment != nil && *xf.ApplyAlignment)

styles_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,4 +897,24 @@ func TestGetStyle(t *testing.T) {
897897
style, err = f.GetStyle(1)
898898
assert.Nil(t, style)
899899
assert.EqualError(t, err, "XML syntax error on line 1: invalid UTF-8")
900+
901+
t.Run("with_negative_index", func(t *testing.T) {
902+
styleSheet := `<styleSheet xmlns="%s"><fonts count="1"><font><sz val="11"/><name val="Calibri"/></font></fonts><fills count="1"><fill><patternFill patternType="none"/></fill></fills><borders count="1"><border/></borders><cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"/></cellStyleXfs><cellXfs count="2"><xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/><xf numFmtId="0" %s xfId="0" applyFont="1" applyFill="1" applyBorder="1"/></cellXfs></styleSheet>`
903+
for _, testCase := range []struct {
904+
label string
905+
attrs string
906+
}{
907+
{"negative fill index", `fontId="0" fillId="-1" borderId="0"`},
908+
{"negative border index", `fontId="0" fillId="0" borderId="-1"`},
909+
{"negative font index", `fontId="-1" fillId="0" borderId="0"`},
910+
} {
911+
f := NewFile()
912+
f.Styles = nil
913+
f.Pkg.Store(defaultXMLPathStyles, fmt.Appendf(nil, styleSheet, NameSpaceSpreadSheet.Value, testCase.attrs))
914+
style, err := f.GetStyle(1)
915+
assert.NoError(t, err, testCase.label)
916+
assert.NotNil(t, style, testCase.label)
917+
assert.NoError(t, f.Close(), testCase.label)
918+
}
919+
})
900920
}

0 commit comments

Comments
 (0)