Skip to content

Commit 324f87b

Browse files
committed
add checking and limits for the worksheet
1 parent 96917e4 commit 324f87b

File tree

8 files changed

+55
-32
lines changed

8 files changed

+55
-32
lines changed

col.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,9 @@ func (f *File) SetColWidth(sheet, startcol, endcol string, width float64) error
444444
if err != nil {
445445
return err
446446
}
447+
if width > MaxColumnWidth {
448+
return errors.New("the width of the column must be smaller than or equal to 255 characters")
449+
}
447450
if min > max {
448451
min, max = max, min
449452
}

col_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ func TestOutlineLevel(t *testing.T) {
236236
assert.EqualError(t, err, "sheet Shee2 is not exist")
237237

238238
assert.NoError(t, f.SetColWidth("Sheet2", "A", "D", 13))
239+
assert.EqualError(t, f.SetColWidth("Sheet2", "A", "D", MaxColumnWidth+1), "the width of the column must be smaller than or equal to 255 characters")
240+
239241
assert.NoError(t, f.SetColOutlineLevel("Sheet2", "B", 2))
240242
assert.NoError(t, f.SetRowOutlineLevel("Sheet1", 2, 7))
241243
assert.EqualError(t, f.SetColOutlineLevel("Sheet1", "D", 8), "invalid outline level")

file.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (f *File) Save() error {
6565
// SaveAs provides a function to create or update to an xlsx file at the
6666
// provided path.
6767
func (f *File) SaveAs(name string, opt ...Options) error {
68-
if len(name) > FileNameLength {
68+
if len(name) > MaxFileNameLength {
6969
return errors.New("file name length exceeds maximum limit")
7070
}
7171
file, err := os.OpenFile(name, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0666)

rows.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,9 @@ func (f *File) SetRowHeight(sheet string, row int, height float64) error {
225225
if row < 1 {
226226
return newInvalidRowNumberError(row)
227227
}
228-
228+
if height > MaxRowHeight {
229+
return errors.New("the height of the row must be smaller than or equal to 409 points")
230+
}
229231
xlsx, err := f.workSheetReader(sheet)
230232
if err != nil {
231233
return err

rows_test.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -91,40 +91,38 @@ func TestRowsError(t *testing.T) {
9191
}
9292

9393
func TestRowHeight(t *testing.T) {
94-
xlsx := NewFile()
95-
sheet1 := xlsx.GetSheetName(0)
94+
f := NewFile()
95+
sheet1 := f.GetSheetName(0)
9696

97-
assert.EqualError(t, xlsx.SetRowHeight(sheet1, 0, defaultRowHeightPixels+1.0), "invalid row number 0")
97+
assert.EqualError(t, f.SetRowHeight(sheet1, 0, defaultRowHeightPixels+1.0), "invalid row number 0")
9898

99-
_, err := xlsx.GetRowHeight("Sheet1", 0)
99+
_, err := f.GetRowHeight("Sheet1", 0)
100100
assert.EqualError(t, err, "invalid row number 0")
101101

102-
assert.NoError(t, xlsx.SetRowHeight(sheet1, 1, 111.0))
103-
height, err := xlsx.GetRowHeight(sheet1, 1)
102+
assert.NoError(t, f.SetRowHeight(sheet1, 1, 111.0))
103+
height, err := f.GetRowHeight(sheet1, 1)
104104
assert.NoError(t, err)
105105
assert.Equal(t, 111.0, height)
106106

107-
assert.NoError(t, xlsx.SetRowHeight(sheet1, 4, 444.0))
108-
height, err = xlsx.GetRowHeight(sheet1, 4)
109-
assert.NoError(t, err)
110-
assert.Equal(t, 444.0, height)
107+
// Test set row height overflow max row height limit.
108+
assert.EqualError(t, f.SetRowHeight(sheet1, 4, MaxRowHeight+1), "the height of the row must be smaller than or equal to 409 points")
111109

112110
// Test get row height that rows index over exists rows.
113-
height, err = xlsx.GetRowHeight(sheet1, 5)
111+
height, err = f.GetRowHeight(sheet1, 5)
114112
assert.NoError(t, err)
115113
assert.Equal(t, defaultRowHeight, height)
116114

117115
// Test get row height that rows heights haven't changed.
118-
height, err = xlsx.GetRowHeight(sheet1, 3)
116+
height, err = f.GetRowHeight(sheet1, 3)
119117
assert.NoError(t, err)
120118
assert.Equal(t, defaultRowHeight, height)
121119

122120
// Test set and get row height on not exists worksheet.
123-
assert.EqualError(t, xlsx.SetRowHeight("SheetN", 1, 111.0), "sheet SheetN is not exist")
124-
_, err = xlsx.GetRowHeight("SheetN", 3)
121+
assert.EqualError(t, f.SetRowHeight("SheetN", 1, 111.0), "sheet SheetN is not exist")
122+
_, err = f.GetRowHeight("SheetN", 3)
125123
assert.EqualError(t, err, "sheet SheetN is not exist")
126124

127-
err = xlsx.SaveAs(filepath.Join("test", "TestRowHeight.xlsx"))
125+
err = f.SaveAs(filepath.Join("test", "TestRowHeight.xlsx"))
128126
if !assert.NoError(t, err) {
129127
t.FailNow()
130128
}

styles.go

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,10 +1037,26 @@ func (f *File) sharedStringsWriter() {
10371037

10381038
// parseFormatStyleSet provides a function to parse the format settings of the
10391039
// cells and conditional formats.
1040-
func parseFormatStyleSet(style string) (*Style, error) {
1041-
format := Style{}
1042-
err := json.Unmarshal([]byte(style), &format)
1043-
return &format, err
1040+
func parseFormatStyleSet(style interface{}) (*Style, error) {
1041+
fs := Style{}
1042+
var err error
1043+
switch v := style.(type) {
1044+
case string:
1045+
err = json.Unmarshal([]byte(v), &fs)
1046+
case *Style:
1047+
fs = *v
1048+
default:
1049+
err = errors.New("invalid parameter type")
1050+
}
1051+
if fs.Font != nil {
1052+
if len(fs.Font.Family) > MaxFontFamilyLength {
1053+
return &fs, errors.New("the length of the font family name must be smaller than or equal to 31")
1054+
}
1055+
if fs.Font.Size > MaxFontSize {
1056+
return &fs, errors.New("font size must be between 1 and 409 points")
1057+
}
1058+
}
1059+
return &fs, err
10441060
}
10451061

10461062
// NewStyle provides a function to create the style for cells by given JSON or
@@ -1909,16 +1925,9 @@ func (f *File) NewStyle(style interface{}) (int, error) {
19091925
var fs *Style
19101926
var err error
19111927
var cellXfsID, fontID, borderID, fillID int
1912-
switch v := style.(type) {
1913-
case string:
1914-
fs, err = parseFormatStyleSet(v)
1915-
if err != nil {
1916-
return cellXfsID, err
1917-
}
1918-
case *Style:
1919-
fs = v
1920-
default:
1921-
return cellXfsID, errors.New("invalid parameter type")
1928+
fs, err = parseFormatStyleSet(style)
1929+
if err != nil {
1930+
return cellXfsID, err
19221931
}
19231932
if fs.DecimalPlaces == 0 {
19241933
fs.DecimalPlaces = 2

styles_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package excelize
33
import (
44
"fmt"
55
"path/filepath"
6+
"strings"
67
"testing"
78

89
"github.com/stretchr/testify/assert"
@@ -200,6 +201,10 @@ func TestNewStyle(t *testing.T) {
200201
assert.NoError(t, err)
201202
_, err = f.NewStyle(Style{})
202203
assert.EqualError(t, err, "invalid parameter type")
204+
_, err = f.NewStyle(&Style{Font: &Font{Family: strings.Repeat("s", MaxFontFamilyLength+1)}})
205+
assert.EqualError(t, err, "the length of the font family name must be smaller than or equal to 31")
206+
_, err = f.NewStyle(&Style{Font: &Font{Size: MaxFontSize + 1}})
207+
assert.EqualError(t, err, "font size must be between 1 and 409 points")
203208
}
204209

205210
func TestGetDefaultFont(t *testing.T) {

xmlDrawing.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ const (
8989

9090
// Excel specifications and limits
9191
const (
92-
FileNameLength = 207
92+
MaxFontFamilyLength = 31
93+
MaxFontSize = 409
94+
MaxFileNameLength = 207
95+
MaxColumnWidth = 255
96+
MaxRowHeight = 409
9397
TotalRows = 1048576
9498
TotalColumns = 16384
9599
TotalSheetHyperlinks = 65529

0 commit comments

Comments
 (0)