From c824f25ea3a1b3cc1e9408086665f9d8ced4dab2 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Tue, 21 Feb 2017 12:32:59 +0100 Subject: [PATCH] MOBILE-2025 core: Catch exceptions in decodeURIComponent --- www/addons/mod/book/services/book.js | 4 +-- www/addons/mod/page/services/page.js | 5 +-- www/addons/mod/resource/services/resource.js | 4 +-- www/addons/mod/wiki/services/handlers.js | 4 +-- www/core/lib/filepool.js | 2 +- www/core/lib/fs.js | 6 ++-- www/core/lib/text.js | 38 +++++++++++++++++++- www/core/lib/util.js | 8 ++--- 8 files changed, 54 insertions(+), 17 deletions(-) diff --git a/www/addons/mod/book/services/book.js b/www/addons/mod/book/services/book.js index a237b482bf3..4afe5974980 100644 --- a/www/addons/mod/book/services/book.js +++ b/www/addons/mod/book/services/book.js @@ -22,7 +22,7 @@ angular.module('mm.addons.mod_book') * @name $mmaModBook */ .factory('$mmaModBook', function($mmFilepool, $mmSite, $mmFS, $http, $log, $q, $mmSitesManager, $mmUtil, mmaModBookComponent, - $mmCourse) { + $mmCourse, $mmText) { $log = $log.getInstance('$mmaModBook'); var self = {}; @@ -278,7 +278,7 @@ angular.module('mm.addons.mod_book') // Remove the chapter folder from the path and add the filename. key = content.filepath.replace('/' + chapter + '/', '') + content.filename; } - map[chapter].paths[decodeURIComponent(key)] = content.fileurl; + map[chapter].paths[$mmText.decodeURIComponent(key)] = content.fileurl; } } } diff --git a/www/addons/mod/page/services/page.js b/www/addons/mod/page/services/page.js index fcdfbc224da..a62778d9f78 100644 --- a/www/addons/mod/page/services/page.js +++ b/www/addons/mod/page/services/page.js @@ -21,7 +21,8 @@ angular.module('mm.addons.mod_page') * @ngdoc service * @name $mmaModPage */ -.factory('$mmaModPage', function($mmFilepool, $mmSite, $mmFS, $http, $log, $q, $mmSitesManager, $mmUtil, mmaModPageComponent) { +.factory('$mmaModPage', function($mmFilepool, $mmSite, $mmFS, $http, $log, $q, $mmSitesManager, $mmUtil, $mmText, + mmaModPageComponent) { $log = $log.getInstance('$mmaModPage'); var self = {}; @@ -55,7 +56,7 @@ angular.module('mm.addons.mod_page') // Add the folders without the leading slash. key = content.filepath.substr(1) + key; } - paths[decodeURIComponent(key)] = url; + paths[$mmText.decodeURIComponent(key)] = url; } }); diff --git a/www/addons/mod/resource/services/resource.js b/www/addons/mod/resource/services/resource.js index 5d017047795..91cd5bafefd 100644 --- a/www/addons/mod/resource/services/resource.js +++ b/www/addons/mod/resource/services/resource.js @@ -22,7 +22,7 @@ angular.module('mm.addons.mod_resource') * @name $mmaModResource */ .factory('$mmaModResource', function($mmFilepool, $mmSite, $mmUtil, $mmFS, $http, $log, $q, $sce, $mmApp, $mmSitesManager, - mmaModResourceComponent, mmCoreNotDownloaded, mmCoreDownloading, mmCoreDownloaded) { + $mmText, mmaModResourceComponent, mmCoreNotDownloaded, mmCoreDownloading, mmCoreDownloaded) { $log = $log.getInstance('$mmaModResource'); var self = {}; @@ -97,7 +97,7 @@ angular.module('mm.addons.mod_resource') indexUrl = url; } else { // Any other file in the resource. - paths[decodeURIComponent(fullpath)] = url; + paths[$mmText.decodeURIComponent(fullpath)] = url; } }); diff --git a/www/addons/mod/wiki/services/handlers.js b/www/addons/mod/wiki/services/handlers.js index 2c610d12100..fde4449c408 100644 --- a/www/addons/mod/wiki/services/handlers.js +++ b/www/addons/mod/wiki/services/handlers.js @@ -23,7 +23,7 @@ angular.module('mm.addons.mod_wiki') */ .factory('$mmaModWikiHandlers', function($mmCourse, $mmaModWiki, $state, $mmContentLinksHelper, $mmCourseHelper, $mmUtil, $q, mmaModWikiComponent, $mmaModWikiPrefetchHandler, mmCoreDownloading, mmCoreNotDownloaded, mmCoreEventPackageStatusChanged, - mmCoreOutdated, $mmCoursePrefetchDelegate, $mmSite, $mmEvents, $mmaModWikiSync, $mmContentLinkHandlerFactory) { + mmCoreOutdated, $mmCoursePrefetchDelegate, $mmSite, $mmEvents, $mmaModWikiSync, $mmContentLinkHandlerFactory, $mmText) { var self = {}; /** @@ -283,7 +283,7 @@ angular.module('mm.addons.mod_wiki') var section = ""; if (typeof params.section != 'undefined') { - section = decodeURIComponent(params.section.replace(/\+/g, ' ')); + section = $mmText.decodeURIComponent(params.section.replace(/\+/g, ' ')); } // Return actions. diff --git a/www/core/lib/filepool.js b/www/core/lib/filepool.js index 80d59322696..df64ec506f5 100644 --- a/www/core/lib/filepool.js +++ b/www/core/lib/filepool.js @@ -1280,7 +1280,7 @@ angular.module('mm.core') filename; // Decode URL. - url = $mmText.decodeHTML(decodeURIComponent(url)); + url = $mmText.decodeHTML($mmText.decodeURIComponent(url)); if (url.indexOf('/webservice/pluginfile') !== -1) { // Remove attributes that do not matter. diff --git a/www/core/lib/fs.js b/www/core/lib/fs.js index 4811ce14c3f..7c600bc7927 100644 --- a/www/core/lib/fs.js +++ b/www/core/lib/fs.js @@ -491,7 +491,7 @@ angular.module('mm.core') * @return {String} The file name normalized. */ self.normalizeFileName = function(filename) { - filename = decodeURIComponent(filename); + filename = $mmText.decodeURIComponent(filename); return filename; }; @@ -1246,7 +1246,7 @@ angular.module('mm.core') number = 1; // Clean the file name. - fileNameWithoutExtension = $mmText.removeSpecialCharactersForFiles(decodeURIComponent(fileNameWithoutExtension)); + fileNameWithoutExtension = $mmText.removeSpecialCharactersForFiles($mmText.decodeURIComponent(fileNameWithoutExtension)); // Index the files by name. angular.forEach(entries, function(entry) { @@ -1276,7 +1276,7 @@ angular.module('mm.core') } }).catch(function() { // Folder doesn't exist, name is unique. Clean it and return it. - return $mmText.removeSpecialCharactersForFiles(decodeURIComponent(fileName)); + return $mmText.removeSpecialCharactersForFiles($mmText.decodeURIComponent(fileName)); }); }; diff --git a/www/core/lib/text.js b/www/core/lib/text.js index 3f4e9d7c724..478e9ad7f2f 100644 --- a/www/core/lib/text.js +++ b/www/core/lib/text.js @@ -618,7 +618,7 @@ angular.module('mm.core') } } return false; - } + }; /** * Strip Unicode long char of a given text. @@ -640,5 +640,41 @@ angular.module('mm.core') return stripped; }; + /** + * Same as Javascript's decodeURI, but if an exception is thrown it will return the original URI. + * + * @module mm.core + * @ngdoc method + * @name $mmText#decodeURI + * @param {String} uri URI to decode. + * @return {String} Decoded URI, or original URI if an exception is thrown. + */ + self.decodeURI = function(uri) { + try { + return decodeURI(uri); + } catch(ex) { + // Error, use the original URI. + } + return uri; + }; + + /** + * Same as Javascript's decodeURIComponent, but if an exception is thrown it will return the original URI. + * + * @module mm.core + * @ngdoc method + * @name $mmText#decodeURIComponent + * @param {String} uri URI to decode. + * @return {String} Decoded URI, or original URI if an exception is thrown. + */ + self.decodeURIComponent = function(uri) { + try { + return decodeURIComponent(uri); + } catch(ex) { + // Error, use the original URI. + } + return uri; + }; + return self; }); diff --git a/www/core/lib/util.js b/www/core/lib/util.js index 2981c3dc952..e261f0f1345 100644 --- a/www/core/lib/util.js +++ b/www/core/lib/util.js @@ -330,7 +330,7 @@ angular.module('mm.core') // with white spaces, special characters... if (path.indexOf(fsRoot > -1)) { path = path.replace(fsRoot, ""); - path = encodeURIComponent(decodeURIComponent(path)); + path = encodeURIComponent($mmText.decodeURIComponent(path)); path = fsRoot + path; } @@ -1468,14 +1468,14 @@ angular.module('mm.core') // Treat elements with src (img, audio, video, ...). media = div[0].querySelectorAll('img, video, audio, source, track'); angular.forEach(media, function(el) { - var src = paths[decodeURIComponent(el.getAttribute('src'))]; + var src = paths[$mmText.decodeURIComponent(el.getAttribute('src'))]; if (typeof src !== 'undefined') { el.setAttribute('src', src); } // Treat video posters. if (el.tagName == 'VIDEO' && el.getAttribute('poster')) { - src = paths[decodeURIComponent(el.getAttribute('poster'))]; + src = paths[$mmText.decodeURIComponent(el.getAttribute('poster'))]; if (typeof src !== 'undefined') { el.setAttribute('poster', src); } @@ -1484,7 +1484,7 @@ angular.module('mm.core') // We do the same for links. angular.forEach(div.find('a'), function(anchor) { - var href = decodeURIComponent(anchor.getAttribute('href')), + var href = $mmText.decodeURIComponent(anchor.getAttribute('href')), url = paths[href]; if (typeof url !== 'undefined') {