From 26c45ed7950fc847c2088c6d77f219f81ec46245 Mon Sep 17 00:00:00 2001 From: Brent Stephens Date: Wed, 26 Jan 2022 13:10:36 -0800 Subject: [PATCH 1/2] Drop trailing slash of prefix in #get_local_files The local filesystem glob in `AssetSync::Storage#get_local_files` uses fuzzy matching when config.prefix is present. This can present a problem in some cases, as it doesn't allow for distinguishing between (e.g.) a folder called `assets/` and another folder called `assets-temp/`. A situation could arise where the latter folder has thousands/millions of files and we mistakenly publish local-only files, or worse we could clobber another directory in the bucket managed in a completely different context. This change allows developers to be more specific in their `config.prefix` by using a trailing slash for their folder name. --- lib/asset_sync/storage.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/asset_sync/storage.rb b/lib/asset_sync/storage.rb index f9dfa73..34571e3 100644 --- a/lib/asset_sync/storage.rb +++ b/lib/asset_sync/storage.rb @@ -140,7 +140,7 @@ def get_local_files log "Using: Directory Search of #{path}/#{self.config.assets_prefix}" Dir.chdir(path) do - to_load = self.config.assets_prefix.present? ? "#{self.config.assets_prefix}/**/**" : '**/**' + to_load = self.config.assets_prefix.present? ? File.join(self.config.assets_prefix, '/**/**') : '**/**' Dir[to_load] end end From 923417f9a49b31a3cf54c6d7990bdf9be93cdfb6 Mon Sep 17 00:00:00 2001 From: Aaron Pfeifer Date: Wed, 28 Aug 2024 13:19:16 -0400 Subject: [PATCH 2/2] Add specs for #get_local_files --- spec/unit/storage_spec.rb | 49 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/spec/unit/storage_spec.rb b/spec/unit/storage_spec.rb index cc7d1d3..eb19baf 100644 --- a/spec/unit/storage_spec.rb +++ b/spec/unit/storage_spec.rb @@ -1,4 +1,5 @@ require File.dirname(__FILE__) + '/../spec_helper' +require 'fileutils' describe AssetSync::Storage do include_context "mock Rails without_yml" @@ -450,4 +451,52 @@ def check_file(file) end end end + + describe '#get_local_files' do + around(:each) do |example| + Dir.mktmpdir do |public_path| + @public_path = public_path + example.call + end + end + + before(:each) do + @config = AssetSync::Config.new + @config.public_path = @public_path + @config.prefix = 'assets' + @storage = AssetSync::Storage.new(@config) + + Dir.mkdir("#{@public_path}/assets") + end + + context 'with empty directory' do + it 'has no files' do + expect(@storage.get_local_files).to eq([]) + end + end + + context 'with non-empty directory' do + before(:each) do + FileUtils.touch("#{@public_path}/assets/application.js") + end + + it 'lists available files' do + expect(@storage.get_local_files).to eq([ + 'assets/application.js' + ]) + end + + context 'with trailing slash on asset prefix' do + before(:each) do + @config.prefix = 'assets/' + end + + it 'lists available files with single slashes' do + expect(@storage.get_local_files).to eq([ + 'assets/application.js' + ]) + end + end + end + end end