Skip to content

Commit 76e5a22

Browse files
authored
Merge pull request #919 from dpalou/MOBILE-2025
MOBILE-2025 core: Catch exceptions in decodeURIComponent
2 parents 7ce480d + c824f25 commit 76e5a22

File tree

8 files changed

+54
-17
lines changed

8 files changed

+54
-17
lines changed

www/addons/mod/book/services/book.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ angular.module('mm.addons.mod_book')
2222
* @name $mmaModBook
2323
*/
2424
.factory('$mmaModBook', function($mmFilepool, $mmSite, $mmFS, $http, $log, $q, $mmSitesManager, $mmUtil, mmaModBookComponent,
25-
$mmCourse) {
25+
$mmCourse, $mmText) {
2626
$log = $log.getInstance('$mmaModBook');
2727

2828
var self = {};
@@ -278,7 +278,7 @@ angular.module('mm.addons.mod_book')
278278
// Remove the chapter folder from the path and add the filename.
279279
key = content.filepath.replace('/' + chapter + '/', '') + content.filename;
280280
}
281-
map[chapter].paths[decodeURIComponent(key)] = content.fileurl;
281+
map[chapter].paths[$mmText.decodeURIComponent(key)] = content.fileurl;
282282
}
283283
}
284284
}

www/addons/mod/page/services/page.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ angular.module('mm.addons.mod_page')
2121
* @ngdoc service
2222
* @name $mmaModPage
2323
*/
24-
.factory('$mmaModPage', function($mmFilepool, $mmSite, $mmFS, $http, $log, $q, $mmSitesManager, $mmUtil, mmaModPageComponent) {
24+
.factory('$mmaModPage', function($mmFilepool, $mmSite, $mmFS, $http, $log, $q, $mmSitesManager, $mmUtil, $mmText,
25+
mmaModPageComponent) {
2526
$log = $log.getInstance('$mmaModPage');
2627

2728
var self = {};
@@ -55,7 +56,7 @@ angular.module('mm.addons.mod_page')
5556
// Add the folders without the leading slash.
5657
key = content.filepath.substr(1) + key;
5758
}
58-
paths[decodeURIComponent(key)] = url;
59+
paths[$mmText.decodeURIComponent(key)] = url;
5960
}
6061
});
6162

www/addons/mod/resource/services/resource.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ angular.module('mm.addons.mod_resource')
2222
* @name $mmaModResource
2323
*/
2424
.factory('$mmaModResource', function($mmFilepool, $mmSite, $mmUtil, $mmFS, $http, $log, $q, $sce, $mmApp, $mmSitesManager,
25-
mmaModResourceComponent, mmCoreNotDownloaded, mmCoreDownloading, mmCoreDownloaded) {
25+
$mmText, mmaModResourceComponent, mmCoreNotDownloaded, mmCoreDownloading, mmCoreDownloaded) {
2626
$log = $log.getInstance('$mmaModResource');
2727

2828
var self = {};
@@ -97,7 +97,7 @@ angular.module('mm.addons.mod_resource')
9797
indexUrl = url;
9898
} else {
9999
// Any other file in the resource.
100-
paths[decodeURIComponent(fullpath)] = url;
100+
paths[$mmText.decodeURIComponent(fullpath)] = url;
101101
}
102102
});
103103

www/addons/mod/wiki/services/handlers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ angular.module('mm.addons.mod_wiki')
2323
*/
2424
.factory('$mmaModWikiHandlers', function($mmCourse, $mmaModWiki, $state, $mmContentLinksHelper, $mmCourseHelper, $mmUtil, $q,
2525
mmaModWikiComponent, $mmaModWikiPrefetchHandler, mmCoreDownloading, mmCoreNotDownloaded, mmCoreEventPackageStatusChanged,
26-
mmCoreOutdated, $mmCoursePrefetchDelegate, $mmSite, $mmEvents, $mmaModWikiSync, $mmContentLinkHandlerFactory) {
26+
mmCoreOutdated, $mmCoursePrefetchDelegate, $mmSite, $mmEvents, $mmaModWikiSync, $mmContentLinkHandlerFactory, $mmText) {
2727
var self = {};
2828

2929
/**
@@ -283,7 +283,7 @@ angular.module('mm.addons.mod_wiki')
283283

284284
var section = "";
285285
if (typeof params.section != 'undefined') {
286-
section = decodeURIComponent(params.section.replace(/\+/g, ' '));
286+
section = $mmText.decodeURIComponent(params.section.replace(/\+/g, ' '));
287287
}
288288

289289
// Return actions.

www/core/lib/filepool.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1280,7 +1280,7 @@ angular.module('mm.core')
12801280
filename;
12811281

12821282
// Decode URL.
1283-
url = $mmText.decodeHTML(decodeURIComponent(url));
1283+
url = $mmText.decodeHTML($mmText.decodeURIComponent(url));
12841284

12851285
if (url.indexOf('/webservice/pluginfile') !== -1) {
12861286
// Remove attributes that do not matter.

www/core/lib/fs.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ angular.module('mm.core')
491491
* @return {String} The file name normalized.
492492
*/
493493
self.normalizeFileName = function(filename) {
494-
filename = decodeURIComponent(filename);
494+
filename = $mmText.decodeURIComponent(filename);
495495
return filename;
496496
};
497497

@@ -1246,7 +1246,7 @@ angular.module('mm.core')
12461246
number = 1;
12471247

12481248
// Clean the file name.
1249-
fileNameWithoutExtension = $mmText.removeSpecialCharactersForFiles(decodeURIComponent(fileNameWithoutExtension));
1249+
fileNameWithoutExtension = $mmText.removeSpecialCharactersForFiles($mmText.decodeURIComponent(fileNameWithoutExtension));
12501250

12511251
// Index the files by name.
12521252
angular.forEach(entries, function(entry) {
@@ -1276,7 +1276,7 @@ angular.module('mm.core')
12761276
}
12771277
}).catch(function() {
12781278
// Folder doesn't exist, name is unique. Clean it and return it.
1279-
return $mmText.removeSpecialCharactersForFiles(decodeURIComponent(fileName));
1279+
return $mmText.removeSpecialCharactersForFiles($mmText.decodeURIComponent(fileName));
12801280
});
12811281
};
12821282

www/core/lib/text.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ angular.module('mm.core')
618618
}
619619
}
620620
return false;
621-
}
621+
};
622622

623623
/**
624624
* Strip Unicode long char of a given text.
@@ -640,5 +640,41 @@ angular.module('mm.core')
640640
return stripped;
641641
};
642642

643+
/**
644+
* Same as Javascript's decodeURI, but if an exception is thrown it will return the original URI.
645+
*
646+
* @module mm.core
647+
* @ngdoc method
648+
* @name $mmText#decodeURI
649+
* @param {String} uri URI to decode.
650+
* @return {String} Decoded URI, or original URI if an exception is thrown.
651+
*/
652+
self.decodeURI = function(uri) {
653+
try {
654+
return decodeURI(uri);
655+
} catch(ex) {
656+
// Error, use the original URI.
657+
}
658+
return uri;
659+
};
660+
661+
/**
662+
* Same as Javascript's decodeURIComponent, but if an exception is thrown it will return the original URI.
663+
*
664+
* @module mm.core
665+
* @ngdoc method
666+
* @name $mmText#decodeURIComponent
667+
* @param {String} uri URI to decode.
668+
* @return {String} Decoded URI, or original URI if an exception is thrown.
669+
*/
670+
self.decodeURIComponent = function(uri) {
671+
try {
672+
return decodeURIComponent(uri);
673+
} catch(ex) {
674+
// Error, use the original URI.
675+
}
676+
return uri;
677+
};
678+
643679
return self;
644680
});

www/core/lib/util.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ angular.module('mm.core')
330330
// with white spaces, special characters...
331331
if (path.indexOf(fsRoot > -1)) {
332332
path = path.replace(fsRoot, "");
333-
path = encodeURIComponent(decodeURIComponent(path));
333+
path = encodeURIComponent($mmText.decodeURIComponent(path));
334334
path = fsRoot + path;
335335
}
336336

@@ -1468,14 +1468,14 @@ angular.module('mm.core')
14681468
// Treat elements with src (img, audio, video, ...).
14691469
media = div[0].querySelectorAll('img, video, audio, source, track');
14701470
angular.forEach(media, function(el) {
1471-
var src = paths[decodeURIComponent(el.getAttribute('src'))];
1471+
var src = paths[$mmText.decodeURIComponent(el.getAttribute('src'))];
14721472
if (typeof src !== 'undefined') {
14731473
el.setAttribute('src', src);
14741474
}
14751475

14761476
// Treat video posters.
14771477
if (el.tagName == 'VIDEO' && el.getAttribute('poster')) {
1478-
src = paths[decodeURIComponent(el.getAttribute('poster'))];
1478+
src = paths[$mmText.decodeURIComponent(el.getAttribute('poster'))];
14791479
if (typeof src !== 'undefined') {
14801480
el.setAttribute('poster', src);
14811481
}
@@ -1484,7 +1484,7 @@ angular.module('mm.core')
14841484

14851485
// We do the same for links.
14861486
angular.forEach(div.find('a'), function(anchor) {
1487-
var href = decodeURIComponent(anchor.getAttribute('href')),
1487+
var href = $mmText.decodeURIComponent(anchor.getAttribute('href')),
14881488
url = paths[href];
14891489

14901490
if (typeof url !== 'undefined') {

0 commit comments

Comments
 (0)