Take the 2-minute tour ×
Information Security Stack Exchange is a question and answer site for Information security professionals. It's 100% free, no registration required.

For one who doesn't know, .html_safe is rails method, which outputs strings as part of html document. The problem is that string is user inputed. What did I do to prevent some bad actions - regexp inserts after every < or > invisible symbol, which breaks all tags, that could be part of html of document. But still feel a bit unsure - does it protect page? Any other ways of use .html_safe, except tags, or symbol doesn't protects me enough?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Here's an article explaining in details how html_safe actually works. In short, you should not call .html_safe in untrusted strings or in strings that contain other untrusted strings (e.g. "trusted #{untrusted} trusted").

If you have a trusted string (not user inputted) that you need unescaped, you can call html_safe in it, and proceed appending other strings to it (or appending it to other SafeBuffers). Every regular string appended to a SafeBuffer will be escaped, and thus can not cause XSS vulnerabilities. If you concatenate two SafeBuffers, however, no escaping will occur.

Also according to the linked article, the rendering of every view starts with am empty SafeBuffer, so every string you append to it will be escaped by default.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.