Skip to content

fix(builder): limits for Check, Preview and Patch on untrusted input (#302) - #322

Open
vaceslav wants to merge 9 commits into
mainfrom
fix/302-builder-limits
Open

vaceslav wants to merge 9 commits into
mainfrom
fix/302-builder-limits

Conversation

@vaceslav

@vaceslav vaceslav commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Closes #302

TemplateInspector.Check, TemplateJson.Preview and TemplateJson.Patch take input from outside (uploads, agent definitions and patches). They now stay fast and small and report limits as problems, never as exceptions.

What changes

  • Witness data: a collection answers every index (lines[999]) with one shared item and enumerates it once. The trial fill in Check and Preview without data are linear in the template.

  • Check (uploads): new TemplateCheckOptions limits: MaximumEntries (1,000), MaximumUnpackedSize (64 MiB), MaximumMarkupSize (8 MiB of XML) and MaximumParagraphLength (20,000 characters).

    • Sizes are counted while reading, and XML is recognised by content, not by name.
    • An unreadable entry is skipped and does not switch the limits off.
    • Exceeding a limit is TB9300 at the entry or paragraph.
  • Definitions: TemplateJsonImportOptions.MaximumLength (1,000,000 characters) and MaximumValues (100,000 JSON values). Exceeding one is the new code TB9206 at "".

  • Patch: each of these is TB9207 at the operation, never an exception:

    • more than 1,000 operations;
    • a result longer than MaximumLength (counted per operation);
    • a patch itself longer than MaximumLength;
    • copying or removing more than 16 × MaximumLength in total;
    • a result deeper than 64 levels;
    • copy into its own source;
    • a lone surrogate.

    A move detaches the node instead of cloning it.

  • Docs: docs/for-developers/template-builder.md lists the limits and their defaults and asks hosts to cap raw uploads.

  • Core: the quadratic scan of unterminated tags is a separate core issue, Linear scan for unterminated tags (quadratic marker patterns) #321. The paragraph limit bounds it for Check.

Decisions to check

  • No separate trial-fill budget: with one enumerated item per collection, nothing multiplies any more.
  • Patch output is now compact JSON (it was indented). This is so that the size budget matches the output: indenting a deeply nested value multiplied its length by up to about 60×. Read still returns indented parts.
  • Fewer full-path checks in uploads with an index: an upload that contains an index no longer runs its loops N times in the trial fill. Full item paths in the {{#else}} branch of {{#if @first}} are therefore no longer filled there.
  • DataContract.CreateWitness returns collections as a non-generic IList that enumerates one item (experimental API, TPLB001).

Public API impact

Additive, builder only (experimental, TPLB001):

  • the four TemplateCheckOptions.Maximum* properties;
  • TemplateJsonImportOptions.MaximumLength and MaximumValues.

Behaviour changes, builder only: Patch output formatting, witness collections. Core: none.

Verification

  • Tests in InputLimitTests.cs:
    • the three inputs from the issue, each under 3 s and 100 MB allocated;
    • just below / just above for every limit;
    • a seeded property test over loop nesting 1–6 and indexes 0–999;
    • regression tests for the review findings.
  • Before the fix, the property test did not finish (more than 5 minutes).
  • dotnet format --verify-no-changes: clean.
  • CI-mode build (-p:ContinuousIntegrationBuild=true): 0 warnings.
  • All solution tests: core 2583/2583 (net10) and 2580 + 3 skipped (net11, skips unchanged), builder 1299/1299, tools 64, converter 95.
  • LibreOffice round trips: not needed (no OpenDocument or core engine code touched).
  • An independent review found 2 critical and 3 important problems. All five are fixed, each with a regression test.

Deferred minors:

  • the markup default is about 0.5 s per MiB (documented);
  • paragraph counting also counts inter-element whitespace, w:instrText and w:delText (errs towards refusing);
  • TB9300 limit locations use part, paragraph N instead of the engine's body, paragraph N;
  • the patch budget over-counts an add that overwrites an existing member.

Witness data answered an index [999] by building 1,000 items for every
collection, each anew: an indexed placeholder and nested loops in an upload
expanded the trial fill to count^depth, and a deep indexed path in a
definition ran Preview out of memory. A witness collection now answers
every index with one shared item and enumerates it once.
TemplateJsonImportOptions.MaximumLength (1,000,000 characters) and
MaximumValues (100,000 JSON values); more is TB9206 at the root. The
export's self-check is not limited.
At most 1,000 operations; the patched definition's length, estimated per
operation, stays within TemplateJsonImportOptions.MaximumLength and its
depth within 64 levels; copy into its own source is refused like move.
Each is TB9207 at the operation, so 23 self-copies no longer exhaust
memory and a too deep result no longer throws.
TemplateCheckOptions.MaximumEntries (1,000), MaximumUnpackedSize (64 MiB),
MaximumMarkupSize (8 MiB of .xml/.rels) and MaximumParagraphLength (20,000
characters). The package is read once before the engine sees it, counting
what the entries really yield; exceeding a limit is TB9300 at the entry or
paragraph and stops the check.
- Check recognises XML entries by content, not name, so a main part named
  document.bin is still within the markup and paragraph limits.
- An entry that cannot be read (encrypted, unknown compression) is skipped
  instead of switching off every limit.
- Patch writes its result compact, as the size budget counts it: indenting
  a deeply nested value multiplied its length. A patch longer than
  MaximumLength is refused; copying and removing more than 16 times
  MaximumLength in total is refused; a move detaches the node instead of
  cloning and serialising it; a string System.Text.Json cannot write (a
  lone surrogate) is TB9207 instead of an exception.
- The witness list exposes only Count as a property, so a path cannot read
  SyncRoot or IsReadOnly from it.
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 82.79070% with 37 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/TriasDev.Templify.Builder/PackageLimits.cs 84.84% 11 Missing and 4 partials ⚠️
src/TriasDev.Templify.Builder/Typed/WitnessList.cs 33.33% 13 Missing and 1 partial ⚠️
src/TriasDev.Templify.Builder/Json/JsonPatch.cs 89.83% 2 Missing and 4 partials ⚠️
src/TriasDev.Templify.Builder/Json/TemplateJson.cs 92.00% 0 Missing and 2 partials ⚠️

📢 Thoughts on this report? Let us know!

This branch has not been deployed

No deployments
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.

Builder: limits for Check, Preview and Patch on untrusted input

2 participants