mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 04:19:05 +08:00
bfc3132bb2
What is the problem here? In multiple controllers, we are accepting a `limit` params but do not impose any upper bound on the values being accepted. Without an upper bound, we may be allowing arbituary users from generating DB queries which may end up exhausing the resources on the server. What is the fix here? A new `fetch_limit_from_params` helper method is introduced in `ApplicationController` that can be used by controller actions to safely get the limit from the params as a default limit and maximum limit has to be set. When an invalid limit params is encountered, the server will respond with the 400 response code.
40 lines
863 B
Ruby
40 lines
863 B
Ruby
# frozen_string_literal: true
|
|
|
|
class UserBookmarkList
|
|
include ActiveModel::Serialization
|
|
|
|
PER_PAGE = 20
|
|
|
|
attr_reader :bookmarks, :per_page, :has_more
|
|
attr_accessor :more_bookmarks_url, :bookmark_serializer_opts
|
|
|
|
def initialize(user:, guardian:, search_term: nil, per_page: nil, page: 0)
|
|
@user = user
|
|
@guardian = guardian
|
|
|
|
@per_page = per_page || PER_PAGE
|
|
@per_page = PER_PAGE if @per_page > PER_PAGE
|
|
|
|
@search_term = search_term
|
|
@page = page.to_i
|
|
|
|
@bookmarks = []
|
|
@bookmark_serializer_opts = {}
|
|
end
|
|
|
|
def load(&blk)
|
|
query =
|
|
BookmarkQuery.new(
|
|
user: @user,
|
|
guardian: @guardian,
|
|
search_term: @search_term,
|
|
page: @page,
|
|
per_page: @per_page,
|
|
)
|
|
|
|
@bookmarks = query.list_all(&blk)
|
|
@has_more = (@page.to_i + 1) * @per_page < query.count
|
|
@bookmarks
|
|
end
|
|
end
|