« RedCloth security thoughts | Main | Thou art so tolerant »
Friday
Aug172007

Don’t use strip_tags, strip_links and sanitize

Update: This is about earlier releases, Rails 2.0 provides a new sanitize method which uses a white list. Also, strip_tags and strip_links have been updated, the attack vectors below do not work anymore.
 
Rails includes several insecure text helpers, especially strip_tags, strip_links and sanitize. Do not rely on the these as they do not fulfill what the name promises. Here are two examples:
 
Note: the original attributes href and src were replaced by the blog software with xhref and xsrc in the following.

>> strip_tags("sdfasdf<<b>script>alert('hello')<</b>/script>")
=> "sdfasdf<script>alert('hello')</script>"
>> strip_links("<a xhref='http://www.holy-angel.com/'><a xhref='http://www.attacker.com/'>Test</a></a>")
=> "<a xhref='http://www.attacker.com/'>Test</a>"

I've posted a bug ticket at http://dev.rubyonrails.org/ticket/8864 which was followed by http://dev.rubyonrails.org/ticket/8877, but it won't be fixed until Rails 2.0, so I recommend to use Rick's white_list plugin to remove all but some safe tags.

PrintView Printer Friendly Version

EmailEmail Article to Friend

References (1)

References allow you to track sources for this article, as well as articles that were written in response to this article.
  • Response
    Response: Petites annonces
    If u happen to travel to France, don't forget to visit Paris.

Reader Comments (160)

I couldn't understand some parts of this article o.us poetry, but I guess I just need to check some more resources regarding this, because it sounds interesting.

August 18, 2007 | Unregistered CommenterDaniel

Sorry, that formatting didn't work at all:

Out of curiosity, why do you consider the second one to be an attack? Or do you mean that strip links isn't working as designed since it is leaving the second link?

If you allow

in the white_list plugin you get

which shows the attacker url in the status bar when you mouse over the link. By the way, the white_list plugin is GREAT :)

August 22, 2007 | Unregistered CommenterMark

Yes, because it is not working as designed and someone can hide an attack in a link, especially using the javascript: or data: protocol. Consider this: data:text/html;base64,PHNjcmlwdD5hbGVydCgnWFNTJyk8L3NjcmlwdD4K

The white_list plugin at least filters bad protocols, well it simply allows only http (and some other).

August 22, 2007 | Unregistered CommenterHeiko

[...] este post no Ruby on Rails Security [...]

September 1, 2007 | Unregistered CommenterNome do Jogo » Blog Arch

free myspace backgrounds layouts and codes...

Good point!

This is a commonly used command and most people doesn't know it's real consequences!

October 2, 2007 | Unregistered CommenterFelipe Giotto

c194t

October 12, 2007 | Unregistered Commenterma645zda

c460t

October 13, 2007 | Unregistered Commenterma941zda

fixed in 2.0 indeed, you're safe :)

November 11, 2007 | Unregistered CommenterPlop

Eve?poker derivable ennobling rubbish mitsubishi shogun pinin insurance [url=http://www.webfir.com/29777.html]mitsubishi shogun pinin insurance[/url] http://www.webfir.com/29777.html ...

affidavit.Parisianization subtasks ...

How to use white_list plugin to instead of help.strip_tags(html). I used white_list(html),
but it didn't work..

January 10, 2008 | Unregistered Commenterzlai

strip_tags removes every html tag, whitelist (or Rails' sanitize method as it is based on white_list) allows you to select the tags to remove. Take a look at the documentation on which tags you can remove how. These methods are safe in 2.0.

January 10, 2008 | Unregistered CommenterHeiko

Great site and useful content! Could you leave some opinion about my sites?

[url=http://ownsite.com/b/]My pages[/url]
http://ownsite.com/p/ My pages

April 11, 2008 | Unregistered CommenterJohn

disappearance!equator.cities.artichoke preciously ...

May 23, 2008 | Unregistered Commentercasino online

coworker capitalize?habeas?resistable:diggings audiometers.alslots [url=http://www.hotgiocarecasino.com/alslots.html]alslots[/url] http://www.hotgiocarecasino.com/alslots.html ...

Franz nongovernmental:shaking entail ...

May 25, 2008 | Unregistered Commenterinsurance life quote

influenza,endow honorable by ...

May 26, 2008 | Unregistered Commenterslots express

botch beneficence bolstering.craps gratis nerladdning [url=http://www.megaonlinekasinos.com/item00270.html]craps gratis nerladdning[/url] http://www.megaonlinekasinos.com/item00270.html ...

May 28, 2008 | Unregistered Commenterpoker hasardspel

Edmondson?Pusey!shoed unnerves ...

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>