views:

23

answers:

1

Wondering if I should ALWAYS use the respond_to/format.xxx block in ALL of my actions in ALL of my controllers. This came up because I realized that, for apps using only HTML response format (no respond_to block), I could send some other type of request (say XML) and get a valid response. Is this considered insecure? Feels to me like it is, and it seems that the solution would be to ALWAYS add the respond_to/format.xxx block to EVERY action on EVERY controller even if you are just using HTML only. That way, any non-html requests will get rejected (as they should).

So, is there ever a situation where it would be OK to leave out the respond_to/format.xxx bits for any given action? Even if you were just doing a simple, standard, HTML-only app? It seems like different versions of rails code generators have generated different things regarding this over time. Just wondering what is considered best practice, and when you can get away without using it.

And if the answer is "Yes, you should use it all the time in every action to be secure", then would it not be considered boilerplate at that point? And shouldn't it be pushed down into rails somehow so we don't have to write it out over and over again every single time?

+1  A: 

I don't think this has any impact on the security of your application. Presuming that the inbound HTTP request is well-formed and the client is authenticated to make the request, you as the server don't really care what the client receives. In fact, the client could be passing in all sorts of headers and parameters that you totally ignore and it's up to the client to handle unexpected formats or errors. This is integral to the underlying architecture of HTTP.

In any case, you can set the routes up to not accept the .format parameter, in which case a request for .xml will result in a 404 or other type of error.

If you only use one format, don't the respond_to block, it clutters code with unnecessary artifacts.

Toby Hede
Yes, that makes sense. If you are using just 1 format, you can just specify that as the default format and not worry about using the respond_to block. So, does that mean that if you are using more than 1 format, then you should ALWAYS use a respond_to block in every controller and every action?
Buddy
I would only use it where I need it, but that's just me.
Toby Hede