Skip to content

Cap total component visits in glyf/gvar composite outlining - #224

Merged
Goldziher merged 1 commit into
harfbuzz:mainfrom
scadastrangelove:fix/glyf-gvar-component-visit-budget
Aug 5, 2026
Merged

Goldziher merged 1 commit into
harfbuzz:mainfrom
scadastrangelove:fix/glyf-gvar-component-visit-budget

Conversation

@scadastrangelove

Copy link
Copy Markdown
Contributor

Closes #220.

Adds glyf::MAX_COMPONENT_VISITS: u32 = 100_000 and threads a budget: &mut u32 parameter
through outline_impl (decremented once per call, checked_sub — None on exhaustion aborts
the whole traversal same as the existing depth-cap None) and through
gvar::outline_var_impl (same mechanism, reusing glyf::MAX_COMPONENT_VISITS). Both public
call sites (glyf::Table::outline, gvar::Table::outline) initialize a fresh budget per
top-level call.

This is deliberately a total node-visit budget, not a depth or branching-factor cap — it's
the shared-subtree reuse that defeats MAX_COMPONENTS, not depth itself, so the fix needs to
bound total work rather than tree shape.

Testing

  • Full suite green: cargo test --all-features --release — 148/149 (pre-existing unrelated
    failure, see Guard CFF2 BLEND operator against empty argument stack #222 for detail).
  • The depth=18/branching=3 case (16.8s before) now completes in ~3ms; depth=20/branching=3
    (previously didn't complete in 20s) now completes in ~2.5ms. Both return the same outcome as
    before (this glyph's outline is empty either way) — only the cost changed.
  • Measured against the real-font baseline in the linked issue (DejaVu Sans, 6253 glyphs,
    8.66ms/6253 glyphs): the capped worst case is now well within the same order of magnitude as
    legitimate composite-glyph parsing, not 13,600× beyond it.

Discovered by the rust-in-peace security pipeline.

MAX_COMPONENTS bounds the depth of a composite-glyph chain, but nothing
bounds how many times a shared component-glyph subtree can be revisited
via sibling components at the same level. A composite glyph whose
components all reference one shared child glyph forces branching^depth
calls to outline_impl while the depth cap is never reached, needing only
depth+1 distinct glyphs to construct. The identical pattern exists in
gvar::outline_var_impl.

Adds glyf::MAX_COMPONENT_VISITS = 100_000 and threads a total-visit
budget through both outline_impl and outline_var_impl, independent of
depth or branching factor.

Discovered by the rust-in-peace security pipeline
(https://github.com/scadastrangelove/rust-in-peace/).
tobocop2 added a commit to tobocop2/xberg that referenced this pull request Aug 5, 2026
…tlining (upstream xberg-io#224)

Upstream pull request harfbuzz/ttf-parser#224, commit dd2337b, applied
verbatim to the vendored v0.25.1 tree.

MAX_COMPONENTS bounds the depth of a single component chain, not how often a
component subtree is revisited through sibling components. A composite glyph
whose components all point at one shared child forces branching^depth calls
while staying under the depth cap and needing only depth+1 distinct glyphs.
Both outline_impl and outline_var_impl now carry a shared visit budget from
the top-level entry point.

The budget as upstream wrote it charges one unit per call, which bounds the
number of calls but not the work inside them, since a single glyph can carry
up to 65535 component records. The next commit charges per record.
Goldziher added a commit to xberg-io/xberg that referenced this pull request Aug 5, 2026
…nreleased correctness and DoS fixes (#1384)

* chore(ttf-parser): vendor upstream ttf-parser v0.25.1 verbatim

Upstream harfbuzz/ttf-parser is currently unmaintained: at the time of
vendoring the most recent commit was 2025-11-22 and correctness fixes were
sitting unreviewed, so waiting on a release is not a plan.

This commit is the upstream v0.25.1 tag, unmodified, and nothing else. Every
upstream fix we carry lands as its own commit on top, so `git log` shows
exactly which upstream pull request was taken and when, and each one can be
reviewed and reverted independently. All xberg-side modifications land in a
final separate commit.

Not vendored: benches/, examples/, c-api/, testing-tools/, meson.build.

* fix(ttf-parser): apply upstream harfbuzz/ttf-parser#228 (dotsection)

Upstream pull request harfbuzz/ttf-parser#228, commit 023f8163, applied
verbatim to the vendored v0.25.1 tree.

ttf-parser rejects any CFF charstring containing the deprecated `dotsection`
operator (escape `12 0`) with UnsupportedOperator, which aborts the whole
charstring and leaves the glyph with no outline. Adobe's Type 1 to Type 2
conversion preserves the operator, so real-world fonts still carry it, and
the glyphs that carry it are exactly the dot-bearing ones: i, j, ! and .
A page rendered from such a PDF silently loses those characters, and OCR run
over the page transcribes the gaps.

The operator takes no arguments and is a no-op for outlining, matching
read-fonts and FreeType. It is now skipped rather than rejected. Upstream's
own two regression cases come with it and pin the behavior, including that
operands already on the argument stack survive it untouched.

* docs(ttf-parser): document Face::style() OS/2 fallback (upstream #203)

Upstream pull request harfbuzz/ttf-parser#203, commit b422ac0, applied
verbatim to the vendored v0.25.1 tree.

Face::style() reads the style from the OS/2 table and falls back to the
head table's mac style bits when OS/2 is absent. The rustdoc did not say
so, leaving callers to read the implementation to find out which table
actually answers the query. Documentation only; no behavior change.

* fix(ttf-parser): apply upstream harfbuzz/ttf-parser#222 (CFF2 BLEND stack guard)

Upstream pull request harfbuzz/ttf-parser#222, commit 3a585f1, applied
verbatim to the vendored v0.25.1 tree.

The CFF2 `blend` operator pops its operand count off the argument stack
without first checking that the stack holds anything. `ArgumentsStack::pop`
is unchecked, so a charstring that reaches `blend` with an empty stack reads
a stale slot and carries on with a bogus operand count. Every other pop in
the CFF2 interpreter already guards its stack depth; this was the last one
that did not. It now returns InvalidArgumentsStackLength like the rest.

* fix(ttf-parser): apply upstream harfbuzz/ttf-parser#223 (avar i16 overflow)

Upstream pull request harfbuzz/ttf-parser#223, commit 32439d2, applied
verbatim to the vendored v0.25.1 tree.

avar::map_value computed `value - from + to` in i16 at three sites. A
segment map whose from and to coordinates sit at opposite extremes pushes
that expression outside i16, and with overflow checks off in release it
wraps silently and yields a normalized coordinate pointing at the wrong
place on the axis. The arithmetic is promoted to i32 behind a shared
shift_coordinate helper, which rejects out-of-range results rather than
wrapping them.

One i32 multiply in the interpolation branch of the same function can
still overflow. That is a separate pre-existing defect, untouched by this
patch, and is being reported upstream rather than diverged here.

* fix(ttf-parser): cap total paint-graph node visits in COLRv1 painting (upstream #225)

Upstream pull request harfbuzz/ttf-parser#225, commit 99aa5e3, applied
verbatim to the vendored v0.25.1 tree.

RecursionStack detects a cycle only on the current root-to-leaf path, since
an entry is popped as soon as its call returns. A paint graph shaped as a
DAG, where the same offset is reachable through several sibling branches and
no single branch cycles, slips past it entirely: a chain of PaintColrLayers
records whose layer slots all point at one shared next-level record forces
branching^depth calls while never re-entering the active path. A total-visit
budget now bounds the work for one top-level paint call regardless of
branching factor.

The comment on the new constant refers to glyf::MAX_COMPONENT_VISITS, which
upstream #224 introduces; that pick lands later in this branch.

* fix(ttf-parser): parse loca for fonts with the maximum 65535 glyphs (upstream #226)

Upstream pull request harfbuzz/ttf-parser#226, commit 86daf57, applied
verbatim to the vendored v0.25.1 tree.

A loca table holds numGlyphs + 1 offsets, so a font at the spec maximum of
65535 glyphs has 65536 of them. The offset count was held in a u16, and
u16::try_from(65536) fails, which made Table::parse return None for the whole
table. Face::parse then leaves glyf as None, so the face yields no TrueType
outlines at all, and the gvar path dies with it through the `glyf?` in
Face::outline_glyph. A second, narrower instance of the same overflow made
the last glyph unreachable even when the table did parse.

The counters and the offset array widen to u32, which also makes good on the
existing comment about ignoring a longer-than-expected loca. The array read
stays bounded at numGlyphs + 1 entries, so nothing new is allocated or read.

This is a latent fix rather than a live one: a scan of 452 system fonts plus
the fixtures in tree found no glyf-flavored face large enough to trigger it.
It also widens the public loca::Table signature, which no crate in this
workspace's dependency graph calls.

* fix(ttf-parser): read fvar HIDDEN_AXIS from bit 0, not reserved bit 3 (upstream #216)

Upstream pull request harfbuzz/ttf-parser#216, commit 9b9e55f, applied
verbatim to the vendored v0.25.1 tree.

The fvar axis record's HIDDEN_AXIS flag is bit 0. VariationAxis::parse read
bit 3, which the spec reserves and requires to be clear, so hidden was
initialized from a bit that carries no meaning: every hidden axis was
reported as visible, and any font that set the reserved bit was reported as
hidden. Confirmed against the spec and against skrifa, which defines
AXIS_HIDDEN_FLAG as 0x1.

A regression test follows in the next commit; the one-line change is
silently reversible and reverting it breaks nothing in the existing suite.

* test(ttf-parser): pin fvar HIDDEN_AXIS bit-0 decoding

xberg-authored follow-up to the previous commit. Upstream #216 ships no test,
and nothing in the existing suite reads VariationAxis::hidden, so reverting
the one-line fix leaves every test green.

Three cases over a synthetic single-axis fvar table: bit 0 set means hidden,
no flags means visible, and reserved bit 3 set means visible. The last one is
what pins the change rather than smoke-testing around it, since it fails in
the opposite direction if the old `flags >> 3` reading comes back.

The test lives at the top level of tests/ rather than in tests/tables/,
because adding a module there would mean editing tests/tables/main.rs, which
is kept byte-identical to upstream so future cherry-picks stay conflict-free.
No feature gate: the dev-dependency on compat/ pulls default features, which
unifies variable-fonts onto the crate in every test build.

* fix(ttf-parser): return None from set_variation for an unknown axis tag (upstream #207)

Upstream pull request harfbuzz/ttf-parser#207, commit 52a9811, applied
verbatim to the vendored v0.25.1 tree.

Face::set_variation documents that it returns None when the face has no such
variation axis, but it returned Some(()) unconditionally: the loop simply
found no matching tag, changed nothing, and reported success. Callers could
not tell a typo'd or unsupported axis from an applied one.

The maintainer's open question on the PR was whether the docs were merely
outdated. They are not: pre-0.20 the method carried an explicit else branch
returning None, and the rewrite that turned it into a loop dropped it. This
restores the documented contract rather than inventing one.

A regression test follows in the next commit; nothing in the existing suite
asserts this return value.

* test(ttf-parser): pin set_variation unknown-axis contract

xberg-authored follow-up to the previous commit. Upstream #207 ships no test
and nothing in the existing suite asserts what set_variation returns, so the
restored contract would silently rot.

Three arms over fixtures already in tree. Only the first exercises the fix:
an unknown tag on a variable face returned Some(()) before #207 and returns
None after, verified by reverting the change on a pod and watching this test
go red. The known-tag case guards against over-correcting into always-None.
The non-variable case is already handled by the `!self.is_variable()` guard
ahead of the loop, so it passes either way; it is kept for the other half of
the documented contract and labelled as such rather than being left to look
like coverage it does not provide.

The test lives at the top level of tests/ rather than in tests/tables/, so
tests/tables/main.rs stays byte-identical to upstream.

* fix(ttf-parser): cap total component visits in glyf/gvar composite outlining (upstream #224)

Upstream pull request harfbuzz/ttf-parser#224, commit dd2337b, applied
verbatim to the vendored v0.25.1 tree.

MAX_COMPONENTS bounds the depth of a single component chain, not how often a
component subtree is revisited through sibling components. A composite glyph
whose components all point at one shared child forces branching^depth calls
while staying under the depth cap and needing only depth+1 distinct glyphs.
Both outline_impl and outline_var_impl now carry a shared visit budget from
the top-level entry point.

The budget as upstream wrote it charges one unit per call, which bounds the
number of calls but not the work inside them, since a single glyph can carry
up to 65535 component records. The next commit charges per record.

* fix(ttf-parser): charge the glyf/gvar visit budget per component record

xberg-authored follow-up to the previous commit. Upstream #224's budget is
charged once on entry to outline_impl / outline_var_impl, which bounds the
number of calls but not the work inside them: a single composite glyph can
carry up to 65535 component records, and components whose glyph range does
not resolve are iterated and skipped without ever recursing. A glyph padded
with unresolvable components therefore costs one budget unit no matter how
wide it is, and can be re-walked until the depth cap stops it. The budget is
now charged per component record in both loops, so total work is bounded by
MAX_COMPONENT_VISITS rather than only total call count.

Charging components_count up front in gvar would not do: it is computed as
`.count() as u16`, so a glyph with an exact multiple of 65536 components
truncates to zero and is charged nothing.

MAX_COMPONENT_VISITS stays at 100_000, still around four orders of magnitude
above any real font, and the per-record cost is O(1).

No test: the observable difference is a timing bound, and a timing assertion
would be flaky in CI. This is the first behavior-affecting xberg divergence
inside the vendored src/ tree, so both sites carry a comment saying so.

* test(ttf-parser): pin a total-visit budget on the CFF charstring interpreter

xberg-authored regression test, committed before the fix. A single glyph whose
charstring calls one global subroutine 150,000 times, each call flat (depth 1)
so it stays under the nesting limit, drives one interpreter invocation per call.
Without a total-visit budget the glyph outlines successfully after doing all the
work; the test asserts outlining fails instead.

Deterministic, not timing-based: the assertion is on the return value. Applied
on its own on a pod, the test fails (the glyph outlines) and the following
commit makes it pass. Lives at the top level of tests/ so tests/tables/main.rs
stays byte-identical to upstream.

This commit alone is red; the interpreter has no budget yet. The fix is next.

* fix(ttf-parser): cap total charstring visits in the CFF interpreter

xberg-authored, not from an upstream PR. Upstream #224 and #225 cap the total
work of glyf/gvar composite outlining and COLRv1 painting, but the CFF
charstring interpreter had no equivalent, and upstream (unmaintained) does not
address it.

STACK_LIMIT bounds how deeply subroutine calls nest on the current path, not how
many are made in total. A charstring that calls a subroutine over and over
without nesting stays under STACK_LIMIT forever and drives one
_parse_char_string invocation per call with no ceiling, exponential in the worst
case. This is the path a PDF renderer exercises most, since CFF is the dominant
embedded-font flavor.

Both the CFF1 and CFF2 interpreters now carry a MAX_CHARSTRING_VISITS budget of
100_000, charged once per invocation through the shared parser context and
returning the existing NestingLimitReached error when exhausted. A real glyph
needs a few hundred. Reusing the existing error keeps the public API unchanged.

Mirrors the shape of upstream #224. This is the second behavior-affecting xberg
change inside the vendored src/ tree, alongside the #224 per-record budget.
The previous commit's regression test now passes.

* feat(ttf-parser): route the transitive ttf-parser onto the vendored copy

Nothing in xberg depends on ttf-parser directly; it arrives through
pdf_oxide, fontdb, lopdf and rustybuzz, which request it by its crates.io
name. Cargo matches [patch.crates-io] entries on package name alone, so a
crate named xberg-ttf-parser cannot redirect them. The compat/ subcrate
carries the upstream name, re-exports the vendored parser, and is what the
workspace patch entry points at. One parser is compiled, so types stay
identical across every consumer. It is never published.

The only edits to vendored source are 17 lines of crate-level lint allows.
The workspace lint set is stricter than upstream's CI, and these are allowed
rather than fixed because div_ceil and is_multiple_of postdate upstream's
1.63 MSRV, so rewriting them would break upstream's own build. For the same
reason the vendored tree is excluded from poly formatting.

The vendored tests keep referring to ttf_parser via a path-only dev-dependency
on the shim, which cargo strips when packaging. tiny-skia-path is bumped to the
0.12 already in the workspace so the lockfile does not carry two copies.

xberg-ttf-parser is published alongside the other vendored crates. Note that
[patch] does not propagate to downstream crates.io consumers: this fixes every
artifact we build and ship, not people who depend on xberg from the registry.

* test(pdf): render a dotsection CFF font end to end through the vendored parser

Carries the regression test from the superseded PR #1363, which proves the
user-visible fix: that xberg actually paints the dot-bearing glyphs, not merely
that the parser accepts the operator. The vendored crate's own tests cover the
charstring interpreter; nothing here proved a glyph reaches the page.

Renders a single-page PDF embedding a synthetic Type 1C font (fontTools-built,
no third-party font data) whose i, j, period, colon, semicolon, exclam and
question glyphs carry the deprecated dotsection operator the way Adobe's Type 1
to Type 2 converter emits it. It rasterizes through the production render path
and asserts per-glyph ink; three control glyphs without dotsection guard the
fixture itself. Stock ttf-parser 0.25.1 drops all seven dotsection glyphs, so
this passes only because the workspace routes onto the vendored parser.

Gated on the `pdf` feature, which CI reaches through `full` -> `formats` -> `pdf`,
so `cargo test -p xberg --features full` runs it. Verified locally as well
(Apple M1 Pro, macOS 14 arm64): `cargo test -p xberg --features pdf` green.

---------

Co-authored-by: Na'aman Hirschfeld <[email protected]>
@Goldziher

Copy link
Copy Markdown
Collaborator

Reviewed and verified against the source. Merging.

The gap is real. MAX_COMPONENTS = 32 (src/tables/glyf.rs:428) bounds only the depth of a single chain. The composite loop recurses per component with no visited-set and no memoization, so sibling components that all point at one shared child each trigger an independent full re-walk of that subtree. Depth stays under the cap while total work grows as branching^depth. A font of ~33 distinct glyph records in well under 1 KB of glyf forces ~2^32 calls to outline_impl. The existing endless_loop test covers cycles, not this DAG fan-out, so nothing currently catches it.

No state leak — this was the thing I most wanted to confirm, since a budget that persisted across calls would make outline_glyph start failing spuriously after enough glyphs. Both entry points allocate a fresh local (let mut budget = MAX_COMPONENT_VISITS in glyf::Table::outline, let mut budget = glyf::MAX_COMPONENT_VISITS in gvar::Table::outline); no static, field, or thread-local. Every top-level call gets the full budget.

The limit is safe. Measured against 649 real faces, the worst single glyph needed 53 component visits and the highest component count in one glyph was 52. 100,000 is roughly three orders of magnitude of headroom. For reference, harfbuzz bounds the same thing with HB_MAX_GRAPH_EDGE_COUNT = 2048 via an equivalent shared counter, and fontations/skrifa mirrors it — so if anything this is the generous end of the range. No risk to valid fonts.

Failure mode is checked_sub(1)?, returning None on the glyf path exactly like the existing depth cap. On the gvar path the abort is silent because outline_var_impl is called without ? and b.bbox.to_rect() is returned regardless — but that is pre-existing behaviour shared with the current depth cap, not something this PR introduces, so it is right that you matched it rather than changed it here.

Rebases cleanly: neither file has changed since your base.

One follow-up, which I'll handle rather than asking you to: no regression test ships with this, so a future refactor could drop the budget with every existing test still passing. I'll add a fan-out test to tests/tables/glyf.rs on top.

Thanks for this — the analysis in the description was accurate and saved a lot of time.

@Goldziher
Goldziher merged commit dd7a716 into harfbuzz:main Aug 5, 2026
marcello3d pushed a commit to descriptinc/ttf-parser that referenced this pull request Oct 16, 2026
…fbuzz#224)

MAX_COMPONENTS bounds chain depth only; shared child glyphs let sibling components force branching^depth re-walks under the cap. Adds a per-call visit budget threaded through both the glyf and gvar recursion.

(cherry picked from commit dd7a716)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants