Skip to content

Commit 340984b

Browse files
rustdoc font links only emit crossorigin when needed
The `crossorigin` attribute may cause issues when the href is not actuall across origins. Specifically, the tag causes the browser to send a preflight OPTIONS request to the href even if it is same-origin. Some tempermental servers may reject all CORS preflect 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 looks like a ___domain-relative url. Co-authored-by: Guillaume Gomez <[email protected]>
1 parent 4b55fe1 commit 340984b

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

src/librustdoc/html/layout.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ struct PageLayout<'a> {
6868
display_krate_version_extra: &'a str,
6969
}
7070

71+
impl PageLayout<'_> {
72+
/// See [`may_remove_crossorigin`].
73+
fn static_root_path_may_remove_crossorigin(&self) -> bool {
74+
may_remove_crossorigin(&self.static_root_path)
75+
}
76+
}
77+
7178
pub(crate) use crate::html::render::sidebar::filters;
7279

7380
pub(crate) fn render<T: Display, S: Display>(
@@ -134,3 +141,50 @@ pub(crate) fn redirect(url: &str) -> String {
134141
</html>"##,
135142
)
136143
}
144+
145+
/// Conservatively determines if `href` is relative to the current origin,
146+
/// so that `crossorigin` may be safely removed from `<link>` elements.
147+
pub(crate) fn may_remove_crossorigin(href: &str) -> bool {
148+
// Reject scheme-relative URLs (`//example.com/`).
149+
if href.starts_with("//") {
150+
return false;
151+
}
152+
// URL is interpreted as having a scheme iff: it starts with an ascii alpha, and only
153+
// contains ascii alphanumeric or `+` `-` `.` up to the `:`.
154+
// https://url.spec.whatwg.org/#url-parsing
155+
let has_scheme = href.split_once(':').is_some_and(|(scheme, _rest)| {
156+
let mut chars = scheme.chars();
157+
chars.next().is_some_and(|c| c.is_ascii_alphabetic())
158+
&& chars.all(|c| c.is_ascii_alphanumeric() || c == '+' || c == '-' || c == '.')
159+
});
160+
// Reject anything with a scheme (`http:`, etc.).
161+
!has_scheme
162+
}
163+
164+
#[cfg(test)]
165+
mod tests {
166+
#[test]
167+
fn test_may_remove_crossorigin() {
168+
use super::may_remove_crossorigin;
169+
170+
assert!(may_remove_crossorigin("font.woff2"));
171+
assert!(may_remove_crossorigin("/font.woff2"));
172+
assert!(may_remove_crossorigin("./font.woff2"));
173+
assert!(may_remove_crossorigin(":D/font.woff2"));
174+
assert!(may_remove_crossorigin("../font.woff2"));
175+
176+
assert!(!may_remove_crossorigin("//example.com/static.files"));
177+
assert!(!may_remove_crossorigin("http://example.com/static.files"));
178+
assert!(!may_remove_crossorigin("https://example.com/static.files"));
179+
assert!(!may_remove_crossorigin("https://example.com:8080/static.files"));
180+
181+
assert!(!may_remove_crossorigin("ftp://example.com/static.files"));
182+
assert!(!may_remove_crossorigin("blob:http://example.com/static.files"));
183+
assert!(!may_remove_crossorigin("javascript:alert('Hello, world!')"));
184+
assert!(!may_remove_crossorigin("//./C:"));
185+
assert!(!may_remove_crossorigin("file:////C:"));
186+
assert!(!may_remove_crossorigin("file:///./C:"));
187+
assert!(!may_remove_crossorigin("data:,Hello%2C%20World%21"));
188+
assert!(!may_remove_crossorigin("hi...:hello"));
189+
}
190+
}

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)