SECURITY: Add rate limits for uploads

This commit is contained in:
Alan Guo Xiang Tan 2024-02-07 12:55:36 +08:00 committed by Nat
parent 819361ba28
commit 003b80e62f
No known key found for this signature in database
GPG Key ID: 4938B35D927EC773
3 changed files with 36 additions and 0 deletions

View File

@ -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

View File

@ -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:

View File

@ -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)