]> cat aescling's git repositories - mastodon.git/commitdiff
Throttle media post (#7337)
authorAkihiko Odaki <akihiko.odaki.4i@stu.hosei.ac.jp>
Thu, 3 May 2018 15:32:00 +0000 (00:32 +0900)
committerEugen Rochko <eugen@zeonfederated.com>
Thu, 3 May 2018 15:32:00 +0000 (17:32 +0200)
The previous rate limit allowed to post media so fast that it is possible
to fill up the disk space even before an administrator notices. The new
rate limit is configured so that it takes 24 hours to eat 10 gigabytes:
10 * 1024 / 8 / (24 * 60 / 30) = 27 (which rounded to 30)

The period is set long so that it does not prevent from attaching several
media to one post, which would happen in a short period. For example,
if the period is 5 minutes, the rate limit would be:
10 * 1024 / 8 / (24 * 60 / 5) = 4

This long period allows to lift the limit up.

config/initializers/rack_attack.rb

index b35452f04f478ae9cecb2cba5c36dcd3224d7d30..310052e9c843f6ea4a7356b3bf2dfd1993ba8dd6 100644 (file)
@@ -53,6 +53,10 @@ class Rack::Attack
     req.ip if req.api_request?
   end
 
+  throttle('throttle_media', limit: 30, period: 30.minutes) do |req|
+    req.authenticated_user_id if req.post? && req.path.start_with('/api/v1/media')
+  end
+
   throttle('protected_paths', limit: 25, period: 5.minutes) do |req|
     req.ip if req.post? && req.path =~ PROTECTED_PATHS_REGEX
   end