Skip to content

Commit

Permalink
Merge pull request #2442 from grantbdev/gb/list-terminology-option
Browse files Browse the repository at this point in the history
Provide public interface alternatives to whitelist/blacklist terminology
  • Loading branch information
mshibuya committed Feb 14, 2021
2 parents 6682f7a + 48cef4d commit 1e60e05
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 5 deletions.
6 changes: 5 additions & 1 deletion lib/carrierwave/uploader/content_type_blacklist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ module ContentTypeBlacklist
# [/(text|application)\/json/]
# end
#
def content_type_blacklist; end
def content_type_blacklist
content_type_blocklist
end

def content_type_blocklist; end

private

Expand Down
6 changes: 5 additions & 1 deletion lib/carrierwave/uploader/content_type_whitelist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module ContentTypeWhitelist
before :cache, :check_content_type_whitelist!
end

def content_type_allowlist; end

##
# Override this method in your uploader to provide a whitelist of files content types
# which are allowed to be uploaded.
Expand All @@ -28,7 +30,9 @@ module ContentTypeWhitelist
# [/(text|application)\/json/]
# end
#
def content_type_whitelist; end
def content_type_whitelist
content_type_allowlist
end

private

Expand Down
6 changes: 5 additions & 1 deletion lib/carrierwave/uploader/extension_blacklist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ module ExtensionBlacklist
# end
#

def extension_blacklist; end
def extension_blacklist
extension_blocklist
end

def extension_blocklist; end

private

Expand Down
6 changes: 5 additions & 1 deletion lib/carrierwave/uploader/extension_whitelist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module ExtensionWhitelist
before :cache, :check_extension_whitelist!
end

def extension_allowlist; end

##
# Override this method in your uploader to provide a white list of extensions which
# are allowed to be uploaded. Compares the file's extension case insensitive.
Expand All @@ -31,7 +33,9 @@ module ExtensionWhitelist
# [/jpe?g/, 'gif', 'png']
# end
#
def extension_whitelist; end
def extension_whitelist
extension_allowlist
end

private

Expand Down
6 changes: 6 additions & 0 deletions spec/uploader/content_type_blacklist_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
expect { uploader.cache!(ruby_file) }.to raise_error(CarrierWave::IntegrityError)
end

it "raises an integrity error if the file has a blocklisted content type" do
allow(uploader).to receive(:content_type_blocklist).and_return(['image/png'])

expect { uploader.cache!(ruby_file) }.to raise_error(CarrierWave::IntegrityError)
end

it "accepts content types as regular expressions" do
allow(uploader).to receive(:content_type_blacklist).and_return([/image\//])

Expand Down
6 changes: 6 additions & 0 deletions spec/uploader/content_type_whitelist_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@
expect { uploader.cache!(bork_file) }.to raise_error(CarrierWave::IntegrityError)
end

it "raises an integrity error the file has not an allowlisted content type" do
allow(uploader).to receive(:content_type_allowlist).and_return(['image/gif'])

expect { uploader.cache!(bork_file) }.to raise_error(CarrierWave::IntegrityError)
end

it "accepts content types as regular expressions" do
allow(uploader).to receive(:content_type_whitelist).and_return([/image\//])

Expand Down
2 changes: 1 addition & 1 deletion spec/uploader/extension_blacklist_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
before { allow(CarrierWave).to receive(:generate_cache_id).and_return(cache_id) }

describe '#cache!' do
before { allow(uploader).to receive(:extension_blacklist).and_return(extension_blacklist) }
before { allow(uploader).to receive(:extension_blocklist).and_return(extension_blacklist) }

context "when there are no blacklisted extensions" do
let(:extension_blacklist) { nil }
Expand Down
8 changes: 8 additions & 0 deletions spec/uploader/extension_whitelist_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@
}).to raise_error(CarrierWave::IntegrityError)
end

it "raises an integrity error if the file has not an allowlisted extension" do
allow(@uploader).to receive(:extension_allowlist).and_return(%w(txt doc xls))

expect(running {
@uploader.cache!(File.open(file_path('test.jpg')))
}).to raise_error(CarrierWave::IntegrityError)
end

it "raises an integrity error if the file has not a whitelisted extension, using start of string matcher" do
allow(@uploader).to receive(:extension_whitelist).and_return(%w(txt))

Expand Down

0 comments on commit 1e60e05

Please sign in to comment.