Skip to content

Use hasOwnProperty with property names from user #1130

@juhoz1024

Description

@juhoz1024

Hi,

I have a corner case with a route called "watch" and Firefox.

Here's the relevant code from Restangular 1.4.0:

config.transformElem = function(elem, isCollection, route, Restangular, force) {
    if (!force && !config.transformLocalElements && !elem[config.restangularFields.fromServer]) {
      return elem;
    }
    var typeTransformers = config.transformers[route];
    var changedElem = elem;
    if (typeTransformers) {
        _.each(typeTransformers, function(transformer) {
           changedElem = transformer(isCollection, changedElem);
        });
    }
    return config.onElemRestangularized(changedElem,
      isCollection, route, Restangular);
};

Besides formatting changes, the code is the same in 1.5.1.

The exact line that's the source of the issue:

    var typeTransformers = config.transformers[route];

What exactly goes wrong:

  1. config.transformers is empty, i.e., {}
  2. route is "watch"
  3. config.transformers[route] should be undefined
  4. instead, it's picking up Object.prototype.watch

Changing the line to:

    var typeTransformers = config.hasOwnProperty(route) && config.transformers[route];

would fix the problem.

Similar checks should be added everywhere the property name comes from outside Restangular.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions