Skip to content

fix: encrypted incremental saves, BOM binary strings, damaged content streams and font-table re-reads - #448

Merged
jafin merged 14 commits into
mainfrom
fix/batch-444-440-435-412-408-395
Nov 26, 2026
Merged

jafin merged 14 commits into
mainfrom
fix/batch-444-440-435-412-408-395

Conversation

@jafin

@jafin jafin commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Why

Six open bugs, fixed in one batch. Each fix has its own commits on this branch: the failing tests first, then the fix.

What changes

Verification

Closes #444
Closes #440
Closes #435
Closes #412
Closes #408
Closes #395

jafin added 14 commits November 26, 2026 20:02
…ar as it inflated

A page or form content stream whose Adler-32 checksum is cut off or wrong is reported as UnreadableContent and nothing of it is drawn, though every byte of it inflated (#440). Images already draw such data (#409).
…ated, and report it

A page's content stream, or a form's or tiling pattern's, whose Adler-32 checksum was cut off or wrong threw in FlateDecode after every byte had inflated, so it was reported as UnreadableContent and nothing of it was drawn (#440). PdfContentStreams.TryGetContent takes tolerateDamage and passes it on as DecodeLimit.TolerateDamage, as page images already do (#409). Page rendering sets it for the page and for forms and patterns, and reports the damage as InvalidValue on the page or the form. Font programs, shading streams and every caller that reads content rather than drawing it keep the exception. Closes #440.
…nly the one named

AFontAndItsGlyphSourceKeepNothingThatGrows listed EmbeddedTrueTypeGlyphSource by name, so a
glyph source added later, for CFF or Type 1, would escape the check that it keeps nothing that
grows. It now finds every PageGlyphSource subclass in the library by reflection (#435).
…declares a raised version

SaveIncremental on an encrypted document opened in Append throws a
NullReferenceException from the security handler, for RC4 and AES alike and
with either password (#412). And a revision that raises the PDF version
leaves the header's version standing on reopening, because nothing writes
the catalog's /Version and the reader does not read it (#395).

These tests fail until both are fixed.
…decrypted

The lexer takes a string opening FE FF or FF FE for UTF-16 text and decodes
it. Three readers of plain binary strings still read that text: a seed
value's certificates, which throw on the decoded characters, and both
document identifier guids, which come back empty. SecondDocumentGuid also
reads the first identifier (#444).

Decrypting has the same fault from the other side. A string that decrypts
to FE FF and an odd number of bytes ends the open with an
IndexOutOfRangeException, and ciphertext that opens FE FF, which for AES is
every initialization vector that does, is decrypted without its first two
bytes and comes out garbled (#408). These tests fail until both are read as
the bytes the file holds.

CryptFilterDocument takes its identifier and initialization vector as
properties, so a test can choose ones that make the ciphertext open FE FF.
…rops reported apart

Two failing tests for #435. A page that selects, in turn, more fonts than the document holds,
each with its own /W, /W2 or /CIDToGIDMap, reads a table again on every selection: only font
programs were charged to the page's allowance, so nothing stopped it. And a glyph dropped because
the allowance ran out was reported as MissingGlyph, which the diagnostics merge by problem and
font, so a genuinely missing glyph shown earlier in the same font hid every drop after it.
The lexer takes a string opening FE FF or FF FE for UTF-16 text, drops the
mark and decodes the rest, completing an odd count with a zero. A seed
value's certificates and the document identifier guids read that text back
as bytes, so a certificate opening with a mark threw and an identifier
opening with one answered the empty guid. They now read the string through
PdfItemValues.TryGetBytes, as the encryption entries do since #443, and
SecondDocumentGuid reads the second identifier rather than the first (#444).

Decryption read the same text back through EncryptionValue, which lost the
mark: ciphertext that opens FE FF, which for AES is every initialization
vector that does, was decrypted two bytes short and came out garbled. The
plaintext was then decoded pair by pair, which ran off the end of a string
that decrypted to FE FF and an odd number of bytes and ended the open with
an IndexOutOfRangeException (#408). A string is now decrypted from the bytes
TryGetBytes gives, and its plaintext decoded by one routine, shared by
PdfString and PdfStringObject, that reads it as the lexer reads the same
bytes unencrypted, odd byte included. The lexer's reading of the ciphertext
is withdrawn whatever mark it saw.

PDFsharp wrote FE FF in front of the ciphertext of a text string rather than
inside it, and those documents still open. Such a string is AES of whole
blocks after the mark, or RC4 of an even length that does not decrypt to a
mark as a whole. An even-length RC4 binary string whose ciphertext opens
FE FF by chance is still taken for one, as it always was.
… it drops as the limit

The page's 1 GiB allowance counted font programs alone. /W, /W2 and /CIDToGIDMap are kept under
the same LRU budget and read again after they are let go of, so a page cycling through hundreds
of fonts with a table each rebuilt one at every selection: 200,000 selections took 126 s over /W,
405 s over /W2 and 58 s over /CIDToGIDMap (#435).

RenderFontCache.Data now charges every read, of any kind, through a new IFontData.ReadBytes: a
font program or a map what its stream decoded to, a WidthTable what its builder allocated, which
is room for every code there can be. Past the allowance it throws FontDataNotReadException
(formerly FontProgramNotReadException, which only the program read raised) instead of reading,
and the per-program BeforeProgramRead/AfterProgramRead pair is gone with it.

The exception is no longer turned into a glyph's "why". DrawGlyph reports it as LimitReached and
skips the glyph; ShowString fetches the width before drawing and, where the width cannot be read,
reports LimitReached and sets the rest of the string no further, since where it would advance to
is unknown. Reported as MissingGlyph, an allowance drop merged into any genuinely missing glyph
of the same font and disappeared.
…ed version in the catalog

SaveIncremental on an encrypted document threw a NullReferenceException from
the security handler (#412). The handler had only ever been prepared for
writing by PrepareEncryption, which a full save calls and which makes new
keys. An appended revision cannot have new keys: the revisions before it
stay encrypted under the old ones, and a file has one encryption dictionary
(ISO 32000-1 7.5.6). SaveIncremental now calls PrepareToAppend, and the
writer encrypts with the encryptors the reader decrypted with - the same file
key and the same crypt filters for strings and streams, RC4, AES-128 and
AES-256 alike. AES encryption for writing is new; AES makes a stream longer,
so the dictionary writes the encrypted length as /Length.

An incremental save also never declared a raised PDF version (#395): the
header is in the bytes copied through, and nothing wrote the catalog's
/Version or read it back. SaveIncremental now writes /Version when Version is
later than what the file declared, and PdfReader takes the later of the
header's version and the catalog's (ISO 32000-1 7.7.2).
…e batch in the changelog

The tests looked the factory up by its old name and so found nothing. The
case that read a Unicode flag the lexer set on ciphertext now asserts that
the flag is withdrawn, which is the rule #408 introduced. PdfDocument.Version
says that a Lazy document reports its header's version.
@coderabbitai

coderabbitai Bot commented Nov 26, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 84e5c699-3d6e-4e31-b6ef-d60f8f5a78ca

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@github-actions github-actions Bot added the bug Something isn't working label Nov 26, 2026
@sonarqubecloud

Copy link
Copy Markdown

@jafin
jafin merged commit 07ed626 into main Nov 26, 2026
6 checks passed
@jafin
jafin deleted the fix/batch-444-440-435-412-408-395 branch November 26, 2026 11:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment