restangular icon indicating copy to clipboard operation
restangular copied to clipboard

JSON Security and returning multiple items in a response

Open iacomus opened this issue 11 years ago • 5 comments

Hi, I'm creating a RESTful web service using Python Flask. For one of my endpoints I'd like to return a list of users. The api endpoint returns JSON in the following format:

{
  "users": [
    {
      "Email": "[email protected]",
      "First": "Tom",
      "Last": "Jones",
      "id": 1
    },
    {
      "Email": "[email protected]",
      "First": "Steven",
      "Last": "Fry",
      "id": 2
    },
    {
      "Email": "[email protected]",
      "First": "Monty",
      "Last": "Python",
      "id": 3
    }
  ]
}

If I do this Restangular responds with Error: Response for getList SHOULD be an array and not an object or something else which is by design as Restangular expects an Array and not a Javascript object. From what I understand there are two options:

  • Wrap the response in an array like so:
[{
  "users": [
    {
      "Email": "[email protected]",
      "First": "Tom",
      "Last": "Jones",
      "id": 1
    },
    {
      "Email": "[email protected]",
      "First": "Steven",
      "Last": "Fry",
      "id": 2
    },
    {
      "Email": "[email protected]",
      "First": "Monty",
      "Last": "Python",
      "id": 3
    }
  ]
} ]

However, according to this article http://flask.pocoo.org/docs/0.10/security/#json-security this is not secure. Is my understanding correct here?

  • The second option is to use this method: https://github.com/mgonto/restangular#my-response-is-actually-wrapped-with-some-metadata-how-do-i-get-the-data-in-that-case

In this case is it possible (would I need to?) to create an interceptor for every getList request on all endpoints that return multiple items e.g. a 'posts' endpoint, 'messages' endpoint etc.? Would this be a feasible or good approach?

Is there anything I'm missing in my understanding?

Many thanks.

iacomus avatar Jan 19 '15 13:01 iacomus

I have the same problem as you.

I would recommend you to add an interceptor (see the doc about it) and always use the same format for your list responses.

e.g.:

{
   "meta": {
     // all metas
   },
  "results": [
   // your list
  ]
}

This would mean a change in your design, if it's a new or small project it will be nuts. For existing larger project maybe use an object to store all informations to know how to map data for each request.

I understand that for /users you would like to have { users: [] } and for /posts { posts: [] } but "results" ain't that bad. You know what data to expect with the GET on /users, /posts --> a list. And if your api is public developers would prefer to have the same name for the list, easier to remember.

Regardless all this I think Restangular should be patched to allow this and look more into this security issue.

My intercptor code:

RestangularProvider.addResponseInterceptor(function(data, operation, what, url, response, deferred) {
    var extractedData;
    // .. to look for getList operations
    if (operation === "getList") {
      // .. and handle the data and meta data
      extractedData = data.results;
      extractedData.meta = data.meta;
    } else {
      extractedData = data;
    }
    return extractedData;
  });

antoinegomez avatar Feb 08 '15 13:02 antoinegomez

True, Restangular expects an array when using list operation as per REST convention. If your API returns different results (like an object with response properties), response interceptor is something you should go for. Typically, when dealing with normal $http in Angular, you would've done the same.

@iacomus To be fair - in your case, you should just remove that "user" property when returning your results and return plain array. Unfortunately, what you are doing now is not the correct way of responding to requests. (I guess that with route "XX" you are going to have XX property that will be an array?)

Generally, what @antoinegomez said is more related to returning additional meta data with your request when you know that every route will have the same response and you want results property returned by default.

grabbou avatar Feb 17 '15 17:02 grabbou

Closing for now. If you will have any further questions related to this issue or problems, feel free to reopen/comment and we will carry on this topic.

grabbou avatar Feb 17 '15 17:02 grabbou

Thanks @grabbou and @antoinegomez

@grabbou I think you're right, probably best to remove the "user" property and return an array however, regarding the security aspect of returning arrays - see the article here http://flask.pocoo.org/docs/0.10/security/#json-security

Is it secure to return arrays? I'm not sure if Angular uses ECMAScript 5 which as the article states "closes this particular attack vector".

iacomus avatar Feb 17 '15 17:02 iacomus

Hey! I know why I haven't heard of this particular vulnerability! Just because it's so old. ECMAScript is a version of Javascript (think of it as Java 6/7/8 and so on) and it's usage is related to browser vendor, not to framework (like Angular or Restangular).

All major browsers you should be worried about are supporting this version for a couple of years and starting to implement ES6 version called "Harmony". (Source: http://en.wikipedia.org/wiki/ECMAScript#Implementations)

Having said so - don't be worried. You can focus on improving your API and making it outstanding :)

grabbou avatar Feb 17 '15 18:02 grabbou