Skip to content

Commit

Permalink
feat(server): add max length for limit params
Browse files Browse the repository at this point in the history
  • Loading branch information
lareii committed Sep 18, 2024
1 parent 2a64337 commit d32b5ca
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 0 deletions.
6 changes: 6 additions & 0 deletions server/handlers/auth/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ func GetFeed(c *fiber.Ctx) error {
limit := c.QueryInt("limit", 10)
offset := c.QueryInt("offset", 0)

if limit > 30 {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"message": "Limit must be less than 30.",
})
}

user, ok := c.Locals("user").(models.User)
if !ok {
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
Expand Down
6 changes: 6 additions & 0 deletions server/handlers/posts/get_post_comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ func GetPostComments(c *fiber.Ctx) error {
limit := c.QueryInt("limit", 10)
offset := c.QueryInt("offset", 0)

if limit > 30 {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"message": "Limit must be less than 30.",
})
}

comments, err := models.GetCommentsByPostID(postID, int64(limit), int64(offset))
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
Expand Down
6 changes: 6 additions & 0 deletions server/handlers/posts/get_posts.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ func GetPosts(c *fiber.Ctx) error {
limit := c.QueryInt("limit", 10)
offset := c.QueryInt("offset", 0)

if limit > 30 {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"message": "Limit must be less than 30.",
})
}

posts, err := models.GetPosts(int64(limit), int64(offset), nil, bson.M{"created_at": -1})
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
Expand Down
6 changes: 6 additions & 0 deletions server/handlers/users/follows.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ func Follows(c *fiber.Ctx) error {
limit := c.QueryInt("limit", 10)
offset := c.QueryInt("offset", 0)

if limit > 30 {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"message": "Limit must be less than 30.",
})
}

slug := c.Params("slug")
user, err := models.GetUserByUsername(slug)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions server/handlers/users/get_user_posts.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ func GetUserPosts(c *fiber.Ctx) error {
limit := c.QueryInt("limit", 10)
offset := c.QueryInt("offset", 0)

if limit > 30 {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"message": "Limit must be less than 30.",
})
}

posts, err := models.GetPosts(int64(limit), int64(offset), bson.M{"author": user.ID}, bson.M{"created_at": -1})
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
Expand Down
6 changes: 6 additions & 0 deletions server/handlers/users/get_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ func GetUsers(c *fiber.Ctx) error {
limit := c.QueryInt("limit", 10)
offset := c.QueryInt("offset", 0)

if limit > 30 {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"message": "Limit must be less than 30.",
})
}

users, err := models.GetUsers(int64(limit), int64(offset), bson.M{}, bson.M{"points": -1})
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
Expand Down

0 comments on commit d32b5ca

Please sign in to comment.