Files
docuseal/app/controllers/api/active_storage_blobs_proxy_controller.rb
T

57 lines
1.7 KiB
Ruby
Raw Normal View History

2024-02-18 11:26:30 +02:00
# frozen_string_literal: true
module Api
class ActiveStorageBlobsProxyController < ApiBaseController
include ActiveStorage::Streaming
skip_before_action :authenticate_user!
skip_authorization_check
2024-02-20 18:24:57 +02:00
before_action :set_cors_headers
2024-04-20 15:47:48 +03:00
before_action :set_noindex_headers
2024-02-20 18:24:57 +02:00
2024-02-18 11:26:30 +02:00
def show
2024-02-25 02:00:18 +02:00
blob_uuid, purp, exp = ApplicationRecord.signed_id_verifier.verified(params[:signed_uuid])
2024-02-18 11:26:30 +02:00
2024-02-28 00:40:19 +02:00
if blob_uuid.blank? || (purp.present? && purp != 'blob') || (exp && exp < Time.current.to_i)
2024-02-19 17:07:37 +02:00
Rollbar.error('Blob not found') if defined?(Rollbar)
return head :not_found
end
2024-02-18 11:26:30 +02:00
blob = ActiveStorage::Blob.find_by!(uuid: blob_uuid)
2024-04-15 19:58:24 +03:00
authorization_check!(blob) if exp.blank?
2024-02-18 11:26:30 +02:00
if request.headers['Range'].present?
send_blob_byte_range_data blob, request.headers['Range']
else
http_cache_forever public: true do
response.headers['Accept-Ranges'] = 'bytes'
response.headers['Content-Length'] = blob.byte_size.to_s
send_blob_stream blob, disposition: params[:disposition]
end
end
end
2024-04-15 19:58:24 +03:00
private
def authorization_check!(blob)
2024-10-02 10:26:43 +03:00
attachment = blob.attachments.take
is_authorized = attachment.name.in?(%w[logo preview_images]) ||
(current_user && attachment.record.account.id == current_user.account_id) ||
2025-01-02 21:53:41 +02:00
(current_user && !Docuseal.multitenant? && current_user.role == 'superadmin') ||
2024-10-02 10:26:43 +03:00
!attachment.record.account.account_configs
.find_or_initialize_by(key: AccountConfig::DOWNLOAD_LINKS_AUTH_KEY).value
2024-04-15 19:58:24 +03:00
return if is_authorized
2024-04-16 14:07:38 +03:00
Rollbar.error('Blob aunauthorized') if defined?(Rollbar)
2024-04-15 19:58:24 +03:00
raise CanCan::AccessDenied
end
2024-02-18 11:26:30 +02:00
end
end