Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
Don't allow trailing newlines in various checks. (#2266)
Browse files Browse the repository at this point in the history
There's a subtle difference between what `\Z` and `\z` consider the "end
of string" which is that the uppercase version allows a single trailing
newline:

```
/\Afoo\Z/.match("foo\n")

/\Afoo\Z/.match("foo\n\n")

/\Afoo\z/.match("foo\n")
```
  • Loading branch information
benpickles authored and tute committed Aug 17, 2016
1 parent 09d6bb7 commit f1ed066
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ Quick Start
```ruby
class User < ActiveRecord::Base
has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\z/
end
```

Expand Down Expand Up @@ -417,7 +417,7 @@ class ActiveRecord::Base
# Validate content type
validates_attachment_content_type :avatar, content_type: /\Aimage/
# Validate filename
validates_attachment_file_name :avatar, matches: [/png\Z/, /jpe?g\Z/]
validates_attachment_file_name :avatar, matches: [/png\z/, /jpe?g\z/]
# Explicitly do not validate
do_not_validate_attachment_file_type :avatar
end
Expand Down
2 changes: 1 addition & 1 deletion lib/paperclip/interpolations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def content_type_extension attachment, style_name
# It's possible, though unlikely, that the mime type is not in the
# database, so just use the part after the '/' in the mime type as the
# extension.
%r{/([^/]*)\Z}.match(attachment.content_type)[1]
%r{/([^/]*)\z}.match(attachment.content_type)[1]
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/paperclip/storage/fog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def self.extended base
end unless defined?(Fog)

base.instance_eval do
unless @options[:url].to_s.match(/\A:fog.*url\Z/)
unless @options[:url].to_s.match(/\A:fog.*url\z/)
@options[:path] = @options[:path].gsub(/:url/, @options[:url]).gsub(/\A:rails_root\/public\/system\//, '')
@options[:url] = ':fog_public_url'
end
Expand All @@ -58,7 +58,7 @@ def self.extended base
end
end

AWS_BUCKET_SUBDOMAIN_RESTRICTON_REGEX = /\A(?:[a-z]|\d(?!\d{0,2}(?:\.\d{1,3}){3}\Z))(?:[a-z0-9]|\.(?![\.\-])|\-(?![\.])){1,61}[a-z0-9]\Z/
AWS_BUCKET_SUBDOMAIN_RESTRICTON_REGEX = /\A(?:[a-z]|\d(?!\d{0,2}(?:\.\d{1,3}){3}\z))(?:[a-z0-9]|\.(?![\.\-])|\-(?![\.])){1,61}[a-z0-9]\z/

def exists?(style = default_style)
if original_filename
Expand Down
2 changes: 1 addition & 1 deletion lib/paperclip/storage/s3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def self.extended base
@s3_server_side_encryption = @options[:s3_server_side_encryption]
end

unless @options[:url].to_s.match(/\A:s3.*url\Z/) || @options[:url] == ":asset_host".freeze
unless @options[:url].to_s.match(/\A:s3.*url\z/) || @options[:url] == ":asset_host".freeze
@options[:path] = path_option.gsub(/:url/, @options[:url]).sub(/\A:rails_root\/public\/system/, "".freeze)
@options[:url] = ":s3_path_url".freeze
end
Expand Down
2 changes: 1 addition & 1 deletion lib/paperclip/validators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def validates_attachment(*attributes)
options = attributes.extract_options!.dup

Paperclip::Validators.constants.each do |constant|
if constant.to_s =~ /\AAttachment(.+)Validator\Z/
if constant.to_s =~ /\AAttachment(.+)Validator\z/
validator_kind = $1.underscore.to_sym

if options.has_key?(validator_kind)
Expand Down
4 changes: 2 additions & 2 deletions spec/paperclip/validators_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
before do
rebuild_class
Dummy.validates_attachment :avatar, file_type_ignorance: true, file_name: [
{ matches: /\A.*\.jpe?g\Z/i, message: :invalid_extension },
{ matches: /\A.{,8}\..+\Z/i, message: [:too_long, count: 8] },
{ matches: /\A.*\.jpe?g\z/i, message: :invalid_extension },
{ matches: /\A.{,8}\..+\z/i, message: [:too_long, count: 8] },
]
end

Expand Down

0 comments on commit f1ed066

Please sign in to comment.