Broken links checker crashes the build with URIError on malformed percent-encoding
Have you read the Contributing Guidelines on issues?
- I have read the Contributing Guidelines on issues.
Prerequisites
- I'm using the latest version of Docusaurus.
- I have tried the
npm run clearoryarn clearcommand. - I have tried
rm -rf node_modules yarn.lock package-lock.jsonand re-installing packages. - I have tried creating a repro with https://new.docusaurus.io.
- I have read the console error message carefully (if applicable).
Description
The broken links checker in packages/docusaurus/src/server/brokenLinks.ts can crash the build when it encounters links containing malformed percent-encoding.
The functions decodeURI(pathname) and decodeURIComponent(hash) throw URIError: URI malformed when the input contains invalid percent-encoded sequences.
For example:
/page% causes decodeURI() to throw during path validation. /page 2#100% can cause decodeURIComponent() to throw during anchor validation.
The exception is caught and re-thrown as an error similar to:
Error: Unable to get broken links for page /docs/intro.
This aborts the build instead of handling the malformed link through the configured broken-link behavior.
The issue also occurs when onBrokenLinks is configured as warn or ignore, provided that anchor checking is still enabled.
The early return in handleBrokenLinks only skips validation when both onBrokenLinks and onBrokenAnchors are set to ignore. Consequently, a malformed link can still crash the build when either path or anchor validation is enabled.
Reproducible demo
https://github.com/diino/docusaurus-broken-link-encoding-repro
The repository contains a minimal reproduction based on a fresh Docusaurus installation.
Steps to reproduce
Create a fresh Docusaurus site:
npm create docusaurus@latest my-site classicConfigure docusaurus.config.js to use a broken-link checking mode such as:
onBrokenLinks: 'warn',Add a Markdown link to docs/intro.md containing malformed percent-encoding:
[broken](/page%)Alternatively, reproduce the anchor case:
[broken](/page 2#100%)Run the production build:
npm run buildObserve the build failure.
The issue can also be reproduced with onBrokenLinks: 'ignore' as long as anchor validation is not also disabled.
Expected behavior
Malformed percent-encoded links should be handled gracefully by the broken links checker.
The checker should report the malformed link as broken, or ignore it according to the configured onBrokenLinks or onBrokenAnchors option, rather than aborting the entire build with an uncaught URI decoding exception.
A malformed URL should not prevent Docusaurus from completing the rest of its broken-link validation.
Actual behavior
The build fails with an uncaught URI decoding exception, wrapped in an error indicating that Docusaurus was unable to retrieve broken links for the page.
Reported error:
Error: Unable to get broken links for page /docs/intro.
Caused by: URIError: URI malformedRelevant source locations:
- packages/docusaurus/src/server/brokenLinks.ts:105 — decodeURI()
- packages/docusaurus/src/server/brokenLinks.ts:223 — error propagation during broken-link validation
The exception occurs during isPathBrokenLink or isAnchorBrokenLink, depending on whether the malformed value is in the path or anchor.
Your environment
- Docusaurus version: 4.0.0 / main branch (reported by the reproduction)
- Node.js: 26.3.0
- Operating system: Windows 11
Self-service
[x] I'd be willing to fix this bug myself.
Source: facebook/docusaurus