mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 09:42:07 +08:00
SECURITY: Add rate limits for uploads
This commit is contained in:
parent
819361ba28
commit
003b80e62f
|
@ -25,6 +25,13 @@ class UploadsController < ApplicationController
|
|||
# capture current user for block later on
|
||||
me = current_user
|
||||
|
||||
RateLimiter.new(
|
||||
current_user,
|
||||
"uploads-per-minute",
|
||||
SiteSetting.max_uploads_per_minute,
|
||||
1.minute.to_i,
|
||||
).performed!
|
||||
|
||||
params.permit(:type, :upload_type)
|
||||
raise Discourse::InvalidParameters if params[:type].blank? && params[:upload_type].blank?
|
||||
# 50 characters ought to be enough for the upload type
|
||||
|
|
|
@ -2228,6 +2228,9 @@ rate_limits:
|
|||
max_complete_multipart_per_minute:
|
||||
default: 10
|
||||
hidden: true
|
||||
max_uploads_per_minute:
|
||||
default: 10
|
||||
hidden: true
|
||||
|
||||
developer:
|
||||
force_hostname:
|
||||
|
|
|
@ -19,6 +19,32 @@ RSpec.describe UploadsController do
|
|||
let(:fake_jpg) { Rack::Test::UploadedFile.new(file_from_fixtures("fake.jpg")) }
|
||||
let(:text_file) { Rack::Test::UploadedFile.new(File.new("#{Rails.root}/LICENSE.txt")) }
|
||||
|
||||
context "when rate limited" do
|
||||
before { RateLimiter.enable }
|
||||
|
||||
use_redis_snapshotting
|
||||
|
||||
it "should return 429 response code when maximum number of uploads per minute has been exceeded for a user" do
|
||||
SiteSetting.max_uploads_per_minute = 1
|
||||
|
||||
post "/uploads.json",
|
||||
params: {
|
||||
file: Rack::Test::UploadedFile.new(logo_file),
|
||||
type: "avatar",
|
||||
}
|
||||
|
||||
expect(response.status).to eq(200)
|
||||
|
||||
post "/uploads.json",
|
||||
params: {
|
||||
file: Rack::Test::UploadedFile.new(logo_file),
|
||||
type: "avatar",
|
||||
}
|
||||
|
||||
expect(response.status).to eq(429)
|
||||
end
|
||||
end
|
||||
|
||||
it "expects a type or upload_type" do
|
||||
post "/uploads.json", params: { file: logo }
|
||||
expect(response.status).to eq(400)
|
||||
|
|
Loading…
Reference in New Issue
Block a user