Repository navigation
Harden EPPlus.Fonts.OpenType against malformed fonts - #2634
Merged
Merged
Conversation
AdrianEPPlus
reviewed
Dec 2, 2026
| @@ -24,6 +24,51 @@ public FontsBinaryReader(Stream input) : base(input) | |||
|
|
|||
| private string _context; | |||
| private int _numberOfReadBytes = 0; | |||
Contributor
There was a problem hiding this comment.
GitHub säger:
The field 'FontsBinaryReader._numberOfReadBytes' is assigned but its value is never used
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Harden EPPlus.Fonts.OpenType against malformed fonts
Summary
A security review of
EPPlus.Fonts.OpenTypefound that a crafted or broken font file could crash the process, make it hang, or make it allocate memory and CPU out of all proportion to the size of the font. This PR fixes those issues in two rounds (two commits) and adds regression tests for each of them.Fonts reach the library from three places: the font folders scanned on the machine, fonts passed in with
OpenTypeFontEngine.GetFromBytes, and fonts embedded in exported documents. The fixes matter most where fonts can come from users or third parties, for example a server that generates PDF or SVG with customer fonts.All findings are availability issues (denial of service) or robustness issues. Nothing found allows code execution or memory corruption, since the library is managed code.
Valid fonts behave as before. Each step was checked against about 600 real font faces: the test fonts,
C:\Windows\Fontsand the user font folder. Where output changes, it is listed under Behavior changes below.Commit 1: parsing (cmap, glyf, TTC, GSUB/GPOS)
CmapSubtable12.GetGlyphMappingsuintloop could also wrap).longloop, clamped to U+10FFFF, overlapping groups skipped (first group wins), reversed groups skipped.CmapSubtable4.GetGlyphMappingsMapCodePointToGlyph.PairPosSubTableFormat2DeserializerClass1Count × Class2Countallocated a matrix of up to 4 billion records from a few bytes.ValueRecord.GetSizewas added.SinglePosSubTableFormat2DeserializerValueCount.GlyfTableLoader, glyph deserializers)DefaultFontFileReader.ExtractTtcFontInvalidDataException.FontsBinaryReader: at most max(256 KB, 16 × table length). Real fonts read at most 10.4 × the table length (measured on 785 tables). If the budget is exceeded, the table is ignored and a warning is recorded.Load warnings: a new
FontLogEventType.FontTableIgnoredvalue is logged throughIFontLoggerwhen a table is ignored. The warnings are also available internally asOpenTypeFont.LoadWarnings.Commit 2: second review (parsing, subsetting, shaping, serialization)
Parsing
ChainingContextualHandler.Discoverno longer recurses; the fixpoint loop inGsubSubsetProcessoralready discovers the referenced lookups.TableLoaderhang: if loading a table threw an exception, every laterLoad()on that loader waited forever inMonitor.Wait. The in-progress flag is now cleared and waiting threads are woken.OpenTypeFontand the scanner (FontScannerV2Core), the directory must fit in the data, every table record must lie within the data, and duplicate tags are rejected. Before, the scanner could allocate a buffer of almost 2 GB from a forgednamelength.MarkClassCount = 0can never attach a mark, and it allocated up to 65535 empty records. It is now skipped.NameTable.LoadFromBytesin the scanner. A kern table over budget is ignored with a warning. A font whose cmap or name table is over budget is rejected withInvalidDataException, since it cannot be used without them. Real fonts read at most 2 × the table length.Subsetting
BuildGlyphMappingused aushortloop counter, and a GSUB substitution that pulled in all 65536 glyph IDs made it wrap around forever. Glyph IDs that do not exist in the font (≥maxp.numGlyphs) are now removed before the mapping is built, and the counter is anint.PairPosSubTableFormat2.Serializewrote the header fields in the wrong order. The bug was latent, because only subsets serialize from objects and they never contained format 2 before.Serialization
LookupListTable.Serializenow writes Extension lookups (GSUB type 7, GPOS type 9, with 32-bit offsets) when the 16-bit layout does not fit. Lists that fit are written byte for byte as before.Shaping
MultipleSubstitutionProcessor.MaxSequenceLength). Real fonts use at most 8.ShapingLimits.MaxSubtableReferences = 65536bounds the references examined. Real fonts have at most 9938 (Gabriola).Behavior changes
FontLogEventType.FontTableIgnored(inEPPlus.Interfaces).InvalidDataException. This applies to a table directory outside the data, a duplicate table tag, a malformed TTC, and cmap or name over the read budget. The scanner skips such files.FontTableIgnoredwarning.Tests
The new test classes are listed below. Each was also run against the code before the fix, and fails there as expected.
CmapSubtable12TestsCmapSubtable4TestsPairPosSubTableFormat2DeserializerTestsSinglePosSubTableFormat2DeserializerTestsGlyfBoundsTestsDefaultFontFileReaderTestsLayoutTableReadBudgetTestsLayoutTableReadAmountTestsGsubSubsetRecursionTestsTableLoaderFailureTestsTableDirectoryValidationTestsMarkToBaseSubTableFormat1DeserializerTestsLigatureSubstSubTableDeserializerTestsBasicTableReadBudgetTestsSubsetGlyphRangeTestsMultipleSubstitutionLimitTestsPairPosFormat2SubsettingTestsLayoutLookupOverflowTestsShapingLookupReferenceLimitTestsGsubTestFontBuilder.The crafted fonts are built in the tests, by patching or extending
Roboto-Regular.ttfand a few other test fonts, so no malicious font files are checked in.Result: 705 tests passed and 12 were skipped. The library builds for all target frameworks with no new warnings.
Known remaining issues (not in this PR)
ThreadStaticShaperCachekeeping fonts alive, and the fsType check that can be bypassed with a self-declared family name.new ushort[-1]inLigatureTableDeserializerwhen the component count is 0,byte,OffsetInFilein the scanner,locanot bounded by the table length,GposTable.Rewritewithout a FeatureList,FontStorestall.