Skip to content

fix(mermaid-text): UTF-8 safety — byte offsets used as char offsets (#29) - #31

Merged
leboiko merged 6 commits into
masterfrom
fix/mermaid-text-utf8-byte-offsets-issue-29
Jul 16, 2026
Merged

leboiko merged 6 commits into
masterfrom
fix/mermaid-text-utf8-byte-offsets-issue-29

Conversation

@leboiko

@leboiko leboiko commented Jul 16, 2026

Copy link
Copy Markdown
Owner

Fixes #29. Reported by @vshylov with an excellent, reproducible write-up.

The bug class

mermaid-text used byte offsets where character offsets were expected, producing two failure modes on non-ASCII (e.g. Cyrillic) diagram text:

  • Panics — slicing a &str at a byte index that falls inside a multi-byte char.
  • Silent corruption — a byte length used to advance a cursor that indexes a Vec<char>, over-advancing on multi-byte labels and eating the next token.

What's fixed (6 sites)

Reported (flowchart + common):

Site Failure Fix
common::strip_keyword_prefix panic on loop до готовности str::get instead of line[..len]
flowchart::try_consume_pipe_label `--> да
flowchart::try_consume_inline_compact_arrow -.да.-> corrupts next node return char count
flowchart::try_consume_inline_quoted_arrow -. "да" .-> corrupts next node (not in report) count chars, not s.len()-tail.len()

Found by a full parser audit (docs/audit-mermaid-text-utf8-2026-07-15.md), confirming the reporter's "broader note":

Site Failure Fix
sequence::parse_participant_decl panic/mis-split on participant İd as … search original string for as, not a to_lowercase() copy
gantt / timeline / journey keyword-strip panic on non-ASCII lines deleted 3 duplicate local copies, routed to the fixed common::strip_keyword_prefix (removing the copy-paste that made the central fix miss them)

git_graph was flagged by the audit but verified clean (its slices only land on ASCII boundaries) — not touched.

Tests

10 new regression tests in crates/mermaid-text/tests/utf8_multibyte.rs, each confirmed to panic/corrupt before its fix and pass after. Assertions are strong (no bracket may leak; all labels must survive; alias must split correctly) — a no-op fix cannot satisfy them.

Also in this PR

Verification

cargo build, cargo fmt --check, cargo clippy --workspace --all-targets -D warnings, cargo test --workspace (1539 passing), and cargo deny check — all green locally.

Answering @vshylov's separate width question in an issue comment (it's a soft hint today, not a hard budget — sequence diagrams deliberately ignore max_width).

🤖 Generated with Claude Code

leboiko and others added 6 commits July 15, 2026 17:28
…ut (#29)

Two reported defects + two found while auditing the sibling arrow
consumers, all the same byte-vs-char class:

- Bug 1 (panic): strip_keyword_prefix sliced at keyword.len() (a byte
  index) without a char-boundary check, so a non-ASCII keyword-prefixed
  line (e.g. `loop до готовности`) panicked when that byte landed inside
  a multi-byte char. Now uses str::get, rejecting non-boundary slices
  instead of crashing. Preserves the ASCII-fast path.
- Bug 2 (silent corruption): try_consume_pipe_label returned a byte
  length that the char-indexed tokenizer added to its cursor,
  over-advancing by byte_len-char_len and eating part of the next node
  token (leaking its `[` bracket into the label). Now returns a char
  count.
- Bug 2b: same defect in try_consume_inline_compact_arrow (`-.label.->`
  / `==label==>`).
- Bug 2c (not in the report): try_consume_inline_quoted_arrow measured
  the consumed slice — which includes the multi-byte quoted label —
  with s.len()-tail.len() (bytes). Now counts chars.

6 new regression tests in tests/utf8_multibyte.rs, each confirmed to
fail before the fix. Full workspace suite green, fmt/clippy clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
)

The reporter predicted the byte-vs-char pattern recurs across the other
parsers. A full sweep (docs/audit-mermaid-text-utf8-2026-07-15.md)
confirmed two more real sites; git_graph was a false positive (verified
its slices only land on ASCII boundaries).

- sequence.rs parse_participant_decl: searched for " as " in a
  to_lowercase() copy, then applied that byte offset to the ORIGINAL
  string. Case-folding that changes byte length (e.g. `İ` U+0130 ->
  3-byte `i̇`) desynced the offset and panicked/mis-split the id/alias.
  Now scans the original bytes for the ASCII " as " separator (always a
  char boundary), dropping the lowercased-copy allocation entirely.
- gantt / timeline / journey each carried their OWN copy of the
  keyword-strip helper still slicing at `line[..keyword.len()]` — the
  copies the common.rs fix never reached. Deleted all three and routed
  them to the now-canonical common::strip_keyword_prefix, removing the
  duplication that caused the miss.

4 new regression tests (case-fold alias + one per parser), each
confirmed to panic before the fix. Full workspace suite green,
fmt/clippy clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Ships the #29 UTF-8 fixes: mermaid-text 0.56.1 (byte-vs-char panics and
label corruption on non-ASCII diagram text) and bumps the app to depend
on it, app version 1.34.74.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…5/0206 ignore)

Four advisories landed on master's dependency tree since #26 and turn
the cargo-deny gate red (unrelated to the #29 fix; folded in here to
unblock this PR):

- RUSTSEC-2026-0204 (crossbeam-epoch invalid-pointer deref): upgraded
  0.9.18 -> 0.9.20 via `cargo update` (semver-compatible, real fix).
- RUSTSEC-2026-0206 (rustybuzz unmaintained): documented ignore — same
  internally-generated-SVG stack as the ttf-parser ignore; no safe
  upgrade per the advisory.
- RUSTSEC-2026-0194 / 0195 (quick-xml < 0.41 DoS): documented ignore —
  reached only via syntect -> plist parsing our BUNDLED theme files
  (never attacker-controlled), and the 0.41 fix is a semver-breaking
  bump that `plist` pins away from. No upgrade path from our side.

Each ignore cites its upstream blocker for the quarterly re-audit.
cargo deny check green.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…in_formatting)

CI's stable toolchain moved 1.95 -> 1.97, whose clippy flags two
pre-existing patterns as -D warnings errors (unrelated to #29, surfaced
by the bump; blocks this PR's Clippy job):

- requirement_diagram.rs: collapse an if-let/else-return into `?`.
- checklinks/mod.rs: drop redundant `&` in four `format!` args.

Both are mechanical, behaviour-preserving. clippy --all-targets
-D warnings and the full suite are green on 1.97.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The CI `audit` job (continue-on-error, non-blocking) went perpetually
red on the same advisories deny.toml already handles, because cargo-audit
reads its OWN config (.cargo/audit.toml), not deny.toml. A permanently-red
audit masks new advisories, so mirror the justified ignore set here to
keep the signal meaningful:

- 0192/0206 (ttf-parser/rustybuzz unmaintained), 0141/0320 (syntect deps)
- 0194/0195 (quick-xml DoS — bundled-theme-only, no upgrade path)

deny.toml stays the source of truth; a header note asks to keep the two
in sync. New advisories (currently memmap2 0186, rand 0097, both
warning-severity) stay visible and non-blocking by design. `cargo audit`
exits 0 locally.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@leboiko
leboiko merged commit ad3460d into master Jul 16, 2026
12 checks passed
leboiko added a commit that referenced this pull request Jul 16, 2026
…#30)

@vshylov opened PR #30 with the correct fix before this maintainer wrote
and merged the superseding #31 without checking for an existing PR. The
shipped mermaid-text 0.56.1 fix is substantially their work; correct the
CHANGELOG credit from "reported by" to "reported and originally fixed by",
naming PR #30.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
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.

Panics and silent label corruption on non-ASCII input (byte offsets used as char offsets)

1 participant