aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPete Bacon Darwin2013-06-20 13:59:46 +0100
committerPete Bacon Darwin2013-06-20 14:32:05 +0100
commitb9dcb35e9bc64cb2f48f3a349ead66c501cbdc48 (patch)
treee45f21f034d6a3df392e178cfc1da1b72f66d133
parent25d9f5a804b7a6a61db6e84e594b1b5fe7ea14bf (diff)
downloadangular.js-b9dcb35e9bc64cb2f48f3a349ead66c501cbdc48.tar.bz2
fix(Angular.js): don't crash on invalid query parameters
-rw-r--r--src/Angular.js25
-rw-r--r--test/AngularSpec.js6
2 files changed, 28 insertions, 3 deletions
diff --git a/src/Angular.js b/src/Angular.js
index cbfd1693..1e1fac4f 100644
--- a/src/Angular.js
+++ b/src/Angular.js
@@ -813,16 +813,35 @@ 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)>
*/
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;
diff --git a/test/AngularSpec.js b/test/AngularSpec.js
index db7ac785..febc0226 100644
--- a/test/AngularSpec.js
+++ b/test/AngularSpec.js
@@ -310,6 +310,12 @@ describe('angular', function() {
expect(parseKeyValue('flag1&key=value&flag2')).
toEqual({flag1: true, key: 'value', flag2: true});
});
+ it('should ignore key values that are not valid URI components', function() {
+ expect(function() { parseKeyValue('%'); }).not.toThrow();
+ expect(parseKeyValue('%')).toEqual({});
+ expect(parseKeyValue('invalid=%')).toEqual({ invalid: undefined });
+ expect(parseKeyValue('invalid=%&valid=good')).toEqual({ invalid: undefined, valid: 'good' });
+ });
});
describe('toKeyValue', function() {