fix: replace null input characters before tokenizing #4252
@hakarrr is attempting to deploy a commit to the MarkedJS Team on Vercel.
A member of the Team first needs to authorize it.
[vc]: #t07dyiXwhLvdj26ZJUi+BiB+/5RcEoR1dRlPb0cdv2Q=:eyJpc01vbm9yZXBvIjp0cnVlLCJ0eXBlIjoiZ2l0aHViIiwicHJvamVjdHMiOlt7Im5hbWUiOiJtYXJrZWQtd2Vic2l0ZSIsInByb2plY3RJZCI6InByal9RbnhOam0wSDI0YlVHQWI1MWIxUnVaa25ReXlZIiwidjAiOmZhbHNlLCJyb290RGlyZWN0b3J5IjpudWxsLCJpbnNwZWN0b3JVcmwiOiJodHRwczovL3ZlcmNlbC5jb20vbWFya2VkanMvbWFya2VkLXdlYnNpdGUvSG4xaUVkQ2RaamM2Znk2Z3duNnk3MjYzZkZFUiIsInByZXZpZXdVcmwiOiJtYXJrZWQtd2Vic2l0ZS1naXQtZm9yay1oYWthcnJyLWZpeC1ub3JtYWxpemUtbi0yMDc5MTYtbWFya2VkanMudmVyY2VsLmFwcCIsIm5leHRDb21taXRTdGF0dXMiOiJERVBMT1lFRCIsImxpdmVGZWVkYmFjayI6eyJyZXNvbHZlZCI6MCwidW5yZXNvbHZlZCI6MCwidG90YWwiOjAsImxpbmsiOiJtYXJrZWQtd2Vic2l0ZS1naXQtZm9yay1oYWthcnJyLWZpeC1ub3JtYWxpemUtbi0yMDc5MTYtbWFya2VkanMudmVyY2VsLmFwcCJ9fV0sInJlcXVlc3RSZXZpZXdVcmwiOiJodHRwczovL3ZlcmNlbC5jb20vdmVyY2VsLWFnZW50L3JlcXVlc3QtcmV2aWV3P293bmVyPW1hcmtlZGpzJnJlcG89bWFya2VkJnByPTQyNTIifQ==
The latest updates on your projects. Learn more about Vercel for GitHub.
| Project | Deployment | Actions | Updated |
| :--- | :----- | :------ | :------ |
| <a href="https://vercel.com/markedjs/marked-website"><sup><img src="https://vercel.com/api/www/avatar?projectId=prj_QnxNjm0H24bUGAb51b1RuZknQyyY&teamId=team_RpARALaqy6bNW7b6MI363ieY&s=32" width="16" height="16" align="middle" alt="" /></sup></a> marked-website | !Ready Ready | Preview | <relative-time datetime="2026-11-21T06:44:33.759Z">Nov 21, 2026 6:44am UTC</relative-time> |
<a href="https://vercel.com/vercel-agent/request-review?owner=markedjs&repo=marked&pr=4252" rel="noreferrer"><picture><source media="(prefers-color-scheme: dark)" srcset="https://agents-vade-review.vercel.sh/request-review-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://agents-vade-review.vercel.sh/request-review-light.svg"><img src="https://agents-vade-review.vercel.sh/request-review-light.svg" alt="Request Review"></picture></a>
The raw value of tokens should ideally be the value of the markdown that rendered that token. If we replace it in the lexer raw will contain the replaced value instead
Agreed, this also changes token.raw, and I missed that API effect in the tests. I'll add raw-preservation coverage and check a tokenizer-level approach before updating the PR. The current lexer-wide replacement shouldn't be treated as ready.
I tested keeping NULs through tokenization and replacing them in token text afterward. That preserves raw for built-in tokens, but breaks documented extensions using custom fields or childTokens. The tree walk can also change caller-supplied tokens, including throwing on frozen ones, so I haven't pushed that prototype.
What normalization/raw boundary do you intend here, especially for the source passed to extension tokenizers? I want to settle that before broadening the change. Existing CR-to-LF normalization would stay unchanged.
Ideally someone using marked lexer to parse markdown into tokens should be able to recreate the original markdown by appending raw from the top level block tokens, and recreate any token's markdown from is child tokens.
We do remove CR for legacy reasons but eventually I would like to get rid of that as well.
If someone uses extensions we cannot guarantee this anyways since all extension can set anything in raw. So this rule only applies to marked without any extensions.
Updated in 4f098174. Built-in tokenization now preserves literal NULs in raw while normalizing the semantic fields, including link labels and destinations. Existing CR handling is unchanged. Extensions and custom tokenizers keep the earlier source-normalization behavior, so their custom fields and child tokens remain compatible.
Added raw-reconstruction tests and frozen prior-result coverage for inline tokens and repeated Lexer.lex() calls, including stored link definitions. Local npm test passes: 2,107 spec tests and 272 unit tests, plus build, module, type and lint checks. Hosted checks still need their own result.
use .md & .html files for these tests
Requested changes.
Merged master into this branch (aeee4be) to resolve a conflict in src/rules.ts: master moved the bare link destination pattern into _bareHref, so I kept that and moved the \x01-\x1f control-character class (NUL is normalized to U+FFFD, not excluded) into _bareHrefChar. npm test passes locally on the merged tree (specs, unit, types, lint). GitHub's web conflict editor does not allow a commit message, so this merge commit has no Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com> trailer. The original commit keeps it. If you squash, please keep that trailer in the squash message.
UziTech
hakarrr
Summary
Replace literal U+0000 input characters with U+FFFD in parsed output, as required by CommonMark section 2.3 and GFM section 2.3, while preserving built-in token
rawvalues. Existing carriage-return handling is unchanged.Built-in tokenization retains the source characters, treats NUL like U+FFFD in the affected delimiter and destination rules, and normalizes semantic token fields afterward. Extensions and custom tokenizer overrides retain pre-tokenization source normalization, preserving their custom fields and declared child tokens. Normalization only visits tokens and link objects owned by the current call, including when a Lexer is reused with frozen prior results.
This is input normalization, not HTML sanitization or a security vulnerability claim. It does not decode additional entities or add a dependency. Preprocess hooks still receive original input. A custom
provideLexerstill owns its source. Direct low-levelblockTokenscalls remain unchanged.Tests
npm test: 2,107 spec tests and 272 unit tests pass, plus docs/build, UMD/CJS, types and lint.References:
Demo comparison for
[x](/a\0b)(the URL contains%00; NUL is invisible in the input editor):Contributor
Committer