Repository navigation
fix: encrypted incremental saves, BOM binary strings, damaged content streams and font-table re-reads - #448
Conversation
…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.
…40-435-412-408-395
…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).
…-444-440-435-412-408-395
…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.
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
|
|
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |



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
PdfSignatureSeedValue.BytesAt,PdfInternals.FirstDocumentGuidandSecondDocumentGuidread throughPdfItemValues.TryGetBytes.SecondDocumentGuidnow reads the second ID, not the first.IndexOutOfRangeException. AES ciphertext whose IV opens FE FF is no longer garbled. Documents written by older PDFsharp, which put the mark outside the ciphertext, still open (MarkOutsideCiphertext). One gap remains: an even-length RC4 string whose ciphertext opens FE FF by chance, about one in 65,536, which is no worse than before.SaveIncrementalon an encrypted document. It threwNullReferenceException, because only a full save prepared the key. The appended revision is now encrypted with the document's own key and crypt filters (RC4, AESV2, AESV3), and keeps/Encryptand/ID[0]. AES output length is computed in advance for/Length./VersionwhenVersionexceeds what the file declared, andPdfReaderreports the later of the header's version and the catalog's. ALazydocument still reports the header's, because it reads no object on open.InvalidValue. Font programs, shading functions and every non-drawing reader still refuse damage.LimitReachedrather thanMissingGlyph. The field-inventory test findsPageGlyphSourcesubclasses by reflection.Verification
dotnet test --solution src/PdfPinata.slnx -f net10.0on the merged branch: 10,767 total, 10,760 passed, 2 skipped. The 5 failures wereDecryptedStringReplacementTests, which still named the renamed factory. The last commit fixes them, and that class now passes 9 of 9./code-review mediumfound the stale test above (fixed), the missing changelog entries (added) and the Lazy-mode version difference (documented onPdfDocument.Version). The RC4 gap above was pre-existing and is left as documented.Closes #444
Closes #440
Closes #435
Closes #412
Closes #408
Closes #395