Skip to content

Escape Markdown image alt text once: upgrade Astro to 7.4.5 - #67

Draft
MaxGhenis wants to merge 2 commits into
masterfrom
fix/markdown-alt-double-escape
Draft

MaxGhenis wants to merge 2 commits into
masterfrom
fix/markdown-alt-double-escape

Conversation

@MaxGhenis

@MaxGhenis MaxGhenis commented Nov 4, 2026 •

Copy link
Copy Markdown
Owner

Draft until the review of this head agrees.

Problem

Markdown images run through a round trip in Astro: the image's props go into a __ASTRO_IMAGE_ placeholder attribute as JSON, Astro decodes some character references in it, parses the JSON, and escapes the values again for the final <img>. Any reference the decode step misses gets escaped a second time, and the browser then shows it as text. For an image's alt text, a screen reader reads the reference out.

  • Astro 5.16.15 decoded only &#x22;, so an apostrophe came out as alt="…forecasters&amp;#x27; average…" (reported on the dev server on 2026-11-04).
  • Astro 7.1.3, which master runs, also decodes apostrophes, and Sätteri's smart punctuation curls them anyway. But an & still comes out as alt="Tom &amp;amp; Jerry".
  • Astro 7.3.4 fixed the ampersand (withastro/astro#18086).

Change

  • astro ^7.1.3 → ^7.4.5 and @astrojs/mdx ^7.0.3 → ^8.1.2. Both pin @astrojs/markdown-satteri 0.4.6, so the tree holds one Sätteri copy. MDX 8's one breaking change only affects sites that set markdown.processor, and this site doesn't. All three releases carry npm SLSA provenance.
  • parse5 ^7.3.0 and entities ^7.0.1 as dev dependencies. Both already sat in the tree (entities through happy-dom), so the lockfile adds no packages.
  • New tests/attribute-escaping.test.ts:
    • Built pages: parses every dist/**/*.html, once with scripting on and once with it off so that <noscript> markup counts. It fails if any text or URL attribute (alt, title, ARIA labels, placeholder, <meta> content, href, src, srcset and similar) would change if decoded a second time, which means the build escaped it twice. entities' decodeHTMLAttribute applies the browser's own rules, so it recognizes every named and numeric reference and ignores misspelled ones such as &aMp;. The scan skips serialized data, such as astro-island props and data-* attributes, which can hold references legitimately.
    • Double-escape scan: pins the scan's behavior on each case the review raised: a named reference (&amp;copy;), <noscript> markup, a misspelled reference, island props and a data-* attribute.
    • Markdown image pipeline: builds a throwaway Astro project in a temp folder. It uses a content collection .md, a content collection .mdx and a src/pages .md file. Each holds images whose alt and title text cover the reported apostrophe, &, quotes, </>, literal entity text, non-ASCII text (NBSP, emoji) and every ASCII punctuation character. The test requires each decoded alt and title to equal its source text. Each file also holds a backtick in a processed image, a public/ image and a remote image, plus a code span in alt text. The fixture keeps Astro's and Vite's caches in its own folder, so it never writes into the site's node_modules.
  • AGENTS.md: documents the test, and adds a rule to keep literal backticks out of .md image alt and title text (below).

Invariants the test checks

  1. For every text or URL attribute value v in the built site, decodeHTMLAttribute(v) === v: the browser's single decode leaves no character reference behind.
  2. For each of the three Markdown paths and every listed string s, the built img has alt === s and title === s.
  3. A backtick in a processed .md image renders as Back&#x60;tick (today's known bug, pinned). It survives in MDX, in public/ images and in remote images, and code span backticks drop out of alt text.

Remaining upstream gap: backticks

On 7.4.5, a literal backtick in .md image text still comes out as &#x60;. Sätteri writes ` as &#x60; in attributes, and Astro's decode chain (still the same on main) handles only quote, apostrophe and ampersand references. That round trip covers only images Astro processes: relative paths, plus remote hosts that the image config allows (this site allows none). public/ images, other remote images and MDX keep backticks intact. I tested every ASCII punctuation character plus NBSP, soft hyphen, zero-width space, emoji, tab and U+2028, one alt each, and only the backtick fails. Until Astro fixes it:

  • AGENTS.md keeps literal backticks out of all .md image text, since an image can move from public/ to a relative path. Backticks that pair into a code span drop out of alt text, so those are fine.
  • The fixture pins the .md value Back&#x60;tick, so its checks fail in the release that fixes this, and also if the attribute goes missing.
  • The built-pages scan fails on any post that includes one.

What else the upgrade changes in the built site

I diffed every HTML, XML, text and JSON file in clean builds of master (7.1.3) and this branch (7.4.5), 301 HTML files each. After normalizing asset hashes, scoped-style ids, whitespace, inline scripts, empty srcset attributes and island uid values, one file differs: voter-guides/2022-11-california/prop-1/. In that guide, a closing quote after a period (procedure.") rendered as an opening quote “ on 7.1.3 and now renders correctly as ”. The normalization hides four more changes:

  • Markdown images no longer carry an empty srcset="" (18 on master, none now).
  • Four pages with React islands (usage/, mackenzie-scott-qaly/, blog/double-eye-contact/ and its crosspost) get new astro-island uid values. The islands hydrated in a local preview with no console errors.
  • The inline island scripts, which Astro ships prebuilt, come out of a different minifier; the island runtime grows from 4,380 to 4,896 characters.
  • One new _astro/rolldown-runtime.*.js chunk appears.

Verification

  • 7.4.5: vitest run tests/attribute-escaping.test.ts → 30 passed.
  • 7.1.3 (master's lockfile, plus a probe post with ![Tom & Jerry's alt](./pixel.svg)): 5 failed, 25 passed. The built-pages scan flags alt="Tom &amp; Jerry’s alt" on the probe page and its crosspost, and the fixture fails alt and title on both .md paths. MDX passes.
  • The previous regex scanner gets all five new scan cases wrong.
  • bun run build, astro check (0 errors) and tsc --noEmit pass locally.

🤖 Generated with Claude Code

Astro 7.1.3 escaped an `&` in a Markdown image's alt or title text twice
(`alt="Tom &amp;amp; Jerry"`), so img.alt held a literal `&amp;` and screen
readers read it out. Astro 7.3.4 fixed this (withastro/astro#18086). This
upgrades astro to 7.4.5 and @astrojs/mdx to 8.1.2, which pin the same
Sätteri release.

tests/attribute-escaping.test.ts fails when a built page escapes an
attribute value twice. It also builds a throwaway Astro project to check
that alt and title text in .md and .mdx images keeps every ASCII
punctuation character. On 7.4.5 a backtick in .md image text still comes
out as `&#x60;`, so AGENTS.md keeps backticks out of image text, and
it.fails canaries flag the release that fixes it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vercel

vercel Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
maxghenis.com Ready Ready Preview Nov 4, 2026 6:32pm UTC

Request Review

Review of e288e93 found that the built-pages scan flagged valid
serialized data (astro-island props holding `&amp;`, data-* attributes),
missed named references outside its short list (`&amp;copy;`), matched
misspelled ones (`&aMp;`) and skipped `<noscript>` markup. The backtick
canaries also passed when an image lost its alt or title.

- Scan only attributes that hold text or URLs, and flag a value when
  entities' decodeHTMLAttribute would change it, which follows the
  browser's own reference rules. Parse with scripting on and off.
- Add self-tests that pin the scan's behavior on each case above.
- Pin the known .md backtick value `Back&#x60;tick` in place of it.fails,
  and check that public/ and remote images, which Astro doesn't
  process, keep backticks, and that code span backticks drop out of
  alt text.
- Qualify the AGENTS.md backtick rule to processed images; the rule
  still covers every .md image.
- Declare entities ^7.0.1 as a dev dependency; the tree already held it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch was successfully deployed

1 active deployment
Preview — 9aa51d6f Deployed Nov 4, 2026 by vercel[bot]
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.

1 participant