Skip to content

fix: replace null input characters before tokenizing - #4252

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

hakarrr wants to merge 16 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 Dec 5, 2026 6:53am 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

Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>
@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>
@hakarrr

hakarrr commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

The .cjs spec is gone; the cases now live in test/specs/new/null_input_character.md and .html. One heads-up: the .md contains literal NUL bytes, so git and GitHub render it as binary and the diff is not readable inline. The cases are the same ones as before, with \0 for NUL; I can switch to a text-only .json fixture like the redos specs if you prefer. I kept test/unit/nullInput.test.js because it covers the raw-preservation behaviour from your earlier note; happy to fold that into fixtures too.

@hakarrr
hakarrr requested a review from UziTech November 30, 2026 07:28
Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>
@hakarrr

hakarrr commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Hi maintainers, I pushed an update that addresses the requested changes. When you have time, could you take another look? If anything else should change, please tell me and I'll fix it. Thanks for the reviews.

hakarrr

@UziTech UziTech left a comment

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.

Thanks for updating the PR — the raw preservation, .md/.html spec fixture format, recheck safety on all updated rules, and unit test coverage look solid.

Requesting a few changes to trim bundle size and avoid unnecessary hot-path overhead:

  1. Bundle size (+1,334 B minified in lib/marked.esm.js / lib/marked.umd.js, +384/+390 B gzipped)

    • TypeScript private fields (normalizeSourceNulls, inlineDepth, inlineHasNull) are not mangled by esbuild, so their full property names are repeated in the minified output. Switching to ECMAScript #private fields or local variables/state and streamlining normalizeTokenNulls / inlineTokens should bring the minified growth well under 1 KB.
  2. Hot-path overhead & redundant work (npm run bench median ~33% slower than commonmark vs 30% on master)

    • In src/Lexer.ts (inlineTokens), every call — including nested calls and inlineQueue runs inside lex() — runs src.includes('\0') and tracks inlineDepth/inlineHasNull even when lex() already checked hasNull === false for the whole input.
    • When lex() does contain \0 and !this.normalizeSourceNulls, queued inlineTokens() calls normalize their tokens in finally, and then lex() walks the entire token tree a second time via normalizeTokenNulls(this.tokens.slice(firstToken)).
    • When normalizeSourceNulls is true, inlineTokens() runs src.replace(other.nullCharacter, '\uFFFD') unconditionally even when !src.includes('\0') (and after lex() already normalized the source).
    • In src/helpers.ts (normalizeLabel), please gate label.replace(other.nullCharacter, '\uFFFD') with label.includes('\0') so NUL-free link labels skip the regex replace.
  3. Minor null -> undefined coercion on link.title (src/Lexer.ts)

    • link.title = link.title?.replace(other.nullCharacter, '\uFFFD') coerces null to undefined; guard with if (link.title) instead.

This branch was successfully deployed

1 active (outdated) deployment
Preview — b0d5103e Deployed Dec 5, 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