Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisroos/393763 to your computer and use it in GitHub Desktop.
Save chrisroos/393763 to your computer and use it in GitHub Desktop.
Check Rails apps to see whether they suffer from the vulnerability in lighthouse ticket 2340
# Introduction
# ------------
# I wanted a fairly simple/standalone way to check whether our rails apps were affected by the ActionMailer/SMTP error
# in lighthouse ticket 2340 - https://rails.lighthouseapp.com/projects/8994/tickets/2340
# Usage
# -----
# Use the script/runner within your rails app
# $ script/runner lighthouse-ticket-2340-rails-smtp-error-check.rb
# Remember to run it in the relevant environment, e.g. production
# $ RAILS_ENV=production script/runner lighthouse-ticket-2340-rails-smtp-error-check.rb
# Mock SMTP service stolen from action_mailer/test/abstract_unit.rb
class MockSMTP
def self.deliveries
@@deliveries
end
def initialize
@@deliveries = []
end
def sendmail(mail, from, to)
@@deliveries << [mail, from, to]
end
def start(*args)
yield self
end
end
class Net::SMTP
def self.new(*args)
MockSMTP.new
end
end
# Store the delivery method for the environment we're running in.
current_delivery_method = ActionMailer::Base.delivery_method
# We have to use smtp as we're interested in the ActionMailer::Base#perform_delivery_smtp method
ActionMailer::Base.delivery_method = :smtp
# A simple test email with a friendly name in the from address string
class TestMailer < ActionMailer::Base
def test_email
recipients 'anyone@example.com'
from 'Friendly name <noreply@example.com>'
subject 'Email subject'
body 'Email body'
end
end
# Deliver the email
TestMailer.deliver_test_email
# Grab the delivered email (stolen from the tests added in http://github.com/rails/rails/commit/da61a6c9671239dbb4a926c3e161ca8663fa0e3f)
# and check whether it contains an angle bracket - which indicates that we'll get errors when trying to send emails
mail = MockSMTP.deliveries.first
mail, from, to = mail
if from.to_s =~ /</
warn "WARNING. It looks like you're suffering from the actionmailer problem described in this ticket https://rails.lighthouseapp.com/projects/8994/tickets/2340"
warn "Sender address was '#{from}'."
if current_delivery_method.to_s =~ /smtp/
warn "SERIOUS. You won't currently be sending any emails from your application. See the ticket mentioned above for a patch that you can apply."
else
puts "NOTE. This isn't too serious at the moment as you're not using smtp (you're using #{current_delivery_method}) but you'll need to bear it in mind if you were to change to using smtp."
end
else
puts "GOOD. Everything appears to be OK according to my very simple checks. Sender address was '#{from}'."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment