8
0

utils.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import ViewDocument from '@ckeditor/ckeditor5-engine/src/view/document';
  6. import ViewDowncastWriter from '@ckeditor/ckeditor5-engine/src/view/downcastwriter';
  7. import AttributeElement from '@ckeditor/ckeditor5-engine/src/view/attributeelement';
  8. import ContainerElement from '@ckeditor/ckeditor5-engine/src/view/containerelement';
  9. import Text from '@ckeditor/ckeditor5-engine/src/view/text';
  10. import { createLinkElement, isLinkElement, ensureSafeUrl, normalizeDecorators } from '../src/utils';
  11. describe( 'utils', () => {
  12. describe( 'isLinkElement()', () => {
  13. it( 'should return true for elements created by createLinkElement', () => {
  14. const element = createLinkElement( 'http://ckeditor.com', new ViewDowncastWriter( new ViewDocument() ) );
  15. expect( isLinkElement( element ) ).to.be.true;
  16. } );
  17. it( 'should return false for other AttributeElements', () => {
  18. expect( isLinkElement( new AttributeElement( 'a' ) ) ).to.be.false;
  19. } );
  20. it( 'should return false for ContainerElements', () => {
  21. expect( isLinkElement( new ContainerElement( 'p' ) ) ).to.be.false;
  22. } );
  23. it( 'should return false for text nodes', () => {
  24. expect( isLinkElement( new Text( 'foo' ) ) ).to.be.false;
  25. } );
  26. } );
  27. describe( 'createLinkElement()', () => {
  28. it( 'should create link AttributeElement', () => {
  29. const element = createLinkElement( 'http://cksource.com', new ViewDowncastWriter( new ViewDocument() ) );
  30. expect( isLinkElement( element ) ).to.be.true;
  31. expect( element.priority ).to.equal( 5 );
  32. expect( element.getAttribute( 'href' ) ).to.equal( 'http://cksource.com' );
  33. expect( element.name ).to.equal( 'a' );
  34. } );
  35. } );
  36. describe( 'ensureSafeUrl()', () => {
  37. it( 'returns the same absolute http URL', () => {
  38. const url = 'http://xx.yy/zz#foo';
  39. expect( ensureSafeUrl( url ) ).to.equal( url );
  40. } );
  41. it( 'returns the same absolute https URL', () => {
  42. const url = 'https://xx.yy/zz';
  43. expect( ensureSafeUrl( url ) ).to.equal( url );
  44. } );
  45. it( 'returns the same absolute ftp URL', () => {
  46. const url = 'ftp://xx.yy/zz';
  47. expect( ensureSafeUrl( url ) ).to.equal( url );
  48. } );
  49. it( 'returns the same absolute ftps URL', () => {
  50. const url = 'ftps://xx.yy/zz';
  51. expect( ensureSafeUrl( url ) ).to.equal( url );
  52. } );
  53. it( 'returns the same absolute mailto URL', () => {
  54. const url = 'mailto://foo@bar.com';
  55. expect( ensureSafeUrl( url ) ).to.equal( url );
  56. } );
  57. it( 'returns the same relative URL (starting with a dot)', () => {
  58. const url = './xx/yyy';
  59. expect( ensureSafeUrl( url ) ).to.equal( url );
  60. } );
  61. it( 'returns the same relative URL (starting with two dots)', () => {
  62. const url = '../../xx/yyy';
  63. expect( ensureSafeUrl( url ) ).to.equal( url );
  64. } );
  65. it( 'returns the same relative URL (starting with a slash)', () => {
  66. const url = '/xx/yyy';
  67. expect( ensureSafeUrl( url ) ).to.equal( url );
  68. } );
  69. it( 'returns the same relative URL (starting with a backslash)', () => {
  70. const url = '\\xx\\yyy';
  71. expect( ensureSafeUrl( url ) ).to.equal( url );
  72. } );
  73. it( 'returns the same relative URL (starting with a letter)', () => {
  74. const url = 'xx/yyy';
  75. expect( ensureSafeUrl( url ) ).to.equal( url );
  76. } );
  77. it( 'returns the same URL even if it contains whitespaces', () => {
  78. const url = ' ./xx/ yyy\t';
  79. expect( ensureSafeUrl( url ) ).to.equal( url );
  80. } );
  81. it( 'returns the same URL even if it contains non ASCII characters', () => {
  82. const url = 'https://kłącze.yy/źdźbło';
  83. expect( ensureSafeUrl( url ) ).to.equal( url );
  84. } );
  85. it( 'accepts non string values', () => {
  86. expect( ensureSafeUrl( undefined ) ).to.equal( 'undefined' );
  87. expect( ensureSafeUrl( null ) ).to.equal( 'null' );
  88. } );
  89. it( 'returns safe URL when a malicious URL starts with javascript:', () => {
  90. const url = 'javascript:alert(1)';
  91. expect( ensureSafeUrl( url ) ).to.equal( '#' );
  92. } );
  93. it( 'returns safe URL when a malicious URL starts with an unknown protocol', () => {
  94. const url = 'foo:alert(1)';
  95. expect( ensureSafeUrl( url ) ).to.equal( '#' );
  96. } );
  97. it( 'returns safe URL when a malicious URL contains spaces', () => {
  98. const url = 'java\u0000script:\talert(1)';
  99. expect( ensureSafeUrl( url ) ).to.equal( '#' );
  100. } );
  101. it( 'returns safe URL when a malicious URL contains spaces (2)', () => {
  102. const url = '\u0000 javascript:alert(1)';
  103. expect( ensureSafeUrl( url ) ).to.equal( '#' );
  104. } );
  105. it( 'returns safe URL when a malicious URL contains a safe part', () => {
  106. const url = 'javascript:alert(1)\nhttp://xxx';
  107. expect( ensureSafeUrl( url ) ).to.equal( '#' );
  108. } );
  109. it( 'returns safe URL when a malicious URL contains a safe part (2)', () => {
  110. const url = 'javascript:alert(1);http://xxx';
  111. expect( ensureSafeUrl( url ) ).to.equal( '#' );
  112. } );
  113. } );
  114. describe( 'normalizeDecorators()', () => {
  115. it( 'should transform an entry object to a normalized array', () => {
  116. const callback = () => {};
  117. const entryObject = {
  118. foo: {
  119. mode: 'manual',
  120. label: 'Foo',
  121. attributes: {
  122. foo: 'foo'
  123. }
  124. },
  125. bar: {
  126. mode: 'automatic',
  127. callback,
  128. attributes: {
  129. bar: 'bar'
  130. }
  131. },
  132. baz: {
  133. mode: 'manual',
  134. label: 'Baz label',
  135. attributes: {
  136. target: '_blank',
  137. rel: 'noopener noreferrer'
  138. }
  139. }
  140. };
  141. expect( normalizeDecorators( entryObject ) ).to.deep.equal( [
  142. {
  143. id: 'linkFoo',
  144. mode: 'manual',
  145. label: 'Foo',
  146. attributes: {
  147. foo: 'foo'
  148. }
  149. },
  150. {
  151. id: 'linkBar',
  152. mode: 'automatic',
  153. callback,
  154. attributes: {
  155. bar: 'bar'
  156. }
  157. },
  158. {
  159. id: 'linkBaz',
  160. mode: 'manual',
  161. label: 'Baz label',
  162. attributes: {
  163. target: '_blank',
  164. rel: 'noopener noreferrer'
  165. }
  166. }
  167. ] );
  168. } );
  169. } );
  170. } );