Skip to content

Commit 2e97165

Browse files
Rollup merge of #144467 - hydro-project:users/mingwes/rustdoc-fix-cors, r=GuillaumeGomez
rustdoc template font links only emit `crossorigin` when needed The `crossorigin` attribute may cause issues when the href is not actually cross-origin. Specifically, the tag causes the browser to send a preflight OPTIONS request to the server even if it is same-origin. Some temperamental servers may reject all CORS preflight requests even if they're actually same-origin, which causes a CORS error and prevents the fonts from loading, even later on. This commit fixes that problem by not emitting `crossorigin` if the url appears to be relative to the same origin.
2 parents 0060d5a + f516d4c commit 2e97165

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

src/librustdoc/html/layout.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ use super::static_files::{STATIC_FILES, StaticFiles};
88
use crate::externalfiles::ExternalHtml;
99
use crate::html::render::{StylePath, ensure_trailing_slash};
1010

11+
#[cfg(test)]
12+
mod tests;
13+
1114
pub(crate) struct Layout {
1215
pub(crate) logo: String,
1316
pub(crate) favicon: String,
@@ -68,6 +71,13 @@ struct PageLayout<'a> {
6871
display_krate_version_extra: &'a str,
6972
}
7073

74+
impl PageLayout<'_> {
75+
/// See [`may_remove_crossorigin`].
76+
fn static_root_path_may_remove_crossorigin(&self) -> bool {
77+
may_remove_crossorigin(&self.static_root_path)
78+
}
79+
}
80+
7181
pub(crate) use crate::html::render::sidebar::filters;
7282

7383
pub(crate) fn render<T: Display, S: Display>(
@@ -134,3 +144,22 @@ pub(crate) fn redirect(url: &str) -> String {
134144
</html>"##,
135145
)
136146
}
147+
148+
/// Conservatively determines if `href` is relative to the current origin,
149+
/// so that `crossorigin` may be safely removed from `<link>` elements.
150+
pub(crate) fn may_remove_crossorigin(href: &str) -> bool {
151+
// Reject scheme-relative URLs (`//example.com/`).
152+
if href.starts_with("//") {
153+
return false;
154+
}
155+
// URL is interpreted as having a scheme iff: it starts with an ascii alpha, and only
156+
// contains ascii alphanumeric or `+` `-` `.` up to the `:`.
157+
// https://url.spec.whatwg.org/#url-parsing
158+
let has_scheme = href.split_once(':').is_some_and(|(scheme, _rest)| {
159+
let mut chars = scheme.chars();
160+
chars.next().is_some_and(|c| c.is_ascii_alphabetic())
161+
&& chars.all(|c| c.is_ascii_alphanumeric() || c == '+' || c == '-' || c == '.')
162+
});
163+
// Reject anything with a scheme (`http:`, etc.).
164+
!has_scheme
165+
}

src/librustdoc/html/layout/tests.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#[test]
2+
fn test_may_remove_crossorigin() {
3+
use super::may_remove_crossorigin;
4+
5+
assert!(may_remove_crossorigin("font.woff2"));
6+
assert!(may_remove_crossorigin("/font.woff2"));
7+
assert!(may_remove_crossorigin("./font.woff2"));
8+
assert!(may_remove_crossorigin(":D/font.woff2"));
9+
assert!(may_remove_crossorigin("../font.woff2"));
10+
11+
assert!(!may_remove_crossorigin("//example.com/static.files"));
12+
assert!(!may_remove_crossorigin("http://example.com/static.files"));
13+
assert!(!may_remove_crossorigin("https://example.com/static.files"));
14+
assert!(!may_remove_crossorigin("https://example.com:8080/static.files"));
15+
16+
assert!(!may_remove_crossorigin("ftp://example.com/static.files"));
17+
assert!(!may_remove_crossorigin("blob:http://example.com/static.files"));
18+
assert!(!may_remove_crossorigin("javascript:alert('Hello, world!')"));
19+
assert!(!may_remove_crossorigin("//./C:"));
20+
assert!(!may_remove_crossorigin("file:////C:"));
21+
assert!(!may_remove_crossorigin("file:///./C:"));
22+
assert!(!may_remove_crossorigin("data:,Hello%2C%20World%21"));
23+
assert!(!may_remove_crossorigin("hi...:hello"));
24+
}

src/librustdoc/html/templates/page.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<meta name="description" content="{{page.description}}"> {# #}
88
<title>{{page.title}}</title> {# #}
99
<script>if(window.___location.protocol!=="file:") {# Hack to skip preloading fonts locally - see #98769 #}
10-
document.head.insertAdjacentHTML("beforeend","{{files.source_serif_4_regular}},{{files.fira_sans_italic}},{{files.fira_sans_regular}},{{files.fira_sans_medium_italic}},{{files.fira_sans_medium}},{{files.source_code_pro_regular}},{{files.source_code_pro_semibold}}".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="{{static_root_path|safe}}${f}">`).join("")) {# #}
10+
document.head.insertAdjacentHTML("beforeend","{{files.source_serif_4_regular}},{{files.fira_sans_italic}},{{files.fira_sans_regular}},{{files.fira_sans_medium_italic}},{{files.fira_sans_medium}},{{files.source_code_pro_regular}},{{files.source_code_pro_semibold}}".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2"{% if !static_root_path_may_remove_crossorigin() %} crossorigin{% endif %} href="{{static_root_path|safe}}${f}">`).join("")) {# #}
1111
</script> {# #}
1212
<link rel="stylesheet" {#+ #}
1313
href="{{static_root_path|safe}}{{files.normalize_css}}"> {# #}

0 commit comments

Comments
 (0)