-
Notifications
You must be signed in to change notification settings - Fork 240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Delegating to devise's built-in authenticate_user! introduces security risk in API context #49
Comments
Bump @gonzalo-bulnes |
Hi @joefiorini and @adg29, First of all, @joefiorini, your comment makes a lot sense to me. But I not sure at all about the solution to implement to this issue. Keeping your numbering, here are some thoughts: 1. Removing the Simple Token Authentication was first designed as a replacement for the token_authenticatable Devise strategy. The fallback in that context is quite natural and I think it still makes sense to many users (Off-topic: @bryanaka that may partially respond to your question). 2. Only fallback for non-JSON requests by default (with opt-in) That might be, but I don't like much the "non-JSON" part of it. Simple Token Authentication should not, IMO, care about the content types you work with. What about XML API in such a case? But there is something to keep from this idea. 3. An global fallback option (opt-in or opt-out) Could be, but I find no reason a priori to make that option global. (I may, and some people do, want to serve both an API and a web app from the same application.) New 4. A fallback option, to be used from That would flexible enough to keep configuration at a controller level. I tend to prefer an opt-out option to keep the gem behaviour consistent, but I also think that the opt-in or opt-out question is not the main point here. The usage could be something like: # app/controllers/api/posts_controller.rb
class API::PostsController < ApplicationController
acts_as_token_authentication_handler_for User, fallback_to_devise: false
# ...
end Eventually, not disabling the fallback for JSON, XML requests (extensible list) could print a warning somewhere which would to an API-specific documentation section. (The idea would be to provide additional support based on content type without depending on it.) @ both: What do you think? |
Fallback option is definitely the best way. |
Thanks for the call 👍 On one hand I think the first approach is correct as well since IMO is the expected behaviour for anyone that has been using devise, but since implementing it right now can be quite dangerous I think the fallback alternative (4th) is the best option at this stage. The 3rd option I guess could create a problem similar to the one I'm facing now (#53) so it doesn't really seem like the way to go. |
+1 for opting-in to the fallback |
👍 for making fallback_to_devise optional |
class HomeController < ApplicationController
acts_as_token_authentication_handler_for User, before_filter: false
before_filter :authenticate_entity_from_token!, only: :destroy
def index
# ... Accessible for unauthenticated users ...
end
def destroy
# ... Only for authenticated users...
end
end My implementation in PR #61 |
Default logic for |
When you disable the Devise fallback, your controllers must handle the cases in which As I see it, even if it must be disabled from time to time, the fallback ensures that Simple Token Authentication behaves as would any Devise strategy: once you enabled it, you can almost forget about it because Devise will take care of errors. |
@joefiorini |
The problem is that when you use |
@donbobka I agree with you in an API scenario - that's the reason to add a fallback_to_devise option in the first case. However, Simple Token Authentication can also be used in non-API scenarios, e.g. sending emails with signin links (see the sign_in_token option documentation and #22). In these cases, the fallback to Devise is expected, and you definitely want your session to be persisted and the anti-CSRF authenticity token to be used. Hence the justification for fallback_to_devise to be there, as an option, but still available. |
Hi everybody, hi @joefiorini, You can now use the fallback_to_devise option to disable the |
Note: since v1.10.0, the preferred syntax for (Respectively, the preferred syntax for |
Just in case anyone else bumps into the same problem: since upgrading to Rails 5, I've discovered that |
This is very frustrating. I haven't been able to disable sessions with any combination of mentioned configurations. |
I'm building a JSON API with an Ember front-end. Therefore, I want to be able to turn of CSRF validation for JSON requests, as references a number of times in previous issues. If I do that, requiring authentication tokens is the only way to secure JSON requests.
However, based on what I can see in https://github.com/gonzalo-bulnes/simple_token_authentication/blob/master/lib/simple_token_authentication/acts_as_token_authentication_handler.rb#L23-L26, this gem delegates to Devise's default authentication mechanism after doing token authentication.
Devise's default authentication makes every request fallback to cookie authentication. That means any existing users will still be using cookies until they sign out & sign back in. Also, when I disable CSRF validation on JSON requests, I'll still be vulnerable to CSRF attacks. The reason I want token authentication is so I can force clients to send identifying information with every request. Falling back to devise's authentication seems to defeat that purpose.
I can think of a few possible ways to solve this:
I'm thinking I might implement option 2 and send a PR. @gonzalo-bulnes any thoughts on this?
The text was updated successfully, but these errors were encountered: