From 50ac4c633634bb0b925d0689460c9ef1080850cd Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Fri, 8 Aug 2014 13:35:25 -0400 Subject: [PATCH] FIX: cleanup aliases in email From and Reply-to fields to prevent Bad sender address syntax errors --- lib/email.rb | 5 +++++ lib/email/message_builder.rb | 6 +++--- spec/components/email/message_builder_spec.rb | 15 +++++++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/email.rb b/lib/email.rb index 07da49eb9..d0029b3d0 100644 --- a/lib/email.rb +++ b/lib/email.rb @@ -21,4 +21,9 @@ module Email email.downcase end + def self.cleanup_alias(name) + # TODO: I'm sure there are more, but I can't find a list + name ? name.gsub(/[:<>]/, '') : name + end + end diff --git a/lib/email/message_builder.rb b/lib/email/message_builder.rb index 310f97f9e..b488bff45 100644 --- a/lib/email/message_builder.rb +++ b/lib/email/message_builder.rb @@ -165,14 +165,14 @@ module Email def alias_email(source) return source if @opts[:from_alias].blank? && SiteSetting.email_site_title.blank? if !@opts[:from_alias].blank? - "#{@opts[:from_alias]} <#{source}>" + "#{Email.cleanup_alias(@opts[:from_alias])} <#{source}>" else - "#{SiteSetting.email_site_title} <#{source}>" + "#{Email.cleanup_alias(SiteSetting.email_site_title)} <#{source}>" end end def site_alias_email(source) - "#{SiteSetting.email_site_title.presence || SiteSetting.title} <#{source}>" + "#{Email.cleanup_alias(SiteSetting.email_site_title.presence || SiteSetting.title)} <#{source}>" end end diff --git a/spec/components/email/message_builder_spec.rb b/spec/components/email/message_builder_spec.rb index a5cb86f70..8586416f4 100644 --- a/spec/components/email/message_builder_spec.rb +++ b/spec/components/email/message_builder_spec.rb @@ -56,6 +56,11 @@ describe Email::MessageBuilder do it "returns a Reply-To header with the reply key" do expect(reply_by_email_builder.header_args['Reply-To']).to eq(SiteSetting.title + " ") end + + it "cleans up the site title" do + SiteSetting.stubs(:title).returns(">>>Obnoxious Title: Deal With It<<<") + expect(reply_by_email_builder.header_args['Reply-To']).to eq("Obnoxious Title Deal With It ") + end end context "With the SiteSetting disabled" do @@ -247,6 +252,16 @@ describe Email::MessageBuilder do expect(build_args[:from]).to eq("The Forum <#{SiteSetting.notification_email}>") end + it "cleans up aliases in the from_alias arg" do + builder = Email::MessageBuilder.new(to_address, from_alias: "Finn: the Dog <3", from: finn_email) + builder.build_args[:from].should == "Finn the Dog 3 <#{finn_email}>" + end + + it "cleans up the email_site_title" do + SiteSetting.stubs(:email_site_title).returns("::>>>Best Forum EU: Award Winning<<<") + expect(build_args[:from]).to eq("Best Forum EU Award Winning <#{SiteSetting.notification_email}>") + end + end end