Bob Follek wrote an interesting post asking the question Why The h Can’t Rails Escape HTML Automatically?.
As many of you know, Django recently introduced an enhancement that automatically auto-escapes all template variables. It’s a very elegant solution and something that I’m really excited about. (Although I could do without the plethora of questions on IRC that begin with “Why suddenly are all my HTML attributes appearing as > and <.) The really cool thing is the amount of flexibility built into the auto-escaping architecture. Auto-escaping can be turned off per-site, per-template level or per-variable level. This gives the developer total control, while still maintaining a safety by design stance.
What’s even more interesting is the comments in Simon Willison’s reference to the aforementioned post. To be more precise the interesting part is the actual comment banter that takes place between koz, Michael Koziarski, a Rails core developer and Simon. Read the comments to get all the details, but essentially koz explains that the difficulty is in the fact that all of the Rails helpers, such as link_to would need to be able to not be escaped. It is a difficult problem but the same problem existed for Django with some of the tags and template filters, and they handled it with a flag that let the render engine know how things should be handled.
I got involved with Rails in early 2005 and I remain involved somewhat with the Rails community, although not as engrossed as I once was. The Rails developer community has been asking for this feature for as long as I can remember. Some will argue that a framework can’t solve lack of developer competence, but the reality is that our frameworks and tools should be safe by design. Security must be something that is designed into the product and not as an afterthought.
Along the same vein as this thinking, it has bugged me for a while now that the database vendors do not make any effort to stop sql injection attacks. They could be seriously thwarted by setting a default database option that does not allow more than a single transaction to be executed at a time. Of course they would need an option to turn this off as needed, but this would certainly help correct the problem in a big way.
Regarding the Rails issue, I agree with koz that it’s not an easy problem to solve, especially with a framework like Rails that is so far along in development. But it’s also not an impossible thing to address either. The auto-escaping Rails plugin looks interesting and I hope that as the Rails framework moves forward they will adopt a solution such as that to deal with this problem effectively. My guess is that it probably took no more than about 40 – 60 hours of development time to get it implemented in Django, although that’s just an assumption on my part.
As an aside one thing that has always impressed me with Michael Koziarski is that although he is quick to defend Rails or set the record straight, he always does so with grace. This is certainly something that a lot of individuals involved in open-source projects could learn from.