Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

fix(api): clamp post and user list limits from query params

Matt Robertsc19ae247291c
diff --git a/app/handlers/apiv1/post.go b/app/handlers/apiv1/post.go
index 6465fc547..2ab482495 100644
--- a/app/handlers/apiv1/post.go
+++ b/app/handlers/apiv1/post.go
@@ -56,10 +56,11 @@ func SearchPosts() web.HandlerFunc {
searchPosts := &query.SearchPosts{
Query: c.QueryParam("query"),
View: viewQueryParams,
- Limit: c.QueryParam("limit"),
Tags: c.QueryParamAsArray("tags"),
ModerationFilter: c.QueryParam("moderation"),
}
+ searchPosts.SetLimitFromString(c.QueryParam("limit"))
+
if myVotesOnly, err := c.QueryParamAsBool("myvotes"); err == nil {
searchPosts.MyVotesOnly = myVotesOnly
}
diff --git a/app/handlers/apiv1/post_test.go b/app/handlers/apiv1/post_test.go
index f13d36de7..a06b902ab 100644
--- a/app/handlers/apiv1/post_test.go
+++ b/app/handlers/apiv1/post_test.go
@@ -85,6 +85,26 @@ func TestCreatePostHandler_AppendsUnreferencedAttachments(t *testing.T) {
Expect(newPost.Description).Equals("Already referenced: ![](fider-image:attachments/referenced.png)\n\n![](fider-image:attachments/standalone.png)")
}
+func TestSearchPostsHandler_ClampsLimit(t *testing.T) {
+ RegisterT(t)
+
+ for _, limit := range []string{"all", "2000000000"} {
+ var searchPosts *query.SearchPosts
+ bus.AddHandler(func(ctx context.Context, q *query.SearchPosts) error {
+ searchPosts = q
+ return nil
+ })
+
+ status, _ := mock.NewServer().
+ OnTenant(mock.DemoTenant).
+ WithURL("http://demo.test.fider.io/api/v1/posts?limit=" + limit).
+ Execute(apiv1.SearchPosts())
+
+ Expect(status).Equals(http.StatusOK)
+ Expect(searchPosts.Limit).Equals("1000")
+ }
+}
+
func TestCreatePostHandler_WithoutTitle(t *testing.T) {
RegisterT(t)
diff --git a/app/handlers/apiv1/user.go b/app/handlers/apiv1/user.go
index cb99dc821..e6936341d 100644
--- a/app/handlers/apiv1/user.go
+++ b/app/handlers/apiv1/user.go
@@ -12,6 +12,9 @@ import (
"github.com/getfider/fider/app/pkg/web"
)
+// maxListUsersLimit is the largest page size accepted by ListUsers
+const maxListUsersLimit = 100
+
// ListUsers returns paginated registered users
func ListUsers() web.HandlerFunc {
return func(c *web.Context) error {
@@ -23,6 +26,8 @@ func ListUsers() web.HandlerFunc {
limit, _ := c.QueryParamAsInt("limit")
if limit <= 0 {
limit = 10
+ } else if limit > maxListUsersLimit {
+ limit = maxListUsersLimit
}
searchUsers := &query.SearchUsers{
diff --git a/app/handlers/apiv1/user_test.go b/app/handlers/apiv1/user_test.go
index 1b6a2ec46..9463de6f6 100644
--- a/app/handlers/apiv1/user_test.go
+++ b/app/handlers/apiv1/user_test.go
@@ -41,6 +41,25 @@ func TestListUsersHandler(t *testing.T) {
Expect(query.Contains("users")).IsTrue()
}
+func TestListUsersHandler_ClampsLimit(t *testing.T) {
+ RegisterT(t)
+
+ var searchUsers *query.SearchUsers
+ bus.AddHandler(func(ctx context.Context, q *query.SearchUsers) error {
+ searchUsers = q
+ return nil
+ })
+
+ status, response := mock.NewServer().
+ AsUser(mock.JonSnow).
+ WithURL("http://demo.test.fider.io/api/v1/users?limit=2000000000").
+ ExecuteAsJSON(apiv1.ListUsers())
+
+ Expect(status).Equals(http.StatusOK)
+ Expect(searchUsers.Limit).Equals(100)
+ Expect(response.Int32("limit")).Equals(100)
+}
+
func TestCreateUser_ExistingEmail(t *testing.T) {
RegisterT(t)
diff --git a/app/handlers/post.go b/app/handlers/post.go
index 5d2f837c5..d74166703 100644
--- a/app/handlers/post.go
+++ b/app/handlers/post.go
@@ -20,10 +20,11 @@ func Index() web.HandlerFunc {
searchPosts := &query.SearchPosts{
Query: c.QueryParam("query"),
View: c.QueryParam("view"),
- Limit: c.QueryParam("limit"),
Tags: c.QueryParamAsArray("tags"),
}
+ searchPosts.SetLimitFromString(c.QueryParam("limit"))
+
if myVotesOnly, err := c.QueryParamAsBool("myvotes"); err == nil {
searchPosts.MyVotesOnly = myVotesOnly
}
diff --git a/app/handlers/post_test.go b/app/handlers/post_test.go
index 59733b1a4..81f0431b2 100644
--- a/app/handlers/post_test.go
+++ b/app/handlers/post_test.go
@@ -39,6 +39,32 @@ func TestIndexHandler(t *testing.T) {
Expect(code).Equals(http.StatusOK)
}
+func TestIndexHandler_ClampsLimit(t *testing.T) {
+ RegisterT(t)
+
+ bus.AddHandler(func(ctx context.Context, q *query.CountPostPerStatus) error {
+ return nil
+ })
+
+ bus.AddHandler(func(ctx context.Context, q *query.GetAllTags) error {
+ return nil
+ })
+
+ var searchPosts *query.SearchPosts
+ bus.AddHandler(func(ctx context.Context, q *query.SearchPosts) error {
+ searchPosts = q
+ return nil
+ })
+
+ code, _ := mock.NewServer().
+ OnTenant(mock.DemoTenant).
+ WithURL("http://demo.test.fider.io/?limit=all").
+ Execute(handlers.Index())
+
+ Expect(code).Equals(http.StatusOK)
+ Expect(searchPosts.Limit).Equals("1000")
+}
+
func TestDetailsHandler(t *testing.T) {
RegisterT(t)
diff --git a/app/models/query/post.go b/app/models/query/post.go
index e87621cf9..8d7b4ae0f 100644
--- a/app/models/query/post.go
+++ b/app/models/query/post.go
@@ -1,6 +1,8 @@
package query
import (
+ "strconv"
+
"github.com/getfider/fider/app/models/entity"
"github.com/getfider/fider/app/models/enum"
)
@@ -57,6 +59,31 @@ type GetAllPosts struct {
Result []*entity.Post
}
+// MaxSearchPostsLimit is the largest number of posts that can be requested over HTTP.
+// There is no offset parameter: the home page and roadmap "show more" links work by
+// increasing the limit, so this is deliberately generous.
+const MaxSearchPostsLimit = 1000
+
+// SetLimitFromString sets Limit from an untrusted value such as a query string parameter.
+// Invalid or non-positive values fall back to the default limit, larger values are clamped
+// to MaxSearchPostsLimit, and "all" means MaxSearchPostsLimit rather than no limit at all.
+func (q *SearchPosts) SetLimitFromString(limit string) {
+ if limit == "all" {
+ q.Limit = strconv.Itoa(MaxSearchPostsLimit)
+ return
+ }
+
+ n, err := strconv.Atoi(limit)
+ switch {
+ case err != nil || n <= 0:
+ q.Limit = ""
+ case n > MaxSearchPostsLimit:
+ q.Limit = strconv.Itoa(MaxSearchPostsLimit)
+ default:
+ q.Limit = strconv.Itoa(n)
+ }
+}
+
func (q *SearchPosts) SetStatusesFromStrings(statuses []string) {
for _, v := range statuses {
var postStatus enum.PostStatus
diff --git a/app/models/query/post_test.go b/app/models/query/post_test.go
new file mode 100644
index 000000000..fafeee9b7
--- /dev/null
+++ b/app/models/query/post_test.go
@@ -0,0 +1,36 @@
+package query_test
+
+import (
+ "testing"
+
+ "github.com/getfider/fider/app/models/query"
+
+ . "github.com/getfider/fider/app/pkg/assert"
+)
+
+func TestSearchPosts_SetLimitFromString(t *testing.T) {
+ RegisterT(t)
+
+ testCases := []struct {
+ input string
+ expected string
+ }{
+ {"", ""},
+ {"abc", ""},
+ {"0", ""},
+ {"-5", ""},
+ {"1", "1"},
+ {"30", "30"},
+ {"1000", "1000"},
+ {"1001", "1000"},
+ {"2000000000", "1000"},
+ {"99999999999999999999", ""},
+ {"all", "1000"},
+ }
+
+ for _, testCase := range testCases {
+ q := &query.SearchPosts{}
+ q.SetLimitFromString(testCase.input)
+ Expect(q.Limit).Equals(testCase.expected)
+ }
+}