@@ -13,7 +13,7 @@ class ActiveStorageBlobsProxyController < ApiBaseController
1313 def show
1414 blob_uuid , purp , exp = ApplicationRecord . signed_id_verifier . verified ( params [ :signed_uuid ] )
1515
16- if blob_uuid . blank? || ( purp . present? && purp != 'blob' ) || ( exp && exp < Time . current . to_i )
16+ if blob_uuid . blank? || purp != 'blob'
1717 Rollbar . error ( 'Blob not found' ) if defined? ( Rollbar )
1818
1919 return head :not_found
@@ -24,8 +24,9 @@ def show
2424 attachment = blob . attachments . take
2525
2626 @record = attachment . record
27+ @record = @record . record if @record . is_a? ( ActiveStorage ::Attachment )
2728
28- authorization_check! ( attachment ) if exp . blank?
29+ authorization_check! ( attachment , @record , exp )
2930
3031 if request . headers [ 'Range' ] . present?
3132 send_blob_byte_range_data blob , request . headers [ 'Range' ]
@@ -41,14 +42,19 @@ def show
4142
4243 private
4344
44- def authorization_check! ( attachment )
45- is_authorized = attachment . name . in? ( %w[ logo preview_images ] ) ||
46- ( current_user && attachment . record . account . id == current_user . account_id ) ||
47- ( current_user && !Docuseal . multitenant? && current_user . role == 'superadmin' ) ||
48- !attachment . record . account . account_configs
49- . find_or_initialize_by ( key : AccountConfig ::DOWNLOAD_LINKS_AUTH_KEY ) . value
45+ def authorization_check! ( attachment , record , exp )
46+ return if attachment . name == 'logo'
47+ return if exp . to_i >= Time . current . to_i
5048
51- return if is_authorized
49+ return if current_user && current_ability . can? ( :read , record )
50+
51+ configs = record . account . account_configs . where ( key : [ AccountConfig ::DOWNLOAD_LINKS_AUTH_KEY ,
52+ AccountConfig ::DOWNLOAD_LINKS_EXPIRE_KEY ] )
53+
54+ require_auth = configs . any? { |c | c . key == AccountConfig ::DOWNLOAD_LINKS_AUTH_KEY && c . value }
55+ require_ttl = configs . none? { |c | c . key == AccountConfig ::DOWNLOAD_LINKS_EXPIRE_KEY && c . value == false }
56+
57+ return if !require_ttl && !require_auth
5258
5359 Rollbar . error ( 'Blob aunauthorized' ) if defined? ( Rollbar )
5460
0 commit comments