| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- /**
- * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
- */
- import ViewDocument from '@ckeditor/ckeditor5-engine/src/view/document';
- import ViewDowncastWriter from '@ckeditor/ckeditor5-engine/src/view/downcastwriter';
- import AttributeElement from '@ckeditor/ckeditor5-engine/src/view/attributeelement';
- import ContainerElement from '@ckeditor/ckeditor5-engine/src/view/containerelement';
- import Text from '@ckeditor/ckeditor5-engine/src/view/text';
- import { createLinkElement, isLinkElement, ensureSafeUrl } from '../src/utils';
- describe( 'utils', () => {
- describe( 'isLinkElement()', () => {
- it( 'should return true for elements created by createLinkElement', () => {
- const element = createLinkElement( 'http://ckeditor.com', new ViewDowncastWriter( new ViewDocument() ) );
- expect( isLinkElement( element ) ).to.be.true;
- } );
- it( 'should return false for other AttributeElements', () => {
- expect( isLinkElement( new AttributeElement( 'a' ) ) ).to.be.false;
- } );
- it( 'should return false for ContainerElements', () => {
- expect( isLinkElement( new ContainerElement( 'p' ) ) ).to.be.false;
- } );
- it( 'should return false for text nodes', () => {
- expect( isLinkElement( new Text( 'foo' ) ) ).to.be.false;
- } );
- } );
- describe( 'createLinkElement()', () => {
- it( 'should create link AttributeElement', () => {
- const element = createLinkElement( 'http://cksource.com', new ViewDowncastWriter( new ViewDocument() ) );
- expect( isLinkElement( element ) ).to.be.true;
- expect( element.priority ).to.equal( 5 );
- expect( element.getAttribute( 'href' ) ).to.equal( 'http://cksource.com' );
- expect( element.name ).to.equal( 'a' );
- } );
- } );
- describe( 'ensureSafeUrl()', () => {
- it( 'returns the same absolute http URL', () => {
- const url = 'http://xx.yy/zz#foo';
- expect( ensureSafeUrl( url ) ).to.equal( url );
- } );
- it( 'returns the same absolute https URL', () => {
- const url = 'https://xx.yy/zz';
- expect( ensureSafeUrl( url ) ).to.equal( url );
- } );
- it( 'returns the same absolute ftp URL', () => {
- const url = 'ftp://xx.yy/zz';
- expect( ensureSafeUrl( url ) ).to.equal( url );
- } );
- it( 'returns the same absolute ftps URL', () => {
- const url = 'ftps://xx.yy/zz';
- expect( ensureSafeUrl( url ) ).to.equal( url );
- } );
- it( 'returns the same absolute mailto URL', () => {
- const url = 'mailto://foo@bar.com';
- expect( ensureSafeUrl( url ) ).to.equal( url );
- } );
- it( 'returns the same relative URL (starting with a dot)', () => {
- const url = './xx/yyy';
- expect( ensureSafeUrl( url ) ).to.equal( url );
- } );
- it( 'returns the same relative URL (starting with two dots)', () => {
- const url = '../../xx/yyy';
- expect( ensureSafeUrl( url ) ).to.equal( url );
- } );
- it( 'returns the same relative URL (starting with a slash)', () => {
- const url = '/xx/yyy';
- expect( ensureSafeUrl( url ) ).to.equal( url );
- } );
- it( 'returns the same relative URL (starting with a backslash)', () => {
- const url = '\\xx\\yyy';
- expect( ensureSafeUrl( url ) ).to.equal( url );
- } );
- it( 'returns the same relative URL (starting with a letter)', () => {
- const url = 'xx/yyy';
- expect( ensureSafeUrl( url ) ).to.equal( url );
- } );
- it( 'returns the same URL even if it contains whitespaces', () => {
- const url = ' ./xx/ yyy\t';
- expect( ensureSafeUrl( url ) ).to.equal( url );
- } );
- it( 'returns the same URL even if it contains non ASCII characters', () => {
- const url = 'https://kłącze.yy/źdźbło';
- expect( ensureSafeUrl( url ) ).to.equal( url );
- } );
- it( 'accepts non string values', () => {
- expect( ensureSafeUrl( undefined ) ).to.equal( 'undefined' );
- expect( ensureSafeUrl( null ) ).to.equal( 'null' );
- } );
- it( 'returns safe URL when a malicious URL starts with javascript:', () => {
- const url = 'javascript:alert(1)';
- expect( ensureSafeUrl( url ) ).to.equal( '#' );
- } );
- it( 'returns safe URL when a malicious URL starts with an unknown protocol', () => {
- const url = 'foo:alert(1)';
- expect( ensureSafeUrl( url ) ).to.equal( '#' );
- } );
- it( 'returns safe URL when a malicious URL contains spaces', () => {
- const url = 'java\u0000script:\talert(1)';
- expect( ensureSafeUrl( url ) ).to.equal( '#' );
- } );
- it( 'returns safe URL when a malicious URL contains spaces (2)', () => {
- const url = '\u0000 javascript:alert(1)';
- expect( ensureSafeUrl( url ) ).to.equal( '#' );
- } );
- it( 'returns safe URL when a malicious URL contains a safe part', () => {
- const url = 'javascript:alert(1)\nhttp://xxx';
- expect( ensureSafeUrl( url ) ).to.equal( '#' );
- } );
- it( 'returns safe URL when a malicious URL contains a safe part (2)', () => {
- const url = 'javascript:alert(1);http://xxx';
- expect( ensureSafeUrl( url ) ).to.equal( '#' );
- } );
- } );
- } );
|