effective_storage 0.4.5 → 0.4.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8de0c7807726e325a8a2f34f1e205c14dd27740daed1f7622db12dd39fb865a2
4
- data.tar.gz: 126797b758eed3ad5d192cde7ce3f365096dfad96ebdf232f217b8b5428b91b1
3
+ metadata.gz: de41d5679792d22ce1d00b2d9f4ba396de441a723eaef5359c8475fa00732538
4
+ data.tar.gz: 9c6e0a0c32fbcaf488ce51b3857aab7322dede9936d842783f04f2ae6eae5c17
5
5
  SHA512:
6
- metadata.gz: bba482aece015938fa979889a7d7da64ab68681100250409a3a147698c97274fe237c7257b2e2d5c6dec6ed97f8ab234e8b7cab654a36ab2a12ca53577996d07
7
- data.tar.gz: 837259796bbbe60101329b80e96bfc0a1743b2b5afbcf42e2a59ef70b6ad97c433a5400bd8aeb91e5f9a5d01a44407599982f37c311fd29353a3b240341bf4e4
6
+ metadata.gz: e07be03e0526dea2ebbcefb438d3ce9a4fd8d3c76eb273d15ed9bfa775b4b7f161daa37d7f189638d58ef79e571e1084ced3722d73562aa088a5cae76be46718
7
+ data.tar.gz: 5fa8b3f7a68a1b0c14dde1e29838f25570ca7bc3f48aa4434be0e95857c5a0d088af6fa67ac5d6f5a3852e85679a9b996583f25799d58bfc19426e535898d09d
@@ -14,6 +14,13 @@
14
14
  module ActiveStorageAuthorization
15
15
  extend ActiveSupport::Concern
16
16
 
17
+ AUTHORIZED_EFFECTIVE_DOWNLOADS = Set.new([
18
+ 'Effective::CarouselItem',
19
+ 'Effective::PageBanner',
20
+ 'Effective::PageSection',
21
+ 'Effective::Permalink'
22
+ ]).freeze
23
+
17
24
  included do
18
25
  rescue_from(Effective::UnauthorizedStorageException, with: :unauthorized_active_storage_request)
19
26
  end
@@ -47,25 +54,31 @@ module ActiveStorageAuthorization
47
54
  private
48
55
 
49
56
  def set_download_blob
50
- @blob ||= ActiveStorage::Blob.where(key: decode_verified_key().try(:dig, :key)).first
57
+ @blob ||= ActiveStorage::Blob.includes(:attachments, :active_storage_extensions).where(key: decode_verified_key().try(:dig, :key)).first
51
58
  end
52
59
 
53
60
  # Authorize the current blob and prevent it from being served if unauthorized
54
61
  def authorize_active_storage!
55
62
  return unless @blob.present?
56
63
 
64
+ # Disable strict loading and let the @blob just pull :attachments
65
+ @blob.strict_loading!(false) if @blob.try(:strict_loading?)
66
+
57
67
  # If the blob is not attached to anything, permit the blob
58
68
  return true if @blob.attachments.blank?
59
69
 
60
70
  # If the blob is an ActiveStorage::Variant it's been previously authorized
61
71
  return true if @blob.attachments.any? { |attachment| authorized_variant_download?(attachment) }
62
72
 
73
+ # If the blob is a known good effective class fast path it
74
+ return true if @blob.attachments.any? { |attachment| authorized_effective_download?(attachment) }
75
+
76
+ # If the blob has been marked public, permit the download (in-memory check, no queries)
77
+ return true if @blob.permission_public?
78
+
63
79
  # If we are authorized on any attached record, permit the download
64
80
  return true if @blob.attachments.any? { |attachment| authorized_attachment_download?(attachment) }
65
81
 
66
- # If the blob has been given permission using Mark Public
67
- return true if authorized?(@blob)
68
-
69
82
  # Otherwise raise a 404 Not Found and block the download
70
83
  head(:not_found)
71
84
 
@@ -106,6 +119,12 @@ module ActiveStorageAuthorization
106
119
  attachment.record_type == 'ActiveStorage::VariantRecord'
107
120
  end
108
121
 
122
+ # These are always public images
123
+ # Fast path them so we don't have to load any user for a permission check
124
+ def authorized_effective_download?(attachment)
125
+ AUTHORIZED_EFFECTIVE_DOWNLOADS.include?(attachment.record_type)
126
+ end
127
+
109
128
  # This is a has_one_attached or has_many_attached record
110
129
  # Or an ActionText::RichText object, that belongs_to a record
111
130
  def authorized_attachment_download?(attachment)
@@ -30,10 +30,22 @@ module EffectiveStorage
30
30
 
31
31
  ActiveStorage::Blobs::RedirectController.class_eval do
32
32
  before_action :authorize_active_storage_redirect!, only: [:show]
33
+
34
+ private
35
+
36
+ def blob_scope
37
+ ActiveStorage::Blob.includes(:attachments, :active_storage_extensions)
38
+ end
33
39
  end
34
40
 
35
41
  ActiveStorage::Representations::RedirectController.class_eval do
36
42
  before_action :authorize_active_storage_redirect!, only: [:show]
43
+
44
+ private
45
+
46
+ def blob_scope
47
+ ActiveStorage::Blob.scope_for_strict_loading.includes(:attachments, :active_storage_extensions)
48
+ end
37
49
  end
38
50
  end
39
51
  end
@@ -1,3 +1,3 @@
1
1
  module EffectiveStorage
2
- VERSION = '0.4.5'.freeze
2
+ VERSION = '0.4.7'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_storage
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.5
4
+ version: 0.4.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-29 00:00:00.000000000 Z
11
+ date: 2026-02-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails