You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: guides/source/security.md
+42-8Lines changed: 42 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -250,6 +250,8 @@ CSRF appears very rarely in CVE (Common Vulnerabilities and Exposures) - less th
250
250
251
251
NOTE:_First, as is required by the W3C, use GETandPOST appropriately. Secondly, a security token in non-GET requests will protect your application from CSRF._
252
252
253
+
#### Use GET and POST Appropriately
254
+
253
255
TheHTTP protocol basically provides two main types of requests -GETandPOST (DELETE, PUT, andPATCH should be used like POST). TheWorldWideWebConsortium (W3C) provides a checklist for choosing HTTPGETorPOST:
254
256
255
257
**UseGETif:**
@@ -287,21 +289,52 @@ There are many other possibilities, like using a `<script>` tag to make a cross-
287
289
288
290
NOTE:We can't distinguish a `<script>` tag's origin—whether it's a tag on your own site or on some other malicious site—so we must block all `<script>` across the board, even if it's actually a safe same-origin script served from your own site. In these cases, explicitly skip CSRF protection on actions that serve JavaScript meant for a `<script>` tag.
289
291
292
+
#### Required Security Token
293
+
290
294
To protect against all other forged requests, we introduce a _required security token_ that our site knows but other sites don't know. We include the security token in requests and verify it on the server. This is done automatically when [`config.action_controller.default_protect_from_forgery`][] is set to `true`, which is the default for newly created Rails applications. You can also do it manually by adding the following to your application controller:
291
295
292
296
```ruby
293
297
protect_from_forgery with: :exception
294
298
```
295
299
296
-
This will include a security token in all forms and Ajax requests generated by Rails. If the security token doesn't match what was expected, an exception will be thrown.
300
+
This will include a security token in all forms generated by Rails. If the
301
+
security token doesn't match what was expected, an exception will be thrown.
302
+
303
+
When submitting forms with [Turbo](https://turbo.hotwired.dev/) the security
304
+
token is required as well. Turbo looks for the token in the `csrf` meta tags of
305
+
your application layout and adds it to request in the `X-CSRF-Token` request
306
+
header. These meta tags are created with the [`csrf_meta_tags`][] helper
NOTE:By default, Rails includes an [unobtrusive scripting adapter](https://github.com/rails/rails/blob/main/actionview/app/assets/javascripts),
299
-
which adds a header called `X-CSRF-Token` with the security token on every non-GET
300
-
Ajax call. Without this header, non-GETAjax requests won't be accepted by Rails.
301
-
When using another library to make Ajax calls, it is necessary to add the security
302
-
token as a default header for Ajax calls in your library. To get the token, have
303
-
a look at `<meta name='csrf-token' content='THE-TOKEN'>` tag printed by
304
-
`<%= csrf_meta_tags %>` in your application view.
337
+
#### Clearing Persistent Cookies
305
338
306
339
It is common to use persistent cookies to store user information, with `cookies.permanent`for example. In this case, the cookies will not be cleared and the out of the box CSRF protection will not be effective. If you are using a different cookie store than the session for this information, you must handle what to do with it yourself:
307
340
@@ -316,6 +349,7 @@ The above method can be placed in the `ApplicationController` and will be called
316
349
Note that _cross-site scripting (XSS) vulnerabilities bypass all CSRF protections_. XSS gives the attacker access to all elements on a page, so they can read the CSRF security token from a form or directly submit the form. Read [more about XSS](#cross-site-scripting-xss) later.
0 commit comments