@ -332,30 +332,42 @@ function isSameOrigin(baseUrl, otherUrl) {
@@ -332,30 +332,42 @@ function isSameOrigin(baseUrl, otherUrl) {
return base . origin === other . origin ;
}
// Validates if URL is safe and allowed , e.g. to avoid XSS.
function isValidUr l ( url , allowRelative ) {
if ( ! url || typeof url !== 'string' ) {
// Checks if URLs use one of the whitelisted protocols , e.g. to avoid XSS.
function isValidProtoco l ( url ) {
if ( ! url ) {
return false ;
}
// RFC 3986 (http://tools.ietf.org/html/rfc3986#section-3.1)
// scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
var protocol = /^[a-z][a-z0-9+\-.]*(?=:)/i . exec ( url ) ;
if ( ! protocol ) {
return allowRelative ;
}
protocol = protocol [ 0 ] . toLowerCase ( ) ;
switch ( protocol ) {
case 'http' :
case 'https' :
case 'ftp' :
case 'mailto' :
case 'tel' :
switch ( url . protocol ) {
case 'http:' :
case 'https:' :
case 'ftp:' :
case 'mailto:' :
case 'tel:' :
return true ;
default :
return false ;
}
}
/ * *
* Attempts to create a valid absolute URL ( utilizing ` isValidProtocol ` ) .
* @ param { URL | string } url - An absolute , or relative , URL .
* @ param { URL | string } baseUrl - An absolute URL .
* @ returns Either a valid { URL } , or ` null ` otherwise .
* /
function createValidAbsoluteUrl ( url , baseUrl ) {
if ( ! url ) {
return null ;
}
try {
var absoluteUrl = baseUrl ? new URL ( url , baseUrl ) : new URL ( url ) ;
if ( isValidProtocol ( absoluteUrl ) ) {
return absoluteUrl ;
}
} catch ( ex ) { /* `new URL()` will throw on incorrect data. */ }
return null ;
}
function shadow ( obj , prop , value ) {
Object . defineProperty ( obj , prop , { value : value ,
enumerable : true ,
@ -2431,7 +2443,7 @@ exports.isNum = isNum;
@@ -2431,7 +2443,7 @@ exports.isNum = isNum;
exports . isString = isString ;
exports . isSpace = isSpace ;
exports . isSameOrigin = isSameOrigin ;
exports . isValidUrl = isValid Url;
exports . createValidAbsoluteUrl = createValidAbsolute Url;
exports . isLittleEndian = isLittleEndian ;
exports . isEvalSupported = isEvalSupported ;
exports . loadJpegStream = loadJpegStream ;