Repository navigation
Check a Markdown link's scheme against the href the document resolves - #929
Conversation
The web-protocol guard read Marked's authored destination while Marked's own renderer wrote that destination into the attribute unescaped, so the string checked and the string emitted were two different readings. An href decodes character references when the markup lands, and `javascript:alert(1)` reads as a relative path to `new URL` and as a script URL to the document: message prose could put a live `javascript:` link in front of a reader. Leaf now writes every link and image itself, escaping the destination into the attribute it was checked as, so the two readings are one.
leaf-agent
left a comment
There was a problem hiding this comment.
Rendering the same destinations through the merge base's markdown.js and this branch's shows the change reaching past the attack it closes: every HTML character reference in a link or image destination now resolves literally instead of decoded.
| message prose | base resolves to | this branch resolves to |
|---|---|---|
[x](https://e.com/?a=1&b=2) |
https://e.com/?a=1&b=2 |
https://e.com/?a=1&b=2 |
[x](https://e.com/?a=1&b=2) |
https://e.com/?a=1&b=2 |
https://e.com/?a=1&b=2 |
 |
the image loads | the image 404s |
This is the reported bug's own mechanism, one step over: a destination is attribute source text, and its character references decode when the markup lands. Marked's default renderer was relying on that decode, and escapeAttribute takes it away, so a legitimate & stops separating query parameters and lands inside one. CommonMark decodes entity references in link destinations, nothing surfaces an error, and an agent that quotes a URL it read out of HTML writes exactly this shape.
It also makes the description's verification false where it matters most — "Rendering 26 link and image cases through the real module against both versions of markdown.js, 3 differ, and only one is a semantic change" misses a fourth difference that is semantic — and that paragraph is what a squash merge writes into main.
One reading of the destination is the right shape. It just has to be taken at the representation the document resolves, rather than at Marked's authored text: decode the destination's character references once, check that reading, and escape it back into the attribute. Then checked, emitted, and resolved are one string, every legitimate destination reaches what the base reached, and javascript: is refused outright instead of resolving to a relative path with a literal : in it.
The added case also passes vacuously: all(protocol in (...) for protocol in admitted) holds over an empty list, so it stays green whether or not a link was rendered at all.
I'm pushing that reading, and a test that carries both halves of the contract, to this branch next.
How the comparison was taken
Each source string was rendered through the real markdown.js — merge base 3e620e3b, this branch, and the pushed reading — under tests/runtime/dom.mjs, then written into a live element and read back through a.href / img.src, which is the resolution the reader gets. Every case in the description's table reproduces; the entity cases are the ones it does not list.  is a second unlisted change: the base resolved it as a javascript: source, this branch emits an <img> with a literal : in its src, and the pushed reading drops the element and renders the alt text, which is what the base already did for a plain javascript: destination.
Writing the attribute from Marked's authored text made the checked string and the emitted string one string, and took away the decode Marked's own renderer was relying on: every character reference in a link or image destination started resolving literally, so `?a=1&b=2` reached the query as one parameter named `a` with `1&b=2` in it, and an image with that shape 404ed. Take the decode here instead, in the one place a destination is read, so that reading is what `safeUrl` checks and what `escapeAttribute` puts back unchanged. Every legitimate destination resolves where it did before the guard changed, and `javascript:` is refused outright rather than resolving to a relative path carrying a literal `:`. The contract test carries both halves now. Its earlier assertion held vacuously over an empty list of anchors, so it stayed green whether or not a link was rendered; it now names the one href the document is allowed to resolve, beside a live destination whose `&` has to survive.
leaf-agent
left a comment
There was a problem hiding this comment.
The decode landed on the destination, but a destination is not the only attribute this renderer now writes from Marked's source text. token.title and an image's token.text arrive undecoded for exactly the same reason, and both still go straight through escapeAttribute, so every character reference in a title or an alt now resolves literally. The merge base handed those attributes back to Marked, which wrote them where the HTML parser decodes them, so this is a regression introduced by writing every link and image here.
| message prose | base resolves to | this branch resolves to |
|---|---|---|
 |
alt Q&A chart |
alt Q&A chart |
 |
title Sales & revenue |
title Sales & revenue |
[x](https://e.com/ "t&u") |
title t&u |
title t&u |
aria-label="View ${label}" on the page-media button is built from that same token.text, so the accessible name takes it too. The reach is wider than message prose: widget-api.js re-exports renderMarkdown as the package-author surface, and conversation/thread-card.js renders range summaries through it.
readHref is the right primitive; it is just named and applied as if destinations were the only source text Marked hands over. Taking the same reading for the title and the alt closes it, and the description's own sentence — "Checked, emitted, and resolved are one string" — then holds for every attribute the renderer writes rather than for the href alone. I'm pushing that to this branch next, with the existing contract test extended to carry an alt and a title beside the href it already pins.
One smaller difference I am not proposing to change: an autolink destination is now decoded too, and CommonMark does not recognize entity references inside <…>. <https://e.com/?a=1&b=2> reached ?a=1&b=2 on the merge base and reaches ?a=1&b=2 here. Separating it would mean a second reading keyed on which producer made the token, which is worse than the difference; the new behavior is also the likelier intent. Worth a maintainer's eye rather than a fix.
How the comparison was taken
Each source string was rendered through the real markdown.js — merge base 4ffc2bdb, this branch's 310783a6, and the pushed reading — under tests/runtime/dom.mjs, then written into a live element and read back through img.alt, a.title and a.href, which is the resolution the reader gets. With the pushed reading in, every case in the description's own table and every entity case above is byte-identical to the merge base or resolves identically to it; the two intended refusals and the autolink above are what remain.
The scheme guard itself holds against the shapes I tried around it — java	script:, 	javascript:, javascript:, javascript:, javascript:, JaVaScRiPt:, data:, vbscript:, and the image forms of each — because new URL() strips ASCII tab and newline from its input just as the document does. � prefixed to javascript: is the one that still yields an anchor, inert as U+FFFD-prefixed relative path, which the description already covers.
A destination was not the only attribute source text in the renderer. A title and an image's alt arrive undecoded for the same reason, and both went straight through `escapeAttribute`, so every character reference in them resolved literally: `` read `Q&A chart` where the merge base read `Q&A chart`, and the page-media button's accessible name took it from the same text. The merge base handed those attributes back to Marked, which wrote them where the HTML parser decodes them, so writing every link and image here is what moved them. So `readHref` becomes `decoded`, which is what it always was — the one reading of the attribute source text Marked hands a renderer — and the title and the alt take it beside the destination. Checked, emitted and resolved are one string for every attribute the renderer writes. The contract test carries an alt and a title beside the href it already pins, so the reading is held at both halves of what it protects.
leaf-agent
left a comment
There was a problem hiding this comment.
An image's alt — and the page-media button's aria-label built from it — now carries the Markdown that authored it. The decode landed, but the alt Marked's own image renderer writes is not token.text: it is parseInline(token.tokens, textRenderer), the alt with its inline tokens flattened. Writing every image here takes token.text instead, so the markers reach the accessible name.
| message prose | base alt | this branch |
|---|---|---|
 |
a b chart |
a *b* chart |
 |
a b |
a **b** |
 b](https://e.com/p.png) |
a inner b |
a [inner](https://e.com/) b |
Nothing catches it: tests/test_chrome_contracts.py, tests/test_render_conversations.py (97) and npm run test:runtime (33) are all green with it in, and the new contract case pins an alt — Q&A — whose entity survives the decode but which holds no inline markup.
What produced it is the shape this branch has now hit three times. Owning emission means owning every field Marked's default renderer touched, and each field read straight off the token is a fresh divergence: round one the destination's character references, round two the title's and the alt's, this one the alt's inline tokens. The reason the guard needs the href is real; extending that to the rest of the element brings along everything else Marked did to those fields, and the diff re-derives it one field at a time. Whether the renderer should write the element at all, or hand Marked a token whose href already carries the checked reading and let its own renderer emit, is the question I'd want settled before this lands — not another field.
So I'm not pushing a third fix. The inline suggestion is the minimal repair if the current shape is the one to keep; it restores every case above to the base's alt, and the new contract case still passes with it in. It also changes page-media images, whose alt and aria-label read token.text on the merge base too — that is pre-existing rather than this branch's, and the suggestion happens to settle it.
The description's closing claim — that beyond the two refusals only the autolink and the lone surrogate differ — does not hold with this in, and a squash merge writes that paragraph into main.
How the comparison was taken
Each source string was rendered through the real markdown.js under tests/runtime/dom.mjs — merge base 4ffc2bdb, this branch's 03b3eaee, and the suggestion — then written into a live element and read back through img.alt and the media button's aria-label. Cases whose alt holds no inline markup are byte-identical across all three: , , , and an image with a title. Titles and hrefs are unchanged by the suggestion.
this.parser.textRenderer is what the vendored bundle's own image renderer reaches for — i&&(n=this.parser.parseInline(i,this.parser.textRenderer)) in skills/leaf/assets/vendor/marked.esm.js — so the suggestion reads the alt the same way rather than reimplementing the flattening.
The Markdown scheme regression authored an ordinary image at `/icon.svg` without supplying that resource, so its assertions passed but browser teardown failed on HTTP 404. The test now fulfills a dedicated `/chart.png` URL with its existing PNG bytes before navigation. This keeps the ordinary-image rendering path distinct from page-media inspection and preserves #929's URL-scheme safety, entity, alt-text, title and keyboard-preview assertions. Validation: the exact browser test failed before the change with the reported 404 and passes after it, including teardown. Ruff lint and formatting checks pass. Product code is unchanged. Closes #4681 — automated triage Co-authored-by: leaf-agent <[email protected]>
Markdown destinations such as
[press me](javascript:alert(1))passed the scheme guard as relative paths, then became executable links when the document decoded the attribute. Links and images now use Marked’s standard renderer, parse that output in an inert template, and validate the destination the document actually reads before publishing it.Keeping Marked responsible for element rendering preserves flattened image alt text, titles, entity handling, autolinks, and URL normalization. Page-media inspection wraps the same rendered image and derives its accessible button name from that image’s alt.
The browser regression covers encoded executable links and images, ordinary URL entities, autolink entities, formatted alt text, titles, and keyboard inspection of page media. Reintroducing raw token alt text makes it fail. An additional adversarial check refused 18 encoded executable link/image variants.
Validation: 121 chrome/conversation browser tests; 1,273 everyday cases (two rerun after local setup); 33 runtime tests; and pre-commit checks.