From b9dcb35e9bc64cb2f48f3a349ead66c501cbdc48 Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Thu, 20 Jun 2013 13:59:46 +0100 Subject: fix(Angular.js): don't crash on invalid query parameters --- src/Angular.js | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/Angular.js b/src/Angular.js index cbfd1693..1e1fac4f 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -812,6 +812,23 @@ function startingTag(element) { ///////////////////////////////////////////////// +/** + * Tries to decode the URI component without throwing an exception. + * + * @private + * @param str value potential URI component to check. + * @returns {boolean} True if `value` can be decoded + * with the decodeURIComponent function. + */ +function tryDecodeURIComponent(value) { + try { + return decodeURIComponent(value); + } catch(e) { + // Ignore any invalid uri component + } +} + + /** * Parses an escaped url query string into key-value pairs. * @returns Object.<(string|boolean)> @@ -819,10 +836,12 @@ function startingTag(element) { function parseKeyValue(/**string*/keyValue) { var obj = {}, key_value, key; forEach((keyValue || "").split('&'), function(keyValue){ - if (keyValue) { + if ( keyValue ) { key_value = keyValue.split('='); - key = decodeURIComponent(key_value[0]); - obj[key] = isDefined(key_value[1]) ? decodeURIComponent(key_value[1]) : true; + key = tryDecodeURIComponent(key_value[0]); + if ( isDefined(key) ) { + obj[key] = isDefined(key_value[1]) ? tryDecodeURIComponent(key_value[1]) : true; + } } }); return obj; -- cgit v1.2.3