1+ using EPPlus . Export . Pdf . Helpers ; // internal PngDecoder (InternalsVisibleTo is already set)
2+ using EPPlus . Export . Pdf . Tests ; // PdfTestBase -> TestBase (license + OpenTemplatePackage)
3+ using OfficeOpenXml ;
4+ using OfficeOpenXml . Export . PdfExport ; // SaveAsPdf extension methods
5+ using System ;
6+ using System . IO ;
7+ using System . Text ;
8+
9+ namespace EPPlusTest . PDF
10+ {
11+ /// <summary>
12+ /// Security regression tests for the embedded-image decoders. The attack: a tiny PNG whose
13+ /// IHDR *declares* enormous dimensions (30000x30000) while the IDAT is a real 1x1 pixel.
14+ /// The decoder sizes its output buffer from the header (width*height*3 in Int32), which
15+ /// overflows to a negative length and throws, aborting the entire export. Reachable by anyone
16+ /// who can get an image into a workbook that gets converted to PDF.
17+ /// </summary>
18+ [ TestClass ]
19+ public class ImageDecoderSecurityTests : PdfTestBase
20+ {
21+ // IHDR says 30000x30000, 8-bit, colour type 6 (RGBA). IDAT is a valid zlib stream for one
22+ // 1x1 pixel. (Exact bytes of evil_oversized_30000x30000.png.)
23+ private static readonly byte [ ] OversizedRgbaPng = new byte [ ]
24+ {
25+ 137 , 80 , 78 , 71 , 13 , 10 , 26 , 10 , 0 , 0 , 0 , 13 ,
26+ 73 , 72 , 68 , 82 , 0 , 0 , 117 , 48 , 0 , 0 , 117 , 48 ,
27+ 8 , 6 , 0 , 0 , 0 , 102 , 39 , 248 , 186 , 0 , 0 , 0 ,
28+ 13 , 73 , 68 , 65 , 84 , 120 , 218 , 99 , 248 , 207 , 192 , 240 ,
29+ 31 , 0 , 5 , 0 , 1 , 255 , 86 , 199 , 47 , 13 , 0 , 0 ,
30+ 0 , 0 , 73 , 69 , 78 , 68 , 174 , 66 , 96 , 130 ,
31+ } ;
32+
33+ private const int OversizedW = 30000 ;
34+ private const int OversizedH = 30000 ;
35+ private const int RgbaColorType = 6 ;
36+
37+ /// <summary>The header parses as a PNG but the dimensions must be rejected by the guard.</summary>
38+ [ TestMethod ]
39+ public void PngDecoder_RejectsOversizedDimensions ( )
40+ {
41+ Assert . IsTrue ( PngDecoder . IsPng ( OversizedRgbaPng ) ) ;
42+
43+ bool ok = PngDecoder . ReadPngHeader (
44+ OversizedRgbaPng , out int w , out int h , out int bitDepth , out int colorType , out _ ) ;
45+
46+ Assert . IsFalse ( ok , "Oversized PNG header must be rejected by the ImageLimits guard." ) ;
47+ // Dimensions are still parsed out (the guard runs after reading them) — confirm the
48+ // payload really is the oversized one, so this test can't pass for the wrong reason.
49+ Assert . AreEqual ( OversizedW , w ) ;
50+ Assert . AreEqual ( OversizedH , h ) ;
51+ Assert . AreEqual ( RgbaColorType , colorType ) ;
52+ }
53+
54+ /// <summary>Calling the decode path directly must throw a clean, typed error — never overflow/OOM.</summary>
55+ [ TestMethod ]
56+ public void DecodePngWithAlpha_OnOversized_ThrowsInvalidData ( )
57+ {
58+ Assert . ThrowsExactly < InvalidDataException > ( ( ) =>
59+ PngDecoder . DecodePngWithAlpha (
60+ OversizedRgbaPng , OversizedW , OversizedH ,
61+ blackAndWhite : false , colorType : RgbaColorType ,
62+ out _ , out _ ) ) ;
63+ }
64+
65+ /// <summary>End-to-end: a workbook containing the oversized image still exports cleanly.</summary>
66+ [ TestMethod ]
67+ public void Export_WithOversizedPng_DoesNotThrow_AndProducesValidPdf ( )
68+ {
69+ using var p = OpenTemplatePackage ( "PDFTest.xlsx" ) ;
70+ var ws = p . Workbook . Worksheets [ 0 ] ;
71+
72+ using ( var imgStream = new MemoryStream ( OversizedRgbaPng ) )
73+ ws . Drawings . AddPicture ( "evil_oversized" , imgStream ) ;
74+
75+ using var ms = new MemoryStream ( ) ;
76+
77+ ws . SaveAsPdf ( ms ) ; // must NOT throw — the bad image is skipped/blanked
78+
79+ byte [ ] bytes = ms . ToArray ( ) ;
80+ Assert . IsTrue ( bytes . Length > 8 , "No PDF was produced." ) ;
81+ Assert . AreEqual ( "%PDF-" , Encoding . ASCII . GetString ( bytes , 0 , 5 ) , "Missing PDF header." ) ;
82+
83+ string tail = Encoding . ASCII . GetString ( bytes , Math . Max ( 0 , bytes . Length - 8 ) , Math . Min ( 8 , bytes . Length ) ) ;
84+ Assert . IsTrue ( tail . Contains ( "%%EOF" ) , "Missing %%EOF trailer — document is truncated." ) ;
85+ }
86+ }
87+ }
0 commit comments