Commit 51f37a11 by Dan Collis-Puro Committed by Jon Yurek

Update regexes to bind to beginning and end of string

This ensures that multiline strings are properly matched in their
entirety.
parent 1c9d2e8b
...@@ -48,7 +48,7 @@ module Paperclip ...@@ -48,7 +48,7 @@ module Paperclip
# Returns the interpolated URL. Will raise an error if the url itself # Returns the interpolated URL. Will raise an error if the url itself
# contains ":url" to prevent infinite recursion. This interpolation # contains ":url" to prevent infinite recursion. This interpolation
# is used in the default :path to ease default specifications. # is used in the default :path to ease default specifications.
RIGHT_HERE = "#{__FILE__.gsub(%r{^\./}, "")}:#{__LINE__ + 3}" RIGHT_HERE = "#{__FILE__.gsub(%r{\A\./}, "")}:#{__LINE__ + 3}"
def url attachment, style_name def url attachment, style_name
raise Errors::InfiniteInterpolationError if caller.any?{|b| b.index(RIGHT_HERE) } raise Errors::InfiniteInterpolationError if caller.any?{|b| b.index(RIGHT_HERE) }
attachment.url(style_name, :timestamp => false, :escape => false) attachment.url(style_name, :timestamp => false, :escape => false)
...@@ -90,7 +90,7 @@ module Paperclip ...@@ -90,7 +90,7 @@ module Paperclip
# Returns the basename of the file. e.g. "file" for "file.jpg" # Returns the basename of the file. e.g. "file" for "file.jpg"
def basename attachment, style_name def basename attachment, style_name
attachment.original_filename.gsub(/#{Regexp.escape(File.extname(attachment.original_filename))}$/, "") attachment.original_filename.gsub(/#{Regexp.escape(File.extname(attachment.original_filename))}\Z/, "")
end end
# Returns the extension of the file. e.g. "jpg" for "file.jpg" # Returns the extension of the file. e.g. "jpg" for "file.jpg"
...@@ -98,7 +98,7 @@ module Paperclip ...@@ -98,7 +98,7 @@ module Paperclip
# of the actual extension. # of the actual extension.
def extension attachment, style_name def extension attachment, style_name
((style = attachment.styles[style_name.to_s.to_sym]) && style[:format]) || ((style = attachment.styles[style_name.to_s.to_sym]) && style[:format]) ||
File.extname(attachment.original_filename).gsub(/^\.+/, "") File.extname(attachment.original_filename).gsub(/\A\.+/, "")
end end
# Returns an extension based on the content type. e.g. "jpeg" for # Returns an extension based on the content type. e.g. "jpeg" for
...@@ -128,7 +128,7 @@ module Paperclip ...@@ -128,7 +128,7 @@ module Paperclip
# It's possible, though unlikely, that the mime type is not in the # 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 # database, so just use the part after the '/' in the mime type as the
# extension. # extension.
%r{/([^/]*)$}.match(attachment.content_type)[1] %r{/([^/]*)\Z}.match(attachment.content_type)[1]
end end
end end
......
module Paperclip module Paperclip
class DataUriAdapter < StringioAdapter class DataUriAdapter < StringioAdapter
REGEXP = /^data:([-\w]+\/[-\w\+]+);base64,(.*)/m REGEXP = /\Adata:([-\w]+\/[-\w\+]+);base64,(.*)/m
def initialize(target_uri) def initialize(target_uri)
@target_uri = target_uri @target_uri = target_uri
......
module Paperclip module Paperclip
class HttpUrlProxyAdapter < UriAdapter class HttpUrlProxyAdapter < UriAdapter
REGEXP = /^https?:\/\// REGEXP = /\Ahttps?:\/\//
def initialize(target) def initialize(target)
super(URI(target)) super(URI(target))
......
...@@ -43,8 +43,8 @@ module Paperclip ...@@ -43,8 +43,8 @@ module Paperclip
end unless defined?(Fog) end unless defined?(Fog)
base.instance_eval do base.instance_eval do
unless @options[:url].to_s.match(/^:fog.*url$/) unless @options[:url].to_s.match(/\A:fog.*url\Z/)
@options[:path] = @options[:path].gsub(/:url/, @options[:url]).gsub(/^:rails_root\/public\/system\//, '') @options[:path] = @options[:path].gsub(/:url/, @options[:url]).gsub(/\A:rails_root\/public\/system\//, '')
@options[:url] = ':fog_public_url' @options[:url] = ':fog_public_url'
end end
Paperclip.interpolates(:fog_public_url) do |attachment, style| Paperclip.interpolates(:fog_public_url) do |attachment, style|
...@@ -53,7 +53,7 @@ module Paperclip ...@@ -53,7 +53,7 @@ module Paperclip
end end
end end
AWS_BUCKET_SUBDOMAIN_RESTRICTON_REGEX = /^(?:[a-z]|\d(?!\d{0,2}(?:\.\d{1,3}){3}$))(?:[a-z0-9]|\.(?![\.\-])|\-(?![\.])){1,61}[a-z0-9]$/ 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) def exists?(style = default_style)
if original_filename if original_filename
......
...@@ -149,8 +149,8 @@ module Paperclip ...@@ -149,8 +149,8 @@ module Paperclip
@s3_server_side_encryption = @options[:s3_server_side_encryption].to_s.upcase @s3_server_side_encryption = @options[:s3_server_side_encryption].to_s.upcase
end end
unless @options[:url].to_s.match(/^:s3.*url$/) || @options[:url] == ":asset_host" unless @options[:url].to_s.match(/\A:s3.*url\Z/) || @options[:url] == ":asset_host"
@options[:path] = @options[:path].gsub(/:url/, @options[:url]).gsub(/^:rails_root\/public\/system/, '') @options[:path] = @options[:path].gsub(/:url/, @options[:url]).gsub(/\A:rails_root\/public\/system/, '')
@options[:url] = ":s3_path_url" @options[:url] = ":s3_path_url"
end end
@options[:url] = @options[:url].inspect if @options[:url].is_a?(Symbol) @options[:url] = @options[:url].inspect if @options[:url].is_a?(Symbol)
...@@ -159,16 +159,16 @@ module Paperclip ...@@ -159,16 +159,16 @@ module Paperclip
end end
Paperclip.interpolates(:s3_alias_url) do |attachment, style| Paperclip.interpolates(:s3_alias_url) do |attachment, style|
"#{attachment.s3_protocol(style, true)}//#{attachment.s3_host_alias}/#{attachment.path(style).gsub(%r{^/}, "")}" "#{attachment.s3_protocol(style, true)}//#{attachment.s3_host_alias}/#{attachment.path(style).gsub(%r{\A/}, "")}"
end unless Paperclip::Interpolations.respond_to? :s3_alias_url end unless Paperclip::Interpolations.respond_to? :s3_alias_url
Paperclip.interpolates(:s3_path_url) do |attachment, style| Paperclip.interpolates(:s3_path_url) do |attachment, style|
"#{attachment.s3_protocol(style, true)}//#{attachment.s3_host_name}/#{attachment.bucket_name}/#{attachment.path(style).gsub(%r{^/}, "")}" "#{attachment.s3_protocol(style, true)}//#{attachment.s3_host_name}/#{attachment.bucket_name}/#{attachment.path(style).gsub(%r{\A/}, "")}"
end unless Paperclip::Interpolations.respond_to? :s3_path_url end unless Paperclip::Interpolations.respond_to? :s3_path_url
Paperclip.interpolates(:s3_domain_url) do |attachment, style| Paperclip.interpolates(:s3_domain_url) do |attachment, style|
"#{attachment.s3_protocol(style, true)}//#{attachment.bucket_name}.#{attachment.s3_host_name}/#{attachment.path(style).gsub(%r{^/}, "")}" "#{attachment.s3_protocol(style, true)}//#{attachment.bucket_name}.#{attachment.s3_host_name}/#{attachment.path(style).gsub(%r{\A/}, "")}"
end unless Paperclip::Interpolations.respond_to? :s3_domain_url end unless Paperclip::Interpolations.respond_to? :s3_domain_url
Paperclip.interpolates(:asset_host) do |attachment, style| Paperclip.interpolates(:asset_host) do |attachment, style|
"#{attachment.path(style).gsub(%r{^/}, "")}" "#{attachment.path(style).gsub(%r{\A/}, "")}"
end unless Paperclip::Interpolations.respond_to? :asset_host end unless Paperclip::Interpolations.respond_to? :asset_host
end end
...@@ -244,7 +244,7 @@ module Paperclip ...@@ -244,7 +244,7 @@ module Paperclip
end end
def s3_object style_name = default_style def s3_object style_name = default_style
s3_bucket.objects[path(style_name).sub(%r{^/},'')] s3_bucket.objects[path(style_name).sub(%r{\A/},'')]
end end
def using_http_proxy? def using_http_proxy?
...@@ -352,7 +352,7 @@ module Paperclip ...@@ -352,7 +352,7 @@ module Paperclip
@queued_for_delete.each do |path| @queued_for_delete.each do |path|
begin begin
log("deleting #{path}") log("deleting #{path}")
s3_bucket.objects[path.sub(%r{^/},'')].delete s3_bucket.objects[path.sub(%r{\A/},'')].delete
rescue AWS::Errors::Base => e rescue AWS::Errors::Base => e
# Ignore this. # Ignore this.
end end
...@@ -397,10 +397,10 @@ module Paperclip ...@@ -397,10 +397,10 @@ module Paperclip
http_headers = http_headers.call(instance) if http_headers.respond_to?(:call) http_headers = http_headers.call(instance) if http_headers.respond_to?(:call)
http_headers.inject({}) do |headers,(name,value)| http_headers.inject({}) do |headers,(name,value)|
case name.to_s case name.to_s
when /^x-amz-meta-(.*)/i when /\Ax-amz-meta-(.*)/i
s3_metadata[$1.downcase] = value s3_metadata[$1.downcase] = value
else else
s3_headers[name.to_s.downcase.sub(/^x-amz-/,'').tr("-","_").to_sym] = value s3_headers[name.to_s.downcase.sub(/\Ax-amz-/,'').tr("-","_").to_sym] = value
end end
end end
end end
......
module Paperclip module Paperclip
class TempfileFactory class TempfileFactory
ILLEGAL_FILENAME_CHARACTERS = /^~/ ILLEGAL_FILENAME_CHARACTERS = /\A~/
def generate(name) def generate(name)
@name = name @name = name
......
...@@ -30,7 +30,7 @@ module Paperclip ...@@ -30,7 +30,7 @@ module Paperclip
options = attributes.extract_options!.dup options = attributes.extract_options!.dup
Paperclip::Validators.constants.each do |constant| Paperclip::Validators.constants.each do |constant|
if constant.to_s =~ /^Attachment(.+)Validator$/ if constant.to_s =~ /\AAttachment(.+)Validator\Z/
validator_kind = $1.underscore.to_sym validator_kind = $1.underscore.to_sym
if options.has_key?(validator_kind) if options.has_key?(validator_kind)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment