Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit b9dcb35

Browse files
fix(Angular.js): don't crash on invalid query parameters
1 parent 25d9f5a commit b9dcb35

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/Angular.js

+22-3
Original file line numberDiff line numberDiff line change
@@ -812,17 +812,36 @@ function startingTag(element) {
812812

813813
/////////////////////////////////////////////////
814814

815+
/**
816+
* Tries to decode the URI component without throwing an exception.
817+
*
818+
* @private
819+
* @param str value potential URI component to check.
820+
* @returns {boolean} True if `value` can be decoded
821+
* with the decodeURIComponent function.
822+
*/
823+
function tryDecodeURIComponent(value) {
824+
try {
825+
return decodeURIComponent(value);
826+
} catch(e) {
827+
// Ignore any invalid uri component
828+
}
829+
}
830+
831+
815832
/**
816833
* Parses an escaped url query string into key-value pairs.
817834
* @returns Object.<(string|boolean)>
818835
*/
819836
function parseKeyValue(/**string*/keyValue) {
820837
var obj = {}, key_value, key;
821838
forEach((keyValue || "").split('&'), function(keyValue){
822-
if (keyValue) {
839+
if ( keyValue ) {
823840
key_value = keyValue.split('=');
824-
key = decodeURIComponent(key_value[0]);
825-
obj[key] = isDefined(key_value[1]) ? decodeURIComponent(key_value[1]) : true;
841+
key = tryDecodeURIComponent(key_value[0]);
842+
if ( isDefined(key) ) {
843+
obj[key] = isDefined(key_value[1]) ? tryDecodeURIComponent(key_value[1]) : true;
844+
}
826845
}
827846
});
828847
return obj;

test/AngularSpec.js

+6
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,12 @@ describe('angular', function() {
310310
expect(parseKeyValue('flag1&key=value&flag2')).
311311
toEqual({flag1: true, key: 'value', flag2: true});
312312
});
313+
it('should ignore key values that are not valid URI components', function() {
314+
expect(function() { parseKeyValue('%'); }).not.toThrow();
315+
expect(parseKeyValue('%')).toEqual({});
316+
expect(parseKeyValue('invalid=%')).toEqual({ invalid: undefined });
317+
expect(parseKeyValue('invalid=%&valid=good')).toEqual({ invalid: undefined, valid: 'good' });
318+
});
313319
});
314320

315321
describe('toKeyValue', function() {

0 commit comments

Comments
 (0)