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
|