Skip to content

Commit c8c4bcc

Browse files
committed
Security: Fix many broken filter regexps
In Ruby, "foo\nbar" =~ /^bar/ will result in a match, because ^ matches at the start of any line, not at the start of the string. In general, we want to use \A and \z in place of ^ and $, respectively. We rely heavily on regular expressions to filter untrusted data. And many of these regular expressions can be fooled easily because they rely on ^ and $ when they shouldn't. See comment_drop_test for a user-exploitable example. This patch does a bulk search-and-replace of the offending patterns. It may easily have missed something somewhere, but it's a good start.
1 parent 173c3b8 commit c8c4bcc

20 files changed

Lines changed: 42 additions & 35 deletions

‎RAILS-2.2-TODO.txt‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,17 @@ X Can we restrict admin cookies to /admin ? No--need /accounts, too.
2626
/ filtered_column_code_macro
2727
/ filtered_column_flikr_macro - lots of issues
2828
/ Do we have trackback support to check? No.
29-
Password change
30-
Verify token required to change e-mail and password
29+
/ Password change
30+
/ Verify token required to change e-mail and password
3131
Everything else
3232
/ Don't ship :session_key in environment.rb!
3333
/ Do we need to override verifiable_request_format? No.
34-
Check redirection in lib/authenticated_system.rb
34+
/ Check redirection in lib/authenticated_system.rb
3535
Detect mass assignment failures in unit tests
36-
Review mass assignment in public controllers
37-
Check regexes for ^ and $
36+
/ Review mass assignment in public controllers - comments
37+
/ Check regexes for ^ and $
38+
Filter IMG tags
39+
Block database updates on POST requests
3840
Review http://guides.rubyonrails.org/security.html again
3941

4042
Admin only

‎app/controllers/admin/articles_controller.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def convert_times_to_utc
138138
with_site_timezone do
139139
date = Time.parse_from_attributes(params[:article], :published_at, :local)
140140
next unless date
141-
params[:article].delete_if { |k, v| k.to_s =~ /^#{:published_at}/ }
141+
params[:article].delete_if { |k, v| k.to_s =~ /\A#{:published_at}/ }
142142
params[:article][:published_at] = local_to_utc(date)
143143
end
144144
end

‎app/controllers/admin/base_controller.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def allow_member?
2222
end
2323

2424
def find_and_sort_templates
25-
@layouts, @templates = site.templates.partition { |t| t.dirname.to_s =~ /layouts$/ }
25+
@layouts, @templates = site.templates.partition { |t| t.dirname.to_s =~ /layouts\z/ }
2626
end
2727

2828
def self.clear_empty_templates_for(model, *attributes)

‎app/controllers/admin/design_controller.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def create
77
return
88
end
99

10-
if params[:filename] =~ /\.(css|js)$/i
10+
if params[:filename] =~ /\.(css|js)\z/i
1111
@resource = @theme.resources.write params[:filename], params[:data]
1212
redirect_to url_for_theme(:controller => 'resources', :action => 'edit', :filename => @resource.basename.to_s)
1313
else

‎app/controllers/feed_controller.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class FeedController < ApplicationController
66
def feed
77
sections = params[:sections].clone
88
last = sections.last
9-
sections.delete(last) if last =~ /\.xml$/
9+
sections.delete(last) if last =~ /\.xml\z/
1010
@section_path = sections.blank? ? '' : sections.join('/')
1111
case last
1212
when 'all_comments.xml'

‎app/drops/comment_drop.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def initialize(source)
1111

1212
def author_url
1313
return nil if source.author_url.blank?
14-
@source.author_url =~ /^https?:\/\// ? @source.author_url : "http://" + @source.author_url
14+
@source.author_url =~ /\Ahttps?:\/\// ? @source.author_url : "http://" + @source.author_url
1515
end
1616

1717
def url

‎app/filters/core_filters.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def textilize(text)
2828

2929
def parse_date(date)
3030
return Time.now.utc if date.blank?
31-
date = "#{date}-1" if date.to_s =~ /^\d{4}-\d{1,2}$/ unless [Time, Date].include?(date.class)
31+
date = "#{date}-1" if date.to_s =~ /\A\d{4}-\d{1,2}\z/ unless [Time, Date].include?(date.class)
3232
date = date.to_time
3333
end
3434

‎app/helpers/application_helper.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module ApplicationHelper
33

44
def author_link_for(comment)
55
return h(comment.author) if comment.author_url.blank?
6-
link_to h(comment.author), "#{'http://' unless comment.author_url =~ /^https?:\/\//}#{comment.author_url}"
6+
link_to h(comment.author), "#{'http://' unless comment.author_url =~ /\Ahttps?:\/\//}#{comment.author_url}"
77
end
88

99
def avatar_for(user, options = {})

‎app/models/asset.rb‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ class Asset < ActiveRecord::Base
1414

1515
class << self
1616
def movie?(content_type)
17-
content_type.to_s =~ /^video/ || extra_content_types[:movie].include?(content_type)
17+
content_type.to_s =~ /\Avideo/ || extra_content_types[:movie].include?(content_type)
1818
end
1919

2020
def audio?(content_type)
21-
content_type.to_s =~ /^audio/ || extra_content_types[:audio].include?(content_type)
21+
content_type.to_s =~ /\Aaudio/ || extra_content_types[:audio].include?(content_type)
2222
end
2323

2424
def other?(content_type)
@@ -72,7 +72,7 @@ def full_filename(thumbnail = nil)
7272

7373
def public_filename_with_host(thumbnail = nil)
7474
returning public_filename_without_host(thumbnail) do |s|
75-
s.gsub! /^\/assets\/[^\/]+\//, "/assets/#{$1}" if Site.multi_sites_enabled
75+
s.gsub! /\A\/assets\/[^\/]+\//, "/assets/#{$1}" if Site.multi_sites_enabled
7676
end
7777
end
7878
alias_method_chain :public_filename, :host

‎app/models/resources.rb‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ def content_type(path)
2020
def [](filename)
2121
path =
2222
case filename
23-
when /\.js$/i then 'javascripts'
24-
when /\.css$/i then 'stylesheets'
23+
when /\.js\z/i then 'javascripts'
24+
when /\.css\z/i then 'stylesheets'
2525
else 'images'
2626
end
2727

0 commit comments

Comments
 (0)