Skip to content

fix(markdown): keep inline image data out of the markdown - #105

Closed
tobocop2 wants to merge 5 commits into
integration/0927from
fix/markdown-inline-image-data
Closed

tobocop2 wants to merge 5 commits into
integration/0927from
fix/markdown-inline-image-data

Conversation

@tobocop2

@tobocop2 tobocop2 commented Sep 26, 2026 •

Copy link
Copy Markdown
Member

I'm planning to replace crawl4ai with crawlberg, and I need saved markdown to stay a reasonable size on ordinary pages.

Problem

An <img> whose address is a data: URL wrote its whole encoded payload into the markdown. The page from the issue produced 319 bytes of markdown for two sentences and one small icon.

Fix

The address of an inline image is now left empty before conversion, so the markdown keeps the alt text as ![icon](<>) and the payload stays out. The same page now produces 67 bytes. This applies to src, the lazy-load attributes, and data: candidates in srcset and data-srcset. When a lazy-load attribute or srcset has a real URL, the markdown still uses that URL. fit_content follows the same rule, and it now keeps these ![alt](<>) lines where it used to prune them.

Video, audio, iframe and source

A data: address on a <video>, <audio>, <iframe> or <source> is emptied the same way, so a video or audio element falls back to a real nested source when it has one.

Raw text

The pre-pass now reads link targets from the page with script, style, title and textarea text masked, so a <base> written inside a title or a <!-- inside a script no longer steers it.

Refs #120

Closes #97

@Goldziher

Copy link
Copy Markdown
Member

State

Base is main, so real CI ran: 24 checks. git merge-tree against origin/main (70844b7ea) merges cleanly. The branch is 3 commits behind main; the five entries the changelog diff appears to drop (#148, #131, #146, #179, #180) are not in the merge-base 538e6391c either — the branch is just behind, nothing was deleted.

The one red check, Validate Rust → Validate vendored C headers match canonical (packages/csharp/.../osx-arm64/.../crawlberg.h, 577/520 vs 579/522), also fails on 70844b7ea itself. Pre-existing, not this PR.

Changelog: entry is under [Unreleased] → ### Fixed, and the [Unreleased] block is byte-identical between CHANGELOG.md and docs-site/src/content/docs/changelog.md (extracted with the same awk the gate uses; both abfc8c49…). No public type, field or doc comment changes, so no binding regeneration is implied and no alef:hash: file is touched.

fit_content is claimed but neither implemented nor tested

The PR body and the changelog both say fit_content "follows the same rule, and it now keeps these ![alt](<>) lines where it used to prune them". crates/crawlberg/src/pruning.rs is not in the diff, and the only assertions on it are:

let fit = md.fit_content.clone().unwrap_or_default();
// ...
assert!(!text.contains("PHN2Zy"), ...);
assert!(text.len() < 200, ...);

Both hold when fit_content is None. Nothing asserts the ![icon] line survives pruning, which is the actual claim. Add assert!(fit.contains("![icon]")) to test_markdown_leaves_out_the_payload_of_an_inline_image, or drop the sentence from the body and the changelog entry.

The fix is <img>-only, and TARGETS already knows the other carriers

drop_inline_data is name.eq_ignore_ascii_case(b"img"). But TARGETS lists source/video/audio/iframe src precisely because the converter renders them: in html-to-markdown-rs 3.14.3, converter/media/embedded.rs::find_source_src reads a nested <source src> and emits it as a link. So <video src="data:video/mp4;base64,…">, <audio src="data:…">, <iframe src="data:text/html,…"> and <picture><source src="data:…"> still copy the entire payload into the markdown — the same defect #97 reports, one element over. Either extend drop_inline_data to those names (a one-line change given the existing table) or say in the changelog that only <img> is covered.

The empty src="" and #190

The rewrite leaves src="". markdown.rs's existing ~keep already names this as the reason base_url cannot be set until #123. Note for whoever acts on #190 that #123 does not fully clear it either: resolve_candidates(list, base, true) over a list whose every candidate is data: returns Some(""), so srcset="" survives there, and #123's attribute_removal(...).or_else(|| value_edit(html, raw, "")) falls back to emptying when it cannot locate the attribute name. The ~keep will be stale the moment #123 lands unless it is narrowed.

Changelog attribution

The #97 entry also describes the raw-text behaviour ("also reads script, style, title and textarea text as text…"), which is #102's subject and is what #123 files separately. If #105 lands alone that sentence credits the wrong issue. Move it or let #123 carry it.

Verified, for the record

  • ~keep Masking keeps every byte offset is true: raw_text.rs's MARKUP_MASK is a single ASCII space substituted for <, so offsets and char boundaries are identical.
  • "the converter then falls back to the image's other address attributes" is true: resolve_effective_src in handlers/image.rs (3.14.3) falls through from an empty or data: src to data-src/data-lazy-src/data-original, then data-srcset/srcset.
  • link_targets.rs is 474 lines, collect_edits 42 — both under poly's limits. No println!/eprintln!/dbg!.
  • a_base_written_inside_title_text_does_not_count and a_comment_opener_inside_script_text_does_not_hide_a_later_link both fail with the masking reverted — real coverage, not guards.

Fine to merge once the fit_content claim is either tested or removed. The <source>/<video>/<iframe> gap and the src="" note can be follow-ups, but the changelog should not promise more than the diff does.

Not verified

No build, no cargo check, no cargo clippy, no tests, no poly lint, no poly fmt --check, no alef, no task. Everything above is from reading the diff, the merge-base history, gh check output, and the vendored html-to-markdown-rs 3.14.3 / html5ever 0.40.1 sources in the local cargo registry. I did not run the new tests and cannot confirm they pass or that the crate compiles.

@tobocop2

Copy link
Copy Markdown
Member Author

Thanks for the review. Every point is answered below, and the changes are in 8a52814de.

State

Agreed on all three. The branch is behind main and deletes no changelog entry, and the vendored header check also fails on main (#162). The [Unreleased] mirror still matches: ./scripts/ci/check-changelog-mirror.sh gives ok: the [Unreleased] blocks match.

fit_content is claimed but neither implemented nor tested

The claim holds, and a test now pins it. The pruner drops a mostly-link line only when it is longer than 20 bytes. ![icon](<>) is 11 bytes, so it stays; the old line with the payload was long and all link, so it went. The scrape test now asserts that fit_content contains ![icon]. With fit_content forced to None the test fails with fit_content must keep the image line, got "", and with the pruner threshold lowered to 5 it fails too.

The fix is <img>-only

Extended. A data: address on <video>, <audio>, <iframe> and <source> is now emptied as well, and a video or audio element then falls back to its nested real <source>. Before, <video src="data:..."> gave [data:video/mp4;base64,...](data:video/mp4;base64,...); now it gives no link. <picture><source> was never a carrier: the converter renders only the picture's <img>, so <picture><source src="data:..."><img src="real.png" alt="p"></picture> already gave ![p](https://example.com/real.png) on main. data: links on <a> stay with #120.

The empty src="" and #190

Agreed, no change here. #105 empties the address; #123 removes the attribute for images and graphics, and when #123 is updated onto this commit it will do the same for the media elements. The two gaps you found in #123 (a srcset whose every candidate is data: becomes srcset="", and the removal falls back to emptying when it cannot find the attribute name) will be fixed in #123. The #190 order is unchanged.

Changelog attribution

Fixed. The #97 entry now covers only the inline data change: images, fit_content and the media elements. The raw-text sentences move to #123's #102 entry when #123 is updated onto this commit.

Not verified

Run on this tree merged with main, Linux x86_64: cargo fmt --all -- --check rc=0; workspace clippy with --all-features --all-targets -D warnings (without the php crate) rc=0; cargo check -p crawlberg --target wasm32-unknown-unknown --locked rc=0; cargo test -p crawlberg --all-features --no-fail-fast rc=0, 1167 passed, 0 failed.

tobocop2 added a commit that referenced this pull request Sep 26, 2026
…ext-base-graphic

The data: rule now covers #105's full media list (img, video, audio, iframe,
source) plus graphic, and removes the attribute on every one of them rather
than emptying it. The two raw-text sentences #105 dropped from its #97
changelog entry move into the #102 entry, in both changelogs.
@tobocop2
tobocop2 added this pull request to stack #305 September 27, 2026 00:29
@tobocop2
tobocop2 marked this pull request as ready for review September 27, 2026 03:36
@Goldziher

Copy link
Copy Markdown
Member

Correct and well tested — ship it — but it bundles a second, undocumented behaviour change that is security-relevant and needs a changelog line before merge.

html/link_targets.rs switches resolve_link_targets to parse mask_raw_text_markup(html). That fixes a <base href> inside <title> hijacking every markdown link. Executed:

<title>x <base href="https://evil.example/"></title><p><a href="leaf.html">x</a></p>   at https://example.com/dir/page.html

main    -> "x\n\n[x](https://evil.example/leaf.html)\n"
PR 105  -> "x\n\n[x](https://example.com/dir/leaf.html)\n"

A good fix, and a real one — but this PR's changelog entry covers only #97, and the same behaviour is claimed by open PRs #123 (Closes #102 #103 #113) and #232 (Closes #201). Add one line, and decide which of 105/123/232 owns it so it is not credited three times.

Two notes on #114, which this PR is filed against:

Severity is lower than #114 implies. main already emitted a contentless ![](…) line for this input — 105 removes the kilobytes, not the line. Executed: main gives ![](data:image/png;base64,iVBOR…), 105 gives ![](<>) (~7 bytes). Root cause is in the dependency, html-to-markdown-rs-3.14.3/src/converter/handlers/image.rs:380, which always emits ![alt](dest) with no skip-when-empty branch.

#114's architectural reason is wrong, though its conclusion stands. #114 says the pre-pass "only rewrites attribute spans and cannot delete an element". It can: astral-tl-0.8.0 exposes HTMLTag::raw() -> &Bytes<'a>, and as_bytes_borrowed() gives a source-borrowed slice, so a span over the whole element with an empty replacement deletes it. The conclusion is still right — "emit the alt text alone, or nothing" is a markdown-shaping decision, and expressing it as an HTML span splice (delete <img>, keep the <video><source> subtree) is more fragile than handling it where the markdown is produced.

Separately, <graphic url="data:"> is still not covered: INLINE_DATA_ELEMENTS is ["img","video","audio","iframe","source"] while TARGETS also contains graphic. Pre-existing and identical on main, so a follow-up rather than a blocker. <source srcset="data:"> is not a sibling hole — the converter reads only src on <source>, and <picture> descends to the inner <img>.

@Goldziher

Copy link
Copy Markdown
Member

Triage note: the approach here is a workaround for a converter defect, and it is the one thing blocking base_url.

Emptying src/srcset before conversion mangles the HTML so the converter will not print a payload it should not print. The converter writes img src verbatim (converter/handlers/image.rs) and already recognises data: there (handlers/image.rs:95, the inline-images collector) — it has no option for what to emit instead. That option belongs in xberg-io/html-to-markdown on ConversionOptions; it removes this PR's rewrite, and #113, #120 and the ![](<>) of #114 with it, and it clears the src="" sentinel that #190 names as blocker 1 for adopting ConversionOptions::base_url.

An <img> whose address is a data: URL copied its whole encoded payload
into the markdown. Before conversion, the relative-link rewrite now
empties data: addresses in src and the lazy-load attributes (data-src,
data-lazy-src, data-original), and drops data: candidates from srcset
and data-srcset. The converter then writes the alt text with an empty
address. A real URL in a lazy-load attribute or srcset still wins.

The markdown, fit_content and wasm output all come from the same
conversion, so all three follow the rule. The images list already
skipped data: sources. data: links on <a> and media stay as written.
The merge from main placed it after the relative-links entries, which
main keeps under Added.
The markdown pre-pass parsed the raw page, so markup inside script, style,
title and textarea text could steer it: a <base> inside a title replaced the
page's base address, and a "<!--" inside a script hid every later tag, which
left relative links unresolved and let an inline image's data payload through.

It now parses the masked copy that link extraction already uses and reads each
attribute value from the original page at the same span. Masking keeps the byte
length, and reading from the original keeps a value exact where the masking
scan disagrees with the parser about quotes.
…e too

The pre-pass emptied a data: address only on images, so a data: video, audio,
iframe or source address was written into the markdown in full. These elements
now get the same treatment, and a video or audio element falls back to a real
nested source.

The scrape test now asserts that fit_content keeps the image line, which the
PR claimed and no test checked, and the changelog entry for #97 no longer
describes the raw-text fix, which belongs to #123.
@Goldziher
Goldziher force-pushed the fix/markdown-inline-image-data branch from 416fd29 to 2e84da7 Compare September 27, 2026 08:53
…mage-data

# Conflicts:
#	CHANGELOG.md
#	docs-site/src/content/docs/changelog.md
@tobocop2

Copy link
Copy Markdown
Member Author

Agreed on the direction. The markdown half of this stack waits for the converter fixes rather than landing as a rewrite of the HTML. The data image payload is now xberg-io/html-to-markdown#528, an option for what to print instead of the payload, and it covers the graphic element too. When that ships, crawlberg adopts it and this PR keeps only the parts that belong to crawlberg. The base-in-title fix gets its changelog line in whichever PR keeps the raw-text masking, so it is credited once.

@tobocop2
tobocop2 removed this pull request from stack #305 September 27, 2026 22:07
@tobocop2
tobocop2 changed the base branch from main to integration/0927 September 27, 2026 22:07
@tobocop2
tobocop2 added this pull request to stack #455 September 27, 2026 22:07
@tobocop2

Copy link
Copy Markdown
Member Author

The data payload fix now lives in html-to-markdown, which gained an option for what to print for an inline data image (xberg-io/html-to-markdown#566), and the base-in-title fix is there too (xberg-io/html-to-markdown#527). crawlberg adopts both through the version bump (#405) and the base_url adoption (#190), which removes this link rewrite, so #97 stays open until then.

@tobocop2 tobocop2 closed this Sep 29, 2026
@Goldziher
Goldziher deleted the fix/markdown-inline-image-data branch October 10, 2026 07:50
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.

fix(markdown): an image embedded in the page copies its whole encoded data into the markdown

2 participants