0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-06 12:09:27 +08:00
discourse/app/controllers/export_csv_controller.rb
Bannon Tanner a3e825da5b
FIX: Race condition allows bypass of data export rate limit in Discourse (#41990)
## Summary

Prevent a race condition in the user archive export rate limit by
implementing an atomic Redis-based reservation. This ensures that
multiple export jobs cannot be enqueued for the same user on the same
day before the background job creates a database record.

## Source

- Patch Triage: https://patch.discourse.org/patch-triage/1380

Co-authored-by: discourse-patch-triage
<272280883+discourse-patch-triage[bot]@users.noreply.github.com>
2026-07-29 13:04:18 -05:00

96 lines
2.8 KiB
Ruby
Vendored

# frozen_string_literal: true
class ExportCsvController < ApplicationController
requires_login
skip_before_action :preload_json, :check_xhr, only: [:show]
def export_entity
entity = export_params[:entity]
entity_id = params.dig(:args, :export_user_id)&.to_i if entity == "user_archive"
guardian.ensure_can_export_entity!(entity, entity_id, export_params[:args])
raise Discourse::InvalidParameters.new(:entity) unless entity.is_a?(String) && entity.size < 100
(export_params[:args] || {}).each do |key, value|
unless value.is_a?(String) && value.size < 100
raise Discourse::InvalidParameters.new("args.#{key}")
end
end
if entity == "user_archive"
archive_user_id = entity_id || current_user.id
requesting_user_id = current_user.id if entity_id
# Rate limit user archive exports to 1 per day
reservation_key = reserve_user_archive_export!(archive_user_id)
unless current_user.admin || reservation_key
render_json_error I18n.t("csv_export.rate_limit_error")
return
end
begin
Jobs.enqueue(
:export_user_archive,
user_id: archive_user_id,
requesting_user_id:,
args: export_params[:args],
)
rescue StandardError
Discourse.redis.del(reservation_key) if reservation_key
raise
end
else
Jobs.enqueue(
:export_csv_file,
entity: entity,
user_id: current_user.id,
args: export_params[:args],
)
end
StaffActionLogger.new(current_user).log_entity_export(entity)
render json: success_json
rescue Discourse::InvalidAccess
render_json_error I18n.t("csv_export.rate_limit_error")
end
def latest_user_archive
user_id = params[:user_id].to_i
# If we can't export the entity, we shouldn't be able to see it either
guardian.ensure_can_export_entity!("user_archive", user_id)
render json:
UserExport
.where(user_id:)
.where("created_at > ?", UserExport::DESTROY_CREATED_BEFORE.ago)
.order(created_at: :desc)
.first
end
private
def reserve_user_archive_export!(user_id)
return nil if UserExport.exists?(user_id: user_id, created_at: Time.zone.now.all_day)
key = user_archive_export_rate_limit_key(user_id)
return nil unless Discourse.redis.set(key, "1", nx: true, ex: seconds_until_tomorrow)
key
end
def user_archive_export_rate_limit_key(user_id)
"user_archive_export_rate_limit:#{user_id}:#{Time.zone.today}"
end
def seconds_until_tomorrow
now = Time.zone.now
[(now.end_of_day - now).ceil, 1].max
end
def export_params
@_export_params ||=
begin
params.require(:entity)
params.permit(:entity, args: [*Report::FILTERS, *UserHistory.staff_filters]).to_h
end
end
end