sanitizer.spec.ts 4.8 KB

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