Merge pull request #4211 from tomhughes/changeset-comment-cleanup

Address review comments for changeset comment limiting
This commit is contained in:
Andy Allan 2023-08-31 10:02:18 +01:00 committed by GitHub
commit cdfd617eba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 100 additions and 9 deletions

View file

@ -17,7 +17,7 @@ module Api
# Check the arguments are sane
raise OSM::APIBadUserInput, "No id was given" unless params[:id]
raise OSM::APIBadUserInput, "No text was given" if params[:text].blank?
raise OSM::APIRateLimitExceeded if current_user.changeset_comments.where("created_at >= ?", Time.now.utc - 1.hour).count >= current_user.max_changeset_comments_per_hour
raise OSM::APIRateLimitExceeded if rate_limit_exceeded?
# Extract the arguments
id = params[:id].to_i
@ -99,5 +99,15 @@ module Api
format.json
end
end
private
##
# Check if the current user has exceed the rate limit for comments
def rate_limit_exceeded?
recent_comments = current_user.changeset_comments.where("created_at >= ?", Time.now.utc - 1.hour).count
recent_comments >= current_user.max_changeset_comments_per_hour
end
end
end

View file

@ -397,14 +397,14 @@ class User < ApplicationRecord
def max_changeset_comments_per_hour
if moderator?
36000
Settings.moderator_changeset_comments_per_hour
else
previous_comments = changeset_comments.limit(200).count
active_reports = issues.with_status(:open).sum(:reports_count)
max_comments = previous_comments / 200.0 * Settings.max_changeset_comments_per_hour
max_comments = max_comments.floor.clamp(Settings.min_changeset_comments_per_hour, Settings.max_changeset_comments_per_hour)
max_comments = max_comments.floor.clamp(Settings.initial_changeset_comments_per_hour, Settings.max_changeset_comments_per_hour)
max_comments /= 2**active_reports
max_comments.floor.clamp(1, Settings.max_changeset_comments_per_hour)
max_comments.floor.clamp(Settings.min_changeset_comments_per_hour, Settings.max_changeset_comments_per_hour)
end
end

View file

@ -56,8 +56,10 @@ max_messages_per_hour: 60
# Rate limit for friending
max_friends_per_hour: 60
# Rate limit for changeset comments
min_changeset_comments_per_hour: 6
min_changeset_comments_per_hour: 1
initial_changeset_comments_per_hour: 6
max_changeset_comments_per_hour: 60
moderator_changeset_comments_per_hour: 36000
# Domain for handling message replies
#messages_domain: "messages.openstreetmap.org"
# MaxMind GeoIPv2 database

View file

@ -19,3 +19,6 @@ avatar_storage: "test"
trace_file_storage: "test"
trace_image_storage: "test"
trace_icon_storage: "test"
# Lower some rate limits for testing
max_changeset_comments_per_hour: 30
moderator_changeset_comments_per_hour: 60

View file

@ -133,15 +133,80 @@ module Api
end
##
# create comment rate limit
def test_create_comment_rate_limit
# create comment rate limit for new users
def test_create_comment_new_user_rate_limit
changeset = create(:changeset, :closed)
user = create(:user)
auth_header = basic_authorization_header user.email, "test"
assert_difference "ChangesetComment.count", Settings.min_changeset_comments_per_hour do
1.upto(Settings.min_changeset_comments_per_hour) do |count|
assert_difference "ChangesetComment.count", Settings.initial_changeset_comments_per_hour do
1.upto(Settings.initial_changeset_comments_per_hour) do |count|
post changeset_comment_path(:id => changeset, :text => "Comment #{count}"), :headers => auth_header
assert_response :success
end
end
assert_no_difference "ChangesetComment.count" do
post changeset_comment_path(:id => changeset, :text => "One comment too many"), :headers => auth_header
assert_response :too_many_requests
end
end
##
# create comment rate limit for experienced users
def test_create_comment_experienced_user_rate_limit
changeset = create(:changeset, :closed)
user = create(:user)
create_list(:changeset_comment, 200, :author_id => user.id, :created_at => Time.now.utc - 1.day)
auth_header = basic_authorization_header user.email, "test"
assert_difference "ChangesetComment.count", Settings.max_changeset_comments_per_hour do
1.upto(Settings.max_changeset_comments_per_hour) do |count|
post changeset_comment_path(:id => changeset, :text => "Comment #{count}"), :headers => auth_header
assert_response :success
end
end
assert_no_difference "ChangesetComment.count" do
post changeset_comment_path(:id => changeset, :text => "One comment too many"), :headers => auth_header
assert_response :too_many_requests
end
end
##
# create comment rate limit for reported users
def test_create_comment_reported_user_rate_limit
changeset = create(:changeset, :closed)
user = create(:user)
create(:issue_with_reports, :reportable => user, :reported_user => user)
auth_header = basic_authorization_header user.email, "test"
assert_difference "ChangesetComment.count", Settings.initial_changeset_comments_per_hour / 2 do
1.upto(Settings.initial_changeset_comments_per_hour / 2) do |count|
post changeset_comment_path(:id => changeset, :text => "Comment #{count}"), :headers => auth_header
assert_response :success
end
end
assert_no_difference "ChangesetComment.count" do
post changeset_comment_path(:id => changeset, :text => "One comment too many"), :headers => auth_header
assert_response :too_many_requests
end
end
##
# create comment rate limit for moderator users
def test_create_comment_moderator_user_rate_limit
changeset = create(:changeset, :closed)
user = create(:moderator_user)
auth_header = basic_authorization_header user.email, "test"
assert_difference "ChangesetComment.count", Settings.moderator_changeset_comments_per_hour do
1.upto(Settings.moderator_changeset_comments_per_hour) do |count|
post changeset_comment_path(:id => changeset, :text => "Comment #{count}"), :headers => auth_header
assert_response :success
end

View file

@ -6,5 +6,16 @@ FactoryBot.define do
# Default to assigning to an administrator
assigned_role { "administrator" }
# Optionally create some reports for this issue
factory :issue_with_reports do
transient do
reports_count { 1 }
end
after(:create) do |issue, evaluator|
create_list(:report, evaluator.reports_count, :issue => issue)
end
end
end
end