2

We have a webapplication. At some points there is a JavaScript based WSIWYG / RichText Editor. It filters some JavaScript but uses HTML text to format it's content.

Unfortunately it does not filter all JavaScript. I was able to proof a XSS attack with an event handler. I think the JavaScript client side filtering of JavaScript is not safe at all, because at client side it can be manipulated.

So I would like to filter or escape JavaScript at the server side. I had a short look at ESAPI for Java. But we have a requirement, I don't know if it is special or a problem: The HTML elements the editor uses should not be filtered or escaped, only JavaScript. The HTML should be ordinary rendered in the browser.

  • Is there a safe way, to escapce or filter JavaScript while keeping the HTML like it is?
  • Does ESAPI or any other API help me doing this?
  • How do I do it.

Thanks in advance.

2 Answers 2

2

It is difficult to state what escaping schemes have to be used to escape JavaScript without knowing whether the application is vulnerable to DOM-based XSS attacks or the run-of-the-mill (reflected and persistent) XSS attacks.

ESAPI for Java will help in both cases though. In the case of DOM-based XSS attacks, you would need to encode the unsafe data multiple times (and using different encoding schemes if necessary) to ensure that each parser in the parsing chain will not be subject to XSS attacks. In the case of reflected or persistent XSS attacks, you'll usually need to apply the escaping only once, in the appropriate context.

It should be kept in mind that, allowing raw HTML on its own is also unsafe, resulting in XSS. You might want to take a look at a different approach to sanitizing inputs; using AntiSamy for filtering HTML might be warranted in this case.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your answer. I don't know about DOM based XSS, yet. I have to read about it. The vulnerability is a persistent XSS one. Out web framework does escape HTML and JS by default, but in this case we switched it of, to allow HTML rendering. That's where the vulnerability comes from. Reflected XSS may be possible in that way, too, but has not been found, yet.
I took anti samy. It was a bit wired to get it's dependency right, but the java coding was realy easy. At first test it works well. Custom configuration (if needed) may be a bit more complex. Thx, again.
@anonymous, you're welcome. I figured that with a rich text editor, you would be better using AntiSamy than ESAPI.
0

You need to parse the HTML and reject any tags and attributes that aren't in a strict whitelist of safe tags/attributes.

The whitelist would not include tags like <script>, <style>, or <link>, and it wouldn't include attributes like onclick, onload, or style.

You should also make sure that href and src attributes use the http or https protocols (or a relative path), and not javascript:.

1 Comment

Thanks for your answer. Isn't there any API that does exactly this for me? I am afraid of doing something wrong, implementing this on my own. There are so much pitfalls hidden in the details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.