security.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. safe_tests = [
  2. "<p>Hi there</p>",
  3. '<h1 class="foo">Hi There!</h1>',
  4. '<a data-cite="foo">citation</a>',
  5. '<div><span>Hi There</span></div>',
  6. ];
  7. unsafe_tests = [
  8. "<script>alert(999);</script>",
  9. '<a onmouseover="alert(999)">999</a>',
  10. '<a onmouseover=alert(999)>999</a>',
  11. '<IMG """><SCRIPT>alert("XSS")</SCRIPT>">',
  12. '<IMG SRC=# onmouseover="alert(999)">',
  13. '<<SCRIPT>alert(999);//<</SCRIPT>',
  14. '<SCRIPT SRC=http://ha.ckers.org/xss.js?< B >',
  15. '<META HTTP-EQUIV="refresh" CONTENT="0;url=data:text/html base64,PHNjcmlwdD5hbGVydCgnWFNTJyk8L3NjcmlwdD4K">',
  16. '<META HTTP-EQUIV="refresh" CONTENT="0; URL=http://;URL=javascript:alert(999);">',
  17. '<IFRAME SRC="javascript:alert(999);"></IFRAME>',
  18. '<IFRAME SRC=# onmouseover="alert(document.cookie)"></IFRAME>',
  19. '<EMBED SRC="data:image/svg+xml;base64,PHN2ZyB4bWxuczpzdmc9Imh0dH A6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcv MjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hs aW5rIiB2ZXJzaW9uPSIxLjAiIHg9IjAiIHk9IjAiIHdpZHRoPSIxOTQiIGhlaWdodD0iMjAw IiBpZD0ieHNzIj48c2NyaXB0IHR5cGU9InRleHQvZWNtYXNjcmlwdCI+YWxlcnQoIlh TUyIpOzwvc2NyaXB0Pjwvc3ZnPg==" type="image/svg+xml" AllowScriptAccess="always"></EMBED>',
  20. // CSS is scrubbed
  21. '<style src="http://untrusted/style.css"></style>',
  22. '<style>div#notebook { background-color: alert-red; }</style>',
  23. '<div style="background-color: alert-red;"></div>',
  24. ];
  25. var truncate = function (s, n) {
  26. // truncate a string with an ellipsis
  27. if (s.length > n) {
  28. return s.substr(0, n-3) + '...';
  29. } else {
  30. return s;
  31. }
  32. };
  33. casper.notebook_test(function () {
  34. this.each(safe_tests, function (self, item) {
  35. var sanitized = self.evaluate(function (item) {
  36. return IPython.security.sanitize_html(item);
  37. }, item);
  38. // string equality may be too strict, but it works for now
  39. this.test.assertEquals(sanitized, item, "Safe: '" + truncate(item, 32) + "'");
  40. });
  41. this.each(unsafe_tests, function (self, item) {
  42. var sanitized = self.evaluate(function (item) {
  43. return IPython.security.sanitize_html(item);
  44. }, item);
  45. this.test.assertNotEquals(sanitized, item,
  46. "Sanitized: '" + truncate(item, 32) +
  47. "' => '" + truncate(sanitized, 32) + "'"
  48. );
  49. this.test.assertEquals(sanitized.indexOf("alert"), -1, "alert removed");
  50. });
  51. });