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

fix($parse): allow assignment on objects in locals #10322

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/ng/directive/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,19 +489,19 @@ var formDirectiveFactory = function(isNgForm) {
alias = controller.$name;

if (alias) {
setter(scope, alias, controller, alias);
setter(scope, null, alias, controller, alias);
attr.$observe(attr.name ? 'name' : 'ngForm', function(newValue) {
if (alias === newValue) return;
setter(scope, alias, undefined, alias);
setter(scope, null, alias, undefined, alias);
alias = newValue;
setter(scope, alias, controller, alias);
setter(scope, null, alias, controller, alias);
parentFormCtrl.$$renameControl(controller, alias);
});
}
formElement.on('$destroy', function() {
parentFormCtrl.$removeControl(controller);
if (alias) {
setter(scope, alias, undefined, alias);
setter(scope, null, alias, undefined, alias);
}
extend(controller, nullFormCtrl); //stop propagating child destruction handlers upwards
});
Expand Down
15 changes: 8 additions & 7 deletions src/ng/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ Parser.prototype = {
}, {
assign: function(scope, value, locals) {
var o = object(scope, locals);
if (!o) object.assign(scope, o = {});
if (!o) object.assign(scope, o = {}, locals);
return getter.assign(o, value);
}
});
Expand All @@ -690,7 +690,7 @@ Parser.prototype = {
var key = ensureSafeMemberName(indexFn(self, locals), expression);
// prevent overwriting of Function.constructor which would break ensureSafeObject check
var o = ensureSafeObject(obj(self, locals), expression);
if (!o) obj.assign(self, o = {});
if (!o) obj.assign(self, o = {}, locals);
return o[key] = value;
}
});
Expand Down Expand Up @@ -800,18 +800,19 @@ Parser.prototype = {
// Parser helper functions
//////////////////////////////////////////////////

function setter(obj, path, setValue, fullExp) {
function setter(obj, locals, path, setValue, fullExp) {
ensureSafeObject(obj, fullExp);
ensureSafeObject(locals, fullExp);

var element = path.split('.'), key;
for (var i = 0; element.length > 1; i++) {
key = ensureSafeMemberName(element.shift(), fullExp);
var propertyObj = ensureSafeObject(obj[key], fullExp);
var propertyObj = (i === 0 && locals && locals[key]) || obj[key];
if (!propertyObj) {
propertyObj = {};
obj[key] = propertyObj;
}
obj = propertyObj;
obj = ensureSafeObject(propertyObj, fullExp);
}
key = ensureSafeMemberName(element.shift(), fullExp);
ensureSafeObject(obj[key], fullExp);
Expand Down Expand Up @@ -938,8 +939,8 @@ function getterFn(path, options, fullExp) {
}

fn.sharedGetter = true;
fn.assign = function(self, value) {
return setter(self, path, value, path);
fn.assign = function(self, value, locals) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is what I'm referring to, @jbedard

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just 1 of 3 assign implementations though. The others already had the third param so it is nothing new, it was just missing from this implementation...

You want to document it to make it public though?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's no reason not to document it --- nobody has a good reason to use it anyways, but we aren't using it as a private API anywhere

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's used within this file, just this version (unlike the other 2/3) did not support it...

return setter(self, locals, path, value, path);
};
getterFnCache[path] = fn;
return fn;
Expand Down
70 changes: 70 additions & 0 deletions test/ng/parseSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,62 @@ describe('parser', function() {
expect(scope.b).toEqual(234);
});

it('should allow use of locals in the left side of an assignment', inject(function($rootScope) {
$rootScope.a = {};
$rootScope.key = "value";
var localA = {};

//getterFn
$rootScope.$eval('a.value = 1', {a: localA});
expect(localA.value).toBe(1);

$rootScope.$eval('w.a.value = 2', {w: {a: localA}});
expect(localA.value).toBe(2);

//field access
$rootScope.$eval('(a).value = 3', {a: localA});
expect(localA.value).toBe(3);

$rootScope.$eval('{c: {b: a}}.c.b.value = 4', {a: localA});
expect(localA.value).toBe(4);

//object index
$rootScope.$eval('a[key] = 5', {a: localA});
expect(localA.value).toBe(5);

$rootScope.$eval('w.a[key] = 6', {w: {a: localA}});
expect(localA.value).toBe(6);

$rootScope.$eval('{c: {b: a}}.c.b[key] = 7', {a: localA});
expect(localA.value).toBe(7);

//Nothing should have touched the $rootScope.a
expect($rootScope.a.value).toBeUndefined();
}));

it('should allow use of locals in sub expressions of the left side of an assignment', inject(function($rootScope, $parse) {
delete $rootScope.x;
$rootScope.$eval('x[a][b] = true', {a: 'foo', b: 'bar'});
expect($rootScope.x.foo.bar).toBe(true);

delete $rootScope.x;
$rootScope.$eval('x.foo[b] = true', {b: 'bar'});
expect($rootScope.x.foo.bar).toBe(true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this block of test repeated? Did you mean?

$rootScope.$eval('x.foo[b] = true', {a: 'foo', b: 'bar'});

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. That's from #10322 (comment) but I changed them to all use $eval which made two equivalent. I think it can just be removed...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or actually I'll change it to what you suggested instead of deleting it...


delete $rootScope.x;
$rootScope.$eval('x[a].bar = true', {a: 'foo'});
expect($rootScope.x.foo.bar).toBe(true);
}));

it('should ignore locals beyond the root object of an assignment expression', inject(function($rootScope) {
var a = {};
var locals = {a: a};
$rootScope.b = {a: {value: 123}};
$rootScope.$eval('b.a.value = 1', locals);
expect(a.value).toBeUndefined();
expect($rootScope.b.a.value).toBe(1);
}));

it('should evaluate assignments in ternary operator', function() {
scope.$eval('a = 1 ? 2 : 3');
expect(scope.a).toBe(2);
Expand Down Expand Up @@ -799,6 +855,12 @@ describe('parser', function() {
}).toThrowMinErr(
'$parse', 'isecfn', 'Referencing Function in Angular expressions is disallowed! ' +
'Expression: a.toString.constructor');

expect(function() {
scope.$eval("c.a = 1", {c: Function.prototype.constructor});
}).toThrowMinErr(
'$parse', 'isecfn', 'Referencing Function in Angular expressions is disallowed! ' +
'Expression: c.a');
});

it('should disallow traversing the Function object in a setter: E02', function() {
Expand Down Expand Up @@ -933,6 +995,14 @@ describe('parser', function() {
'$parse', 'isecobj', 'Referencing Object in Angular expressions is disallowed! ' +
'Expression: foo["bar"]["keys"](foo)');
});

it('should NOT allow access to Object constructor in assignment locals', function() {
expect(function() {
scope.$eval("O.constructor.a = 1", {O: Object});
}).toThrowMinErr(
'$parse', 'isecobj', 'Referencing Object in Angular expressions is disallowed! ' +
'Expression: O.constructor.a');
});
});

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