Skip to content
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

update request_forgery_protection docs #15608

Conversation

donbobka
Copy link
Contributor

I made some documentation changes in request_forgery_protection.rb:

  1. Due to protect_from_forgery by default use :null_session method, some lines of documentation are obsolete.
  2. Change controller name from ApplicationController to ApiController. ApplicationController can mislead developers, because usually rails application consist of non-api controllers and api controllers

@@ -27,20 +27,17 @@ class InvalidCrossOriginRequest < ActionControllerError #:nodoc:
# It's important to remember that XML or JSON requests are also affected and if
# you're building an API you'll need something like:
#
# class ApplicationController < ActionController::Base
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to keep ApplicationController. Even if the application consist on api an non-api it is still correct and doesn't need to explain what is a ApiController and how it would be used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree

@donbobka
Copy link
Contributor Author

I've just updated commit

@donbobka
Copy link
Contributor Author

I've just updated docs:

  • delete action some_api_action from ApplicationController, because actions in ApplicationController is bad practice.
  • change explanation, what you should do in case of api application

@sponomarev
Copy link
Contributor

👍

@donbobka
Copy link
Contributor Author

IMO following sentence is wrong:

Only HTML and JavaScript requests are checked, so this will not protect your XML API (presumably you'll have a different authentication scheme there anyway).

From here

In code there is no limitation to CSRF-token checks only for html/javascript

@rafaelfranca
Copy link
Member

Yes. I think you are right. @jeremy do you know if this sentence is true (or were)?

@jeremy
Copy link
Member

jeremy commented Jun 11, 2014

It is true. Only requests that maintain a session are checked, which means HTML for most apps. APIs typically aren't session oriented, so there will be no CSRF token to check against: https://github.com/rails/rails/blob/master/actionpack/lib/action_controller/metal/request_forgery_protection.rb#L248-L262

@donbobka
Copy link
Contributor Author

@jeremy, in code that you noticed there is no limitation for html or javascript, besides exclusion HEAD and GET requests:

!protect_against_forgery? || request.get? || request.head?

APIs typically aren't session oriented, so there will be no CSRF token to check against

Rails has a good method for API CSRF protection: :null_session

Look at commit 6efc5bf where this sentence appeared. Version before changes is more truthful, because it's that what makes the code.

All requests are checked except GET requests as these should be idempotent.

IMO: First and second paragraphs should be about standard CSRF protection that you will get in new rails application. And third paragraph should explain API case of rails usage.

@jeremy
Copy link
Member

jeremy commented Jun 12, 2014

Correct, there is no explicit format check in the code.

The docs intend to communicate that session-oriented requests, like those made from a browser, are verified for authenticity.

That happens to include HTML and JS requests, but it isn't limited to these. It's only written this way to be understandable to Rails users: that "browser requests" need CSRF checks. The docs may certainly be updated to to reflect this. They should be accurate about how CSRF decisions are made but should be very easy to understand.

@donbobka
Copy link
Contributor Author

Why are JS and HTML requests highlights here as something special?

What about a following variant:

All requests are checked except GET requests as these should be idempotent. Keep in mind that all session-oriented requests should be CSRF protected, including Javascript and HTML requests.`

@sponomarev
Copy link
Contributor

@donbobka looks very sensible and clear

@donbobka
Copy link
Contributor Author

@rafaelfranca What do you think about @a86ee8a?

@rafaelfranca
Copy link
Member

Sorry for the delay of 4 years 😱! Can you rebase this PR?

@donbobka
Copy link
Contributor Author

donbobka commented Apr 6, 2018

@rafaelfranca I rebased it, but there are no changes against current master. And I see that changes already merged
https://github.com/rails/rails/blame/master/actionpack/lib/action_controller/metal/request_forgery_protection.rb#L18

So, I think we can close it.

@rafaelfranca
Copy link
Member

Cool! I'll close. Thanks

@donbobka donbobka deleted the feature/doc_request_forgery_protection_for_api branch April 6, 2018 21:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants