1
1
//! HTTP Security Headers.
2
2
//!
3
- //! Adapted from [helmetjs](https://helmetjs.github.io/).
4
- //!
5
- //! ## Example
6
- //! ```
7
- //! let mut headers = http::Headers::new();
8
- //! http_types::security::default(&mut headers);
9
- //! assert_eq!(headers["X-Content-Type-Options"], "nosniff");
10
- //! assert_eq!(headers["X-XSS-Protection"], "1; mode=block");
11
- //! ```
3
+ // //! ## Example
4
+ // //!
5
+ // //! ```
6
+ // //! use http_types::Response;
7
+ // //!
8
+ // //! let mut res = Response::new(StatusCode::Ok);
9
+ // //! http_types::security::default(&mut res);
10
+ // //! assert_eq!(res["X-Content-Type-Options"], "nosniff");
11
+ // //! assert_eq!(res["X-XSS-Protection"], "1; mode=block");
12
+ // //! ```
12
13
use crate :: headers:: { HeaderName , Headers } ;
13
14
pub use csp:: { ContentSecurityPolicy , ReportTo , ReportToEndpoint , Source } ;
14
15
15
16
mod csp;
16
17
17
18
/// Apply a set of default protections.
18
19
///
19
- /// ## Examples
20
- /// ```
21
- /// let mut headers = http::Headers::new();
22
- /// http_types::security::default(&mut headers);
23
- /// assert_eq!(headers["X-Content-Type-Options"], "nosniff");
24
- /// assert_eq!(headers["X-XSS-Protection"], "1; mode=block");
25
- /// ```
20
+ // /// ## Examples
21
+ // /// ```
22
+ // /// use http_types::Response;
23
+ // ///
24
+ // /// let mut res = Response::new(StatusCode::Ok);
25
+ // /// http_types::security::default(&mut headers);
26
+ // /// assert_eq!(headers["X-Content-Type-Options"], "nosniff");
27
+ // /// assert_eq!(headers["X-XSS-Protection"], "1; mode=block");
28
+ // /// ```
26
29
pub fn default ( mut headers : impl AsMut < Headers > ) {
27
30
dns_prefetch_control ( & mut headers) ;
28
31
nosniff ( & mut headers) ;
@@ -36,12 +39,14 @@ pub fn default(mut headers: impl AsMut<Headers>) {
36
39
///
37
40
/// [read more](https://helmetjs.github.io/docs/dns-prefetch-control/)
38
41
///
39
- /// ## Examples
40
- /// ```
41
- /// let mut headers = http::Headers::new();
42
- /// http_types::security::dns_prefetch_control(&mut headers);
43
- /// assert_eq!(headers["X-DNS-Prefetch-Control"], "on");
44
- /// ```
42
+ // /// ## Examples
43
+ // /// ```
44
+ // /// use http_types::Response;
45
+ // ///
46
+ // /// let mut res = Response::new(StatusCode::Ok);
47
+ // /// http_types::security::dns_prefetch_control(&mut headers);
48
+ // /// assert_eq!(headers["X-DNS-Prefetch-Control"], "on");
49
+ // /// ```
45
50
#[ inline]
46
51
pub fn dns_prefetch_control ( mut headers : impl AsMut < Headers > ) {
47
52
headers
@@ -63,12 +68,14 @@ pub enum FrameOptions {
63
68
///
64
69
/// [read more](https://helmetjs.github.io/docs/frameguard/)
65
70
///
66
- /// ## Examples
67
- /// ```
68
- /// let mut headers = http::Headers::new();
69
- /// http_types::security::frameguard(&mut headers, None);
70
- /// assert_eq!(headers["X-Frame-Options"], "sameorigin");
71
- /// ```
71
+ // /// ## Examples
72
+ // /// ```
73
+ // /// use http_types::Response;
74
+ // ///
75
+ // /// let mut res = Response::new(StatusCode::Ok);
76
+ // /// http_types::security::frameguard(&mut headers, None);
77
+ // /// assert_eq!(headers["X-Frame-Options"], "sameorigin");
78
+ // /// ```
72
79
#[ inline]
73
80
pub fn frameguard ( mut headers : impl AsMut < Headers > , guard : Option < FrameOptions > ) {
74
81
let kind = match guard {
@@ -83,13 +90,15 @@ pub fn frameguard(mut headers: impl AsMut<Headers>, guard: Option<FrameOptions>)
83
90
///
84
91
/// [read more](https://helmetjs.github.io/docs/hide-powered-by/)
85
92
///
86
- /// ## Examples
87
- /// ```
88
- /// let mut headers = http::Headers::new();
89
- /// headers.as_mut().insert("X-Powered-By", "Tide/Rust".parse().unwrap());
90
- /// http_types::security::hide_powered_by(&mut headers);
91
- /// assert_eq!(headers.get("X-Powered-By"), None);
92
- /// ```
93
+ // /// ## Examples
94
+ // /// ```
95
+ // /// use http_types::Response;
96
+ // ///
97
+ // /// let mut res = Response::new(StatusCode::Ok);
98
+ // /// headers.as_mut().insert("X-Powered-By", "Tide/Rust".parse().unwrap());
99
+ // /// http_types::security::hide_powered_by(&mut headers);
100
+ // /// assert_eq!(headers.get("X-Powered-By"), None);
101
+ // /// ```
93
102
#[ inline]
94
103
pub fn hide_powered_by ( mut headers : impl AsMut < Headers > ) {
95
104
headers
@@ -104,12 +113,14 @@ pub fn hide_powered_by(mut headers: impl AsMut<Headers>) {
104
113
///
105
114
/// [read more](https://helmetjs.github.io/docs/hsts/)
106
115
///
107
- /// ## Examples
108
- /// ```
109
- /// let mut headers = http::Headers::new();
110
- /// http_types::security::hsts(&mut headers);
111
- /// assert_eq!(headers["Strict-Transport-Security"], "max-age=5184000");
112
- /// ```
116
+ // /// ## Examples
117
+ // /// ```
118
+ // /// use http_types::Response;
119
+ // ///
120
+ // /// let mut res = Response::new(StatusCode::Ok);
121
+ // /// http_types::security::hsts(&mut headers);
122
+ // /// assert_eq!(headers["Strict-Transport-Security"], "max-age=5184000");
123
+ // /// ```
113
124
#[ inline]
114
125
pub fn hsts ( mut headers : impl AsMut < Headers > ) {
115
126
headers
@@ -123,12 +134,14 @@ pub fn hsts(mut headers: impl AsMut<Headers>) {
123
134
///
124
135
/// [read more](https://helmetjs.github.io/docs/dont-sniff-mimetype/)
125
136
///
126
- /// ## Examples
127
- /// ```
128
- /// let mut headers = http::Headers::new();
129
- /// http_types::security::nosniff(&mut headers);
130
- /// assert_eq!(headers["X-Content-Type-Options"], "nosniff");
131
- /// ```
137
+ // /// ## Examples
138
+ // /// ```
139
+ // /// use http_types::Response;
140
+ // ///
141
+ // /// let mut res = Response::new(StatusCode::Ok);
142
+ // /// http_types::security::nosniff(&mut headers);
143
+ // /// assert_eq!(headers["X-Content-Type-Options"], "nosniff");
144
+ // /// ```
132
145
#[ inline]
133
146
pub fn nosniff ( mut headers : impl AsMut < Headers > ) {
134
147
headers
@@ -141,12 +154,14 @@ pub fn nosniff(mut headers: impl AsMut<Headers>) {
141
154
///
142
155
/// [read more](https://helmetjs.github.io/docs/xss-filter/)
143
156
///
144
- /// ## Examples
145
- /// ```
146
- /// let mut headers = http::Headers::new();
147
- /// http_types::security::xss_filter(&mut headers);
148
- /// assert_eq!(headers["X-XSS-Protection"], "1; mode=block");
149
- /// ```
157
+ // /// ## Examples
158
+ // /// ```
159
+ // /// use http_types::Response;
160
+ // ///
161
+ // /// let mut res = Response::new(StatusCode::Ok);
162
+ // /// http_types::security::xss_filter(&mut headers);
163
+ // /// assert_eq!(headers["X-XSS-Protection"], "1; mode=block");
164
+ // /// ```
150
165
#[ inline]
151
166
pub fn xss_filter ( mut headers : impl AsMut < Headers > ) {
152
167
headers
@@ -183,14 +198,16 @@ pub enum ReferrerOptions {
183
198
/// [Mozilla Developer Network](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy)
184
199
///
185
200
///
186
- /// ## Examples
187
- /// ```
188
- /// let mut headers = http::Headers::new();
189
- /// http_types::security::referrer_policy(&mut headers, Some(http_types::security::ReferrerOptions::UnsafeUrl));
190
- /// http_types::security::referrer_policy(&mut headers, None);
191
- /// let mut referrerValues: Vec<&str> = headers.get_all("Referrer-Policy").iter().map(|x| x.to_str().unwrap()).collect();
192
- /// assert_eq!(referrerValues.sort(), vec!("unsafe-url", "no-referrer").sort());
193
- /// ```
201
+ // /// ## Examples
202
+ // /// ```
203
+ // /// use http_types::Response;
204
+ // ///
205
+ // /// let mut res = Response::new(StatusCode::Ok);
206
+ // /// http_types::security::referrer_policy(&mut headers, Some(http_types::security::ReferrerOptions::UnsafeUrl));
207
+ // /// http_types::security::referrer_policy(&mut headers, None);
208
+ // /// let mut referrerValues: Vec<&str> = headers.get_all("Referrer-Policy").iter().map(|x| x.to_str().unwrap()).collect();
209
+ // /// assert_eq!(referrerValues.sort(), vec!("unsafe-url", "no-referrer").sort());
210
+ // /// ```
194
211
#[ inline]
195
212
pub fn referrer_policy ( mut headers : impl AsMut < Headers > , referrer : Option < ReferrerOptions > ) {
196
213
let policy = match referrer {
0 commit comments