mirror of
https://github.com/discourse/discourse.git
synced 2024-12-22 22:54:08 +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.
24 lines
714 B
Ruby
24 lines
714 B
Ruby
# frozen_string_literal: true
|
|
|
|
RSpec.shared_examples "invalid limit params" do |endpoint, max_limit, extra_params|
|
|
let(:params) { extra_params&.dig(:params) || {} }
|
|
|
|
it "returns 400 response code when limit params is negative" do
|
|
get endpoint, params: { limit: -1 }.merge(params)
|
|
|
|
expect(response.status).to eq(400)
|
|
end
|
|
|
|
it "returns 400 response code when limit params is suspicious" do
|
|
get endpoint, params: { limit: "1; DROP TABLE users" }.merge(params)
|
|
|
|
expect(response.status).to eq(400)
|
|
end
|
|
|
|
it "returns 400 response code when limit params exceeds the max limit" do
|
|
get endpoint, params: { limit: max_limit + 1 }.merge(params)
|
|
|
|
expect(response.status).to eq(400)
|
|
end
|
|
end
|