has_download.rb 946 B

12345678910111213141516171819202122232425262728293031323334353637
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. module ApplicationController::HasDownload
  3. extend ActiveSupport::Concern
  4. def send_data(...)
  5. super
  6. set_null_csp
  7. end
  8. def send_file(...)
  9. super
  10. set_null_csp
  11. end
  12. private
  13. def file_id
  14. @file_id ||= params[:id]
  15. end
  16. def download_file
  17. @download_file ||= ::ApplicationController::HasDownload::DownloadFile.new(file_id, disposition: sanitized_disposition)
  18. end
  19. def sanitized_disposition
  20. disposition = params.fetch(:disposition, 'inline')
  21. valid_disposition = %w[inline attachment]
  22. return disposition if valid_disposition.include?(disposition)
  23. raise Exceptions::Forbidden, "Invalid disposition #{disposition} requested. Only #{valid_disposition.join(', ')} are valid."
  24. end
  25. def set_null_csp
  26. request.content_security_policy = ActionDispatch::ContentSecurityPolicy.new.tap { |p| p.default_src :none }
  27. end
  28. end