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

Commit bd8ad0f

Browse files
chirayukbtford
authored andcommitted
fix($parse): disallow passing Function to Array.sort
Fix the following exploit: hasOwnProperty.constructor.prototype.valueOf = valueOf.call; ["a", "alert(1)"].sort(hasOwnProperty.constructor); The exploit: • 1. Array.sort takes a comparison function and passes it 2 parameters to compare. 2. It then calls .valueOf() if the result is not a primitive. • The Function object conveniently accepts two string arguments so we can use this to construct a function. However, this doesn't do much unless we can execute it. • We set the valueOf function on Function.prototype to Function.prototype.call. This causes the function that we constructed to be executed when sort calls .valueOf() on the result of the comparison. The fix is in two parts. • Disallow passing unsafe objects to function calls as parameters. • Do not traverse the Function object when setting a path.
1 parent 6639ca9 commit bd8ad0f

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

src/ng/parse.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ Parser.prototype = {
747747
if (args) {
748748
var i = argsFn.length;
749749
while (i--) {
750-
args[i] = argsFn[i](scope, locals);
750+
args[i] = ensureSafeObject(argsFn[i](scope, locals), expressionText);
751751
}
752752
}
753753

@@ -835,19 +835,19 @@ Parser.prototype = {
835835
//////////////////////////////////////////////////
836836

837837
function setter(obj, path, setValue, fullExp) {
838+
ensureSafeObject(obj, fullExp);
838839

839840
var element = path.split('.'), key;
840841
for (var i = 0; element.length > 1; i++) {
841842
key = ensureSafeMemberName(element.shift(), fullExp);
842-
var propertyObj = obj[key];
843+
var propertyObj = ensureSafeObject(obj[key], fullExp);
843844
if (!propertyObj) {
844845
propertyObj = {};
845846
obj[key] = propertyObj;
846847
}
847848
obj = propertyObj;
848849
}
849850
key = ensureSafeMemberName(element.shift(), fullExp);
850-
ensureSafeObject(obj, fullExp);
851851
ensureSafeObject(obj[key], fullExp);
852852
obj[key] = setValue;
853853
return setValue;

test/ng/parseSpec.js

+37
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,43 @@ describe('parser', function() {
725725
'Expression: a.toString.constructor = 1');
726726
});
727727

728+
it('should disallow traversing the Function object in a setter: E02', function() {
729+
expect(function() {
730+
// This expression by itself isn't dangerous. However, one can use this to
731+
// automatically call an object (e.g. a Function object) when it is automatically
732+
// toString'd/valueOf'd by setting the RHS to Function.prototype.call.
733+
scope.$eval('hasOwnProperty.constructor.prototype.valueOf = 1');
734+
}).toThrowMinErr(
735+
'$parse', 'isecfn', 'Referencing Function in Angular expressions is disallowed! ' +
736+
'Expression: hasOwnProperty.constructor.prototype.valueOf = 1');
737+
});
738+
739+
it('should disallow passing the Function object as a parameter: E03', function() {
740+
expect(function() {
741+
// This expression constructs a function but does not execute it. It does lead the
742+
// way to execute it if one can get the toString/valueOf of it to call the function.
743+
scope.$eval('["a", "alert(1)"].sort(hasOwnProperty.constructor)');
744+
}).toThrow();
745+
});
746+
747+
it('should prevent exploit E01', function() {
748+
// This is a tracking exploit. The two individual tests, it('should … : E02') and
749+
// it('should … : E03') test for two parts to block this exploit. This exploit works
750+
// as follows:
751+
//
752+
// • Array.sort takes a comparison function and passes it 2 parameters to compare. If
753+
// the result is non-primitive, sort then invokes valueOf() on the result.
754+
// • The Function object conveniently accepts two string arguments so we can use this
755+
// to construct a function. However, this doesn't do much unless we can execute it.
756+
// • We set the valueOf property on Function.prototype to Function.prototype.call.
757+
// This causes the function that we constructed to be executed when sort calls
758+
// .valueOf() on the result of the comparison.
759+
expect(function() {
760+
scope.$eval('' +
761+
'hasOwnProperty.constructor.prototype.valueOf=valueOf.call;' +
762+
'["a","alert(1)"].sort(hasOwnProperty.constructor)');
763+
}).toThrow();
764+
});
728765

729766
it('should NOT allow access to Function constructor that has been aliased', function() {
730767
scope.foo = { "bar": Function };

0 commit comments

Comments
 (0)