Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions crates/mdbook-html/src/html_handlebars/hbs_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ impl HtmlHandlebars {
}

debug!("Emitting redirects");
detect_redirect_loops(redirects)?;
let redirects = combine_fragment_redirects(redirects);

for (original, (dest, fragment_map)) in redirects {
Expand Down Expand Up @@ -639,6 +640,69 @@ struct RenderChapterContext<'a> {
chapter_titles: &'a HashMap<PathBuf, String>,
}

/// Returns the redirect source path used in `[output.html.redirect]` keys.
fn canonical_redirect_source(path: &str) -> String {
if path.starts_with('/') {
path.to_string()
} else {
format!("/{path}")
}
}

fn redirect_destination_is_external(dest: &str) -> bool {
let dest = dest.trim();
dest.starts_with("http://") || dest.starts_with("https://") || dest.starts_with("//")
}

/// Detects cycles in `[output.html.redirect]` before emitting redirect pages.
fn detect_redirect_loops(redirects: &HashMap<String, String>) -> Result<()> {
use std::collections::HashSet;

let mut visited = HashSet::new();
let mut sources: Vec<_> = redirects.keys().collect();
sources.sort();
for start in sources {
if visited.contains(start) {
continue;
}
let mut path: Vec<&str> = Vec::new();
let mut current = start.as_str();
loop {
if let Some(pos) = path.iter().position(|&p| p == current) {
let mut cycle = String::new();
for source in &path[pos..] {
if !cycle.is_empty() {
cycle.push_str(" -> ");
}
cycle.push_str(source);
if let Some(dest) = redirects.get(*source) {
cycle.push_str(" -> ");
cycle.push_str(dest);
}
}
bail!("redirect loop detected: {cycle}");
}
visited.insert(current.to_owned());
path.push(current);
let Some(dest) = redirects.get(current) else {
break;
};
if redirect_destination_is_external(dest) {
break;
}
let canonical = canonical_redirect_source(dest);
let Some((next, _)) = redirects
.get_key_value(&canonical)
.or_else(|| redirects.get_key_value(dest))
else {
break;
};
current = next.as_str();
}
}
Ok(())
}

/// Redirect mapping.
///
/// The key is the source path (like `foo/bar.html`). The value is a tuple
Expand Down
15 changes: 15 additions & 0 deletions tests/testsuite/redirects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ There must be an entry without the `#` fragment to determine the default destina
});
}

// Redirect loops should fail at build time.
#[test]
fn redirect_loop() {
BookTest::from_dir("redirects/redirect_loop").run("build", |cmd| {
cmd.expect_failure().expect_stderr(str![[r#"
INFO Book building has started
INFO Running the html backend
ERROR Rendering failed
[TAB]Caused by: Unable to emit redirects
[TAB]Caused by: redirect loop detected: /chapter_1.html#a -> chapter_2.html#b -> /chapter_2.html#b -> chapter_1.html#a

"#]]);
});
}

// Invalid redirect for an existing page.
#[test]
fn redirect_existing_page() {
Expand Down
6 changes: 6 additions & 0 deletions tests/testsuite/redirects/redirect_loop/book.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[book]
title = "redirect_loop"

[output.html.redirect]
"/chapter_1.html#a" = "chapter_2.html#b"
"/chapter_2.html#b" = "chapter_1.html#a"
4 changes: 4 additions & 0 deletions tests/testsuite/redirects/redirect_loop/src/SUMMARY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Summary

- [Chapter 1](./chapter_1.md)
- [Chapter 2](./chapter_2.md)
1 change: 1 addition & 0 deletions tests/testsuite/redirects/redirect_loop/src/chapter_1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Chapter 1
1 change: 1 addition & 0 deletions tests/testsuite/redirects/redirect_loop/src/chapter_2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Chapter 2
Loading