This repository has been archived by the owner on Jul 13, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jon Yurek
committed
Jan 29, 2014
1 parent
52840dc
commit 41c092d
Showing
20 changed files
with
165 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module Paperclip | ||
class MediaTypeSpoofDetector | ||
def self.using(file, name) | ||
new(file, name) | ||
end | ||
|
||
def initialize(file, name) | ||
@file = file | ||
@name = name | ||
end | ||
|
||
def spoofed? | ||
if ! @name.blank? | ||
! supplied_file_media_type.include?(calculated_media_type) | ||
end | ||
end | ||
|
||
private | ||
|
||
def supplied_file_media_type | ||
MIME::Types.type_for(@name).collect(&:media_type) | ||
end | ||
|
||
def calculated_media_type | ||
type_from_file_command.split("/").first | ||
end | ||
|
||
def type_from_file_command | ||
begin | ||
Paperclip.run("file", "-b --mime-type :file", :file => @file.path) | ||
rescue Cocaine::CommandLineError | ||
"" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
lib/paperclip/validators/media_type_spoof_detection_validator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require 'active_model/validations/presence' | ||
|
||
module Paperclip | ||
module Validators | ||
class MediaTypeSpoofDetectionValidator < ActiveModel::EachValidator | ||
def validate_each(record, attribute, value) | ||
adapter = Paperclip.io_adapters.for(value) | ||
if Paperclip::MediaTypeSpoofDetector.using(adapter, value.original_filename).spoofed? | ||
record.errors.add(attribute, :spoofed_media_type) | ||
end | ||
end | ||
end | ||
|
||
module HelperMethods | ||
# Places ActiveModel validations on the presence of a file. | ||
# Options: | ||
# * +if+: A lambda or name of an instance method. Validation will only | ||
# be run if this lambda or method returns true. | ||
# * +unless+: Same as +if+ but validates if lambda or method returns false. | ||
def validates_media_type_spoof_detection(*attr_names) | ||
options = _merge_attributes(attr_names) | ||
validates_with MediaTypeSpoofDetectionValidator, options.dup | ||
validate_before_processing MediaTypeSpoofDetectionValidator, options.dup | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<html></html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require './test/helper' | ||
|
||
class MediaTypeSpoofDetectorTest < Test::Unit::TestCase | ||
should 'reject a file that is named .html and identifies as PNG' do | ||
file = File.open(fixture_file("5k.png")) | ||
assert Paperclip::MediaTypeSpoofDetector.using(file, "5k.html").spoofed? | ||
end | ||
|
||
should 'not reject a file that is named .jpg and identifies as PNG' do | ||
file = File.open(fixture_file("5k.png")) | ||
assert ! Paperclip::MediaTypeSpoofDetector.using(file, "5k.jpg").spoofed? | ||
end | ||
|
||
should 'not reject a file that is named .html and identifies as HTML' do | ||
file = File.open(fixture_file("empty.html")) | ||
assert ! Paperclip::MediaTypeSpoofDetector.using(file, "empty.html").spoofed? | ||
end | ||
|
||
should 'not reject a file that does not have a name' do | ||
file = File.open(fixture_file("empty.html")) | ||
assert ! Paperclip::MediaTypeSpoofDetector.using(file, "").spoofed? | ||
end | ||
|
||
should 'not reject when the supplied file is an IOAdapter' do | ||
adapter = Paperclip.io_adapters.for(File.new(fixture_file("5k.png"))) | ||
assert ! Paperclip::MediaTypeSpoofDetector.using(adapter, adapter.original_filename).spoofed? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
test/validators/media_type_spoof_detection_validator_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
require './test/helper' | ||
|
||
class MediaTypeSpoofDetectionValidatorTest < Test::Unit::TestCase | ||
def setup | ||
rebuild_model | ||
@dummy = Dummy.new | ||
end | ||
|
||
should "be on the attachment without being explicitly added" do | ||
assert Dummy.validators_on(:avatar).any?{ |validator| validator.kind == :media_type_spoof_detection } | ||
end | ||
end |