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

fix($parse): Preserve expensive checks when runnning $eval inside an expression #13850

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
39 changes: 37 additions & 2 deletions src/ng/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -1757,10 +1757,19 @@ function $ParseProvider() {
csp: noUnsafeEval,
expensiveChecks: true
};
var runningChecksEnabled = false;

return function $parse(exp, interceptorFn, expensiveChecks) {
$parse.$$runningExpensiveChecks = function() {
return runningChecksEnabled;
};

return $parse;

function $parse(exp, interceptorFn, expensiveChecks) {
var parsedExpression, oneTime, cacheKey;

expensiveChecks = expensiveChecks || runningChecksEnabled;

switch (typeof exp) {
case 'string':
exp = exp.trim();
Expand All @@ -1786,6 +1795,9 @@ function $ParseProvider() {
} else if (parsedExpression.inputs) {
parsedExpression.$$watchDelegate = inputsWatchDelegate;
}
if (expensiveChecks) {
parsedExpression = expensiveChecksInterceptor(parsedExpression);
}
cache[cacheKey] = parsedExpression;
}
return addInterceptor(parsedExpression, interceptorFn);
Expand All @@ -1796,7 +1808,30 @@ function $ParseProvider() {
default:
return addInterceptor(noop, interceptorFn);
}
};
}

function expensiveChecksInterceptor(fn) {
if (!fn) return fn;
expensiveCheckFn.$$watchDelegate = fn.$$watchDelegate;
expensiveCheckFn.assign = expensiveChecksInterceptor(fn.assign);
expensiveCheckFn.constant = fn.constant;
expensiveCheckFn.literal = fn.literal;
for (var i = 0; fn.inputs && i < fn.inputs.length; ++i) {
fn.inputs[i] = expensiveChecksInterceptor(fn.inputs[i]);
}

return expensiveCheckFn;

function expensiveCheckFn(scope, locals, assign, inputs) {
var expensiveCheckOldValue = runningChecksEnabled;
runningChecksEnabled = true;
try {
return fn(scope, locals, assign, inputs);
} finally {
runningChecksEnabled = expensiveCheckOldValue;
}
}
}

function expressionInputDirtyCheck(newValue, oldValueOfValue) {

Expand Down
3 changes: 2 additions & 1 deletion src/ng/rootScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,7 @@ function $RootScopeProvider() {
});
}

asyncQueue.push({scope: this, expression: expr, locals: locals});
asyncQueue.push({scope: this, expression: $parse(expr), locals: locals});
},

$$postDigest: function(fn) {
Expand Down Expand Up @@ -1090,6 +1090,7 @@ function $RootScopeProvider() {
$applyAsync: function(expr) {
var scope = this;
expr && applyAsyncQueue.push($applyAsyncExpression);
expr = $parse(expr);
scheduleApplyAsync();

function $applyAsyncExpression() {
Expand Down
78 changes: 77 additions & 1 deletion test/ng/parseSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1681,7 +1681,6 @@ describe('parser', function() {
$filterProvider = filterProvider;
}]));


forEach([true, false], function(cspEnabled) {
describe('csp: ' + cspEnabled, function() {

Expand Down Expand Up @@ -2400,6 +2399,64 @@ describe('parser', function() {
'$parse', 'isecwindow', 'Referencing the Window in Angular expressions is disallowed! ' +
'Expression: foo.w = 1');
}));

they('should propagate expensive checks when calling $prop',
['foo.w && true',
'$eval("foo.w && true")',
'this["$eval"]("foo.w && true")',
'bar;$eval("foo.w && true")',
'$eval("foo.w && true");bar',
'$eval("foo.w && true", null, false)',
'$eval("foo");$eval("foo.w && true")',
'$eval("$eval(\\"foo.w && true\\")")',
'$eval("foo.e()")',
'$evalAsync("foo.w && true")',
'this["$evalAsync"]("foo.w && true")',
'bar;$evalAsync("foo.w && true")',
'$evalAsync("foo.w && true");bar',
'$evalAsync("foo.w && true", null, false)',
'$evalAsync("foo");$evalAsync("foo.w && true")',
'$evalAsync("$evalAsync(\\"foo.w && true\\")")',
'$evalAsync("foo.e()")',
'$evalAsync("$eval(\\"foo.w && true\\")")',
'$eval("$evalAsync(\\"foo.w && true\\")")',
'$watch("foo.w && true")',
'$watchCollection("foo.w && true", foo.f)',
'$watchGroup(["foo.w && true"])',
'$applyAsync("foo.w && true")'], function(expression) {
inject(function($parse, $window) {
scope.foo = {
w: $window,
bar: 'bar',
e: function() { scope.$eval("foo.w && true"); },
f: function() {}
};
expect($parse.$$runningExpensiveChecks()).toEqual(false);
expect(function() {
scope.$eval($parse(expression, null, true));
scope.$digest();
}).toThrowMinErr(
'$parse', 'isecwindow', 'Referencing the Window in Angular expressions is disallowed! ' +
'Expression: foo.w && true');
expect($parse.$$runningExpensiveChecks()).toEqual(false);
});
});

they('should restore the state of $$runningExpensiveChecks when the expression $prop throws',
['$eval("foo.t()")',
'$evalAsync("foo.t()", {foo: foo})'], function(expression) {
inject(function($parse, $window) {
scope.foo = {
t: function() { throw new Error(); }
};
expect($parse.$$runningExpensiveChecks()).toEqual(false);
expect(function() {
scope.$eval($parse(expression, null, true));
scope.$digest();
}).toThrow();
expect($parse.$$runningExpensiveChecks()).toEqual(false);
});
});
});
});

Expand Down Expand Up @@ -2966,6 +3023,25 @@ describe('parser', function() {
expect(log).toEqual('');
}));

it('should work with expensive checks', inject(function($parse, $rootScope, log) {
var fn = $parse('::foo', null, true);
$rootScope.$watch(fn, function(value, old) { if (value !== old) log(value); });

$rootScope.$digest();
expect($rootScope.$$watchers.length).toBe(1);

$rootScope.foo = 'bar';
$rootScope.$digest();
expect($rootScope.$$watchers.length).toBe(0);
expect(log).toEqual('bar');
log.reset();

$rootScope.foo = 'man';
$rootScope.$digest();
expect($rootScope.$$watchers.length).toBe(0);
expect(log).toEqual('');
}));

it('should have a stable value if at the end of a $digest it has a defined value', inject(function($parse, $rootScope, log) {
var fn = $parse('::foo');
$rootScope.$watch(fn, function(value, old) { if (value !== old) log(value); });
Expand Down
8 changes: 4 additions & 4 deletions test/ng/rootScopeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1387,7 +1387,7 @@ describe('Scope', function() {
expect(child.log).toBe('child context');
}));

it('should operate only with a single queue across all child and isolate scopes', inject(function($rootScope) {
it('should operate only with a single queue across all child and isolate scopes', inject(function($rootScope, $parse) {
var childScope = $rootScope.$new();

Choose a reason for hiding this comment

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

Hi

var isolateScope = $rootScope.$new(true);

Expand All @@ -1398,9 +1398,9 @@ describe('Scope', function() {
expect(childScope.$$asyncQueue).toBe($rootScope.$$asyncQueue);
expect(isolateScope.$$asyncQueue).toBeUndefined();
expect($rootScope.$$asyncQueue).toEqual([
{scope: $rootScope, expression: 'rootExpression'},
{scope: childScope, expression: 'childExpression'},
{scope: isolateScope, expression: 'isolateExpression'}
{scope: $rootScope, expression: $parse('rootExpression')},
{scope: childScope, expression: $parse('childExpression')},
{scope: isolateScope, expression: $parse('isolateExpression')}
]);
}));

Expand Down