Skip to content

Whitelist rel values - #224

Open
Odonno wants to merge 2 commits into
rust-ammonia:masterfrom
Odonno:feat/whitelist-rel
Open

Whitelist rel values#224
Odonno wants to merge 2 commits into
rust-ammonia:masterfrom
Odonno:feat/whitelist-rel

Conversation

@Odonno

@Odonno Odonno commented Oct 28, 2025

Copy link
Copy Markdown

Closes #223

@notriddle notriddle left a comment

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.

What are you specifically trying to accomplish here? Is there just one rel attribute you want to allow sometimes?

If so, I would consider using an implementation closer to our handling of class instead of trying to pull in the full IANA list off the web at build time.

Comment thread build.rs
Comment on lines +13 to +22
let html = client
.get(LINK_RELATIONS_PAGE)
.header(
reqwest::header::USER_AGENT,
"Mozilla/5.0 (compatible; CopilotBot/1.0)",
)
.send()
.expect("Failed to fetch page")
.text()
.expect("Failed to read response text");

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.

We can't do that. This would break ammonia in build environments like docs.rs that don't have internet access.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

No problem, I can change that.

Comment thread src/lib.rs
Comment on lines +66 to +75
static VALID_RELS: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/src/whitelists/rel.txt"
))
.lines()
.map(str::trim)
.filter(|s| !s.is_empty())
.collect()
});

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.

Many of the IANA-registered rel attributes are not safe (for example, canonical would allow hijacking your search results). You can't just allow all of them; you need to go through each one, make sure it's appropriate to your use case, and only allow the ones that are.

@Odonno Odonno Oct 29, 2025

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The ones that seem legit are at least nofollow and the new ones ugc and sponsored. However, I don't know the exact list of valid rel values.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I asked ChatGPT and it gave me this list, what do you think?

Value Description
alternate Link to an alternate version of the document (e.g., print, translation)
author Link to the author of the document
bookmark Permanent bookmarkable link
external Link to an external site
help Link to a help document
license Link to copyright or licensing information
next Next document in a sequence
nofollow Tells search engines not to follow the link (often used for paid links)
noopener Prevents the new page from accessing window.opener (security measure)
noreferrer Prevents sending the HTTP referrer header
prefetch Suggests the browser should prefetch the linked resource
prev Previous document in a sequence
search Link to a search tool for the document
tag Specifies that the link is a tag (often used in blogs)
ugc User Generated Content — used for links in comments, forums, etc.
sponsored Paid or promotional link — used for affiliate or ad links

@GreenReaper GreenReaper Jun 14, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

rel = "me" (similar to author, "a resource about the author of the link's context" i.e. "this is also me" on profile page links) would be useful for pypa/readme_renderer#305, but I can see why it might not be desired for all use-cases.

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.

Whitelisting rel properties

3 participants