← Back to Pull requests

fix: replace null input characters before tokenizing #4252

Open
hakarrr opened this pull request with 321 lines diff across 5 files
View on GitHub
hakarrr hakarrr commented on Nov 17, 2026 11:47 UTC
Author

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:

  • https://spec.commonmark.org/0.31.2/#insecure-characters

  • https://github.github.com/gfm/#insecure-characters
  • Demo comparison for [x](/a\0b) (the URL contains %00; NUL is invisible in the input editor):

  • Current master&options=%7B%22gfm%22%3Afalse%7D&version=master)

  • CommonMark reference&options=%7B%22gfm%22%3Afalse%7D&version=commonmark)
  • 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.
  • Diff: 321 lines (+311 / -10) across 5 files Reviewer: UziTech Review duration: 15088m
    All Vouch review confidence checks passed
    Residual risk 0.07 = Change Risk (0.67) × (1 − Review Confidence (0.89)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.67 · 321 lines diff across 5 files
    0.67
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.89 · 5 substantive comment(s)
    0.89
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    89%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.345
    Sensitive Paths sensitive_paths
    0.067
    Real PR Conversation Timeline
    11 items
    vercel[bot] vercel[bot] commented on Nov 17, 2026 11:47 UTC
    Bot

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

    A member of the Team first needs to authorize it.

    vercel[bot] vercel[bot] commented on Nov 17, 2026 14:53 UTC
    Bot

    [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>

    UziTech UziTech commented on Nov 18, 2026 03:28 UTC
    Contributor

    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 on Nov 18, 2026 03:33 UTC
    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 on Nov 18, 2026 03:56 UTC
    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 on Nov 18, 2026 05:34 UTC
    Contributor

    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 hakarrr commented on Nov 18, 2026 05:56 UTC
    Author

    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.

    UziTech UziTech commented on code on Nov 21, 2026 07:02 UTC
    Code Review
    test/specs/new/null_input_character.cjs:1

    use .md & .html files for these tests

    UziTech UziTech requested changes on Nov 21, 2026 07:02 UTC
    Changes Requested Reviewer

    Requested changes.

    hakarrr hakarrr commented on Nov 24, 2026 17:38 UTC
    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.

    Reviewers
    Assignees
    hakarrr hakarrr
    Labels
    risk: low confidence: 89.3% reviewed sensitive_module
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 66.6% (HIGH) · Higher = larger blast radius
    66.6%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 89.3% · Lower = rushed or shallow review
    89.3%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 7.1% · Safe to merge
    7.1%