Skip to content

Commit aae5dc6

Browse files
authored
Merge pull request #778 from pietroalbini/headers
Add security headers
2 parents d923d80 + 6be063f commit aae5dc6

File tree

9 files changed

+72
-33
lines changed

9 files changed

+72
-33
lines changed

src/headers.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use rocket::fairing::{Fairing, Info, Kind};
2+
use rocket::http::Header;
3+
use rocket::{Request, Response};
4+
5+
static HEADERS: &[(&str, &str)] = &[
6+
("x-xss-protection", "1; mode=block"),
7+
("strict-transport-security", "max-age=63072000"),
8+
("x-content-type-options", "nosniff"),
9+
("x-frame-options", "DENY"),
10+
("referrer-policy", "no-referrer, strict-origin-when-cross-origin"),
11+
("content-security-policy", "default-src 'none'; script-src 'self'; style-src 'self'; img-src 'self' avatars.githubusercontent.com; font-src 'self'; manifest-src 'self'; frame-src player.vimeo.com"),
12+
];
13+
14+
pub(crate) struct InjectHeaders;
15+
16+
impl Fairing for InjectHeaders {
17+
fn info(&self) -> Info {
18+
Info {
19+
name: "HTTP headers injector",
20+
kind: Kind::Response,
21+
}
22+
}
23+
24+
fn on_response(&self, _request: &Request, response: &mut Response) {
25+
for (key, value) in HEADERS {
26+
response.set_header(Header::new(*key, *value));
27+
}
28+
}
29+
}

src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ extern crate serde_derive;
1818

1919
mod cache;
2020
mod category;
21+
mod headers;
2122
mod production;
2223
mod redirect;
2324
mod rust_version;
@@ -284,6 +285,7 @@ fn main() {
284285

285286
rocket::ignite()
286287
.attach(Template::fairing())
288+
.attach(headers::InjectHeaders)
287289
.mount(
288290
"/",
289291
routes![

templates/components/tools/install-script.hbs renamed to static/scripts/tools-install.js

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1-
<script type="text/javascript">
2-
var platform_override = null;
1+
var platform_override = null;
2+
3+
function vis(elem, apply) {
4+
["db", "di", "dn"].forEach(c => {
5+
if (c === apply) {
6+
elem.classList.add(c);
7+
} else {
8+
elem.classList.remove(c);
9+
}
10+
});
11+
}
312

413
function detect_platform() {
514
"use strict";
@@ -34,7 +43,7 @@ function detect_platform() {
3443
// rust-www/#692 - FreeBSD epiphany!
3544
if (navigator.appVersion.indexOf("FreeBSD")!=-1) {os = "unix";}
3645
}
37-
46+
3847
// Firefox Quantum likes to hide platform and appVersion but oscpu works
3948
if (navigator.oscpu) {
4049
if (navigator.oscpu.indexOf("Windows")!=-1) {os = "win";}
@@ -57,40 +66,40 @@ function adjust_for_platform() {
5766
var unknown_div = document.getElementById("platform-instructions-unknown");
5867
var default_div = document.getElementById("platform-instructions-default");
5968

60-
unix_div.style.display = "none";
61-
win_div.style.display = "none";
62-
unknown_div.style.display = "none";
63-
default_div.style.display = "none";
69+
vis(unix_div, "dn");
70+
vis(win_div, "dn");
71+
vis(unknown_div, "dn");
72+
vis(default_div, "dn");
6473

6574
if (platform == "unix") {
66-
unix_div.style.display = "block";
75+
vis(unix_div, "db");
6776
} else if (platform == "win") {
68-
win_div.style.display = "block";
77+
vis(win_div, "db");
6978
} else if (platform == "unknown") {
70-
unknown_div.style.display = "block";
79+
vis(unknown_div, "db");
7180
} else {
72-
default_div.style.display = "block";
81+
vis(default_div, "db");
7382
}
7483

7584
var platform_specific = document.getElementsByClassName("platform-specific");
7685
for (var el of platform_specific) {
7786
var el_is_not_win = el.className.indexOf("not-win") !== -1;
7887
var el_is_inline = el.tagName.toLowerCase() == "span";
79-
var el_visible_style = "block";
88+
var el_visible_class = "db";
8089
if (el_is_inline) {
81-
el_visible_style = "inline";
90+
el_visible_class = "di";
8291
}
8392
if (platform == "win") {
8493
if (el_is_not_win) {
85-
el.style.display = "none";
94+
vis(el, "dn");
8695
} else {
87-
el.style.display = el_visible_style;
96+
vis(el, el_visible_class);
8897
}
8998
} else {
9099
if (el_is_not_win) {
91-
el.style.display = el_visible_style;
100+
vis(el, el_visible_class);
92101
} else {
93-
el.style.display = "none";
102+
vis(el, "dn");
94103
}
95104
}
96105
}
@@ -131,7 +140,7 @@ function set_up_cycle_button() {
131140

132141
if (idx == key.length) {
133142
if (cycle_button !== null) {
134-
cycle_button.style.display = "block";
143+
vis(cycle_button, "db");
135144
}
136145
unlocked = true;
137146
cycle_platform();
@@ -184,5 +193,3 @@ function check_initial_override() {
184193
set_up_cycle_button();
185194
fill_in_bug_report_values();
186195
}());
187-
188-
</script>

templates/components/footer.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<h4>Social</h4>
3030
<div class="flex flex-row flex-wrap">
3131
<a href="https://twitter.com/rustlang"><img src="/static/images/twitter.svg" alt="twitter logo" title="Twitter"/></a>
32-
<a href="https://www.youtube.com/channel/UCaYhcUwRBNscFNUKTjgPFiA"><img style="padding-top: 6px; padding-bottom:6px" src="/static/images/youtube.svg" alt="youtube logo" title="YouTube"/></a>
32+
<a href="https://www.youtube.com/channel/UCaYhcUwRBNscFNUKTjgPFiA"><img class="pv2" src="/static/images/youtube.svg" alt="youtube logo" title="YouTube"/></a>
3333
<a href="https://discord.gg/rust-lang"><img src="/static/images/discord.svg" alt="discord logo" title="Discord"/></a>
3434
<a href="https://github.com/rust-lang"><img src="/static/images/github.svg" alt="github logo" title="GitHub"/></a>
3535
</div>

templates/components/tools/rustup.hbs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
<div class="row">
2-
<div id="platform-instructions-unix" class="instructions" style="display: block;">
2+
<div id="platform-instructions-unix" class="instructions db">
33
<p>It looks like you’re running macOS, Linux, or another Unix-like OS. To download Rustup and install Rust, run the following in your terminal, then follow the on-screen instructions.</p>
44
<pre><code class="db w-100">curl https://sh.rustup.rs -sSf | sh</code></pre>
55
</div>
6-
<div id="platform-instructions-win" class="instructions" style="display: none;">
6+
<div id="platform-instructions-win" class="instructions dn">
77
<p>It looks like you’re running Windows. To install Rust, download and run</p>
88
<a href="https://win.rustup.rs" class="button button-secondary">rustup‑init.exe</a>
99
<p>...then follow the onscreen instructions.</p>
1010
<p><b>Windows Subsystem for Linux</b></p>
1111
<p>If you’re a Windows Subsystem for Linux user run the following in your terminal, then follow the on-screen instructions to install Rust.</p>
1212
<pre><code class="db w-100">curl https://sh.rustup.rs -sSf | sh</code></pre>
1313
</div>
14-
<div id="platform-instructions-unknown" class="instructions" style="display: none;">
14+
<div id="platform-instructions-unknown" class="instructions dn">
1515
<!-- unrecognized platform: ask for help -->
1616
<p>
1717
Rust runs on Windows, Linux, macOS, FreeBSD and NetBSD. If
@@ -45,7 +45,7 @@
4545
</p>
4646
</div>
4747
</div>
48-
<div id="platform-instructions-default" class="instructions" style="display: none;">
48+
<div id="platform-instructions-default" class="instructions dn">
4949
<div>
5050
<p>
5151
To install Rust, if you are running a Unix such as WSL, Linux or macOS,<br>
@@ -64,3 +64,5 @@
6464
</div>
6565
</div>
6666
</div>
67+
68+
<script type="text/javascript" src="/static/scripts/tools-install.js"></script>

templates/components/what/embedded/get-started.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
</div>
4141
</div>
4242
<hr>
43-
<div style="text-align: center;">
43+
<div class="tc">
4444
<p>
4545
<a href="https://docs.rust-embedded.org/" class="button button-secondary">
4646
More Documentation

templates/components/what/wasm/production.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
</p>
3939
</div>
4040
<div class="four columns">
41-
<a href="https://hacks.mozilla.org/2018/01/oxidizing-source-maps-with-rust-and-webassembly/" style="display: block;">
41+
<a href="https://hacks.mozilla.org/2018/01/oxidizing-source-maps-with-rust-and-webassembly/" class="db">
4242
<img src="/static/images/firefox.png" alt="firefox" />
4343
</a>
4444
</div>

templates/policies/media-guide.hbs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
<ul>
4141
<li>Rust</li>
4242
<li>Cargo</li>
43-
<li><img src="https://www.rust-lang.org/logos/rust-logo-blk.svg" alt="rust logo"></li>
44-
<li><img src="https://www.rust-lang.org/logos/cargo.png" alt="cargo logo"></li>
43+
<li><img src="/logos/rust-logo-blk.svg" alt="rust logo"></li>
44+
<li><img src="/logos/cargo.png" alt="cargo logo"></li>
4545
</ul>
4646
<p>Trademarks are names and designs that tell the world the source of a good or service. Protecting trademarks for an open source project is particularly important. Anyone can change the source code and produce a product from that code, so it’s important that only the original product, or variations that have been approved by the project, use the project’s trademarks. By limiting use of the Rust Trademarks, Mozilla and the Rust project can help users and developers know they’re getting the product produced by the Rust project and not someone else’s modified version. The trademark assures users and developers of the quality and safety of the product they’re using.</p>
4747
</div>

templates/tools/install.hbs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@
5656
<h3>Configuring the <code>PATH</code> environment variable</h3>
5757
<p>
5858
In the Rust development environment, all tools are installed to the
59-
<span class="platform-specific not-win" style="display: inline;">
59+
<span class="platform-specific not-win di">
6060
<code>~/.cargo/bin</code>
6161
</span>
62-
<span class="platform-specific win" style="display: none;">
62+
<span class="platform-specific win dn">
6363
<code>%USERPROFILE%\.cargo\bin</code>
6464
</span> directory,
6565
and this is where you will find the Rust toolchain, including
@@ -81,7 +81,7 @@
8181
console fails, this is the most likely reason.
8282
</p>
8383
</div>
84-
<div class="platform-specific win" style="display: none;">
84+
<div class="platform-specific win dn">
8585
<h3>Windows considerations</h3>
8686
<p>
8787
On Windows, Rust additionally requires the C++ build tools
@@ -116,7 +116,6 @@
116116
<a href="https://forge.rust-lang.org/other-installation-methods.html" class="button button-secondary">Learn more</a>
117117
</div>
118118
</section>
119-
{{> components/tools/install-script }}
120119

121120
{{/inline}}
122121
{{~> (parent)~}}

0 commit comments

Comments
 (0)