Skip to content

Commit b38753e

Browse files
Add 'X-DNS-Prefetch-Control' header to helmet.
Co-authored-by: Sergio Benitez <sb@sergio.bz>
1 parent ac4cca7 commit b38753e

File tree

3 files changed

+64
-8
lines changed

3 files changed

+64
-8
lines changed

contrib/lib/src/helmet/mod.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@
2323
//!
2424
//! # Supported Headers
2525
//!
26-
//! | HTTP Header | Description | Policy | Default? |
27-
//! | --------------------------- | -------------------------------------- | ------------ | -------- |
28-
//! | [X-XSS-Protection] | Prevents some reflected XSS attacks. | [`XssFilter`] | ✔ |
29-
//! | [X-Content-Type-Options] | Prevents client sniffing of MIME type. | [`NoSniff`] | ✔ |
30-
//! | [X-Frame-Options] | Prevents [clickjacking]. | [`Frame`] | ✔ |
31-
//! | [Strict-Transport-Security] | Enforces strict use of HTTPS. | [`Hsts`] | ? |
32-
//! | [Expect-CT] | Enables certificate transparency. | [`ExpectCt`] | ✗ |
33-
//! | [Referrer-Policy] | Enables referrer policy. | [`Referrer`] | ✗ |
26+
//! | HTTP Header | Description | Policy | Default? |
27+
//! | --------------------------- | -------------------------------------- | ------------- | -------- |
28+
//! | [X-XSS-Protection] | Prevents some reflected XSS attacks. | [`XssFilter`] | ✔ |
29+
//! | [X-Content-Type-Options] | Prevents client sniffing of MIME type. | [`NoSniff`] | ✔ |
30+
//! | [X-Frame-Options] | Prevents [clickjacking]. | [`Frame`] | ✔ |
31+
//! | [Strict-Transport-Security] | Enforces strict use of HTTPS. | [`Hsts`] | ? |
32+
//! | [Expect-CT] | Enables certificate transparency. | [`ExpectCt`] | ✗ |
33+
//! | [Referrer-Policy] | Enables referrer policy. | [`Referrer`] | ✗ |
34+
//! | [X-DNS-Prefetch-Control] | Controls browser DNS prefetching. | [`Prefetch`] | ✗ |
3435
//!
3536
//! <small>? If TLS is enabled when the application is launched, in a
3637
//! non-development environment (e.g., staging or production), HSTS is
@@ -43,6 +44,7 @@
4344
//! [Strict-Transport-Security]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Strict-Transport-Security
4445
//! [Expect-CT]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Expect-CT
4546
//! [Referrer-Policy]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy
47+
//! [X-DNS-Prefetch-Control]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-DNS-Prefetch-Control
4648
//! [clickjacking]: https://en.wikipedia.org/wiki/Clickjacking
4749
//!
4850
//! [`XssFilter`]: helmet::XssFilter
@@ -51,6 +53,7 @@
5153
//! [`Hsts`]: helmet::Hsts
5254
//! [`ExpectCt`]: helmet::ExpectCt
5355
//! [`Referrer`]: helmet::Referrer
56+
//! [`Prefetch`]: helmet::Prefetch
5457
//!
5558
//! # Usage
5659
//!

contrib/lib/src/helmet/policy.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,14 @@ macro_rules! impl_policy {
9090
)
9191
}
9292

93+
// Keep this in-sync with the top-level module docs.
9394
impl_policy!(XssFilter, "X-XSS-Protection");
9495
impl_policy!(NoSniff, "X-Content-Type-Options");
9596
impl_policy!(Frame, "X-Frame-Options");
9697
impl_policy!(Hsts, "Strict-Transport-Security");
9798
impl_policy!(ExpectCt, "Expect-CT");
9899
impl_policy!(Referrer, "Referrer-Policy");
100+
impl_policy!(Prefetch, "X-DNS-Prefetch-Control");
99101

100102
/// The [Referrer-Policy] header: controls the value set by the browser for the
101103
/// [Referer] header.
@@ -399,3 +401,36 @@ impl Into<Header<'static>> for &XssFilter {
399401
Header::new(XssFilter::NAME, policy_string)
400402
}
401403
}
404+
405+
/// The [X-DNS-Prefetch-Control] header: controls browser DNS prefetching.
406+
///
407+
/// Tells the browser if it should perform domain name resolution on both links
408+
/// that the user may choose to follow as well as URLs for items referenced by
409+
/// the document including images, CSS, JavaScript, and so forth. Disabling
410+
/// prefetching is useful if you don't control the link on the pages, or know
411+
/// that you don't want to leak information to these domains.
412+
///
413+
/// [X-DNS-Prefetch-Control]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-DNS-Prefetch-Control
414+
pub enum Prefetch {
415+
/// Enables DNS prefetching. This is the browser default.
416+
On,
417+
/// Disables DNS prefetching. This is the helmet policy default.
418+
Off,
419+
}
420+
421+
impl Default for Prefetch {
422+
fn default() -> Prefetch {
423+
Prefetch::Off
424+
}
425+
}
426+
427+
impl Into<Header<'static>> for &Prefetch {
428+
fn into(self) -> Header<'static> {
429+
let policy_string = match self {
430+
Prefetch::On => "on",
431+
Prefetch::Off => "off",
432+
};
433+
434+
Header::new(Prefetch::NAME, policy_string)
435+
}
436+
}

contrib/lib/tests/helmet.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,22 @@ mod helmet_tests {
130130
"max-age=30, enforce, report-uri=\"https://www.google.com\"");
131131
});
132132
}
133+
134+
#[test]
135+
fn prefetch_test() {
136+
let helmet = SpaceHelmet::default().enable(Prefetch::default());
137+
dispatch!(helmet, |response: LocalResponse<'_>| {
138+
assert_header!(response, "X-DNS-Prefetch-Control", "off");
139+
});
140+
141+
let helmet = SpaceHelmet::default().enable(Prefetch::Off);
142+
dispatch!(helmet, |response: LocalResponse<'_>| {
143+
assert_header!(response, "X-DNS-Prefetch-Control", "off");
144+
});
145+
146+
let helmet = SpaceHelmet::default().enable(Prefetch::On);
147+
dispatch!(helmet, |response: LocalResponse<'_>| {
148+
assert_header!(response, "X-DNS-Prefetch-Control", "on");
149+
});
150+
}
133151
}

0 commit comments

Comments
 (0)