From 9577702e8d2519c1a60f5ac4058e63bd7b919815 Mon Sep 17 00:00:00 2001 From: Chirayu Krishnappa Date: Fri, 8 Nov 2013 20:44:32 -0800 Subject: fix($resource): don't use $parse for @dotted.member params and paramDefaults support looking up the parameter value from the data object. The syntax for that is `@nested.property.name`. Currently, $resource uses $parse to do this. This is too liberal (you can use values like `@a=b` or `@a | filter` and have it work - which doesn't really make sense). It also puts up a dependency on $parse which is has restrictions to secure expressions used in templates. The value here, though a string, is specified in Javascript code and shouldn't have those restrictions. --- src/ngResource/resource.js | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) (limited to 'src/ngResource/resource.js') diff --git a/src/ngResource/resource.js b/src/ngResource/resource.js index 56d32f5e..f2e7ff62 100644 --- a/src/ngResource/resource.js +++ b/src/ngResource/resource.js @@ -2,6 +2,28 @@ var $resourceMinErr = angular.$$minErr('$resource'); +// Helper functions and regex to lookup a dotted path on an object +// stopping at undefined/null. The path must be composed of ASCII +// identifiers (just like $parse) +var MEMBER_NAME_REGEX = /^(\.[a-zA-Z_$][0-9a-zA-Z_$]*)+$/; + +function isValidDottedPath(path) { + return (path != null && path !== '' && path !== 'hasOwnProperty' && + MEMBER_NAME_REGEX.test('.' + path)); +} + +function lookupDottedPath(obj, path) { + if (!isValidDottedPath(path)) { + throw $resourceMinErr('badmember', 'Dotted member path "@{0}" is invalid.', path); + } + var keys = path.split('.'); + for (var i = 0, ii = keys.length; i < ii && obj !== undefined; i++) { + var key = keys[i]; + obj = (obj !== null) ? obj[key] : undefined; + } + return obj; +} + /** * @ngdoc overview * @name ngResource @@ -285,7 +307,8 @@ var $resourceMinErr = angular.$$minErr('$resource'); */ angular.module('ngResource', ['ng']). - factory('$resource', ['$http', '$parse', '$q', function($http, $parse, $q) { + factory('$resource', ['$http', '$q', function($http, $q) { + var DEFAULT_ACTIONS = { 'get': {method:'GET'}, 'save': {method:'POST'}, @@ -297,10 +320,7 @@ angular.module('ngResource', ['ng']). forEach = angular.forEach, extend = angular.extend, copy = angular.copy, - isFunction = angular.isFunction, - getter = function(obj, path) { - return $parse(path)(obj); - }; + isFunction = angular.isFunction; /** * We need our custom method because encodeURIComponent is too aggressive and doesn't follow @@ -415,7 +435,7 @@ angular.module('ngResource', ['ng']). forEach(actionParams, function(value, key){ if (isFunction(value)) { value = value(); } ids[key] = value && value.charAt && value.charAt(0) == '@' ? - getter(data, value.substr(1)) : value; + lookupDottedPath(data, value.substr(1)) : value; }); return ids; } -- cgit v1.2.3