Open platform to collect and prioritize feedback
fix(api): allow unlimited post search for API key requests
Matt Roberts2d0bd4038ef4
diff --git a/app/const.go b/app/const.goindex 27c29a942..e049e7d6c 100644--- a/app/const.go+++ b/app/const.go@@ -32,5 +32,6 @@ var (TenantCtxKey = createKey("TENANT")LocaleCtxKey = createKey("LOCALE")UserCtxKey = createKey("USER")+ APIKeyCtxKey = createKey("API_KEY")LogPropsCtxKey = createKey("LOG_PROPS"))diff --git a/app/handlers/apiv1/post.go b/app/handlers/apiv1/post.goindex 2ab482495..ae0d09ffc 100644--- a/app/handlers/apiv1/post.go+++ b/app/handlers/apiv1/post.go@@ -59,7 +59,8 @@ func SearchPosts() web.HandlerFunc {Tags: c.QueryParamAsArray("tags"),ModerationFilter: c.QueryParam("moderation"),}- searchPosts.SetLimitFromString(c.QueryParam("limit"))+ // API keys (staff only) may fetch every post, as there is no offset to page through them+ searchPosts.SetLimitFromString(c.QueryParam("limit"), c.IsAuthenticatedByAPIKey())if myVotesOnly, err := c.QueryParamAsBool("myvotes"); err == nil {searchPosts.MyVotesOnly = myVotesOnlydiff --git a/app/handlers/apiv1/post_test.go b/app/handlers/apiv1/post_test.goindex a06b902ab..c7e9ad1a9 100644--- a/app/handlers/apiv1/post_test.go+++ b/app/handlers/apiv1/post_test.go@@ -15,10 +15,13 @@ import ("github.com/getfider/fider/app/models/cmd""github.com/getfider/fider/app/handlers/apiv1"+ "github.com/getfider/fider/app/middlewares". "github.com/getfider/fider/app/pkg/assert""github.com/getfider/fider/app/pkg/bus""github.com/getfider/fider/app/pkg/env"+ "github.com/getfider/fider/app/pkg/jwt""github.com/getfider/fider/app/pkg/mock"+ "github.com/getfider/fider/app/pkg/web")func TestCreatePostHandler(t *testing.T) {@@ -85,26 +88,83 @@ func TestCreatePostHandler_AppendsUnreferencedAttachments(t *testing.T) {Expect(newPost.Description).Equals("Already referenced: \n\n")}+func executeSearchPosts(limit string, configure func(server *mock.Server) *mock.Server) (int, *query.SearchPosts, bool) {+ var searchPosts *query.SearchPosts+ bus.AddHandler(func(ctx context.Context, q *query.SearchPosts) error {+ searchPosts = q+ return nil+ })+ bus.AddHandler(func(ctx context.Context, q *query.GetUserByAPIKey) error {+ if q.APIKey == "1234567890" {+ q.Result = mock.JonSnow+ return nil+ }+ return app.ErrNotFound+ })+ bus.AddHandler(func(ctx context.Context, q *query.GetUserByID) error {+ q.Result = mock.JonSnow+ return nil+ })++ isAuthenticated := false+ server := mock.NewServer().+ OnTenant(mock.DemoTenant).+ WithURL("http://demo.test.fider.io/api/v1/posts?limit=" + limit).+ Use(middlewares.User()).+ Use(func(next web.HandlerFunc) web.HandlerFunc {+ return func(c *web.Context) error {+ isAuthenticated = c.IsAuthenticated()+ return next(c)+ }+ })+ status, _ := configure(server).Execute(apiv1.SearchPosts())+ return status, searchPosts, isAuthenticated+}+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- })+ sessionToken, _ := jwt.Encode(jwt.FiderClaims{UserID: mock.JonSnow.ID, UserName: mock.JonSnow.Name})- status, _ := mock.NewServer().- OnTenant(mock.DemoTenant).- WithURL("http://demo.test.fider.io/api/v1/posts?limit=" + limit).- Execute(apiv1.SearchPosts())+ anonymous := func(server *mock.Server) *mock.Server { return server }+ withSession := func(server *mock.Server) *mock.Server {+ return server.AddCookie(web.CookieAuthName, sessionToken)+ }++ for _, limit := range []string{"all", "2000000000"} {+ status, searchPosts, isAuthenticated := executeSearchPosts(limit, anonymous)+ Expect(status).Equals(http.StatusOK)+ Expect(isAuthenticated).IsFalse()+ Expect(searchPosts.Limit).Equals("1000")+ status, searchPosts, isAuthenticated = executeSearchPosts(limit, withSession)Expect(status).Equals(http.StatusOK)+ Expect(isAuthenticated).IsTrue()Expect(searchPosts.Limit).Equals("1000")}}+func TestSearchPostsHandler_APIKeyIsNotClamped(t *testing.T) {+ RegisterT(t)++ withAPIKey := func(server *mock.Server) *mock.Server {+ return server.AddHeader("Authorization", "Bearer 1234567890")+ }++ status, searchPosts, isAuthenticated := executeSearchPosts("all", withAPIKey)+ Expect(status).Equals(http.StatusOK)+ Expect(isAuthenticated).IsTrue()+ Expect(searchPosts.Limit).Equals("all")++ status, searchPosts, _ = executeSearchPosts("5000", withAPIKey)+ Expect(status).Equals(http.StatusOK)+ Expect(searchPosts.Limit).Equals("5000")++ status, searchPosts, _ = executeSearchPosts("-5", withAPIKey)+ Expect(status).Equals(http.StatusOK)+ Expect(searchPosts.Limit).Equals("")+}+func TestCreatePostHandler_WithoutTitle(t *testing.T) {RegisterT(t)diff --git a/app/handlers/post.go b/app/handlers/post.goindex d74166703..22e98ead2 100644--- a/app/handlers/post.go+++ b/app/handlers/post.go@@ -23,7 +23,7 @@ func Index() web.HandlerFunc {Tags: c.QueryParamAsArray("tags"),}- searchPosts.SetLimitFromString(c.QueryParam("limit"))+ searchPosts.SetLimitFromString(c.QueryParam("limit"), false)if myVotesOnly, err := c.QueryParamAsBool("myvotes"); err == nil {searchPosts.MyVotesOnly = myVotesOnlydiff --git a/app/middlewares/user.go b/app/middlewares/user.goindex 721e2c220..02ff8dad5 100644--- a/app/middlewares/user.go+++ b/app/middlewares/user.go@@ -28,6 +28,7 @@ func User() web.MiddlewareFunc {token stringuser *entity.UserfromSignUpCookie bool+ fromAPIKey bool)cookie, err := c.Request.Cookie(web.CookieAuthName)@@ -97,6 +98,7 @@ func User() web.MiddlewareFunc {return err}user = getUserByAPIKey.Result+ fromAPIKey = trueif !user.IsCollaborator() {return c.HandleValidation(validate.Failed("API Key is invalid"))@@ -139,6 +141,9 @@ func User() web.MiddlewareFunc {}c.SetUser(user)+ if fromAPIKey {+ c.SetAuthenticatedByAPIKey()+ }}return next(c)diff --git a/app/middlewares/user_test.go b/app/middlewares/user_test.goindex 5db5a5622..69739f937 100644--- a/app/middlewares/user_test.go+++ b/app/middlewares/user_test.go@@ -67,6 +67,36 @@ func TestUser_WithCookie(t *testing.T) {Expect(response.Header()["Set-Cookie"]).HasLen(0)}+func TestUser_WithCookie_IsNotAuthenticatedByAPIKey(t *testing.T) {+ RegisterT(t)++ token, _ := jwt.Encode(jwt.FiderClaims{+ UserID: mock.JonSnow.ID,+ UserName: mock.JonSnow.Name,+ })++ bus.AddHandler(func(ctx context.Context, q *query.GetUserByID) error {+ q.Result = mock.JonSnow+ return nil+ })++ // The cookie takes precedence, so the Bearer header is ignored+ server := mock.NewServer()+ server.Use(middlewares.User())+ status, _ := server.+ OnTenant(mock.DemoTenant).+ WithURL("http://example.com/api/v1").+ AddCookie(web.CookieAuthName, token).+ AddHeader("Authorization", "Bearer 1234567890").+ Execute(func(c *web.Context) error {+ Expect(c.IsAuthenticated()).IsTrue()+ Expect(c.IsAuthenticatedByAPIKey()).IsFalse()+ return c.NoContent(http.StatusOK)+ })++ Expect(status).Equals(http.StatusOK)+}+func TestUser_Blocked(t *testing.T) {RegisterT(t)@@ -343,6 +373,7 @@ func TestUser_ValidAPIKey(t *testing.T) {WithURL("http://example.com/api/v1").AddHeader("Authorization", "Bearer 1234567890").Execute(func(c *web.Context) error {+ Expect(c.IsAuthenticatedByAPIKey()).IsTrue()return c.String(http.StatusOK, c.User().Name)})diff --git a/app/models/query/post.go b/app/models/query/post.goindex 8d7b4ae0f..38abaa7b7 100644--- a/app/models/query/post.go+++ b/app/models/query/post.go@@ -65,11 +65,16 @@ type GetAllPosts struct {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) {+// Invalid or non-positive values fall back to the default limit. Unless unlimited is true,+// larger values are clamped to MaxSearchPostsLimit and "all" means MaxSearchPostsLimit+// rather than no limit at all.+func (q *SearchPosts) SetLimitFromString(limit string, unlimited bool) {if limit == "all" {- q.Limit = strconv.Itoa(MaxSearchPostsLimit)+ if unlimited {+ q.Limit = "all"+ } else {+ q.Limit = strconv.Itoa(MaxSearchPostsLimit)+ }return}@@ -77,7 +82,7 @@ func (q *SearchPosts) SetLimitFromString(limit string) {switch {case err != nil || n <= 0:q.Limit = ""- case n > MaxSearchPostsLimit:+ case n > MaxSearchPostsLimit && !unlimited:q.Limit = strconv.Itoa(MaxSearchPostsLimit)default:q.Limit = strconv.Itoa(n)diff --git a/app/models/query/post_test.go b/app/models/query/post_test.goindex fafeee9b7..3cdba7fe0 100644--- a/app/models/query/post_test.go+++ b/app/models/query/post_test.go@@ -12,25 +12,32 @@ func TestSearchPosts_SetLimitFromString(t *testing.T) {RegisterT(t)testCases := []struct {- input string- expected string+ input string+ unlimited bool+ expected string}{- {"", ""},- {"abc", ""},- {"0", ""},- {"-5", ""},- {"1", "1"},- {"30", "30"},- {"1000", "1000"},- {"1001", "1000"},- {"2000000000", "1000"},- {"99999999999999999999", ""},- {"all", "1000"},+ {"", false, ""},+ {"abc", false, ""},+ {"0", false, ""},+ {"-5", false, ""},+ {"1", false, "1"},+ {"30", false, "30"},+ {"1000", false, "1000"},+ {"1001", false, "1000"},+ {"2000000000", false, "1000"},+ {"99999999999999999999", false, ""},+ {"all", false, "1000"},+ {"", true, ""},+ {"abc", true, ""},+ {"-5", true, ""},+ {"30", true, "30"},+ {"5000", true, "5000"},+ {"all", true, "all"},}for _, testCase := range testCases {q := &query.SearchPosts{}- q.SetLimitFromString(testCase.input)+ q.SetLimitFromString(testCase.input, testCase.unlimited)Expect(q.Limit).Equals(testCase.expected)}}diff --git a/app/pkg/web/context.go b/app/pkg/web/context.goindex a172c6826..db2f64599 100644--- a/app/pkg/web/context.go+++ b/app/pkg/web/context.go@@ -353,6 +353,17 @@ func (c *Context) SetUser(user *entity.User) {c.Set(app.UserCtxKey, user)}+// SetAuthenticatedByAPIKey marks the current user as authenticated with an API key+func (c *Context) SetAuthenticatedByAPIKey() {+ c.Set(app.APIKeyCtxKey, true)+}++// IsAuthenticatedByAPIKey returns true if the current user was authenticated with an API key+func (c *Context) IsAuthenticatedByAPIKey() bool {+ byAPIKey, _ := c.Value(app.APIKeyCtxKey).(bool)+ return byAPIKey && c.IsAuthenticated()+}+// AddCookie adds a cookiefunc (c *Context) AddCookie(name, value string, expires time.Time) *http.Cookie {cookie := &http.Cookie{