SECURITY: Impose a upper bound on limit params in various controllers

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.
This commit is contained in:
Alan Guo Xiang Tan
2023-07-28 12:53:46 +01:00
committed by David Taylor
parent 0976c8fad6
commit bfc3132bb2
32 changed files with 232 additions and 115 deletions

View File

@ -1182,6 +1182,8 @@ class UsersController < ApplicationController
end
end
SEARCH_USERS_LIMIT = 50
def search_users
term = params[:term].to_s.strip
@ -1204,10 +1206,11 @@ class UsersController < ApplicationController
params[:include_staged_users],
)
options[:last_seen_users] = !!ActiveModel::Type::Boolean.new.cast(params[:last_seen_users])
if params[:limit].present?
options[:limit] = params[:limit].to_i
raise Discourse::InvalidParameters.new(:limit) if options[:limit] <= 0
if limit = fetch_limit_from_params(default: nil, max: SEARCH_USERS_LIMIT)
options[:limit] = limit
end
options[:topic_id] = topic_id if topic_id
options[:category_id] = category_id if category_id
@ -1733,6 +1736,8 @@ class UsersController < ApplicationController
render json: success_json
end
BOOKMARKS_LIMIT = 20
def bookmarks
user = fetch_user_from_params
guardian.ensure_can_edit!(user)
@ -1740,7 +1745,15 @@ class UsersController < ApplicationController
respond_to do |format|
format.json do
bookmark_list = UserBookmarkList.new(user: user, guardian: guardian, params: params)
bookmark_list =
UserBookmarkList.new(
user: user,
guardian: guardian,
search_term: params[:q],
page: params[:page],
per_page: fetch_limit_from_params(default: nil, max: BOOKMARKS_LIMIT),
)
bookmark_list.load
if bookmark_list.bookmarks.empty?
@ -1789,10 +1802,9 @@ class UsersController < ApplicationController
UserBookmarkList.new(
user: current_user,
guardian: guardian,
params: {
per_page: USER_MENU_LIST_LIMIT - reminder_notifications.size,
},
per_page: USER_MENU_LIST_LIMIT - reminder_notifications.size,
)
bookmark_list.load do |query|
if exclude_bookmark_ids.present?
query.where("bookmarks.id NOT IN (?)", exclude_bookmark_ids)