2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-03-05 15:27:34 +08:00
discourse/app/controllers/export_csv_controller.rb
Joffrey JAFFEUX 6f14e93e09
FIX: Missing requires_login — authentication relies solely on guardian call (#37716)
## Summary

`ExportCsvController` lacks `requires_login`, causing anonymous requests
to `POST /export_csv/export_entity.json` to receive a misleading 422
"rate limit" error instead of a proper 403 "not logged in" response.

Guardians were blocking any exploit so there was no security exploit.

## Source

- Patch Triage: https://patch.discourse.org/patch-triage/298
- Original Commit:
https://github.com/discourse/discourse/blob/main/app/controllers/export_csv_controller.rb

---

🤖 Generated via [Patch Triage](https://patch.discourse.org/patch-triage)
2026-02-11 12:25:58 +01:00

74 lines
2.2 KiB
Ruby

# 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)
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"
requesting_user_id = current_user.id if entity_id
# Rate limit user archive exports to 1 per day
unless current_user.admin ||
UserExport.where(
user_id: entity_id || current_user.id,
created_at: (Time.zone.now.all_day),
).count == 0
render_json_error I18n.t("csv_export.rate_limit_error")
return
end
Jobs.enqueue(
:export_user_archive,
user_id: entity_id || current_user.id,
requesting_user_id:,
args: export_params[:args],
)
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 export_params
@_export_params ||=
begin
params.require(:entity)
params.permit(:entity, args: [*Report::FILTERS, *UserHistory.staff_filters]).to_h
end
end
end