Skip to content

Resolve relative links in fetched pages against the page URL - #5

Closed
sharmis-tp wants to merge 2 commits into
cppalliance:masterfrom
sharmis-tp:absolute-links
Closed

sharmis-tp wants to merge 2 commits into
cppalliance:masterfrom
sharmis-tp:absolute-links

Conversation

@sharmis-tp

Copy link
Copy Markdown
Collaborator

web/fetch returned links and image sources as the page wrote them, so /learn came back as [Learn](/learn), which a model cannot fetch. readabilityrs takes the base URL but its fix_relative_urls_in_html ignores it, and htmd never resolves links.

Before either renderer runs, every relative a href and img src is now resolved against the page's final URL, or its first <base href> when that is http or https. Fragments, absolute URLs, and references that resolve to another scheme stay as written. The walk is iterative, so deep nesting cannot overflow the stack.

html5ever and markup5ever_rcdom are the crates htmd already builds on, at its versions, so the lockfile gains no crate.

Tested: six new tests in response.rs (readability mode, raw mode, <base>, a non-http <base>, links left as written, no base URL), and the release library fetching https://www.rust-lang.org/ returns no relative links.

@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ac0c1a20-fcbb-4ee1-b3ee-1058c265484d
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@sharmis-tp sharmis-tp self-assigned this Nov 24, 2026
Err(e) => return soft_or_hard(&e),
};
let base_url = final_url.to_string();
let base_url = final_url.clone();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this making a copy of the buffer or just incrementing an Arc?

@vinniefalco

vinniefalco commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Looks good in principle. Is it possible that this could leak internal network details? Are there security issues?

@sharmis-tp

Copy link
Copy Markdown
Collaborator Author

Leaks: no. The change only rewrites text in the result, so it sends no request and fetches no link. Links resolve against the final URL, which already passed the address policy and is printed on the url: line. The policy refuses userinfo on the request and on every redirect, so no credentials can end up in a link. A page can point its links at an internal address with , but it could already write those as absolute links, and any fetch of one goes through the full address policy.

Security: yes, one, now fixed. Resolving makes each link as long as the base URL, and the page chooses the base with . Many short links under a long base grew the HTML 127x to 1,538x before rendering, enough at the 8 MiB body cap to exhaust memory. The new commit leaves a link as written when its absolute form would be over MAX_URL_CHARS, which a fetch would refuse anyway, and stops resolving once it has added as many bytes as the page holds. Growth is now at most ~2x, and there are two tests for it.

@sharmis-tp

Copy link
Copy Markdown
Collaborator Author

Merged as part of #6.

@sharmis-tp sharmis-tp closed this Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants