I am writing a rails application with the AngularJS interface, it is part of the tutorial series that I am writing on connecting rails and corner symbols . This means that the rails application communicates with the browser exclusively in JSON.
In angularjs $ http documentation, it describes the potential json security vulnerability when a json request can be embedded in a script tag, plus some tricky use of jsonp to allow something akin to a cross-site scripting attack. I found several other pages, in particular, I thought I described it well and dates from 2008, so this is not a new problem.
Apparently, this is not a vulnerability in the standard rails json rendering, since by default the rails return an object containing an array. But when working with angularjs, we seem to set root: false (although I have to admit that I cannot find where I did this, but it definitely does not give the root root).
In any case, the bottom line is that the angular documentation recommends prefixing any json response with c)]} ', therefore:
['one','two']
becomes
)]}', ['one','two']
Angular will then automatically disable this again.
I am looking for a way to do this elegantly. I saw a lot of questions and answers on stackoverflow about this, but most of them were either related to much earlier versions of the rails before the JSON processing was more thoroughly built-in or seemed to require me to create a large code template. I am looking for a method that I can apply to an application controller or as a helper method that will work everywhere.
The controller that I am currently using is as follows:
class ClubsController < ApplicationController respond_to :json
This does not cause any templates - the rendering action skips the template engine. I can make this work by changing the visualization bar so that:
respond_with json: @clubs
And creating template / club / index.json.erb files containing
)]}', <%= raw(@clubs.to_json) %>
But I would have to create a template for each action on each controller, which would look like a template. Instead, I would like to be able to modify views / layouts / application.json.erb to have something like:
)]}', <%= yield %>
But this does not work, because we only get templates when calling response_with. And if we call response_with, we will not be able to put @clubs in the answer - so in the end we get:
)]}',
As a complete answer.
An alternative, perhaps, would be to override the as_json method to add what I want, but it looks like a sledgehammer. Ideally, there would be a place where I could introduce a helper method, for example:
render prepend_vulnerability_protection(json: @clubs)
So, after all this, two questions:
- This is even a real problem, or Rails already has some other protection, which means I donβt have to worry about it at all.
- Is there a way to do this centrally, or do I need to bite a bullet and create all the template templates? I can modify scaffold generators to do this, so this is not the end of the world, but it looks like a lot of patterns.