Module: ActiveRecord::Tenanted::Storage::DiskService

Defined in:
lib/active_record/tenanted/storage.rb

Instance Method Summary collapse

Instance Method Details

#path_for(key) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/active_record/tenanted/storage.rb', line 19

def path_for(key)
  return super unless ActiveRecord::Tenanted.connection_class && key.include?("/")

  if key.split("/").intersect?(%w[. ..])
    raise ActiveStorage::InvalidKeyError, "key has path traversal segments"
  end

  tenant, key = key.split("/", 2)

  if tenant.blank? || key.blank?
    raise ActiveStorage::InvalidKeyError, "key has a blank segment"
  end

  begin
    path = File.expand_path(File.join(root, tenant, folder_for(key), key))
  rescue ArgumentError
    raise ActiveStorage::InvalidKeyError, "key is an invalid string"
  end

  unless path.start_with?(File.expand_path(root) + "/")
    raise ActiveStorage::InvalidKeyError, "key is outside of disk service root"
  end

  path
rescue Encoding::CompatibilityError
  raise ActiveStorage::InvalidKeyError, "key has incompatible encoding"
end

#rootObject



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/active_record/tenanted/storage.rb', line 7

def root
  if klass = ActiveRecord::Tenanted.connection_class
    unless tenant = klass.current_tenant
      raise NoTenantError, "Cannot access Active Storage Disk service without a tenant"
    end

    sprintf(@root, tenant: tenant)
  else
    super
  end
end