Skip to content

fix: replace null input characters before tokenizing - #4252

Open
hakarrr wants to merge 15 commits into
markedjs:masterfrom
hakarrr:fix/normalize-null-input
Open

hakarrr wants to merge 15 commits into
markedjs:masterfrom
hakarrr:fix/normalize-null-input

Conversation

@hakarrr

@hakarrr hakarrr commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

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 raw values. 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 provideLexer still owns its source. Direct low-level blockTokens calls remain unchanged.

Tests

  • Local npm test: 2,107 spec tests and 272 unit tests pass, plus docs/build, UMD/CJS, types and lint.
  • Regression coverage includes built-in block and child raw reconstruction, semantic fields, extensions with custom fields and child tokens, custom tokenizer overrides, reused default options, frozen caller inline tokens, repeated block lexing and frozen prior link definitions.
  • A separate final-tree comparison against the prior published head covered 16,000 NUL-free cases and 18,000 NUL-injected cases, with matching output, expected raw reconstruction and zero crashes. This is bounded coverage, not a full-conformance claim.
  • Hosted checks are separate from these local results.

References:

Demo comparison for [x](/a\0b) (the URL contains %00; NUL is invisible in the input editor):

Contributor

  • Tests exist to check the behavior and guard against regressions.
  • No new feature documentation is needed.

Committer

  • CI is green.
  • Squash and merge using the project's conventional commit guidelines.

hakarrr and others added 4 commits November 17, 2026 17:16
Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>
Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>
Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>
Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>
@vercel

vercel Bot commented Nov 17, 2026

Copy link
Copy Markdown

@hakarrr is attempting to deploy a commit to the MarkedJS Team on Vercel.

A member of the Team first needs to authorize it.

@vercel

vercel Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated
marked-website Ready Ready Preview Nov 21, 2026 6:44am UTC

Request Review

@UziTech

UziTech commented Nov 18, 2026

Copy link
Copy Markdown
Member

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

@hakarrr

hakarrr commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

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.

@hakarrr

hakarrr commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

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.

@UziTech

UziTech commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

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.

hakarrr and others added 5 commits November 18, 2026 11:24
Keep custom tokenizer normalization compatible and avoid rewriting prior lexer results.

Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>
Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>
Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>
Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>
Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>
@hakarrr

hakarrr commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

Updated in 4f09817. 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.

Comment thread test/specs/new/null_input_character.cjs Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use .md & .html files for these tests

@hakarrr

hakarrr commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

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.

hakarrr and others added 5 commits November 28, 2026 10:33
Add the .md and .html fixture pair requested in review. The .md holds the literal NUL bytes.

Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>
The web upload added a numeric prefix to the file name. It is re-added under the right name next.

Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>
Drop the numeric prefix the web upload added, so the .html matches its .md pair.

Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>
The .md holds the literal NUL bytes, as in the earlier upload, under the right name.

Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>
Replaced by the md and html fixtures.

Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>

This branch was successfully deployed

1 active (outdated) deployment
Preview — 4f098174 Deployed Nov 21, 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.

2 participants