This project is archived and is in readonly mode.
[PATCH] ActionMailer is html escaping plain text messages
-
Michael Koziarski
- Milestone cleared.
- Tag set to regression
- Assigned user set to Yehuda Katz (wycats)
This is probably as a result of unifying the AM and AC use of AV, it's a regression and should be fixed before 3.0
-
Amos King
I can't reproduce this issue with the latest. Can I get an example so I can tackle this.
-
Rodrigo Rosenfeld Rosas
I've just found the root cause. url_for is not marking the url as html_safe and the ERB template used by the plain text templating is sanitizing the text by default as the other ERB templates.
This mean that now the plain text ERB template should be something like:
Please, visit <%= raw @confirmation_url %>
I think plain text templates should not be sanitized in favor of the least surprise principle...
What do you think?
-
Akira Matsuda
I also got stuck on this problem.
I think plain text templates should not be sanitized in favor of the least surprise principle... What do you think?
- Plain text template should better not be html_escaped.
-
Akira Matsuda
Sorry, I meant, +1
-
Santiago Pastorino
- State changed from new to open
- Assigned user changed from Yehuda Katz (wycats) to Mikel Lindsaar
- Importance changed from to Low
-
José Valim
- Assigned user changed from Mikel Lindsaar to Yehuda Katz (wycats)
This is not related to the mail gem, but the ActionView bit. Assigning it back to Mr. Katz.
-
Wincent Colaiuta
I've had to use
rawin a few places in my non-HTML templates for this reason.It's not limited to templates with the
.text.erbextension. I've also had to do it in.js.erbtemplates. I imagine that if I had other non-HTML ERB templates it would behave the same way too.No idea if this is limited to ERB, seeing as the only other template format I use is Haml, which is obviously always producing HTML output only.
-
Fjan
I just wanted to point out that the it's not just URLs and not just ampersands: any text gets HTML escaped. This is going to trip people up who accidentally use < and > characters in an e-mail address for example. My plain text e-mail templates are now littered with .html_safe everywhere to prevent this.
As far as I can tell from the code this is going to be pretty hard to fix without ruining the nice unification of the mailer views with the regular views we just had. My suggestion is to define a new erb character sequence such as <%== to mean a "raw concat". That will beatify the code a little and make sure that we can upgrade old templates with a simple search/replace, mailer or regular view.
-
Fjan
Just a quick follow up to myself, I looked at the code and all that is needed to make "<%== ..." behave as "<%= raw ..." is this snippet in an initializer:
module ActionView class Template module Handlers class Erubis < ::Erubis::Eruby def add_expr_escaped(src, code) src << "@output_buffer.safe_concat(" << escape_text(code.html_safe) << ");" end end end end endIt can be made more efficient by adding to the string directly.
-
Wincent Colaiuta
I think adding new syntax is a non-solution to this problem which just wall-papers over the underlying issue.
The fact is that it doesn't make sense to HTML-escape content which is not HTML.
Merely adding a short-cut syntax for avoiding the undesired escaping is just giving us a more concise means of doing something that we shouldn't have to do in the first place, and IMO it would be worse than just ignoring the problem.
-
Fjan
I agree a proper fix where plain text templates do net get escaped would be better.
However, I still like that new syntax for the regular views. I just did a quick count for "html_safe" and I came up with 390 in my views and helpers. So it would sure save a lot of typing and it can also make the templates more efficient by not converting to an output_buffer and back to a string (as I did above).
I simply like to use some like without all the html_safe stuff:
<%= "<b>Alert</b>".html_safe if level<0 %> -
Fjan
Oops, that method in my first posts should have read:
def add_expr_escaped(src, code) src << "@output_buffer.safe_concat((" << code << ").to_s);" endAlso apologies for the lack of proofreading in the second post. I'm actually tempted to override the XSS escaping when upgrading from Rails 2 projects by doing a global search/replace of <%= with <%== now. The on-by-default escaping idea is sound in theory but it's just introducing more problems than it solves when upgrading an old and stable code base.
-
Fjan
- Title changed from ActionMailer is html escaping ampersand (&) in Urls in plain text messages to ActionMailer is html escaping plain text messages
-
Wincent Colaiuta
I agree a proper fix where plain text templates do net get escaped would be better.
However, I still like that new syntax for the regular views.
Fjan, your proposed shortcut syntax probably belongs in a separate ticket.
-
Fjan
You are right. Sorry for the spam here, not thinking to clearly after typing 390 html_safes. I'll put in a new ticket tomorrow.
-
Bertg
- Tag set to actionview, escaping, formats, html_safe, safe_buffer, xss
We have been investigating this issue, and it goes beyond ActionMailer.
We have detected this happening when rendering csv files, ical compatible files etc.. Basically html escaping is happening all the time in ActionView regardless of format.
Templates and Handlers always choose a ActionView::SafeBuffer or a subclass of it. This is desired behaviour in case of html (or alike, e.g.: xml) but not in any other context like plain text emails or csv.
The template should be smart enough to choose the correct buffer based on the format passed to it. E.g.: when the extension of a template is .text.haml it should not be doing any escaping, i.e. not be using the safe buffer.
We do think the priority of this bug should be high, as it's influencing all formats besides html, and thus breaking these.
The impact is probably going to be beyond rails, but in any rails3 compatible handler, like haml.
-
Fjan
Now that my patch to add a non-escaping option to Erubis has been committed to 3.0 stable this problem can be fixed by simply letting the handler check if the template identifier contains "html" and disabling escape otherwise. Attached is a monkey patch that adds one line to the template setup:
:escape => template.identifier !~ /\.html/ # Note: :escape => true disables escapingNote that this patch will only work if the patch mentioned above is also applied.
-
Rodrigo Rosenfeld Rosas
Worth noting that this monkey patch will only work on ERB templates but won't work for HAML for instance...
-
Fjan
- Title changed from ActionMailer is html escaping plain text messages to [PATCH] ActionMailer is html escaping plain text messages
Here is the one-line patch that can be applied to edge. Sorry, I have no idea how to add a test for this, I'm kind of new to all this.
-
Matthew Stopa
Does anyone know if this issue has been fixed yet? It looks like things have changed a lot in Rails3 since the patch was submitted. We're experiencing the problem currently where our users are getting their emails with escaped characters. Anyone know a workaround?
-
Bertg
I have no idea what the status on this ticket is, but the work around we used is to indicate EACH string as html safe manually. That works great :)
-
Matthew Stopa
That hasn't worked for me at all. Even with a basic ActionMailer where I specify the body as body => "blah blah".html_safe I am still seeing escaped characters in the emails it sends.
-
Fjan
@Matthew: I've been using the monkey patch I provided in the November 11th post in production for quite some time now and it works great. Just drop it in the config/initializers directory.
