Skip to content

Instantly share code, notes, and snippets.

@pixeltrix
Created May 20, 2014 11:00
Show Gist options
  • Save pixeltrix/6aaf928d59ccdb6f52cb to your computer and use it in GitHub Desktop.
Save pixeltrix/6aaf928d59ccdb6f52cb to your computer and use it in GitHub Desktop.
Tool to report whether a Rails application may be vulnerable to 'Unsafe Query Risk in Active Record'
# Run this code inside your Rails application
#
# Rails 2.3:
# script/runner unsafe_query_check.rb
#
# Rails 3.0 and later
# rails runner unsafe_query_check.rb
connection = ActiveRecord::Base.connection
tables = connection.tables
columns = tables.map{ |t| connection.columns(t).map(&:name) }.flatten.uniq
conflicts = tables & columns
if conflicts.empty?
puts "Your Rails application is not vulnerable to 'Unsafe Query Risk in Active Record'"
else
puts "Your Rails application is possibly vulnerable to 'Unsafe Query Risk in Active Record'"
puts "Please check the following conflicts between table and column names:"
puts conflicts.inspect
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment