sanitizer.spec.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import 'jest';
  4. import { defaultSanitizer } from '@jupyterlab/apputils';
  5. describe('defaultSanitizer', () => {
  6. describe('#sanitize()', () => {
  7. it('should allow h1 tags', () => {
  8. const h1 = '<h1>foo</h1>';
  9. expect(defaultSanitizer.sanitize(h1)).toBe(h1);
  10. });
  11. it('should allow h2 tags', () => {
  12. const h2 = '<h2>foo</h2>';
  13. expect(defaultSanitizer.sanitize(h2)).toBe(h2);
  14. });
  15. it('should not allow svg tags', () => {
  16. const svg = '<svg>foo</svg>';
  17. expect(defaultSanitizer.sanitize(svg)).toBe('foo');
  18. });
  19. it('should allow img tags and some attributes', () => {
  20. const img =
  21. '<img src="smiley.gif" alt="Smiley face" height="42" width="42" />';
  22. expect(defaultSanitizer.sanitize(img)).toBe(img);
  23. });
  24. it('should allow span tags and class attribute', () => {
  25. const span = '<span class="foo">bar</span>';
  26. expect(defaultSanitizer.sanitize(span)).toBe(span);
  27. });
  28. it('should set the rel attribute for <a> tags to "nofollow', () => {
  29. const a = '<a rel="foo" href="bar">Baz</a>';
  30. const expected = a.replace('foo', 'nofollow');
  31. expect(defaultSanitizer.sanitize(a)).toBe(expected);
  32. });
  33. it('should allow the class attribute for code tags', () => {
  34. const code = '<code class="foo">bar</code>';
  35. expect(defaultSanitizer.sanitize(code)).toBe(code);
  36. });
  37. it('should allow the class attribute for div tags', () => {
  38. const div = '<div class="foo">bar</div>';
  39. expect(defaultSanitizer.sanitize(div)).toBe(div);
  40. });
  41. it('should allow the class attribute for p tags', () => {
  42. const p = '<p class="foo">bar</p>';
  43. expect(defaultSanitizer.sanitize(p)).toBe(p);
  44. });
  45. it('should allow the class attribute for pre tags', () => {
  46. const pre = '<pre class="foo">bar</pre>';
  47. expect(defaultSanitizer.sanitize(pre)).toBe(pre);
  48. });
  49. it('should strip script tags', () => {
  50. const script = '<script>alert("foo")</script>';
  51. expect(defaultSanitizer.sanitize(script)).toBe('');
  52. });
  53. it('should strip iframe tags', () => {
  54. const script = '<iframe src=""></iframe>';
  55. expect(defaultSanitizer.sanitize(script)).toBe('');
  56. });
  57. it('should strip link tags', () => {
  58. const link = '<link rel="stylesheet" type="text/css" href="theme.css">';
  59. expect(defaultSanitizer.sanitize(link)).toBe('');
  60. });
  61. it('should pass through simple well-formed whitelisted markup', () => {
  62. const div = '<div><p>Hello <b>there</b></p></div>';
  63. expect(defaultSanitizer.sanitize(div)).toBe(div);
  64. });
  65. it('should allow video tags with some attributes', () => {
  66. const video =
  67. '<video src="my/video.mp4" height="42" width="42"' +
  68. ' autoplay controls loop muted></video>';
  69. expect(defaultSanitizer.sanitize(video)).toBe(video);
  70. });
  71. it('should allow audio tags with some attributes', () => {
  72. const audio =
  73. '<audio src="my/audio.ogg autoplay loop ' + 'controls muted"></audio>';
  74. expect(defaultSanitizer.sanitize(audio)).toBe(audio);
  75. });
  76. it('should allow input tags but disable them', () => {
  77. const html = defaultSanitizer.sanitize(
  78. '<input type="checkbox" checked />'
  79. );
  80. const div = document.createElement('div');
  81. let input: HTMLInputElement;
  82. div.innerHTML = html;
  83. input = div.querySelector('input')!;
  84. expect(input.disabled).toBe(true);
  85. });
  86. // Test unwanted inline CSS style stripping
  87. it('should allow harmless inline CSS', () => {
  88. const div = '<div style="color:green"></div>';
  89. expect(defaultSanitizer.sanitize(div)).toBe(div);
  90. });
  91. it("should strip 'content' properties from inline CSS", () => {
  92. const div = '<div style="color: green; content: attr(title)"></div>';
  93. expect(defaultSanitizer.sanitize(div)).toBe(
  94. '<div style="color:green"></div>'
  95. );
  96. });
  97. it("should strip 'counter-increment' properties from inline CSS", () => {
  98. const div = '<div style="counter-increment: example-counter;"></div>';
  99. expect(defaultSanitizer.sanitize(div)).toBe('<div></div>');
  100. });
  101. it("should strip 'counter-reset' properties from inline CSS", () => {
  102. const div = '<div style="counter-reset: chapter-count 0;"></div>';
  103. expect(defaultSanitizer.sanitize(div)).toBe('<div></div>');
  104. });
  105. it("should strip 'widows' properties from inline CSS", () => {
  106. const div = '<div style="widows: 2;"></div>';
  107. expect(defaultSanitizer.sanitize(div)).toBe('<div></div>');
  108. });
  109. it("should strip 'orphans' properties from inline CSS", () => {
  110. const div = '<div style="orphans: 3;"></div>';
  111. expect(defaultSanitizer.sanitize(div)).toBe('<div></div>');
  112. });
  113. });
  114. });