From ed08428638d13ff219eb14a965c8ab5fa0c8c376 Mon Sep 17 00:00:00 2001 From: Joe Pea Date: Sun, 26 Apr 2020 15:46:56 -0700 Subject: [PATCH] feat: also update the client isExternal check --- src/core/fetch/index.js | 35 ++++++++++++----------------------- 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/src/core/fetch/index.js b/src/core/fetch/index.js index 9d2769b8..7f3e951a 100644 --- a/src/core/fetch/index.js +++ b/src/core/fetch/index.js @@ -20,30 +20,19 @@ function loadNested(path, qs, file, next, vm, first) { ).then(next, _ => loadNested(path, qs, file, next, vm)); } +// Borrowed from https://j11y.io/snippets/getting-a-fully-qualified-url. +function qualifyURL(url){ + var img = document.createElement('img'); + img.src = url; // set string url + url = img.src; // get qualified url + img.src = ''; // prevent the server request + return url; +} + function isExternal(url) { - let match = url.match( - /^([^:\/?#]+:)?(?:\/\/([^\/?#]*))?([^?#]+)?(\?[^#]*)?(#.*)?/ - ); - if ( - typeof match[1] === 'string' && - match[1].length > 0 && - match[1].toLowerCase() !== location.protocol - ) { - return true; - } - if ( - typeof match[2] === 'string' && - match[2].length > 0 && - match[2].replace( - new RegExp( - ':(' + { 'http:': 80, 'https:': 443 }[location.protocol] + ')?$' - ), - '' - ) !== location.host - ) { - return true; - } - return false; + url = qualifyURL(url) + url = new URL(url) + return url.origin !== location.origin } export function fetchMixin(proto) {