From 0a11dec262a1122cf4253a30f414fc85bf532a7f Mon Sep 17 00:00:00 2001 From: s0ph1e Date: Mon, 16 Oct 2017 21:53:12 +0300 Subject: [PATCH 1/2] Fix malformed url issue, closes #251 --- lib/utils/index.js | 25 +++++++++++++++---------- test/unit/utils/utils-test.js | 9 ++++++++- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/lib/utils/index.js b/lib/utils/index.js index f3cb366c..152d0e22 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -19,9 +19,9 @@ function isUrl (path) { } function getUrl (currentUrl, path) { - var pathObject = url.parse(path); + const pathObject = url.parse(path); if (isUrl(path) && !pathObject.protocol) { - var urlObject = url.parse(currentUrl); + const urlObject = url.parse(currentUrl); pathObject.protocol = urlObject.protocol; path = url.format(pathObject); } @@ -33,8 +33,8 @@ function getUnixPath (filepath) { } function getRelativePath (path1, path2) { - var dirname = path.dirname(path1); - var relativePath = path.relative(dirname, path2); + const dirname = path.dirname(path1); + const relativePath = path.relative(dirname, path2); return getUnixPath(relativePath); } @@ -45,7 +45,7 @@ function getRelativePath (path1, path2) { * @returns {string} decoded pathname */ function getPathnameFromUrl (u) { - var pathname = url.parse(u).pathname; + const pathname = url.parse(u).pathname; return decodeURI(pathname); } @@ -96,8 +96,8 @@ function getFilenameExtension (filepath) { function shortenFilename (filename) { if (filename.length >= MAX_FILENAME_LENGTH) { - var shortFilename = filename.substring(0, 20) + getFilenameExtension(filename); - logger.debug('shorten filename: ' + filename + ' -> ' + shortFilename); + const shortFilename = filename.substring(0, 20) + getFilenameExtension(filename); + logger.debug(`[utils] shorten filename: ${filename} -> ${shortFilename}`); return shortFilename; } return filename; @@ -110,7 +110,12 @@ function waitAllFulfilled (promises) { } function normalizeUrl (u, opts) { - return normalize(u, extend({removeTrailingSlash: false}, opts)); + try { + return normalize(u, extend({removeTrailingSlash: false}, opts)); + } catch (e) { + logger.debug(`[utils] can't normalize url ${u} because of ${e}`); + return u; + } } function urlsEqual (url1, url2) { @@ -118,7 +123,7 @@ function urlsEqual (url1, url2) { } function isUriSchemaSupported (path) { - var protocol = url.parse(path).protocol; + const protocol = url.parse(path).protocol; return !protocol || protocol && isUrl(path); } @@ -127,7 +132,7 @@ function getTypeByMime (mimeType) { } function getTypeByFilename (filename) { - var ext = getFilenameExtension(filename); + const ext = getFilenameExtension(filename); return typeByExt[ext]; } diff --git a/test/unit/utils/utils-test.js b/test/unit/utils/utils-test.js index d5c40520..53599411 100644 --- a/test/unit/utils/utils-test.js +++ b/test/unit/utils/utils-test.js @@ -225,5 +225,12 @@ describe('Utils', function () { it('should return false for /path and /path/', function() { should(utils.urlsEqual('http://example.com/path', 'http://example.com/path/')).be.eql(false); }); - }) + }); + + describe('#normalizeUrl', () => { + it('should return original url if it is malformed', () => { + const malformedUrl = 'http://example.com/%%IMAGEURL%%/bar1q2blitz.png'; + should(utils.normalizeUrl(malformedUrl)).be.eql(malformedUrl); + }); + }); }); From e94be2e76932563cac27e0a249b4b67b34381d3e Mon Sep 17 00:00:00 2001 From: s0ph1e Date: Mon, 16 Oct 2017 22:35:45 +0300 Subject: [PATCH 2/2] Update getting filepath from malformed url --- lib/utils/index.js | 7 +++++-- test/unit/utils/utils-test.js | 3 +++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/utils/index.js b/lib/utils/index.js index 152d0e22..8c7015c5 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -46,7 +46,11 @@ function getRelativePath (path1, path2) { */ function getPathnameFromUrl (u) { const pathname = url.parse(u).pathname; - return decodeURI(pathname); + try { + return decodeURI(pathname); + } catch (e) { + return pathname; + } } /** @@ -113,7 +117,6 @@ function normalizeUrl (u, opts) { try { return normalize(u, extend({removeTrailingSlash: false}, opts)); } catch (e) { - logger.debug(`[utils] can't normalize url ${u} because of ${e}`); return u; } } diff --git a/test/unit/utils/utils-test.js b/test/unit/utils/utils-test.js index 53599411..6a49c964 100644 --- a/test/unit/utils/utils-test.js +++ b/test/unit/utils/utils-test.js @@ -91,6 +91,9 @@ describe('Utils', function () { it('should decode escaped chars', function () { utils.getFilepathFromUrl('https://example.co/logo/logo-mobile%20(1).svg?q=650').should.equal('logo/logo-mobile (1).svg'); }); + it('should return path as is if url is malformed', () => { + utils.getFilepathFromUrl('https://example.co/%%IMAGE%%/logo.png').should.equal('%%IMAGE%%/logo.png'); + }); }); describe('#getHashFromUrl', function () {