From 413ef2fbc8280e70a5880880e33a24104b3c0857 Mon Sep 17 00:00:00 2001 From: Alexander Marks Date: Tue, 7 May 2019 11:37:53 -0700 Subject: [PATCH 1/4] Upstreaming cl/245273850 --- test/unit/resolveurl.html | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test/unit/resolveurl.html b/test/unit/resolveurl.html index 26d708b682..1794c28725 100644 --- a/test/unit/resolveurl.html +++ b/test/unit/resolveurl.html @@ -170,6 +170,38 @@ assert.equal(actual, expected); }); + test('resolveUrl when called with relative url and a bad baseURI', function () { + const el = document.querySelector('x-resolve'); + const expected = 'relative/path.png'; + const actual = + el.resolveUrl('relative/path.png', '/not/a/full/uri'); + assert.equal(actual, expected); + }); + + test('resolveUrl when called with a full url and a bad baseURI', function () { + const el = document.querySelector('x-resolve'); + const expected = 'https://example.com/foo.png'; + const actual = + el.resolveUrl('https://example.com/foo.png', '/not/a/full/uri'); + assert.equal(actual, expected); + }); + + test('resolveUrl when called with a protocol-relative url and a bad baseURI', function () { + const el = document.querySelector('x-resolve'); + const expected = '//example.com/foo.png'; + const actual = + el.resolveUrl('//example.com/foo.png', '/not/a/full/uri'); + assert.equal(actual, expected); + }); + + test('resolveUrl when called with a garbage url', function () { + const el = document.querySelector('x-resolve'); + const expected = '.../foo.png'; + const actual = + el.resolveUrl('.../foo.png', 'https://example.org/bar'); + assert.equal(actual, expected); + }); + test('resolveUrl api with assetpath', function() { var el = document.createElement('p-r-ap'); // Manually calculate expected URL, to avoid dependence on From 940b3cdc21ce3329085c94af243f45fcf3a8d1d9 Mon Sep 17 00:00:00 2001 From: Alexander Marks Date: Tue, 7 May 2019 11:45:20 -0700 Subject: [PATCH 2/4] Add URL try/catch --- lib/utils/resolve-url.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/utils/resolve-url.js b/lib/utils/resolve-url.js index ae5e5e2ad5..fa98841801 100644 --- a/lib/utils/resolve-url.js +++ b/lib/utils/resolve-url.js @@ -28,9 +28,6 @@ export function resolveUrl(url, baseURI) { if (url && ABS_URL.test(url)) { return url; } - if (url === '//') { - return url; - } // Lazy feature detection. if (workingURL === undefined) { workingURL = false; @@ -46,7 +43,12 @@ export function resolveUrl(url, baseURI) { baseURI = document.baseURI || window.location.href; } if (workingURL) { - return (new URL(url, baseURI)).href; + try { + return (new URL(url, baseURI)).href; + } catch (e) { + // Bad url or baseURI structure. Do not attempt to resolve. + return url; + } } // Fallback to creating an anchor into a disconnected document. if (!resolveDoc) { From 1f080595a55f977b9096c9f386044a8aa655fa03 Mon Sep 17 00:00:00 2001 From: Alexander Marks Date: Tue, 7 May 2019 13:26:14 -0700 Subject: [PATCH 3/4] Remove unneccessary test --- test/unit/resolveurl.html | 8 -------- 1 file changed, 8 deletions(-) diff --git a/test/unit/resolveurl.html b/test/unit/resolveurl.html index 1794c28725..2ce6c8b27f 100644 --- a/test/unit/resolveurl.html +++ b/test/unit/resolveurl.html @@ -194,14 +194,6 @@ assert.equal(actual, expected); }); - test('resolveUrl when called with a garbage url', function () { - const el = document.querySelector('x-resolve'); - const expected = '.../foo.png'; - const actual = - el.resolveUrl('.../foo.png', 'https://example.org/bar'); - assert.equal(actual, expected); - }); - test('resolveUrl api with assetpath', function() { var el = document.createElement('p-r-ap'); // Manually calculate expected URL, to avoid dependence on From 3db56085062d71a74633a48aa40eec87e7d09d23 Mon Sep 17 00:00:00 2001 From: Alexander Marks Date: Wed, 8 May 2019 16:41:37 -0700 Subject: [PATCH 4/4] Add check for // --- lib/utils/resolve-url.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/utils/resolve-url.js b/lib/utils/resolve-url.js index fa98841801..5dd89cbcf2 100644 --- a/lib/utils/resolve-url.js +++ b/lib/utils/resolve-url.js @@ -28,6 +28,9 @@ export function resolveUrl(url, baseURI) { if (url && ABS_URL.test(url)) { return url; } + if (url === '//') { + return url; + } // Lazy feature detection. if (workingURL === undefined) { workingURL = false;