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

Commit 8264d08

Browse files
fix(Angular.js): don't crash on invalid query parameters
1 parent a790813 commit 8264d08

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
@@ -821,17 +821,36 @@ function startingTag(element) {
821821

822822
/////////////////////////////////////////////////
823823

824+
/**
825+
* Tries to decode the URI component without throwing an exception.
826+
*
827+
* @private
828+
* @param str value potential URI component to check.
829+
* @returns {boolean} True if `value` can be decoded
830+
* with the decodeURIComponent function.
831+
*/
832+
function tryDecodeURIComponent(value) {
833+
try {
834+
return decodeURIComponent(value);
835+
} catch(e) {
836+
// Ignore any invalid uri component
837+
}
838+
}
839+
840+
824841
/**
825842
* Parses an escaped url query string into key-value pairs.
826843
* @returns Object.<(string|boolean)>
827844
*/
828845
function parseKeyValue(/**string*/keyValue) {
829846
var obj = {}, key_value, key;
830847
forEach((keyValue || "").split('&'), function(keyValue){
831-
if (keyValue) {
848+
if ( keyValue ) {
832849
key_value = keyValue.split('=');
833-
key = decodeURIComponent(key_value[0]);
834-
obj[key] = isDefined(key_value[1]) ? decodeURIComponent(key_value[1]) : true;
850+
key = tryDecodeURIComponent(key_value[0]);
851+
if ( isDefined(key) ) {
852+
obj[key] = isDefined(key_value[1]) ? tryDecodeURIComponent(key_value[1]) : true;
853+
}
835854
}
836855
});
837856
return obj;

test/AngularSpec.js

+6
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,12 @@ describe('angular', function() {
312312
expect(parseKeyValue('flag1&key=value&flag2')).
313313
toEqual({flag1: true, key: 'value', flag2: true});
314314
});
315+
it('should ignore key values that are not valid URI components', function() {
316+
expect(function() { parseKeyValue('%'); }).not.toThrow();
317+
expect(parseKeyValue('%')).toEqual({});
318+
expect(parseKeyValue('invalid=%')).toEqual({ invalid: undefined });
319+
expect(parseKeyValue('invalid=%&valid=good')).toEqual({ invalid: undefined, valid: 'good' });
320+
});
315321
});
316322

317323
describe('toKeyValue', function() {

0 commit comments

Comments
 (0)