|
1 | | -rocket_csrf |
2 | | -=========== |
| 1 | +# rocket_csrf_token |
3 | 2 |
|
4 | | -CSRF (Cross-Site Request Forgery) protection for [Rocket](https://rocket.rs) |
5 | | -web framework. |
| 3 | +A slightly more maintained version of [rocket_csrf](https://github.com/kotovalexarian/rocket_csrf). |
6 | 4 |
|
7 | | -> **WARNING!** |
8 | | -> The implementation is very simple for now and may not be ready for production. |
| 5 | +## Usage |
9 | 6 |
|
10 | | -Discussion about CSRF protection in Rocket is |
11 | | -[here](https://github.com/SergioBenitez/Rocket/issues/14). |
12 | | - |
13 | | - |
14 | | - |
15 | | -Table of contents |
16 | | ------------------ |
17 | | - |
18 | | -* [Overview](#rocket_csrf) |
19 | | -* [Table of contents](#table-of-contents) |
20 | | -* [Usage](#usage) |
21 | | -* [TODO](#todo) |
22 | | - |
23 | | - |
24 | | - |
25 | | -Usage |
26 | | ------ |
27 | | - |
28 | | -Attach [fairing](https://rocket.rs/v0.5-rc/guide/fairings/#fairings) to the Rocket |
29 | | -instance: |
| 7 | +Attach [fairing](https://rocket.rs/v0.5-rc/guide/fairings/#fairings) to the Rocket instance: |
30 | 8 |
|
31 | 9 | ```rust |
32 | 10 | #![feature(decl_macro)] |
33 | 11 |
|
34 | | -#[macro_use] extern crate rocket; |
35 | | -#[macro_use] extern crate serde_derive; |
| 12 | +#[macro_use] |
| 13 | +extern crate rocket; |
| 14 | +#[macro_use] |
| 15 | +extern crate serde_derive; |
36 | 16 |
|
37 | 17 | use rocket_dyn_templates::Template; |
38 | 18 |
|
39 | 19 | #[launch] |
40 | 20 | fn rocket() -> _ { |
41 | | - rocket::ignite() |
42 | | - .attach(rocket_csrf::Fairing::default()) |
43 | | - .attach(Template::fairing()) |
44 | | - .mount("/", routes![new, create]) |
| 21 | + rocket::build() |
| 22 | + .attach(rocket_csrf_token::Fairing::default()) |
| 23 | + .attach(Template::fairing()) |
| 24 | + .mount("/", routes![new, create]) |
45 | 25 | } |
46 | 26 | ``` |
47 | 27 |
|
48 | | -You also can configure |
49 | | -[fairing](https://rocket.rs/v0.5-rc/guide/fairings/#fairings): |
| 28 | +You also can configure [fairing](https://rocket.rs/v0.5-rc/guide/fairings/#fairings): |
50 | 29 |
|
51 | 30 | ```rust |
52 | 31 | #[launch] |
53 | 32 | fn rocket() -> _ { |
54 | | - rocket::ignite() |
55 | | - .attach(rocket_csrf::Fairing::new( |
56 | | - rocket_csrf::CsrfConfig::default() |
57 | | - .with_cookie_name("foobar") |
58 | | - .with_cookie_len(64) |
59 | | - .with_lifetime(time::Duration::days(3)), |
60 | | - )) |
61 | | - .attach(Template::fairing()) |
62 | | - .mount("/", routes![new, create]) |
| 33 | + rocket::build() |
| 34 | + .attach( |
| 35 | + rocket_csrf_token::Fairing::new( |
| 36 | + rocket_csrf_token::CsrfConfig |
| 37 | + ::default() |
| 38 | + .with_cookie_name("foobar") |
| 39 | + .with_cookie_len(64) |
| 40 | + .with_lifetime(rocket::time::Duration::days(3)) |
| 41 | + ) |
| 42 | + ) |
| 43 | + .attach(Template::fairing()) |
| 44 | + .mount("/", routes![new, create]) |
63 | 45 | } |
64 | 46 | ``` |
65 | 47 |
|
66 | | -Add [guard](https://rocket.rs/v0.5-rc/guide/requests/#request-guards) to any |
67 | | -request where you want to have access to session's CSRF token (e.g. to include |
68 | | -it in forms) or verify it (e.g. to validate form): |
| 48 | +Add [guard](https://rocket.rs/v0.5-rc/guide/requests/#request-guards) to any request where you want to have access to session's CSRF token (e.g. to include it in forms) or verify it (e.g. to validate form): |
69 | 49 |
|
70 | 50 | ```rust |
71 | 51 | use rocket::form::Form; |
72 | 52 | use rocket::response::Redirect; |
73 | | -use rocket_csrf::CsrfToken; |
| 53 | +use rocket_csrf_token::CsrfToken; |
74 | 54 | use rocket_dyn_templates::Template; |
75 | 55 |
|
76 | 56 | #[get("/comments/new")] |
77 | 57 | fn new(csrf_token: CsrfToken) -> Template { |
78 | | - // your code |
| 58 | + // your code |
79 | 59 | } |
80 | 60 |
|
81 | 61 | #[post("/comments", data = "<form>")] |
82 | 62 | fn create(csrf_token: CsrfToken, form: Form<Comment>) -> Redirect { |
83 | | - // your code |
| 63 | + // your code |
84 | 64 | } |
85 | 65 | ``` |
86 | 66 |
|
87 | | -Get CSRF token from |
88 | | -[guard](https://rocket.rs/v0.5-rc/guide/requests/#request-guards) |
89 | | -to use it in [templates](https://rocket.rs/v0.5-rc/guide/responses/#templates): |
| 67 | +Get CSRF token from [guard](https://rocket.rs/v0.5-rc/guide/requests/#request-guards) to use it in [templates](https://rocket.rs/v0.5-rc/guide/responses/#templates): |
90 | 68 |
|
91 | 69 | ```rust |
92 | 70 | #[get("/comments/new")] |
93 | 71 | fn new(csrf_token: CsrfToken) -> Template { |
94 | | - let authenticity_token: &str = csrf_token.authenticity_token(); |
| 72 | + let authenticity_token: &str = csrf_token.authenticity_token(); |
95 | 73 |
|
96 | | - // your code |
| 74 | + // your code |
97 | 75 | } |
98 | 76 | ``` |
99 | 77 |
|
100 | | -Add CSRF token to your HTML forms in |
101 | | -[templates](https://rocket.rs/v0.5-rc/guide/responses/#templates): |
| 78 | +Add CSRF token to your HTML forms in [templates](https://rocket.rs/v0.5-rc/guide/responses/#templates): |
102 | 79 |
|
103 | 80 | ```html |
104 | 81 | <form method="post" action="/comments"> |
105 | | - <input type="hidden" name="authenticity_token" value="{{ authenticity_token }}"/> |
106 | | - <!-- your fields --> |
| 82 | + <input |
| 83 | + type="hidden" |
| 84 | + name="authenticity_token" |
| 85 | + value="{{ authenticity_token }}" |
| 86 | + /> |
| 87 | + <!-- your fields --> |
107 | 88 | </form> |
108 | 89 | ``` |
109 | 90 |
|
110 | | -Add attribute `authenticity_token` to your |
111 | | -[forms](https://rocket.rs/v0.5-rc/guide/requests/#forms): |
| 91 | +Add attribute `authenticity_token` to your [forms](https://rocket.rs/v0.5-rc/guide/requests/#forms): |
112 | 92 |
|
113 | 93 | ```rust |
114 | 94 | #[derive(FromForm)] |
115 | 95 | struct Comment { |
116 | | - authenticity_token: String, |
117 | | - // your attributes |
| 96 | + authenticity_token: String, |
| 97 | + // your attributes |
118 | 98 | } |
119 | 99 | ``` |
120 | 100 |
|
121 | | -Validate [forms](https://rocket.rs/v0.5-rc/guide/requests/#forms) to have valid |
122 | | -authenticity token: |
| 101 | +Validate [forms](https://rocket.rs/v0.5-rc/guide/requests/#forms) to have valid authenticity token: |
123 | 102 |
|
124 | 103 | ```rust |
125 | 104 | #[post("/comments", data = "<form>")] |
126 | 105 | fn create(csrf_token: CsrfToken, form: Form<Comment>) -> Redirect { |
127 | | - if let Err(_) = csrf_token.verify(&form.authenticity_token) { |
128 | | - return Redirect::to(uri!(new)); |
129 | | - } |
| 106 | + if let Err(_) = csrf_token.verify(&form.authenticity_token) { |
| 107 | + return Redirect::to(uri!(new)); |
| 108 | + } |
130 | 109 |
|
131 | | - // your code |
| 110 | + // your code |
132 | 111 | } |
133 | 112 | ``` |
134 | 113 |
|
135 | 114 | See the complete code in [minimal example](examples/minimal). |
136 | 115 |
|
| 116 | +## TODO |
137 | 117 |
|
138 | | - |
139 | | -TODO |
140 | | ----- |
141 | | - |
142 | | -* [ ] Add fairing to verify all requests as an option. |
143 | | -* [ ] Add [data guard](https://api.rocket.rs/v0.5-rc/rocket/data/trait.FromData.html) to verify forms with a guard. |
144 | | -* [ ] Add helpers to render form field. |
145 | | -* [ ] Add helpers to add HTML meta tags for Ajax with `X-CSRF-Token` header. |
146 | | -* [ ] Verify `X-CSRF-Token` header. |
147 | | -* [ ] Use authenticity token encryption from [Ruby on Rails](https://github.com/rails/rails/blob/v6.0.3.4/actionpack/lib/action_controller/metal/request_forgery_protection.rb). |
148 | | -* [ ] Allow to configure CSRF protection (CSRF token byte length, cookie name, etc.). |
149 | | -* [ ] Set cookie to expire with session. |
| 118 | +- [ ] Add fairing to verify all requests as an option. |
| 119 | +- [ ] Add [data guard](https://api.rocket.rs/v0.5-rc/rocket/data/trait.FromData.html) to verify forms with a guard. |
| 120 | +- [ ] Add helpers to render form field. |
| 121 | +- [ ] Add helpers to add HTML meta tags for Ajax with `X-CSRF-Token` header. |
| 122 | +- [ ] Verify `X-CSRF-Token` header. |
| 123 | +- [ ] Use authenticity token encryption from [Ruby on Rails](https://github.com/rails/rails/blob/v6.0.3.4/actionpack/lib/action_controller/metal/request_forgery_protection.rb). |
| 124 | +- [ ] Allow to configure CSRF protection (CSRF token byte length, cookie name, etc.). |
| 125 | +- [ ] Set cookie to expire with session. |
0 commit comments