@@ -12,6 +12,7 @@ import {HttpHeaders} from '../src/headers';
1212import {
1313 JSONP_ERR_HEADERS_NOT_SUPPORTED ,
1414 JSONP_ERR_NO_CALLBACK ,
15+ JSONP_ERR_UNSAFE_URL ,
1516 JSONP_ERR_WRONG_METHOD ,
1617 JSONP_ERR_WRONG_RESPONSE_TYPE ,
1718 JsonpCallbackContext ,
@@ -25,7 +26,7 @@ import {toArray} from 'rxjs/operators';
2526import { MockDocument } from './jsonp_mock' ;
2627
2728describe ( 'JsonpClientBackend' , ( ) => {
28- const SAMPLE_REQ = new HttpRequest < never > ( 'JSONP' , '/test' ) ;
29+ const SAMPLE_REQ = new HttpRequest < never > ( 'JSONP' , 'https://example.com /test' ) ;
2930 let home : any ;
3031 let document : MockDocument ;
3132 let backend : JsonpClientBackend ;
@@ -127,6 +128,44 @@ describe('JsonpClientBackend', () => {
127128 } ) ;
128129 } ) ;
129130
131+ describe ( 'URL protocols' , ( ) => {
132+ it ( 'allows absolute HTTP(S) URLs' , ( ) => {
133+ const urls = [
134+ 'http://example.com/test' ,
135+ 'https://example.com/test' ,
136+ 'HTTP://example.com/test' ,
137+ ] ;
138+
139+ for ( const url of urls ) {
140+ const subscription = backend . handle ( SAMPLE_REQ . clone < never > ( { url} ) ) . subscribe ( ) ;
141+
142+ subscription . unsubscribe ( ) ;
143+ }
144+ } ) ;
145+
146+ it ( 'rejects URLs without absolute HTTP(S) protocols before creating a script element' , ( ) => {
147+ const urls = [
148+ '//example.com/test' ,
149+ '/test' ,
150+ 'test' ,
151+ 'data:text/javascript,alert(1)' ,
152+ 'blob:https://example.com/jsonp' ,
153+ 'javascript:alert(1)' ,
154+ 'file:///tmp/jsonp.js' ,
155+ 'filesystem:https://example.com/temporary/jsonp.js' ,
156+ 'ftp://example.com/jsonp.js' ,
157+ 'custom-scheme://example.com/jsonp.js' ,
158+ ] ;
159+
160+ for ( const url of urls ) {
161+ expect ( ( ) => backend . handle ( SAMPLE_REQ . clone < never > ( { url} ) ) ) . toThrowError (
162+ `NG02826: ${ JSONP_ERR_UNSAFE_URL } ` ,
163+ ) ;
164+ expect ( document . mock ) . toBeUndefined ( ) ;
165+ }
166+ } ) ;
167+ } ) ;
168+
130169 describe ( 'throws an error' , ( ) => {
131170 it ( 'when request method is not JSONP' , ( ) =>
132171 expect ( ( ) => backend . handle ( SAMPLE_REQ . clone < never > ( { method : 'GET' } ) ) ) . toThrowError (
0 commit comments