Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Fix a XSS vulnerability with bad input to json_dumps.
Django's JSON serialization does not handle escaping of any characters to make them safe for injecting into HTML. This allows an attacker who can provide part of a JSON-serializable object to craft a string that can break out of a <script> tag and create its own, injecting a custom script. To fix this, we escape '<', '>', and '&' characters in the resulting string, preventing a </script> from executing.
- Loading branch information
Showing
2 changed files
with
27 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| from __future__ import unicode_literals | ||
|
|
||
| from djblets.testing.testcases import TestCase | ||
| from djblets.util.templatetags.djblets_js import json_dumps | ||
|
|
||
|
|
||
| class JSTagTests(TestCase): | ||
| """Unit tests for djblets_js template tags.""" | ||
| def test_json_dumps_xss(self): | ||
| """Testing json_dumps doesn't allow XSS injection""" | ||
| # This is bug 3406. | ||
| obj = { | ||
| 'xss': '</script><script>alert(1);</script>' | ||
| } | ||
|
|
||
| self.assertEqual( | ||
| json_dumps(obj), | ||
| '{"xss": "\\u003C/script\\u003E\\u003Cscript\\u003E' | ||
| 'alert(1);\\u003C/script\\u003E"}') |