Skip to content

Commit c0a08e9

Browse files
committed
fix($parse): allow assignment on objects in locals
Fixes angular#4664
1 parent d21dff2 commit c0a08e9

File tree

3 files changed

+67
-10
lines changed

3 files changed

+67
-10
lines changed

src/ng/directive/form.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -489,19 +489,19 @@ var formDirectiveFactory = function(isNgForm) {
489489
alias = controller.$name;
490490

491491
if (alias) {
492-
setter(scope, alias, controller, alias);
492+
setter(scope, null, alias, controller, alias);
493493
attr.$observe(attr.name ? 'name' : 'ngForm', function(newValue) {
494494
if (alias === newValue) return;
495-
setter(scope, alias, undefined, alias);
495+
setter(scope, null, alias, undefined, alias);
496496
alias = newValue;
497-
setter(scope, alias, controller, alias);
497+
setter(scope, null, alias, controller, alias);
498498
parentFormCtrl.$$renameControl(controller, alias);
499499
});
500500
}
501501
formElement.on('$destroy', function() {
502502
parentFormCtrl.$removeControl(controller);
503503
if (alias) {
504-
setter(scope, alias, undefined, alias);
504+
setter(scope, null, alias, undefined, alias);
505505
}
506506
extend(controller, nullFormCtrl); //stop propagating child destruction handlers upwards
507507
});

src/ng/parse.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ Parser.prototype = {
664664
assign: function(scope, value, locals) {
665665
var o = object(scope, locals);
666666
if (!o) object.assign(scope, o = {});
667-
return setter(o, field, value, expression);
667+
return setter(o, null, field, value, expression);
668668
}
669669
});
670670
},
@@ -799,18 +799,19 @@ Parser.prototype = {
799799
// Parser helper functions
800800
//////////////////////////////////////////////////
801801

802-
function setter(obj, path, setValue, fullExp) {
802+
function setter(obj, locals, path, setValue, fullExp) {
803803
ensureSafeObject(obj, fullExp);
804+
ensureSafeObject(locals, fullExp);
804805

805806
var element = path.split('.'), key;
806807
for (var i = 0; element.length > 1; i++) {
807808
key = ensureSafeMemberName(element.shift(), fullExp);
808-
var propertyObj = ensureSafeObject(obj[key], fullExp);
809+
var propertyObj = (i === 0 && locals && locals[key]) || obj[key];
809810
if (!propertyObj) {
810811
propertyObj = {};
811812
obj[key] = propertyObj;
812813
}
813-
obj = propertyObj;
814+
obj = ensureSafeObject(propertyObj, fullExp);
814815
}
815816
key = ensureSafeMemberName(element.shift(), fullExp);
816817
ensureSafeObject(obj[key], fullExp);
@@ -937,8 +938,8 @@ function getterFn(path, options, fullExp) {
937938
}
938939

939940
fn.sharedGetter = true;
940-
fn.assign = function(self, value) {
941-
return setter(self, path, value, path);
941+
fn.assign = function(self, value, locals) {
942+
return setter(self, locals, path, value, path);
942943
};
943944
getterFnCache[path] = fn;
944945
return fn;

test/ng/parseSpec.js

+56
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,48 @@ describe('parser', function() {
488488
expect(scope.b).toEqual(234);
489489
});
490490

491+
it('should allow passing object as locals to the root left-expression of an assignment', inject(function($rootScope) {
492+
$rootScope.a = {};
493+
$rootScope.key = "value";
494+
var localA = {};
495+
496+
//getterFn
497+
$rootScope.$eval('a.value = 1', {a: localA});
498+
expect(localA.value).toBe(1);
499+
500+
$rootScope.$eval('w.a.value = 2', {w: {a: localA}});
501+
expect(localA.value).toBe(2);
502+
503+
//field access
504+
$rootScope.$eval('(a).value = 3', {a: localA});
505+
expect(localA.value).toBe(3);
506+
507+
$rootScope.$eval('{c: {b: a}}.c.b.value = 4', {a: localA});
508+
expect(localA.value).toBe(4);
509+
510+
//object index
511+
$rootScope.$eval('a[key] = 5', {a: localA});
512+
expect(localA.value).toBe(5);
513+
514+
$rootScope.$eval('w.a[key] = 6', {w: {a: localA}});
515+
expect(localA.value).toBe(6);
516+
517+
$rootScope.$eval('{c: {b: a}}.c.b[key] = 7', {a: localA});
518+
expect(localA.value).toBe(7);
519+
520+
//Nothing should have touched the $rootScope.a
521+
expect($rootScope.a.value).toBeUndefined();
522+
}));
523+
524+
it('should ignore locals beyond the root object of an assignment expression', inject(function($rootScope) {
525+
var a = {};
526+
var locals = {a: a};
527+
$rootScope.b = {a: {value: 123}};
528+
$rootScope.$eval('b.a.value = 1', locals);
529+
expect(a.value).toBeUndefined();
530+
expect($rootScope.b.a.value).toBe(1);
531+
}));
532+
491533
it('should evaluate assignments in ternary operator', function() {
492534
scope.$eval('a = 1 ? 2 : 3');
493535
expect(scope.a).toBe(2);
@@ -799,6 +841,12 @@ describe('parser', function() {
799841
}).toThrowMinErr(
800842
'$parse', 'isecfn', 'Referencing Function in Angular expressions is disallowed! ' +
801843
'Expression: a.toString.constructor');
844+
845+
expect(function() {
846+
scope.$eval("c.a = 1", {c: Function.prototype.constructor});
847+
}).toThrowMinErr(
848+
'$parse', 'isecfn', 'Referencing Function in Angular expressions is disallowed! ' +
849+
'Expression: c.a');
802850
});
803851

804852
it('should disallow traversing the Function object in a setter: E02', function() {
@@ -933,6 +981,14 @@ describe('parser', function() {
933981
'$parse', 'isecobj', 'Referencing Object in Angular expressions is disallowed! ' +
934982
'Expression: foo["bar"]["keys"](foo)');
935983
});
984+
985+
it('should NOT allow access to Object constructor in assignment locals', function() {
986+
expect(function() {
987+
scope.$eval("O.constructor.a = 1", {O: Object});
988+
}).toThrowMinErr(
989+
'$parse', 'isecobj', 'Referencing Object in Angular expressions is disallowed! ' +
990+
'Expression: O.constructor.a');
991+
});
936992
});
937993

938994
describe('Window and $element/node', function() {

0 commit comments

Comments
 (0)