Skip to content

Commit

Permalink
Use Addressable::URI.site when sanitizing S3 URIs
Browse files Browse the repository at this point in the history
Closes #135

S3 URIs can have bucket names that are capitalized.
`Addressable::URI.normalized_site` converts the site to lower case which
would cause the substitution in `ActiveEncode.sanitize_uri`'s S3 case to
fail from an unmatched Regex. Switching to `Addressable::URI.site`
preserves capitalization and should fix the bug.
  • Loading branch information
masaball committed Jan 27, 2025
1 parent 27e1591 commit 1e963e3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ else
end

case ENV['RAILS_VERSION']
when /^[56]/, /^7.0/
gem 'concurrent-ruby', '1.3.4'
when /^6.0/
gem 'sass-rails', '>= 6'
gem 'webpacker', '~> 4.0'
Expand Down
2 changes: 1 addition & 1 deletion lib/active_encode/filename_sanitizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def sanitize_uri(input_url)
when /^file\:\/\/\//
input_url.to_s.gsub(/file:\/\//, '')
when /^s3\:\/\//
input_url.to_s.gsub(/#{Addressable::URI.parse(input_url).normalized_site}/, '')
input_url.to_s.gsub(/#{Addressable::URI.parse(input_url).site}/, '')
when /^https?:\/\//
input_url
end
Expand Down
14 changes: 10 additions & 4 deletions spec/units/filename_sanitizer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@
uri = "file:///path/to/file"
expect(ActiveEncode.sanitize_uri(uri)).to eq "/path/to/file"
end
it 'relativizes s3 uris' do
uri = "s3://mybucket/guitar.mp4"
expect(ActiveEncode.sanitize_uri(uri)).to eq "/guitar.mp4"
end
it 'does nothing for http(s) uris' do
uri = "https://www.googleapis.com/drive/v3/files/1WkWJ12WecI9hX-PmEbuKDGLPK_mN3kYP?alt=media"
expect(ActiveEncode.sanitize_uri(uri)).to eq uri
end
context 's3' do
it 'relativizes s3 uris' do
uri = "s3://mybucket/guitar.mp4"
expect(ActiveEncode.sanitize_uri(uri)).to eq "/guitar.mp4"
end
it 'handles capitalized buckets' do
uri = "s3://MYBucket/guitar.mp4"
expect(ActiveEncode.sanitize_uri(uri)).to eq "/guitar.mp4"
end
end
end
end

0 comments on commit 1e963e3

Please sign in to comment.