diff options
Diffstat (limited to 'src/librustdoc/html')
| -rw-r--r-- | src/librustdoc/html/format.rs | 27 | ||||
| -rw-r--r-- | src/librustdoc/html/layout.rs | 29 | ||||
| -rw-r--r-- | src/librustdoc/html/layout/tests.rs | 24 | ||||
| -rw-r--r-- | src/librustdoc/html/render/mod.rs | 6 | ||||
| -rw-r--r-- | src/librustdoc/html/static/css/rustdoc.css | 4 | ||||
| -rw-r--r-- | src/librustdoc/html/templates/page.html | 2 |
6 files changed, 86 insertions, 6 deletions
diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index be8a2d511e9..493fdc6fb1b 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -15,11 +15,11 @@ use std::slice; use itertools::{Either, Itertools}; use rustc_abi::ExternAbi; use rustc_ast::join_path_syms; -use rustc_attr_data_structures::{ConstStability, StabilityLevel, StableSince}; use rustc_data_structures::fx::FxHashSet; use rustc_hir as hir; use rustc_hir::def::DefKind; use rustc_hir::def_id::{DefId, LOCAL_CRATE}; +use rustc_hir::{ConstStability, StabilityLevel, StableSince}; use rustc_metadata::creader::{CStore, LoadedMacro}; use rustc_middle::ty::{self, TyCtxt, TypingMode}; use rustc_span::symbol::kw; @@ -368,6 +368,8 @@ pub(crate) enum HrefError { Private, // Not in external cache, href link should be in same page NotInExternalCache, + /// Refers to an unnamable item, such as one defined within a function or const block. + UnnamableItem, } /// This function is to get the external macro path because they are not in the cache used in @@ -479,6 +481,26 @@ fn generate_item_def_id_path( Ok((url_parts, shortty, fqp)) } +/// Checks if the given defid refers to an item that is unnamable, such as one defined in a const block. +fn is_unnamable(tcx: TyCtxt<'_>, did: DefId) -> bool { + let mut cur_did = did; + while let Some(parent) = tcx.opt_parent(cur_did) { + match tcx.def_kind(parent) { + // items defined in these can be linked to, as long as they are visible + DefKind::Mod | DefKind::ForeignMod => cur_did = parent, + // items in impls can be linked to, + // as long as we can link to the item the impl is on. + // since associated traits are not a thing, + // it should not be possible to refer to an impl item if + // the base type is not namable. + DefKind::Impl { .. } => return false, + // everything else does not have docs generated for it + _ => return true, + } + } + return false; +} + fn to_module_fqp(shortty: ItemType, fqp: &[Symbol]) -> &[Symbol] { if shortty == ItemType::Module { fqp } else { &fqp[..fqp.len() - 1] } } @@ -552,6 +574,9 @@ pub(crate) fn href_with_root_path( } _ => original_did, }; + if is_unnamable(cx.tcx(), did) { + return Err(HrefError::UnnamableItem); + } let cache = cx.cache(); let relative_to = &cx.current; diff --git a/src/librustdoc/html/layout.rs b/src/librustdoc/html/layout.rs index 1f92c521d46..2782e8e0058 100644 --- a/src/librustdoc/html/layout.rs +++ b/src/librustdoc/html/layout.rs @@ -8,6 +8,9 @@ use super::static_files::{STATIC_FILES, StaticFiles}; use crate::externalfiles::ExternalHtml; use crate::html::render::{StylePath, ensure_trailing_slash}; +#[cfg(test)] +mod tests; + pub(crate) struct Layout { pub(crate) logo: String, pub(crate) favicon: String, @@ -68,6 +71,13 @@ struct PageLayout<'a> { display_krate_version_extra: &'a str, } +impl PageLayout<'_> { + /// See [`may_remove_crossorigin`]. + fn static_root_path_may_remove_crossorigin(&self) -> bool { + may_remove_crossorigin(&self.static_root_path) + } +} + pub(crate) use crate::html::render::sidebar::filters; pub(crate) fn render<T: Display, S: Display>( @@ -134,3 +144,22 @@ pub(crate) fn redirect(url: &str) -> String { </html>"##, ) } + +/// Conservatively determines if `href` is relative to the current origin, +/// so that `crossorigin` may be safely removed from `<link>` elements. +pub(crate) fn may_remove_crossorigin(href: &str) -> bool { + // Reject scheme-relative URLs (`//example.com/`). + if href.starts_with("//") { + return false; + } + // URL is interpreted as having a scheme iff: it starts with an ascii alpha, and only + // contains ascii alphanumeric or `+` `-` `.` up to the `:`. + // https://url.spec.whatwg.org/#url-parsing + let has_scheme = href.split_once(':').is_some_and(|(scheme, _rest)| { + let mut chars = scheme.chars(); + chars.next().is_some_and(|c| c.is_ascii_alphabetic()) + && chars.all(|c| c.is_ascii_alphanumeric() || c == '+' || c == '-' || c == '.') + }); + // Reject anything with a scheme (`http:`, etc.). + !has_scheme +} diff --git a/src/librustdoc/html/layout/tests.rs b/src/librustdoc/html/layout/tests.rs new file mode 100644 index 00000000000..d4a19ee9abf --- /dev/null +++ b/src/librustdoc/html/layout/tests.rs @@ -0,0 +1,24 @@ +#[test] +fn test_may_remove_crossorigin() { + use super::may_remove_crossorigin; + + assert!(may_remove_crossorigin("font.woff2")); + assert!(may_remove_crossorigin("/font.woff2")); + assert!(may_remove_crossorigin("./font.woff2")); + assert!(may_remove_crossorigin(":D/font.woff2")); + assert!(may_remove_crossorigin("../font.woff2")); + + assert!(!may_remove_crossorigin("//example.com/static.files")); + assert!(!may_remove_crossorigin("http://example.com/static.files")); + assert!(!may_remove_crossorigin("https://example.com/static.files")); + assert!(!may_remove_crossorigin("https://example.com:8080/static.files")); + + assert!(!may_remove_crossorigin("ftp://example.com/static.files")); + assert!(!may_remove_crossorigin("blob:http://example.com/static.files")); + assert!(!may_remove_crossorigin("javascript:alert('Hello, world!')")); + assert!(!may_remove_crossorigin("//./C:")); + assert!(!may_remove_crossorigin("file:////C:")); + assert!(!may_remove_crossorigin("file:///./C:")); + assert!(!may_remove_crossorigin("data:,Hello%2C%20World%21")); + assert!(!may_remove_crossorigin("hi...:hello")); +} diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 872dbbcd19e..a46253237db 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -50,12 +50,10 @@ use std::{fs, str}; use askama::Template; use itertools::Either; use rustc_ast::join_path_syms; -use rustc_attr_data_structures::{ - ConstStability, DeprecatedSince, Deprecation, RustcVersion, StabilityLevel, StableSince, -}; use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet}; -use rustc_hir::Mutability; +use rustc_hir::attrs::{DeprecatedSince, Deprecation}; use rustc_hir::def_id::{DefId, DefIdSet}; +use rustc_hir::{ConstStability, Mutability, RustcVersion, StabilityLevel, StableSince}; use rustc_middle::ty::print::PrintTraitRefExt; use rustc_middle::ty::{self, TyCtxt}; use rustc_span::symbol::{Symbol, sym}; diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index 99b3da8b2cd..c48863b4681 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -1838,6 +1838,10 @@ instead, we check that it's not a "finger" cursor. border-right: 3px solid var(--target-border-color); } +a.tooltip { + font-family: var(--font-family); +} + .code-header a.tooltip { color: inherit; margin-right: 15px; diff --git a/src/librustdoc/html/templates/page.html b/src/librustdoc/html/templates/page.html index 7af99e7097c..398436e3fe1 100644 --- a/src/librustdoc/html/templates/page.html +++ b/src/librustdoc/html/templates/page.html @@ -7,7 +7,7 @@ <meta name="description" content="{{page.description}}"> {# #} <title>{{page.title}}</title> {# #} <script>if(window.location.protocol!=="file:") {# Hack to skip preloading fonts locally - see #98769 #} - 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("")) {# #} + 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("")) {# #} </script> {# #} <link rel="stylesheet" {#+ #} href="{{static_root_path|safe}}{{files.normalize_css}}"> {# #} |
