marked.spec.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* eslint no-script-url:0 */
  2. import marked from 'sentry/utils/marked';
  3. function expectMarkdown(test) {
  4. expect(marked(test[0])).toEqual('<p>' + test[1] + '</p>\n');
  5. }
  6. describe('marked', function () {
  7. it('normal links get rendered as html', function () {
  8. for (const test of [
  9. ['[x](http://example.com)', '<a href="http://example.com">x</a>'],
  10. ['[x](https://example.com)', '<a href="https://example.com">x</a>'],
  11. ['[x](mailto:foo@example.com)', '<a href="mailto:foo@example.com">x</a>'],
  12. ]) {
  13. expectMarkdown(test);
  14. }
  15. });
  16. it('rejected links should be rendered as plain text', function () {
  17. for (const test of [
  18. ['[x](javascript:foo)', 'javascript:foo'],
  19. ['[x](java\nscript:foo)', '[x](java\nscript:foo)'],
  20. ['[x](data:foo)', 'data:foo'],
  21. ['[x](vbscript:foo)', 'vbscript:foo'],
  22. ]) {
  23. expectMarkdown(test);
  24. }
  25. });
  26. it('normal images get rendered as html', function () {
  27. for (const test of [
  28. ['![](http://example.com)', '<img alt="" src="http://example.com">'],
  29. ['![x](http://example.com)', '<img alt="x" src="http://example.com">'],
  30. ['![x](https://example.com)', '<img alt="x" src="https://example.com">'],
  31. ]) {
  32. expectMarkdown(test);
  33. }
  34. });
  35. it("rejected images shouldn't be rendered at all", function () {
  36. for (const test of [['![x](javascript:foo)', '']]) {
  37. expectMarkdown(test);
  38. }
  39. });
  40. it('escapes XSS and removes invalid attributes on img', function () {
  41. [
  42. [
  43. `[test](http://example.com\""#><img/onerror='alert\(location\)'/src=>)
  44. ![test](http://example.com"/onerror='alert\(location\)'/)`,
  45. `<a href="http://example.com"><img src="">"&gt;test</a>
  46. <img alt="test" src="http://example.com">`,
  47. ],
  48. [
  49. '<script> <img <script> src=x onerror=alert(1) />',
  50. '&lt;script&gt; &lt;img &lt;script&gt; src=x onerror=alert(1) /&gt;',
  51. ],
  52. ].forEach(expectMarkdown);
  53. });
  54. });