Skip to content

Commit 8472512

Browse files
committed
Harden popular links period handling
1 parent bfff450 commit 8472512

3 files changed

Lines changed: 27 additions & 2 deletions

File tree

db.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ func (s *SQLiteDB) LoadAll() ([]*Link, error) {
8484
if err != nil {
8585
return nil, err
8686
}
87+
defer rows.Close()
88+
8789
for rows.Next() {
8890
link := new(Link)
8991
var created, lastEdit int64
@@ -178,6 +180,8 @@ func (s *SQLiteDB) LoadStats() (ClickStats, error) {
178180
if err != nil {
179181
return nil, err
180182
}
183+
defer rows.Close()
184+
181185
stats := make(map[string]int)
182186
for rows.Next() {
183187
var id string
@@ -266,6 +270,8 @@ func (s *SQLiteDB) GetLinksByOwner(owner string) ([]*Link, error) {
266270
if err != nil {
267271
return nil, err
268272
}
273+
defer rows.Close()
274+
269275
for rows.Next() {
270276
link := new(Link)
271277
var created, lastEdit int64

golink.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import (
4444

4545
const (
4646
defaultHostname = "go"
47+
maxStatsPeriod = 365 * 24 * time.Hour
4748

4849
// Used as a placeholder short name for generating the XSRF defense token,
4950
// when creating new links.
@@ -580,12 +581,12 @@ func parseStatsPeriod(period string) (time.Duration, bool) {
580581
if period == "" {
581582
return 0, false
582583
}
583-
if d, err := time.ParseDuration(period); err == nil && d > 0 {
584+
if d, err := time.ParseDuration(period); err == nil && d > 0 && d <= maxStatsPeriod {
584585
return d, true
585586
}
586587
if days, ok := strings.CutSuffix(period, "d"); ok {
587588
n, err := strconv.Atoi(days)
588-
if err == nil && n > 0 {
589+
if err == nil && n > 0 && n <= int(maxStatsPeriod/(24*time.Hour)) {
589590
return time.Duration(n) * 24 * time.Hour, true
590591
}
591592
}

golink_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,10 +973,28 @@ func TestParseStatsPeriod(t *testing.T) {
973973
want: 168 * time.Hour,
974974
wantOK: true,
975975
},
976+
{
977+
name: "max duration",
978+
period: "8760h",
979+
want: maxStatsPeriod,
980+
wantOK: true,
981+
},
982+
{
983+
name: "too long duration",
984+
period: "8761h",
985+
},
986+
{
987+
name: "too many days",
988+
period: "366d",
989+
},
976990
{
977991
name: "negative",
978992
period: "-7d",
979993
},
994+
{
995+
name: "overflowing days",
996+
period: "100000000000000000000d",
997+
},
980998
{
981999
name: "invalid",
9821000
period: "nope",

0 commit comments

Comments
 (0)