Skip to content

Commit 01086b9

Browse files
authored
Merge pull request #1084 from gotify/sort-key-big
fix: limit sortKey to at most 255 bytes
2 parents 5ce7f0a + 7272a93 commit 01086b9

4 files changed

Lines changed: 164 additions & 12 deletions

File tree

‎api/application.go‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/gin-gonic/gin"
1414
"github.com/gotify/server/v3/auth"
15+
"github.com/gotify/server/v3/fracdex"
1516
"github.com/gotify/server/v3/model"
1617
"github.com/h2non/filetype"
1718
"gorm.io/gorm"
@@ -94,6 +95,9 @@ type ApplicationParams struct {
9495
func (a *ApplicationAPI) CreateApplication(ctx *gin.Context) {
9596
applicationParams := ApplicationParams{}
9697
if err := ctx.Bind(&applicationParams); err == nil {
98+
if !validateSortKey(ctx, applicationParams.SortKey) {
99+
return
100+
}
97101
tokenPublic, tokenPrivate := generateApplicationToken()
98102
app := model.Application{
99103
Name: applicationParams.Name,
@@ -263,6 +267,9 @@ func (a *ApplicationAPI) UpdateApplication(ctx *gin.Context) {
263267
if app != nil && app.UserID == auth.GetUserID(ctx) {
264268
applicationParams := ApplicationParams{}
265269
if err := ctx.Bind(&applicationParams); err == nil {
270+
if !validateSortKey(ctx, applicationParams.SortKey) {
271+
return
272+
}
266273
app.Description = applicationParams.Description
267274
app.Name = applicationParams.Name
268275
app.DefaultPriority = applicationParams.DefaultPriority
@@ -602,6 +609,14 @@ func ValidApplicationImageExt(ext string) bool {
602609
}
603610
}
604611

612+
func validateSortKey(ctx *gin.Context, sortKey string) bool {
613+
if err := fracdex.ValidateOrderKey(sortKey); err != nil {
614+
ctx.AbortWithError(400, err)
615+
return false
616+
}
617+
return true
618+
}
619+
605620
func handleApplicationError(ctx *gin.Context, err error) {
606621
if errors.Is(err, gorm.ErrDuplicatedKey) {
607622
ctx.AbortWithError(400, errors.New("sort key is not unique"))

‎api/application_test.go‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,32 @@ func (s *ApplicationSuite) Test_CreateApplication_expectBadRequestOnEmptyName()
103103
}
104104
}
105105

106+
func (s *ApplicationSuite) Test_CreateApplication_expectBadRequestOnTooLongSortKey() {
107+
s.db.User(5)
108+
109+
test.WithUser(s.ctx, 5)
110+
s.withFormData("name=app&sortKey=" + strings.Repeat("z", 256))
111+
s.a.CreateApplication(s.ctx)
112+
113+
assert.Equal(s.T(), 400, s.recorder.Code)
114+
if app, err := s.db.GetApplicationsByUser(5); assert.NoError(s.T(), err) {
115+
assert.Empty(s.T(), app)
116+
}
117+
}
118+
119+
func (s *ApplicationSuite) Test_CreateApplication_expectBadRequestOnInvalidSortKey() {
120+
s.db.User(5)
121+
122+
test.WithUser(s.ctx, 5)
123+
s.withFormData("name=app&sortKey=a10")
124+
s.a.CreateApplication(s.ctx)
125+
126+
assert.Equal(s.T(), 400, s.recorder.Code)
127+
if app, err := s.db.GetApplicationsByUser(5); assert.NoError(s.T(), err) {
128+
assert.Empty(s.T(), app)
129+
}
130+
}
131+
106132
func (s *ApplicationSuite) Test_CreateApplication_ignoresReadOnlyPropertiesInParams() {
107133
s.db.User(5)
108134

@@ -764,6 +790,38 @@ func (s *ApplicationSuite) Test_UpdateApplication_duplicateSortKey() {
764790
assert.Equal(s.T(), 400, s.recorder.Code)
765791
}
766792

793+
func (s *ApplicationSuite) Test_UpdateApplication_expectBadRequestOnTooLongSortKey() {
794+
user := s.db.User(5)
795+
user.App(1) // sortKey=a0
796+
797+
s.withFormData("name=new_name&sortKey=" + strings.Repeat("z", 256))
798+
test.WithUser(s.ctx, 5)
799+
s.ctx.Params = gin.Params{{Key: "id", Value: "1"}}
800+
801+
s.a.UpdateApplication(s.ctx)
802+
803+
assert.Equal(s.T(), 400, s.recorder.Code)
804+
if app, err := s.db.GetApplicationByID(1); assert.NoError(s.T(), err) {
805+
assert.Equal(s.T(), "a0", app.SortKey)
806+
}
807+
}
808+
809+
func (s *ApplicationSuite) Test_UpdateApplication_expectBadRequestOnInvalidSortKey() {
810+
user := s.db.User(5)
811+
user.App(1) // sortKey=a0
812+
813+
s.withFormData("name=new_name&sortKey=a10")
814+
test.WithUser(s.ctx, 5)
815+
s.ctx.Params = gin.Params{{Key: "id", Value: "1"}}
816+
817+
s.a.UpdateApplication(s.ctx)
818+
819+
assert.Equal(s.T(), 400, s.recorder.Code)
820+
if app, err := s.db.GetApplicationByID(1); assert.NoError(s.T(), err) {
821+
assert.Equal(s.T(), "a0", app.SortKey)
822+
}
823+
}
824+
767825
func (s *ApplicationSuite) withFormData(formData string) {
768826
s.ctx.Request = httptest.NewRequest("POST", "/token", strings.NewReader(formData))
769827
s.ctx.Request.Header.Set("Content-Type", "application/x-www-form-urlencoded")

‎fracdex/fracdex.go‎

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,31 @@ const (
1212
base62Digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
1313
smallestInt = "A00000000000000000000000000"
1414
zero = "a0"
15+
maxKeyLen = 255
1516
)
1617

1718
// KeyBetween returns a key that sorts lexicographically between a and b.
1819
// Either a or b can be empty strings. If a is empty it indicates smallest key,
1920
// If b is empty it indicates largest key.
2021
// b must be empty string or > a.
2122
func KeyBetween(a, b string) (string, error) {
22-
if a != "" {
23-
err := validateOrderKey(a)
24-
if err != nil {
25-
return "", err
26-
}
23+
if err := ValidateOrderKey(a); err != nil {
24+
return "", err
2725
}
28-
if b != "" {
29-
err := validateOrderKey(b)
30-
if err != nil {
31-
return "", err
32-
}
26+
if err := ValidateOrderKey(b); err != nil {
27+
return "", err
3328
}
29+
key, err := keyBetweenInternal(a, b)
30+
if err != nil {
31+
return "", err
32+
}
33+
if len(key) > maxKeyLen {
34+
return "", errors.New("range overflow")
35+
}
36+
return key, nil
37+
}
38+
39+
func keyBetweenInternal(a, b string) (string, error) {
3440
if a != "" && b != "" && a >= b {
3541
return "", fmt.Errorf("%s >= %s", a, b)
3642
}
@@ -57,6 +63,9 @@ func KeyBetween(a, b string) (string, error) {
5763
if res == "" {
5864
return "", errors.New("range underflow")
5965
}
66+
if res == smallestInt {
67+
return res + midpoint("", ""), nil
68+
}
6069
return res, nil
6170
}
6271

@@ -192,7 +201,13 @@ func getIntPart(key string) (string, error) {
192201
return key[0:intPartLen], nil
193202
}
194203

195-
func validateOrderKey(key string) error {
204+
func ValidateOrderKey(key string) error {
205+
if key == "" {
206+
return nil
207+
}
208+
if len(key) > maxKeyLen {
209+
return fmt.Errorf("invalid order key: too long")
210+
}
196211
if key == smallestInt {
197212
return fmt.Errorf("invalid order key: %s", key)
198213
}
@@ -203,6 +218,11 @@ func validateOrderKey(key string) error {
203218
if err != nil {
204219
return err
205220
}
221+
for _, c := range key {
222+
if !strings.ContainsRune(base62Digits, c) {
223+
return fmt.Errorf("invalid order key: %s", key)
224+
}
225+
}
206226
f := key[len(i):]
207227
if strings.HasSuffix(f, "0") {
208228
return fmt.Errorf("invalid order key: %s", key)
@@ -294,7 +314,7 @@ func Float64Approx(key string) (float64, error) {
294314
return 0.0, errors.New("invalid order key")
295315
}
296316

297-
err := validateOrderKey(key)
317+
err := ValidateOrderKey(key)
298318
if err != nil {
299319
return 0.0, err
300320
}

‎fracdex/fracdex_test.go‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,62 @@ func TestToFloat64Approx(t *testing.T) {
116116
test("a400", 0.0, "invalid order key: a400")
117117
test("a!", 0.0, "invalid order key: a!")
118118
}
119+
120+
func FuzzValidateOrderKey(f *testing.F) {
121+
for _, seed := range [][2]string{
122+
{"", ""},
123+
{"a0", "a1"},
124+
{"Zz", "a0"},
125+
{"a0V", "a1"},
126+
{"", "Y00"},
127+
{"bzz", ""},
128+
{"b125", "b129"},
129+
{"zzzzzzzzzzzzzzzzzzzzzzzzzzz", ""},
130+
{"", "A00000000000000000000000001"},
131+
{strings.Repeat("z", 255), strings.Repeat("z", 256)},
132+
{"a0 ", "a0!"},
133+
} {
134+
f.Add(seed[0], seed[1])
135+
}
136+
137+
f.Fuzz(func(t *testing.T, a, b string) {
138+
// Validating must never panic
139+
if ValidateOrderKey(a) != nil || ValidateOrderKey(b) != nil {
140+
return
141+
}
142+
143+
// Any valid key must be safe to use with other fracdex functions.
144+
checkBetween := func(lo, hi string) {
145+
if lo != "" && hi != "" && lo >= hi {
146+
return
147+
}
148+
mid, err := KeyBetween(lo, hi)
149+
if err != nil {
150+
return
151+
}
152+
assert.NoError(t, ValidateOrderKey(mid), "KeyBetween(%q, %q) = %q", lo, hi, mid)
153+
assert.NotEmpty(t, mid, "KeyBetween(%q, %q)", lo, hi)
154+
if lo != "" {
155+
assert.Greater(t, mid, lo, "KeyBetween(%q, %q) = %q", lo, hi, mid)
156+
}
157+
if hi != "" {
158+
assert.Less(t, mid, hi, "KeyBetween(%q, %q) = %q", lo, hi, mid)
159+
}
160+
}
161+
162+
checkBetween(a, "")
163+
checkBetween("", a)
164+
checkBetween(b, "")
165+
checkBetween("", b)
166+
checkBetween(a, b)
167+
checkBetween(b, a)
168+
169+
for _, key := range []string{a, b} {
170+
if key == "" {
171+
continue
172+
}
173+
_, err := Float64Approx(key)
174+
assert.NoError(t, err, "Float64Approx(%q)", key)
175+
}
176+
})
177+
}

0 commit comments

Comments
 (0)