Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.
C# audio-converteravaloniabatch-conversionconvertercsharpdocument-converterdocx-to-pdfdotnetdrag-and-dropfile-conversionfile-converterheic-converterhwphwpximage-converterlinuxmacospdf-convertervideo-converterwindows
feat(hwp): resource limits and fuzzing for the document readers
KnifeLemonc79393b67cef
diff --git a/.github/workflows/fuzz.yml b/.github/workflows/fuzz.ymlnew file mode 100644index 00000000..caa5c0ca--- /dev/null+++ b/.github/workflows/fuzz.yml@@ -0,0 +1,58 @@+# Nightly mutation fuzzing of the document readers (tests/Filee.Engines.Tests/DocumentFuzzTests.cs): each reader gets a+# few minutes of inputs changed from seeds written in code, with a new random seed every night. Pull requests run the+# same tests briefly with a fixed seed as part of the normal test run; this job only adds the long run. A failure+# names the seed (FILEE_FUZZ_SEED repeats it) and uploads the inputs that failed.+name: Fuzz++on:+ schedule:+ - cron: "17 18 * * *" # 03:17 in Korea+ workflow_dispatch:+ inputs:+ seconds:+ description: Seconds per reader+ default: "180"+ seed:+ description: Seed to repeat (empty for a new one)+ default: ""++permissions:+ contents: read++concurrency:+ group: fuzz+ cancel-in-progress: true++jobs:+ fuzz:+ runs-on: ubuntu-24.04+ timeout-minutes: 25+ steps:+ - uses: actions/checkout@v7++ - uses: actions/setup-dotnet@v6+ with:+ global-json-file: global.json++ - name: Build+ run: dotnet build tests/Filee.Engines.Tests/Filee.Engines.Tests.csproj -c Release++ - name: Fuzz the document readers+ env:+ FILEE_FUZZ_SECONDS: ${{ inputs.seconds || '180' }}+ FILEE_FUZZ_SEED: ${{ inputs.seed }}+ FILEE_FUZZ_OUT: ${{ runner.temp }}/fuzz-failures+ run: |+ mkdir -p "$FILEE_FUZZ_OUT"+ dotnet test tests/Filee.Engines.Tests/Filee.Engines.Tests.csproj --no-build -c Release --filter "Category=Fuzz" \+ --blame-crash --results-directory TestResults --logger "console;verbosity=detailed"++ - name: Upload the inputs that failed+ if: failure()+ uses: actions/upload-artifact@v7+ with:+ name: fuzz-failures+ path: |+ ${{ runner.temp }}/fuzz-failures/+ TestResults/+ if-no-files-found: ignorediff --git a/docs/ENGINES.md b/docs/ENGINES.mdindex e2b6f68e..bf918f9f 100644--- a/docs/ENGINES.md+++ b/docs/ENGINES.md@@ -628,7 +628,8 @@ sits on the office donut (settings schema 27 teaches existing donuts `.ofd`).resource files (PublicRes, DocumentRes, a page's PageRes, and the unnamed `Res.xml` WPS writes next to them).Parts are looked up by name inside the ZIP and never written out under their own names; references are resolvedagainst the referring part (or the package root for "/…"), "." and ".." applied, and anything that leaves the- package, a URL or a drive path finds nothing. XML is read without DTDs; a part may unpack to 64 MB, a picture to+ package, a URL or a drive path finds nothing. The package is read like HWPX and DOCX (`Documents/DocumentPackage.cs`,+ see [HWPX reader](#hwpx-reader)): XML without DTDs; a part may unpack to 64 MB, a picture to32 MB (larger ones are left out), the whole package to 1 GB; at most 100,000 parts, 10,000 pages, 2,000,000drawing objects, page blocks nested 32 deep. DeltaX / DeltaY "g n d" repeats never yield more values than thetext has characters.@@ -1234,6 +1235,31 @@ OLE objects, charts, video, form controls, text art (its text is kept), arcs / pmaster pages, memos, character ratio, relative size and offset, paragraph borders, picture cropping and rotation.Password-protected (encrypted) HWPX and DRM-wrapped files stop with a clear error.+**Limits.** Untrusted documents must not hang, exhaust the memory or crash Filee. The HWPX, DOCX and OFD readers (and+the HWP 5.0 reader and the layout engine when they come) share `Documents/DocumentLimits.cs`; a document that passes+one stops with `error.document_too_complex` and says which:++- the package (`Documents/DocumentPackage.cs`): at most 100,000 parts; a part may unpack to 64 MB, all parts together+ to 1 GB, counted while they unpack (a ZIP header can understate a size), and a part over 4 MB to no more than 200+ times its packed size; parts are found by name inside the package only, and extracted pictures get names Filee+ makes (`hwpx-media-1.png`), never the part's;+- XML (`Infrastructure/SafeXml.cs`, also used for KML and GPX): no DTD and no resolver, so no entity expands and+ nothing outside the package is read; at most 512 levels deep and 5,000,000 elements in a package;+- the document (`Documents/DocumentBudget.cs`): tables in tables, text boxes, notes, groups and fields nest at most 32+ levels; at most 1,000,000 paragraphs and 200,000 tables, pictures and drawing objects; table columns, spans and+ row spans are cut to the table (at most 16,384 columns); Word style chains are followed 20 styles deep;+- pictures (`Documents/PictureSize.cs`): one whose PNG, JPEG, GIF, BMP or WebP header claims more than+ 16,384 × 16,384 pixels is left out before anything decodes it;+- layout (`Documents/LayoutGuard.cs`, for the layout engine): at most 20,000 pages, 64 empty pages in a row, ten minutes;+ cancellation and the time are checked per paragraph, and an object larger than the page is clipped and placed.++Embedded fonts (DOCX `word/fonts`, HWPX and OFD font files) are not read; a reader that uses them must pass them+through Filee's SFNT parser (`Fonts/SfntFont.cs`) first. `DocumentHostileInputTests` holds ZIP bombs, deep nesting,+DTDs and huge pictures; `DocumentFuzzTests` changes packages written in code at random (attributes set to extreme+values, elements removed, repeated, renamed or nested, text and bytes damaged) and allows a reader only to read a+file or refuse it with `InvalidDataException` / `ConversionRefusedException`. It runs briefly with every test run and+for a few minutes per reader every night (`.github/workflows/fuzz.yml`); `FILEE_FUZZ_SEED` repeats a run.+## E-booksThe built-in e-book engine (`src/Filee.Engines/Ebooks`) reads books into the same document model as the HWPXdiff --git a/src/Filee.App/Assets/i18n/ar.json b/src/Filee.App/Assets/i18n/ar.jsonindex 5c54c4b6..b4d9688a 100644--- a/src/Filee.App/Assets/i18n/ar.json+++ b/src/Filee.App/Assets/i18n/ar.json@@ -129,6 +129,7 @@"error.fon_unsupported": "هذا النوع من خطوط .fon غير مدعوم","error.fon_too_large": "خط .fon كبير جدًا بحيث لا يمكن تحويله","error.fon_damaged": "ملف .fon تالف أو غير مكتمل",+ "error.document_too_complex": "المستند كبير جدًا أو معقّد جدًا بحيث لا يمكن قراءته بأمان","error.m4s_needs_init": "ملف .m4s هذا جزء من فيديو متدفّق، ولا يمكن قراءته دون مقطع البداية (init) المنفصل الخاص به","notice.pdf_not_smaller": "تعذّر تقليل الحجم، لذا نُسخ الملف كما هو","notice.cad_newer_left_out": "حُذفت كائنات لا يدعمها إصدار AutoCAD المختار (أسهم توضيحية متعددة، شبكات …)",diff --git a/src/Filee.App/Assets/i18n/de.json b/src/Filee.App/Assets/i18n/de.jsonindex 6a234a92..b48177ad 100644--- a/src/Filee.App/Assets/i18n/de.json+++ b/src/Filee.App/Assets/i18n/de.json@@ -129,6 +129,7 @@"error.fon_unsupported": "Diese Art von .fon-Schrift wird nicht unterstützt","error.fon_too_large": "Die .fon-Schrift ist zu groß zum Konvertieren","error.fon_damaged": "Die .fon-Datei ist beschädigt oder unvollständig",+ "error.document_too_complex": "Das Dokument ist zu groß oder zu komplex, um es sicher zu lesen","error.m4s_needs_init": "Diese .m4s-Datei ist ein Stück eines gestreamten Videos und lässt sich ohne ihr separates Startsegment (init) nicht lesen","notice.pdf_not_smaller": "Die Datei ließ sich nicht verkleinern und wurde unverändert kopiert","notice.cad_newer_left_out": "Objekte, die die gewählte AutoCAD-Version nicht unterstützt (Multiführungslinien, Netze …), wurden weggelassen",diff --git a/src/Filee.App/Assets/i18n/en.json b/src/Filee.App/Assets/i18n/en.jsonindex a168cd0e..746e9025 100644--- a/src/Filee.App/Assets/i18n/en.json+++ b/src/Filee.App/Assets/i18n/en.json@@ -135,6 +135,7 @@"error.fon_unsupported": "This kind of .fon font isn't supported","error.fon_too_large": "The .fon font is too large to convert","error.fon_damaged": "The .fon file is damaged or cut short",+ "error.document_too_complex": "The document is too large or too complex to read safely","error.m4s_needs_init": "This .m4s is one piece of a streamed video: it can't be read without its separate start (init) segment","notice.pdf_not_smaller": "Couldn't make it smaller, so it was copied as it is","notice.cad_newer_left_out": "Objects the chosen AutoCAD version doesn't have (multileaders, meshes …) were left out",diff --git a/src/Filee.App/Assets/i18n/es.json b/src/Filee.App/Assets/i18n/es.jsonindex dea96a59..83c051b7 100644--- a/src/Filee.App/Assets/i18n/es.json+++ b/src/Filee.App/Assets/i18n/es.json@@ -129,6 +129,7 @@"error.fon_unsupported": "Este tipo de fuente .fon no es compatible","error.fon_too_large": "La fuente .fon es demasiado grande para convertirla","error.fon_damaged": "El archivo .fon está dañado o incompleto",+ "error.document_too_complex": "El documento es demasiado grande o complejo para leerlo de forma segura","error.m4s_needs_init": "Este .m4s es un fragmento de un vídeo en streaming y no se puede leer sin su segmento de inicio (init) aparte","notice.pdf_not_smaller": "No se pudo reducir su tamaño, por lo que se copió tal cual","notice.cad_newer_left_out": "Se omitieron objetos que la versión de AutoCAD elegida no tiene (directrices múltiples, mallas…)",diff --git a/src/Filee.App/Assets/i18n/fr.json b/src/Filee.App/Assets/i18n/fr.jsonindex 948a24c1..f29a60ee 100644--- a/src/Filee.App/Assets/i18n/fr.json+++ b/src/Filee.App/Assets/i18n/fr.json@@ -135,6 +135,7 @@"error.fon_unsupported": "Ce type de police .fon n'est pas pris en charge","error.fon_too_large": "La police .fon est trop grande pour être convertie","error.fon_damaged": "Le fichier .fon est endommagé ou incomplet",+ "error.document_too_complex": "Le document est trop volumineux ou trop complexe pour être lu en toute sécurité","error.m4s_needs_init": "Ce .m4s est un morceau d'une vidéo diffusée en streaming : il ne peut pas être lu sans son segment de départ (init) séparé","notice.pdf_not_smaller": "Impossible de réduire sa taille : le fichier a été copié tel quel","notice.cad_newer_left_out": "Des objets que la version d'AutoCAD choisie n'a pas (multilignes de repère, maillages…) ont été omis",diff --git a/src/Filee.App/Assets/i18n/hi.json b/src/Filee.App/Assets/i18n/hi.jsonindex f8f1ea2f..ee0368fe 100644--- a/src/Filee.App/Assets/i18n/hi.json+++ b/src/Filee.App/Assets/i18n/hi.json@@ -129,6 +129,7 @@"error.fon_unsupported": "इस तरह का .fon फ़ॉन्ट समर्थित नहीं है","error.fon_too_large": ".fon फ़ॉन्ट बदलने के लिए बहुत बड़ा है","error.fon_damaged": ".fon फ़ाइल खराब है या अधूरी है",+ "error.document_too_complex": "दस्तावेज़ इतना बड़ा या जटिल है कि उसे सुरक्षित रूप से पढ़ा नहीं जा सकता","error.m4s_needs_init": "यह .m4s स्ट्रीम किए गए वीडियो का एक हिस्सा है: इसके अलग शुरुआती (init) सेगमेंट के बिना इसे पढ़ा नहीं जा सकता","notice.pdf_not_smaller": "इसे छोटा नहीं किया जा सका, इसलिए जैसा है वैसा ही कॉपी कर दिया गया","notice.cad_newer_left_out": "चुने गए AutoCAD संस्करण में न होने वाले ऑब्जेक्ट (मल्टीलीडर, मेश …) छोड़ दिए गए",diff --git a/src/Filee.App/Assets/i18n/ja.json b/src/Filee.App/Assets/i18n/ja.jsonindex 2c897a5d..bd821d60 100644--- a/src/Filee.App/Assets/i18n/ja.json+++ b/src/Filee.App/Assets/i18n/ja.json@@ -129,6 +129,7 @@"error.fon_unsupported": "この種類の.fonフォントには対応していません","error.fon_too_large": ".fonフォントが大きすぎて変換できません","error.fon_damaged": ".fonファイルが破損しているか、途中で切れています",+ "error.document_too_complex": "文書が大きすぎるか複雑すぎるため、安全に読み込めません","error.m4s_needs_init": "この .m4s はストリーミング動画の一部で、別にある開始(init)セグメントがないと読み込めません","notice.pdf_not_smaller": "サイズを小さくできなかったため、元のままコピーしました","notice.cad_newer_left_out": "選んだ AutoCAD のバージョンにないオブジェクト(マルチ引出線、メッシュ …)を省きました",diff --git a/src/Filee.App/Assets/i18n/ko.json b/src/Filee.App/Assets/i18n/ko.jsonindex bdf82d15..3013b8e0 100644--- a/src/Filee.App/Assets/i18n/ko.json+++ b/src/Filee.App/Assets/i18n/ko.json@@ -135,6 +135,7 @@"error.fon_unsupported": "지원하지 않는 종류의 .fon 글꼴이에요","error.fon_too_large": ".fon 글꼴이 너무 커서 변환할 수 없어요","error.fon_damaged": ".fon 파일이 손상되었거나 잘렸어요",+ "error.document_too_complex": "문서가 너무 크거나 복잡해서 안전하게 읽을 수 없어요","error.m4s_needs_init": "스트리밍 동영상의 한 조각인 .m4s 파일이에요. 따로 있는 시작(init) 세그먼트 없이는 읽을 수 없어요","notice.pdf_not_smaller": "더 작게 만들 수 없어서 그대로 복사했어요","notice.cad_newer_left_out": "선택한 AutoCAD 버전에 없는 개체(다중 지시선, 메시 …)는 빼고 저장했어요",diff --git a/src/Filee.App/Assets/i18n/ne.json b/src/Filee.App/Assets/i18n/ne.jsonindex b765447f..82fcb734 100644--- a/src/Filee.App/Assets/i18n/ne.json+++ b/src/Filee.App/Assets/i18n/ne.json@@ -129,6 +129,7 @@"error.fon_unsupported": "यस प्रकारको .fon फन्ट समर्थित छैन","error.fon_too_large": ".fon फन्ट रूपान्तरण गर्न धेरै ठूलो छ","error.fon_damaged": ".fon फाइल बिग्रिएको वा अपूर्ण छ",+ "error.document_too_complex": "कागजात सुरक्षित रूपमा पढ्न नसकिने गरी धेरै ठूलो वा जटिल छ","error.m4s_needs_init": "यो .m4s स्ट्रिम गरिएको भिडियोको एउटा टुक्रा हो: छुट्टै सुरुवाती (init) खण्डबिना यसलाई पढ्न सकिँदैन","notice.pdf_not_smaller": "यसलाई सानो बनाउन सकिएन, त्यसैले जस्तो छ त्यस्तै प्रतिलिपि गरियो","notice.cad_newer_left_out": "छानिएको AutoCAD संस्करणमा नभएका वस्तुहरू (मल्टिलिडर, मेस …) छोडिए",diff --git a/src/Filee.App/Assets/i18n/pl.json b/src/Filee.App/Assets/i18n/pl.jsonindex 0bfb19c6..23b597c8 100644--- a/src/Filee.App/Assets/i18n/pl.json+++ b/src/Filee.App/Assets/i18n/pl.json@@ -135,6 +135,7 @@"error.fon_unsupported": "Ten rodzaj czcionki .fon nie jest obsługiwany","error.fon_too_large": "Czcionka .fon jest zbyt duża, aby ją przekonwertować","error.fon_damaged": "Plik .fon jest uszkodzony lub niekompletny",+ "error.document_too_complex": "Dokument jest zbyt duży lub zbyt złożony, aby bezpiecznie go odczytać","error.m4s_needs_init": "Ten plik .m4s to fragment wideo strumieniowego i nie da się go odczytać bez osobnego segmentu początkowego (init)","notice.pdf_not_smaller": "Nie udało się zmniejszyć pliku, więc skopiowano go bez zmian","notice.cad_newer_left_out": "Pominięto obiekty, których wybrana wersja AutoCAD nie ma (multiodnośniki, siatki…)",diff --git a/src/Filee.App/Assets/i18n/pt-BR.json b/src/Filee.App/Assets/i18n/pt-BR.jsonindex c98e43d6..16eb4a1d 100644--- a/src/Filee.App/Assets/i18n/pt-BR.json+++ b/src/Filee.App/Assets/i18n/pt-BR.json@@ -135,6 +135,7 @@"error.fon_unsupported": "Este tipo de fonte .fon não é compatível","error.fon_too_large": "A fonte .fon é grande demais para converter","error.fon_damaged": "O arquivo .fon está danificado ou incompleto",+ "error.document_too_complex": "O documento é grande ou complexo demais para ser lido com segurança","error.m4s_needs_init": "Este .m4s é um pedaço de um vídeo transmitido por streaming e não pode ser lido sem o seu segmento inicial (init) separado","notice.pdf_not_smaller": "Não deu para deixar menor, então foi copiado como está","notice.cad_newer_left_out": "Objetos que a versão do AutoCAD escolhida não tem (multilinhas de chamada, malhas…) ficaram de fora",diff --git a/src/Filee.App/Assets/i18n/pt-PT.json b/src/Filee.App/Assets/i18n/pt-PT.jsonindex 8692facc..5227026e 100644--- a/src/Filee.App/Assets/i18n/pt-PT.json+++ b/src/Filee.App/Assets/i18n/pt-PT.json@@ -135,6 +135,7 @@"error.fon_unsupported": "Este tipo de fonte .fon não é suportado","error.fon_too_large": "A fonte .fon é demasiado grande para converter","error.fon_damaged": "O ficheiro .fon está danificado ou incompleto",+ "error.document_too_complex": "O documento é demasiado grande ou complexo para ser lido em segurança","error.m4s_needs_init": "Este .m4s é um pedaço de um vídeo em streaming e não pode ser lido sem o respetivo segmento inicial (init) separado","notice.pdf_not_smaller": "Não foi possível reduzir o tamanho, pelo que o ficheiro foi copiado sem alterações","notice.cad_newer_left_out": "Os objetos que a versão de AutoCAD escolhida não suporta (linhas de chamada múltiplas, malhas…) foram omitidos",diff --git a/src/Filee.App/Assets/i18n/ru.json b/src/Filee.App/Assets/i18n/ru.jsonindex 7683e5f0..0d1e7640 100644--- a/src/Filee.App/Assets/i18n/ru.json+++ b/src/Filee.App/Assets/i18n/ru.json@@ -135,6 +135,7 @@"error.fon_unsupported": "Этот тип шрифта .fon не поддерживается","error.fon_too_large": "Шрифт .fon слишком велик для преобразования","error.fon_damaged": "Файл .fon повреждён или обрезан",+ "error.document_too_complex": "Документ слишком большой или сложный, чтобы безопасно его прочитать","error.m4s_needs_init": "Этот .m4s — фрагмент потокового видео: без отдельного начального сегмента (init) его не прочитать","notice.pdf_not_smaller": "Уменьшить не получилось, файл скопирован как есть","notice.cad_newer_left_out": "Объекты, которых нет в выбранной версии AutoCAD (мультивыноски, сети…), не сохранены",diff --git a/src/Filee.App/Assets/i18n/th.json b/src/Filee.App/Assets/i18n/th.jsonindex 0c597319..52aaa355 100644--- a/src/Filee.App/Assets/i18n/th.json+++ b/src/Filee.App/Assets/i18n/th.json@@ -129,6 +129,7 @@"error.fon_unsupported": "ไม่รองรับฟอนต์ .fon ประเภทนี้","error.fon_too_large": "ฟอนต์ .fon มีขนาดใหญ่เกินกว่าจะแปลงได้","error.fon_damaged": "ไฟล์ .fon เสียหายหรือไม่สมบูรณ์",+ "error.document_too_complex": "เอกสารใหญ่หรือซับซ้อนเกินกว่าจะอ่านได้อย่างปลอดภัย","error.m4s_needs_init": "ไฟล์ .m4s นี้เป็นชิ้นส่วนหนึ่งของวิดีโอสตรีมมิง และอ่านไม่ได้หากไม่มีเซกเมนต์เริ่มต้น (init) ที่แยกไว้","notice.pdf_not_smaller": "ไม่สามารถบีบอัดให้เล็กลงได้ จึงคัดลอกไฟล์ตามเดิม","notice.cad_newer_left_out": "ละวัตถุที่ AutoCAD เวอร์ชันที่เลือกไม่มี (มัลติลีดเดอร์ เมช …) ไว้",diff --git a/src/Filee.App/Assets/i18n/tr.json b/src/Filee.App/Assets/i18n/tr.jsonindex e350fbc6..efac198d 100644--- a/src/Filee.App/Assets/i18n/tr.json+++ b/src/Filee.App/Assets/i18n/tr.json@@ -129,6 +129,7 @@"error.fon_unsupported": "Bu tür .fon yazı tipi desteklenmiyor","error.fon_too_large": ".fon yazı tipi dönüştürmek için çok büyük","error.fon_damaged": ".fon dosyası hasarlı veya eksik",+ "error.document_too_complex": "Belge güvenli şekilde okunamayacak kadar büyük veya karmaşık","error.m4s_needs_init": "Bu .m4s, akışla gelen bir videonun bir parçası: ayrı başlangıç (init) segmenti olmadan okunamaz","notice.pdf_not_smaller": "Küçültülemediği için olduğu gibi kopyalandı","notice.cad_newer_left_out": "Seçilen AutoCAD sürümünün desteklemediği nesneler (çoklu kılavuzlar, ağlar…) çıkarıldı",diff --git a/src/Filee.App/Assets/i18n/zh-CN.json b/src/Filee.App/Assets/i18n/zh-CN.jsonindex 4b96b320..4ffdde51 100644--- a/src/Filee.App/Assets/i18n/zh-CN.json+++ b/src/Filee.App/Assets/i18n/zh-CN.json@@ -135,6 +135,7 @@"error.fon_unsupported": "不支持此类 .fon 字体","error.fon_too_large": ".fon 字体太大,无法转换","error.fon_damaged": ".fon 文件已损坏或不完整",+ "error.document_too_complex": "文档过大或过于复杂,无法安全读取","error.m4s_needs_init": "此 .m4s 是流媒体视频的一个片段,没有单独的起始(init)分段就无法读取","notice.pdf_not_smaller": "无法再缩小,已原样复制","notice.cad_newer_left_out": "已省略所选 AutoCAD 版本没有的对象(多重引线、网格……)",diff --git a/src/Filee.App/Assets/i18n/zh-TW.json b/src/Filee.App/Assets/i18n/zh-TW.jsonindex 8a56d201..b83c0221 100644--- a/src/Filee.App/Assets/i18n/zh-TW.json+++ b/src/Filee.App/Assets/i18n/zh-TW.json@@ -135,6 +135,7 @@"error.fon_unsupported": "不支援此類 .fon 字型","error.fon_too_large": ".fon 字型太大,無法轉換","error.fon_damaged": ".fon 檔案已損壞或不完整",+ "error.document_too_complex": "文件過大或過於複雜,無法安全讀取","error.m4s_needs_init": "此 .m4s 是串流影片的一個片段,沒有另外的起始(init)區段就無法讀取","notice.pdf_not_smaller": "無法再縮小,已原樣複製","notice.cad_newer_left_out": "已略過所選 AutoCAD 版本沒有的物件(複線引線、網格……)",diff --git a/src/Filee.Engines/Documents/DocumentBudget.cs b/src/Filee.Engines/Documents/DocumentBudget.csnew file mode 100644index 00000000..631f19cb--- /dev/null+++ b/src/Filee.Engines/Documents/DocumentBudget.cs@@ -0,0 +1,58 @@+// What one document reader may still build: nesting (tables in tables, text boxes, notes, groups, fields) and the+// number of paragraphs and objects. Readers call it where they recurse or add to the model, so a small file that+// expands into millions of paragraphs, or nests deeply enough to overflow the stack in a writer, is refused.++namespace Filee.Engines.Documents;++/// <summary>Counts what a reader builds against <see cref="DocumentLimits"/>.</summary>+internal sealed class DocumentBudget(DocumentLimits limits)+{+ private int _depth;+ private int _paragraphs;+ private int _objects;++ public DocumentLimits Limits { get; } = limits;++ /// <summary>Current nesting depth.</summary>+ public int Depth => _depth;++ public int Paragraphs => _paragraphs;++ public int Objects => _objects;++ /// <summary>Enters a nested level (a table cell, text box, note, group); dispose the result to leave it.</summary>+ public Level Enter()+ {+ if (_depth >= Limits.MaxNestingDepth)+ throw DocumentLimits.Exceeded($"Tables, text boxes, notes or groups are nested more than {Limits.MaxNestingDepth} levels deep.");+ _depth++;+ return new Level(this);+ }++ /// <summary>Checks a count of open fields (fields inside fields).</summary>+ public void Fields(int open)+ {+ if (open > Limits.MaxNestingDepth)+ throw DocumentLimits.Exceeded($"Fields are nested more than {Limits.MaxNestingDepth} levels deep.");+ }++ /// <summary>Counts a paragraph.</summary>+ public void Paragraph()+ {+ if (++_paragraphs > Limits.MaxParagraphs)+ throw DocumentLimits.Exceeded($"The document has more than {DocumentLimits.Count(Limits.MaxParagraphs)} paragraphs.");+ }++ /// <summary>Counts a table, picture, shape, text box or equation.</summary>+ public void Object()+ {+ if (++_objects > Limits.MaxObjects)+ throw DocumentLimits.Exceeded($"The document has more than {DocumentLimits.Count(Limits.MaxObjects)} tables, pictures and drawing objects.");+ }++ /// <summary>A nested level; leaves it when disposed.</summary>+ public readonly struct Level(DocumentBudget budget) : IDisposable+ {+ public void Dispose() => budget._depth--;+ }+}diff --git a/src/Filee.Engines/Documents/DocumentLimits.cs b/src/Filee.Engines/Documents/DocumentLimits.csnew file mode 100644index 00000000..892cf8e0--- /dev/null+++ b/src/Filee.Engines/Documents/DocumentLimits.cs@@ -0,0 +1,96 @@+// How much of an untrusted document Filee reads and lays out, shared by the document readers (HWPX, DOCX, OFD, and+// the HWP 5.0 reader when it comes) and the layout engine, so a hostile file can't hang, exhaust the memory or crash+// Filee. Every limit is far above what real documents need: a document that passes one is refused with+// error.document_too_complex rather than read in part.+//+// Where they apply:+// * DocumentPackage ZIP packages: entries, bytes per part and in total (counted while unpacking, so a header that+// understates a part does not help), the inflate ratio, XML depth and elements (SafeXml).+// * DocumentBudget readers: nesting of tables, text boxes, notes, groups and fields; paragraphs and objects.+// * PictureSize pictures whose header claims more than MaxPicturePixels are left out.+// * LayoutGuard the layout loop: pages, empty pages in a row, the time budget and cancellation.+// A compound file (CFB) reader adds its stream sizes to MaxPartBytes / MaxPackageBytes and must check sector chains+// for cycles itself.+//+// Embedded fonts are not read by any reader (DOCX odttf parts, HWPX and OFD font files keep only their names). A+// reader that ever uses them must pass them through Filee's own SFNT parser (Fonts/SfntFont) before Skia or HarfBuzz.++using System.Globalization;+using Filee.Core.Conversion;+using Filee.Engines.Infrastructure;++namespace Filee.Engines.Documents;++/// <summary>Limits for reading and laying out one document.</summary>+internal sealed record DocumentLimits+{+ /// <summary>The limits Filee uses.</summary>+ public static DocumentLimits Default { get; } = new();++ /// <summary>The error key of a document refused for passing a limit.</summary>+ public const string ErrorKey = "error.document_too_complex";++ /// <summary>Bytes one part of a package (an XML part, a picture) may unpack to.</summary>+ public long MaxPartBytes { get; init; } = 64L * 1024 * 1024;++ /// <summary>Bytes all parts read from one package may unpack to together.</summary>+ public long MaxPackageBytes { get; init; } = 1024L * 1024 * 1024;++ /// <summary>Entries in a package (real documents have a few per page or picture).</summary>+ public int MaxPackageEntries { get; init; } = 100_000;++ /// <summary>+ /// How many times its compressed size a part may unpack to, once it is larger than <see cref="InflateRatioFloor"/>.+ /// Deflate reaches about 1,000 on runs of one byte; XML from word processors stays far below 100.+ /// </summary>+ public int MaxInflateRatio { get; init; } = 200;++ /// <summary>What a part may always unpack to, whatever its ratio (small parts of repeated markup compress well).</summary>+ public long InflateRatioFloor { get; init; } = 4L * 1024 * 1024;++ /// <summary>Levels of XML elements in a part. Tables nested <see cref="MaxNestingDepth"/> deep stay well within.</summary>+ public int MaxXmlDepth { get; init; } = 512;++ /// <summary>XML elements in all parts of a package together (each costs some 100 bytes of memory in the tree).</summary>+ public int MaxXmlElements { get; init; } = 5_000_000;++ /// <summary>Levels of tables in tables, text boxes, notes, groups and fields.</summary>+ public int MaxNestingDepth { get; init; } = 32;++ /// <summary>Paragraphs in a document (cells, notes, headers and text boxes included).</summary>+ public int MaxParagraphs { get; init; } = 1_000_000;++ /// <summary>Tables, pictures, shapes, text boxes and equations in a document.</summary>+ public int MaxObjects { get; init; } = 200_000;++ /// <summary>+ /// Columns of a table (as many as a spreadsheet has). Column numbers and spans beyond are cut there, so a damaged+ /// cell address can't make a table of two billion columns.+ /// </summary>+ public int MaxTableColumns { get; init; } = 16_384;++ /// <summary>Pages the layout engine makes of one document.</summary>+ public int MaxPages { get; init; } = 20_000;++ /// <summary>Pixels of one picture (16,384 × 16,384, beyond every camera); bigger pictures are left out.</summary>+ public long MaxPicturePixels { get; init; } = 16_384L * 16_384;++ /// <summary>Pages in a row with nothing placed on them before the layout counts as stuck.</summary>+ public int MaxConsecutiveEmptyPages { get; init; } = 64;++ /// <summary>How long laying out one document may take.</summary>+ public TimeSpan LayoutTimeBudget { get; init; } = TimeSpan.FromMinutes(10);++ /// <summary>The XML limits of one part, given how many elements the package's other parts held.</summary>+ internal XmlLimits Xml(int elementsBefore, bool preserveWhitespace) =>+ new(MaxXmlDepth, MaxXmlElements, MaxPartBytes, preserveWhitespace, ElementsBefore: elementsBefore);++ /// <summary>The refusal for a document that passes a limit; <paramref name="detail"/> says which (in English).</summary>+ public static ConversionRefusedException Exceeded(string detail) => new(ErrorKey, detail);++ /// <summary>"64 MB" for messages.</summary>+ internal static string Megabytes(long bytes) => (bytes / (1024 * 1024)).ToString("N0", CultureInfo.InvariantCulture) + " MB";++ /// <summary>"20,000" for messages.</summary>+ internal static string Count(long count) => count.ToString("N0", CultureInfo.InvariantCulture);+}diff --git a/src/Filee.Engines/Documents/DocumentPackage.cs b/src/Filee.Engines/Documents/DocumentPackage.csnew file mode 100644index 00000000..0cccc713--- /dev/null+++ b/src/Filee.Engines/Documents/DocumentPackage.cs@@ -0,0 +1,240 @@+// The ZIP package of a document (HWPX, DOCX, OFD), read within DocumentLimits: parts are looked up by name inside the+// package only, every part is counted while it is unpacked (its own size, the package's total and the inflate ratio),+// XML is read by SafeXml without DTDs or a resolver, and parts are only ever written out under names Filee makes.+// A damaged package fails with InvalidDataException, one that passes a limit with error.document_too_complex.++using System.IO.Compression;+using System.Text;+using System.Xml;+using System.Xml.Linq;+using Filee.Engines.Infrastructure;++namespace Filee.Engines.Documents;++/// <summary>A document package opened for reading.</summary>+internal sealed class DocumentPackage : IDisposable+{+ private readonly ZipArchive _zip;+ private long _total;+ private int _elements;++ private DocumentPackage(ZipArchive zip, DocumentLimits limits)+ {+ _zip = zip;+ Limits = limits;+ }++ public DocumentLimits Limits { get; }++ /// <summary>Bytes unpacked so far.</summary>+ public long BytesRead => _total;++ /// <summary>XML elements read so far.</summary>+ public int ElementsRead => _elements;++ public IReadOnlyCollection<ZipArchiveEntry> Entries => _zip.Entries;++ /// <summary>Opens the package at <paramref name="path"/>; <paramref name="notAPackage"/> makes the error for a file that is no ZIP.</summary>+ public static DocumentPackage Open(string path, DocumentLimits? limits = null, Func<Exception>? notAPackage = null) =>+ Open(() => ZipFile.OpenRead(path), limits ?? DocumentLimits.Default, notAPackage);++ /// <summary>A package in a stream (left open).</summary>+ public static DocumentPackage Open(Stream stream, DocumentLimits? limits = null, Func<Exception>? notAPackage = null) =>+ Open(() => new ZipArchive(stream, ZipArchiveMode.Read, leaveOpen: true), limits ?? DocumentLimits.Default, notAPackage);++ private static DocumentPackage Open(Func<ZipArchive> open, DocumentLimits limits, Func<Exception>? notAPackage)+ {+ ZipArchive zip;+ try+ {+ zip = open();+ }+ catch (Exception ex) when (ex is InvalidDataException or EndOfStreamException or ArgumentException or NotSupportedException+ || ex is IOException and not (FileNotFoundException or DirectoryNotFoundException))+ {+ throw notAPackage?.Invoke() ?? new InvalidDataException("The file is not a ZIP package. It may be damaged.", ex);+ }+ try+ {+ // The central directory is read when the entries are first listed.+ if (zip.Entries.Count > limits.MaxPackageEntries)+ throw DocumentLimits.Exceeded($"The package has more than {DocumentLimits.Count(limits.MaxPackageEntries)} parts.");+ }+ catch (Exception ex) when (ex is InvalidDataException or EndOfStreamException or ArgumentException or IOException)+ {+ zip.Dispose();+ throw notAPackage?.Invoke() ?? new InvalidDataException("The file is not a ZIP package. It may be damaged.", ex);+ }+ catch+ {+ zip.Dispose();+ throw;+ }+ return new DocumentPackage(zip, limits);+ }++ public void Dispose() => _zip.Dispose();++ /// <summary>The entry of that exact name; null when there is none.</summary>+ public ZipArchiveEntry? GetEntry(string name)+ {+ try+ {+ return name.Length == 0 ? null : _zip.GetEntry(name);+ }+ catch (ArgumentException)+ {+ return null;+ }+ }++ /// <summary>+ /// The unpacked bytes of <paramref name="entry"/>, counted against the part, package and ratio limits. A part whose+ /// header already claims too much is refused before it is unpacked.+ /// </summary>+ public Stream Open(ZipArchiveEntry entry)+ {+ var name = entry.FullName;+ if (entry.Length > Limits.MaxPartBytes)+ throw PartTooLarge(name);+ if (_total + entry.Length > Limits.MaxPackageBytes)+ throw PackageTooLarge();+ Stream inner;+ try+ {+ inner = entry.Open();+ }+ catch (Exception ex) when (ex is IOException or NotSupportedException or ArgumentException)+ {+ throw new InvalidDataException($"The part {name} of the package can't be unpacked ({ex.Message}).", ex);+ }+ var compressed = Math.Max(1, entry.CompressedLength);+ long counted = 0;+ return new LimitedStream(new DamageStream(inner, name), Limits.MaxPartBytes, () => PartTooLarge(name), read =>+ {+ _total += read - counted;+ counted = read;+ if (_total > Limits.MaxPackageBytes)+ throw PackageTooLarge();+ if (read > Limits.InflateRatioFloor && read / compressed > Limits.MaxInflateRatio)+ throw DocumentLimits.Exceeded($"The part {name} unpacks to more than {Limits.MaxInflateRatio} times its packed size.");+ });+ }++ /// <summary>+ /// An XML part read by <see cref="SafeXml"/>; its elements count against the package's. Throws+ /// <see cref="XmlException"/> for a part that is not well-formed XML (callers say which part of which format).+ /// </summary>+ public XDocument LoadXml(ZipArchiveEntry entry, bool preserveWhitespace = true)+ {+ using var stream = Open(entry);+ try+ {+ var document = SafeXml.Load(stream, Limits.Xml(_elements, preserveWhitespace),+ message => DocumentLimits.Exceeded($"{entry.FullName}: {message}"), out var elements);+ _elements += elements;+ return document;+ }+ catch (Exception ex) when (ex is DecoderFallbackException or ArgumentException)+ {+ throw new XmlException(ex.Message, ex);+ }+ }++ /// <summary>The bytes of a part; null when it holds more than <paramref name="max"/> (the part is then left out).</summary>+ public byte[]? ReadBytes(ZipArchiveEntry entry, long max)+ {+ if (entry.Length > max)+ return null;+ using var stream = Open(entry);+ using var copy = new MemoryStream((int)Math.Min(entry.Length, 1024 * 1024));+ var buffer = new byte[81920];+ int count;+ while ((count = stream.Read(buffer)) > 0)+ {+ if (copy.Length + count > max)+ return null;+ copy.Write(buffer, 0, count);+ }+ return copy.ToArray();+ }++ /// <summary>+ /// Unpacks a picture into a new file at <paramref name="path"/>. Returns false (and leaves no file) when its+ /// header claims more than <see cref="DocumentLimits.MaxPicturePixels"/>.+ /// </summary>+ public bool ExtractPicture(ZipArchiveEntry entry, string path)+ {+ using (var source = Open(entry))+ using (var target = File.Create(path))+ source.CopyTo(target);+ if (PictureSize.Read(path) is { } size && (long)size.Width * size.Height > Limits.MaxPicturePixels)+ {+ File.Delete(path);+ return false;+ }+ return true;+ }++ /// <summary>+ /// The extension of a part name for a file Filee writes: "." and up to 8 ASCII letters or digits, else "" (a name+ /// in a hostile package may hold anything, ":stream" included).+ /// </summary>+ public static string SafeExtension(string name)+ {+ var dot = name.LastIndexOf('.');+ if (dot < 0 || name.Length - dot - 1 is < 1 or > 8 || name.AsSpan(dot + 1).ContainsAnyExcept(AsciiLettersAndDigits))+ return "";+ return name[dot..].ToLowerInvariant();+ }++ private static readonly System.Buffers.SearchValues<char> AsciiLettersAndDigits =+ System.Buffers.SearchValues.Create("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");++ /// <summary>Reads a part; a package cut short or damaged inside a part is invalid data, not a failing disk.</summary>+ private sealed class DamageStream(Stream inner, string name) : Stream+ {+ public override bool CanRead => true;+ public override bool CanSeek => false;+ public override bool CanWrite => false;+ public override long Length => throw new NotSupportedException();++ public override long Position+ {+ get => throw new NotSupportedException();+ set => throw new NotSupportedException();+ }++ public override int Read(byte[] buffer, int offset, int count) => Read(buffer.AsSpan(offset, count));++ public override int Read(Span<byte> buffer)+ {+ try+ {+ return inner.Read(buffer);+ }+ catch (IOException ex)+ {+ throw new InvalidDataException($"The part {name} of the package is damaged ({ex.Message}).", ex);+ }+ }++ protected override void Dispose(bool disposing)+ {+ if (disposing)+ inner.Dispose();+ base.Dispose(disposing);+ }++ public override void Flush() { }+ public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();+ public override void SetLength(long value) => throw new NotSupportedException();+ public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();+ }++ private Exception PartTooLarge(string name) =>+ DocumentLimits.Exceeded($"The part {name} unpacks to more than {DocumentLimits.Megabytes(Limits.MaxPartBytes)}.");++ private Exception PackageTooLarge() =>+ DocumentLimits.Exceeded($"The package unpacks to more than {DocumentLimits.Megabytes(Limits.MaxPackageBytes)}.");+}diff --git a/src/Filee.Engines/Documents/LayoutGuard.cs b/src/Filee.Engines/Documents/LayoutGuard.csnew file mode 100644index 00000000..79c142fa--- /dev/null+++ b/src/Filee.Engines/Documents/LayoutGuard.cs@@ -0,0 +1,71 @@+// Guards for the layout loop, so a document can't keep the layout engine busy forever: every paragraph checks the+// cancellation token and the time budget, every page counts against MaxPages, and pages in a row that receive nothing+// mean the layout is stuck. An object larger than every page is clipped to the area it goes into and placed, so it+// can't push itself from page to page without end.++using System.Diagnostics;++namespace Filee.Engines.Documents;++/// <summary>Checks a layout run against <see cref="DocumentLimits"/>.</summary>+internal sealed class LayoutGuard+{+ private readonly DocumentLimits _limits;+ private readonly CancellationToken _cancellationToken;+ private readonly Func<TimeSpan> _elapsed;+ private int _pages;+ private int _emptyInARow;++ /// <param name="elapsed">Time since the layout started (tests pass their own clock).</param>+ public LayoutGuard(DocumentLimits limits, CancellationToken cancellationToken, Func<TimeSpan>? elapsed = null)+ {+ _limits = limits;+ _cancellationToken = cancellationToken;+ if (elapsed is null)+ {+ var start = Stopwatch.GetTimestamp();+ elapsed = () => Stopwatch.GetElapsedTime(start);+ }+ _elapsed = elapsed;+ }++ /// <summary>Pages finished so far.</summary>+ public int Pages => _pages;++ /// <summary>Called before each paragraph is laid out: throws when the job was cancelled or the time is up.</summary>+ /// <exception cref="OperationCanceledException">The job was cancelled.</exception>+ public void Paragraph()+ {+ _cancellationToken.ThrowIfCancellationRequested();+ if (_elapsed() > _limits.LayoutTimeBudget)+ throw DocumentLimits.Exceeded($"Laying out the document took longer than {Describe(_limits.LayoutTimeBudget)}.");+ }++ private static string Describe(TimeSpan time) => time.TotalMinutes >= 1+ ? time.TotalMinutes == 1 ? "1 minute" : $"{time.TotalMinutes:0} minutes"+ : time.TotalSeconds == 1 ? "1 second" : $"{time.TotalSeconds:0} seconds";++ /// <summary>Called when a page is finished; <paramref name="empty"/> when nothing was placed on it.</summary>+ public void PageFinished(bool empty)+ {+ if (++_pages > _limits.MaxPages)+ throw DocumentLimits.Exceeded($"The document has more than {DocumentLimits.Count(_limits.MaxPages)} pages.");+ _emptyInARow = empty ? _emptyInARow + 1 : 0;+ if (_emptyInARow > _limits.MaxConsecutiveEmptyPages)+ throw DocumentLimits.Exceeded($"The layout made more than {_limits.MaxConsecutiveEmptyPages} empty pages in a row.");+ }++ /// <summary>+ /// The size an object is placed with in an area of <paramref name="areaWidth"/> × <paramref name="areaHeight"/>+ /// (the body of an empty page): its own size when it fits, else clipped to the area. <c>Clipped</c> tells the+ /// layout to draw only that part rather than move the object to the next page again. Sizes that are not finite+ /// numbers or negative count as 0.+ /// </summary>+ public static (double Width, double Height, bool Clipped) Fit(double width, double height, double areaWidth, double areaHeight)+ {+ static double Clean(double value) => double.IsFinite(value) && value > 0 ? value : 0;+ (width, height, areaWidth, areaHeight) = (Clean(width), Clean(height), Clean(areaWidth), Clean(areaHeight));+ var clipped = width > areaWidth || height > areaHeight;+ return (Math.Min(width, areaWidth), Math.Min(height, areaHeight), clipped);+ }+}diff --git a/src/Filee.Engines/Documents/PictureSize.cs b/src/Filee.Engines/Documents/PictureSize.csnew file mode 100644index 00000000..ce944b0a--- /dev/null+++ b/src/Filee.Engines/Documents/PictureSize.cs@@ -0,0 +1,146 @@+// The pixel size a picture's header claims, read without decoding it, so a picture that would take gigabytes once+// decoded (a few kilobytes of PNG can claim 100,000 × 100,000 pixels) is left out before anything decodes it. PNG,+// JPEG, GIF, BMP and WebP; other formats (TIFF, EMF, WMF, SVG) give no size here and are checked where they are drawn.++using System.Buffers.Binary;++namespace Filee.Engines.Documents;++/// <summary>Reads the width and height from a picture's header.</summary>+internal static class PictureSize+{+ /// <summary>JPEG segments looked at before the frame header (files have a dozen).</summary>+ private const int MaxJpegSegments = 1000;++ /// <summary>The size in pixels; null for formats it does not know and headers it can't read.</summary>+ public static (int Width, int Height)? Read(string path)+ {+ try+ {+ using var stream = File.OpenRead(path);+ return Read(stream);+ }+ catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)+ {+ return null;+ }+ }++ /// <inheritdoc cref="Read(string)"/>+ public static (int Width, int Height)? Read(Stream stream)+ {+ Span<byte> head = stackalloc byte[30];+ var length = ReadUpTo(stream, head);+ head = head[..length];+ if (head is [0x89, (byte)'P', (byte)'N', (byte)'G', ..] && head.Length >= 24)+ return Size(BinaryPrimitives.ReadUInt32BigEndian(head[16..]), BinaryPrimitives.ReadUInt32BigEndian(head[20..]));+ if (head is [(byte)'G', (byte)'I', (byte)'F', (byte)'8', ..] && head.Length >= 10)+ return Size(BinaryPrimitives.ReadUInt16LittleEndian(head[6..]), BinaryPrimitives.ReadUInt16LittleEndian(head[8..]));+ if (head is [(byte)'B', (byte)'M', ..] && head.Length >= 26)+ {+ var header = BinaryPrimitives.ReadUInt32LittleEndian(head[14..]);+ return header == 12+ ? Size(BinaryPrimitives.ReadUInt16LittleEndian(head[18..]), BinaryPrimitives.ReadUInt16LittleEndian(head[20..]))+ : Size((uint)Math.Abs((long)BinaryPrimitives.ReadInt32LittleEndian(head[18..])), (uint)Math.Abs((long)BinaryPrimitives.ReadInt32LittleEndian(head[22..])));+ }+ if (head is [(byte)'R', (byte)'I', (byte)'F', (byte)'F', _, _, _, _, (byte)'W', (byte)'E', (byte)'B', (byte)'P', ..] && head.Length >= 30)+ return WebP(head);+ if (head is [0xFF, 0xD8, ..])+ return Jpeg(stream, head);+ return null;+ }++ private static (int Width, int Height)? WebP(ReadOnlySpan<byte> head)+ {+ var chunk = head.Slice(12, 4);+ if (chunk.SequenceEqual("VP8X"u8))+ {+ var width = head[24] | head[25] << 8 | head[26] << 16;+ var height = head[27] | head[28] << 8 | head[29] << 16;+ return Size((uint)width + 1, (uint)height + 1);+ }+ if (chunk.SequenceEqual("VP8L"u8) && head[20] == 0x2F)+ {+ var bits = BinaryPrimitives.ReadUInt32LittleEndian(head[21..]);+ return Size((bits & 0x3FFF) + 1, ((bits >> 14) & 0x3FFF) + 1);+ }+ if (chunk.SequenceEqual("VP8 "u8))+ return Size(BinaryPrimitives.ReadUInt16LittleEndian(head[26..]) & 0x3FFFu, BinaryPrimitives.ReadUInt16LittleEndian(head[28..]) & 0x3FFFu);+ return null;+ }++ /// <summary>The first frame header (SOFn) after the segments before it.</summary>+ private static (int Width, int Height)? Jpeg(Stream stream, ReadOnlySpan<byte> head)+ {+ // Continue from byte 2 of the file: the head was read already.+ var buffered = new Queue<byte>(head[2..].ToArray());+ int Next()+ {+ if (buffered.Count > 0)+ return buffered.Dequeue();+ return stream.ReadByte();+ }++ Span<byte> frame = stackalloc byte[5];+ for (var segment = 0; segment < MaxJpegSegments; segment++)+ {+ var marker = Next();+ if (marker < 0)+ return null;+ if (marker != 0xFF)+ return null;+ var code = Next();+ while (code == 0xFF)+ code = Next();+ if (code < 0 || code is 0xD9 or 0xDA)+ return null; // end of image or scan data before any frame header+ if (code is 0x01 or (>= 0xD0 and <= 0xD7))+ continue; // markers without a length+ var high = Next();+ var low = Next();+ if (high < 0 || low < 0)+ return null;+ var length = high << 8 | low;+ if (length < 2)+ return null;+ if (code is >= 0xC0 and <= 0xCF and not (0xC4 or 0xC8 or 0xCC))+ {+ for (var i = 0; i < frame.Length; i++)+ {+ var b = Next();+ if (b < 0)+ return null;+ frame[i] = (byte)b;+ }+ return Size(BinaryPrimitives.ReadUInt16BigEndian(frame[3..]), BinaryPrimitives.ReadUInt16BigEndian(frame[1..]));+ }+ var skip = length - 2;+ for (; skip > 0 && buffered.Count > 0; skip--)+ buffered.Dequeue();+ if (stream.CanSeek)+ {+ if (stream.Seek(skip, SeekOrigin.Current) > stream.Length)+ return null;+ continue;+ }+ for (; skip > 0; skip--)+ {+ if (Next() < 0)+ return null;+ }+ }+ return null;+ }++ private static (int Width, int Height)? Size(uint width, uint height) =>+ width == 0 || height == 0 ? null : ((int)Math.Min(width, int.MaxValue), (int)Math.Min(height, int.MaxValue));++ private static int ReadUpTo(Stream stream, Span<byte> buffer)+ {+ var total = 0;+ int read;+ while (total < buffer.Length && (read = stream.Read(buffer[total..])) > 0)+ total += read;+ return total;+ }+}diff --git a/src/Filee.Engines/Geo/KmlFormat.cs b/src/Filee.Engines/Geo/KmlFormat.csindex c6c35c37..c22612bd 100644--- a/src/Filee.Engines/Geo/KmlFormat.cs+++ b/src/Filee.Engines/Geo/KmlFormat.cs@@ -11,6 +11,7 @@ using System.IO.Compression;using System.Text.Json;using System.Xml;using System.Xml.Linq;+using Filee.Engines.Infrastructure;namespace Filee.Engines.Geo;@@ -409,67 +410,15 @@ internal static class GeoXml/// <summary>/// Parses XML without DTDs or external entities, so a hostile file can't read files or expand into gigabytes, and- /// refuses elements nested more than <see cref="MaxElementDepth"/> levels. The tree is built here from the reader- /// rather than by <c>XDocument.Load</c>, which takes time quadratic in the depth (a 1.4 MB file nested 100,000 deep- /// kept it busy for 48 seconds, the reader alone needs a quarter of a second) and so couldn't be allowed to see such- /// a file. Namespace declarations and prefixes are not kept; names are the local name and namespace.+ /// refuses elements nested more than <see cref="MaxElementDepth"/> levels (see <see cref="SafeXml"/>). Namespace+ /// declarations and prefixes are not kept; names are the local name and namespace./// </summary>public static XDocument Load(Stream stream, int maxElements = MaxElements){- var settings = new XmlReaderSettings- {- DtdProcessing = DtdProcessing.Prohibit,- XmlResolver = null,- MaxCharactersFromEntities = 0,- CheckCharacters = false,- // About 1 GB of memory at the most: the tree takes some 16 bytes for every character.- MaxCharactersInDocument = 1L << 26,- };try{- using var reader = XmlReader.Create(stream, settings);- var document = new XDocument();- XContainer current = document;- var depth = 0;- var elements = 0;- while (reader.Read())- {- switch (reader.NodeType)- {- case XmlNodeType.Element:- if (++depth > MaxElementDepth)- throw new InvalidDataException($"The file nests elements more than {MaxElementDepth} levels deep.");- if (++elements > maxElements)- throw new InvalidDataException("The file has more than " + maxElements.ToString("N0", System.Globalization.CultureInfo.InvariantCulture) + " elements.");- var element = new XElement(XName.Get(reader.LocalName, reader.NamespaceURI));- var empty = reader.IsEmptyElement;- if (reader.HasAttributes)- {- while (reader.MoveToNextAttribute())- {- if (reader.NamespaceURI != "http://www.w3.org/2000/xmlns/")- element.SetAttributeValue(XName.Get(reader.LocalName, reader.NamespaceURI), reader.Value);- }- reader.MoveToElement();- }- current.Add(element);- if (empty)- depth--;- else- current = element;- break;- case XmlNodeType.EndElement:- current = current.Parent ?? (XContainer)document;- depth--;- break;- case XmlNodeType.Text or XmlNodeType.CDATA or XmlNodeType.SignificantWhitespace or XmlNodeType.Whitespace:- // Text outside the root element (white space between declarations) is no content.- if (current is XElement)- current.Add(new XText(reader.Value));- break;- }- }- return document;+ // About 1 GB of memory at the most: the tree takes some 16 bytes for every character.+ return SafeXml.Load(stream, new XmlLimits(MaxElementDepth, maxElements, 1L << 26, KeepNamespaceDeclarations: false, CheckCharacters: false));}catch (XmlException ex){diff --git a/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.Drawing.cs b/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.Drawing.csindex c70003a5..0d8a4388 100644--- a/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.Drawing.cs+++ b/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.Drawing.cs@@ -3,6 +3,7 @@using System.Globalization;using System.Xml.Linq;+using Filee.Engines.Documents;namespace Filee.Engines.Hwp.Hwpx.Docx;@@ -58,6 +59,7 @@ internal sealed partial class DocxReader{foreach (var obj in objects){+ _budget.Object();if (obj.Name == Pic + "pic"){if (Picture(obj, width, height, anchor) is { } picture)@@ -79,6 +81,7 @@ internal sealed partial class DocxReaderprivate IEnumerable<HInline> Group(XElement group, int width, int height, HAnchor? anchor,(double X, double Y, double ScaleX, double ScaleY) parent){+ using var level = _budget.Enter();var xfrm = group.Element(Wpg + "grpSpPr")?.Element(A + "xfrm") ?? group.Element(A + "grpSpPr")?.Element(A + "xfrm");var chOff = xfrm?.Element(A + "chOff");var chExt = xfrm?.Element(A + "chExt");@@ -230,6 +233,7 @@ internal sealed partial class DocxReader/// <summary>Text box paragraphs; Word's document grid does not apply inside text boxes.</summary>private void ReadTextBoxContent(XElement content, List<HBlock> output){+ using var level = _budget.Enter();_textBoxDepth++;try{@@ -400,13 +404,12 @@ internal sealed partial class DocxReaderreturn null;if (_media.TryGetValue(rel.Target, out var existing))return existing;- if (_zip.GetEntry(rel.Target) is not { } entry)+ if (_package.GetEntry(rel.Target) is not { } entry)return null;Directory.CreateDirectory(_mediaFolder);- var path = Path.Combine(_mediaFolder, $"docx-media-{_media.Count + 1}{Path.GetExtension(rel.Target)}");- using (var source = entry.Open())- using (var target = File.Create(path))- source.CopyTo(target);+ string? path = Path.Combine(_mediaFolder, $"docx-media-{_media.Count + 1}{DocumentPackage.SafeExtension(rel.Target)}");+ if (!_package.ExtractPicture(entry, path))+ path = null; // larger than any picture should be: left out_media[rel.Target] = path;return path;}@@ -422,10 +425,12 @@ internal sealed partial class DocxReadercontinue;if (shape.Name.LocalName == "group"){+ using var level = _budget.Enter();foreach (var inline in Vml(shape, format))yield return inline;continue;}+ _budget.Object();if (shape.Name.LocalName is "shapetype")continue;diff --git a/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.Styles.cs b/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.Styles.csindex 328980fe..537fcc60 100644--- a/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.Styles.cs+++ b/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.Styles.cs@@ -294,8 +294,10 @@ internal sealed partial class DocxReadervar root = part?.Xml.Root;if (root is null)return;- var abstracts = root.Elements(W + "abstractNum")- .ToDictionary(a => Attr(a, "abstractNumId") ?? "", a => a);+ // Of two definitions with one id (a damaged file) the first counts.+ var abstracts = new Dictionary<string, XElement>();+ foreach (var abstractNum in root.Elements(W + "abstractNum"))+ abstracts.TryAdd(Attr(abstractNum, "abstractNumId") ?? "", abstractNum);foreach (var num in root.Elements(W + "num")){var numId = Attr(num, "numId") ?? "";@@ -335,7 +337,11 @@ internal sealed partial class DocxReader// ───────────────────────── Resolution ─────────────────────────/// <summary>Merged properties of a paragraph or character style including everything it is based on.</summary>- private (ParaProps Para, RunProps Run) ResolveStyle(string? styleId)+ private (ParaProps Para, RunProps Run) ResolveStyle(string? styleId) => ResolveStyle(styleId, 0);++ /// <param name="depth">Styles below this one in the chain: a chain longer than <see cref="MaxStyleChain"/> (a+ /// damaged or hostile file; real chains are a few long) starts from the defaults instead of overflowing the stack.</param>+ private (ParaProps Para, RunProps Run) ResolveStyle(string? styleId, int depth){if (styleId is null || !_styles.TryGetValue(styleId, out var style))return (_defaultPara.Clone(), _defaultRun.Clone());@@ -344,7 +350,9 @@ internal sealed partial class DocxReader// Guard against basedOn cycles in damaged files._resolvedStyles[styleId] = (_defaultPara.Clone(), _defaultRun.Clone());- var (para, run) = style.BasedOn is { } parent && parent != styleId ? ResolveStyle(parent) : (_defaultPara.Clone(), _defaultRun.Clone());+ var (para, run) = style.BasedOn is { } parent && parent != styleId && depth < MaxStyleChain+ ? ResolveStyle(parent, depth + 1)+ : (_defaultPara.Clone(), _defaultRun.Clone());para.Apply(style.PPr);run.Apply(style.RPr);_resolvedStyles[styleId] = (para.Clone(), run.Clone());@@ -387,7 +395,7 @@ internal sealed partial class DocxReader{// Character styles only carry run properties; apply their chain on top of the paragraph's.var chain = new List<Style>();- for (var current = style; current is not null && chain.Count < 20; current = current.BasedOn is { } b ? _styles.GetValueOrDefault(b) : null)+ for (var current = style; current is not null && chain.Count < MaxStyleChain; current = current.BasedOn is { } b ? _styles.GetValueOrDefault(b) : null)chain.Add(current);for (var i = chain.Count - 1; i >= 0; i--)run.Apply(chain[i].RPr);@@ -683,8 +691,16 @@ internal sealed partial class DocxReaderprivate static string? Val(XElement? element) => Attr(element, "val");+ /// <summary>+ /// A length in twips (or another whole number). Values beyond ±100 million (70 km of twips; real ones stay below+ /// 50,000) are cut there, so arithmetic on them (HWPUNIT is 5 twips, Math.Abs) can't overflow.+ /// </summary>private static int? Twips(XElement? element, string name) =>- double.TryParse(Attr(element, name), NumberStyles.Float, CultureInfo.InvariantCulture, out var value) ? (int)value : null;+ double.TryParse(Attr(element, name), NumberStyles.Float, CultureInfo.InvariantCulture, out var value) && !double.IsNaN(value)+ ? (int)Math.Clamp(value, -MaxTwips, MaxTwips)+ : null;++ private const int MaxTwips = 100_000_000;private static long? Long(XElement? element, string name) =>long.TryParse(Attr(element, name), NumberStyles.Integer, CultureInfo.InvariantCulture, out var value) ? value : null;diff --git a/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.Tables.cs b/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.Tables.csindex d5eaf6e4..2baeadab 100644--- a/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.Tables.cs+++ b/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.Tables.cs@@ -10,6 +10,8 @@ internal sealed partial class DocxReader{private HTable ReadTable(XElement tbl){+ _budget.Object();+ using var level = _budget.Enter();var tblPr = tbl.Element(W + "tblPr");var style = _styles.GetValueOrDefault(Val(tblPr?.Element(W + "tblStyle")) ?? _defaultTableStyle ?? "");var styleChain = StyleChain(style);@@ -64,16 +66,18 @@ internal sealed partial class DocxReaderif (trPr?.Element(W + "trHeight") is { } height && Twips(height, "val") is > 0 and var h)row.Height = HwpxUnits.FromTwips(h);- var column = Twips(trPr?.Element(W + "gridBefore"), "val") ?? 0;+ // Column numbers and spans of damaged files stay within the limit, so they can't overflow.+ var maxColumns = _budget.Limits.MaxTableColumns;+ var column = Math.Clamp(Twips(trPr?.Element(W + "gridBefore"), "val") ?? 0, 0, maxColumns);foreach (var tc in tr.Elements().SelectMany(e => e.Name == W + "tc" ? [e] : FlattenCells(e))){var tcPr = tc.Element(W + "tcPr");- var span = Math.Max(1, Twips(tcPr?.Element(W + "gridSpan"), "val") ?? 1);+ var span = Math.Clamp(Twips(tcPr?.Element(W + "gridSpan"), "val") ?? 1, 1, maxColumns);var vMerge = tcPr?.Element(W + "vMerge");if (vMerge is not null && Val(vMerge) is not "restart" && mergeOrigin.TryGetValue(column, out var origin)){origin.RowSpan++;- column += span;+ column = Math.Min(column + span, maxColumns);continue;}@@ -119,7 +123,7 @@ internal sealed partial class DocxReaderelsemergeOrigin.Remove(column);row.Cells.Add(cell);- column += span;+ column = Math.Min(column + span, maxColumns);}table.Rows.Add(row);table.ColumnCount = Math.Max(table.ColumnCount, column);@@ -130,7 +134,7 @@ internal sealed partial class DocxReaderprivate List<Style> StyleChain(Style? style){var chain = new List<Style>();- for (var current = style; current is not null && chain.Count < 20; current = current.BasedOn is { } b ? _styles.GetValueOrDefault(b) : null)+ for (var current = style; current is not null && chain.Count < MaxStyleChain; current = current.BasedOn is { } b ? _styles.GetValueOrDefault(b) : null)chain.Insert(0, current);return chain;}diff --git a/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.cs b/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.csindex 8a3f316c..0006b843 100644--- a/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.cs+++ b/src/Filee.Engines/Hwp/Hwpx/Docx/DocxReader.cs@@ -4,11 +4,14 @@// footnotes and endnotes, bookmarks and hyperlinks (including the links of a Word table of contents).//// Split over partial files: this one walks the package and the text, DocxReader.Styles resolves formatting,-// DocxReader.Tables and DocxReader.Drawing handle tables and graphic objects.+// DocxReader.Tables and DocxReader.Drawing handle tables and graphic objects. The package is read within+// DocumentLimits (Documents/DocumentPackage); embedded fonts (word/fonts) are not read.using System.IO.Compression;using System.Text;+using System.Xml;using System.Xml.Linq;+using Filee.Engines.Documents;namespace Filee.Engines.Hwp.Hwpx.Docx;@@ -28,10 +31,14 @@ internal sealed partial class DocxReaderprivate static readonly XNamespace M = "http://schemas.openxmlformats.org/officeDocument/2006/math";private static readonly XNamespace Dc = "http://purl.org/dc/elements/1.1/";- private readonly ZipArchive _zip;+ /// <summary>Styles followed through basedOn at the most (real chains are a few long; damaged files loop).</summary>+ private const int MaxStyleChain = 20;++ private readonly DocumentPackage _package;+ private readonly DocumentBudget _budget;private readonly string _mediaFolder;private readonly Dictionary<string, Part?> _parts = [];- private readonly Dictionary<string, string> _media = [];+ private readonly Dictionary<string, string?> _media = [];private readonly Dictionary<string, XElement> _footnotes = [];private readonly Dictionary<string, XElement> _endnotes = [];private readonly List<FieldState> _fields = [];@@ -44,12 +51,16 @@ internal sealed partial class DocxReaderprivate int _sectionIndex;private int _textBoxDepth;+ /// <summary>True while a note is read: Word has no notes in notes, and a note that refers to itself would never end.</summary>+ private bool _inNote;+/// <summary>The part being read; relationship ids (images, links) are resolved against it.</summary>private Part _part;- private DocxReader(ZipArchive zip, string mediaFolder)+ private DocxReader(DocumentPackage package, string mediaFolder){- _zip = zip;+ _package = package;+ _budget = new DocumentBudget(package.Limits);_mediaFolder = mediaFolder;var mainPath = RelationshipsOf("").FirstOrDefault(r => r.Value.Type.EndsWith("/officeDocument", StringComparison.Ordinal)).Value.Target ?? "word/document.xml";@@ -57,10 +68,27 @@ internal sealed partial class DocxReader}/// <summary>Reads <paramref name="path"/>; embedded pictures are extracted into <paramref name="mediaFolder"/>.</summary>- public static HDocument Read(string path, string mediaFolder)+ public static HDocument Read(string path, string mediaFolder) => Read(path, mediaFolder, DocumentLimits.Default);++ /// <summary>Reads within other <paramref name="limits"/> than <see cref="DocumentLimits.Default"/> (for tests).</summary>+ internal static HDocument Read(string path, string mediaFolder, DocumentLimits limits){- using var zip = ZipFile.OpenRead(path);- return new DocxReader(zip, mediaFolder).ReadDocument();+ using var package = DocumentPackage.Open(path, limits,+ () => new InvalidDataException("The file is not a Word document (it is not a ZIP package). It may be damaged or protected by document security (DRM) software."));+ return new DocxReader(package, mediaFolder).ReadDocument();+ }++ /// <summary>An XML part; damaged XML is invalid data with the part's name.</summary>+ private XDocument LoadXml(ZipArchiveEntry entry, bool preserveWhitespace)+ {+ try+ {+ return _package.LoadXml(entry, preserveWhitespace);+ }+ catch (XmlException ex)+ {+ throw new InvalidDataException($"The Word document cannot be read ({entry.FullName} is not valid XML). It may be encrypted or damaged. {ex.Message}", ex);+ }}// ───────────────────────── Package ─────────────────────────@@ -79,11 +107,10 @@ internal sealed partial class DocxReaderif (_parts.TryGetValue(path, out var cached))return cached;Part? part = null;- if (_zip.GetEntry(path) is { } entry)+ if (_package.GetEntry(path) is { } entry){- using var stream = entry.Open();// Whitespace matters: <w:t xml:space="preserve"> </w:t> is a real space.- part = new Part(path, XDocument.Load(stream, LoadOptions.PreserveWhitespace), RelationshipsOf(path));+ part = new Part(path, LoadXml(entry, preserveWhitespace: true), RelationshipsOf(path));}_parts[path] = part;return part;@@ -95,10 +122,9 @@ internal sealed partial class DocxReadervar folder = partPath.Contains('/') ? partPath[..partPath.LastIndexOf('/')] : "";var relsPath = partPath.Length == 0 ? "_rels/.rels" : $"{folder}/_rels/{partPath[(partPath.LastIndexOf('/') + 1)..]}.rels".TrimStart('/');var result = new Dictionary<string, Relationship>();- if (_zip.GetEntry(relsPath) is not { } entry)+ if (_package.GetEntry(relsPath) is not { } entry)return result;- using var stream = entry.Open();- foreach (var rel in XDocument.Load(stream).Root?.Elements(PackageRelationships + "Relationship") ?? [])+ foreach (var rel in LoadXml(entry, preserveWhitespace: false).Root?.Elements(PackageRelationships + "Relationship") ?? []){var id = (string?)rel.Attribute("Id") ?? "";var target = (string?)rel.Attribute("Target") ?? "";@@ -201,11 +227,10 @@ internal sealed partial class DocxReaderprivate string? Title(){- var core = _zip.GetEntry("docProps/core.xml");+ var core = _package.GetEntry("docProps/core.xml");if (core is null)return null;- using var stream = core.Open();- return NullIfEmpty(XDocument.Load(stream).Descendants(Dc + "title").FirstOrDefault()?.Value);+ return NullIfEmpty(LoadXml(core, preserveWhitespace: false).Descendants(Dc + "title").FirstOrDefault()?.Value);}private void AddSection(HDocument document, List<HBlock> blocks, XElement? sectPr,@@ -531,6 +556,7 @@ internal sealed partial class DocxReaderprivate void ReadParagraph(XElement p, List<HBlock> output){+ _budget.Paragraph();var pPr = p.Element(W + "pPr");var (props, mark) = ParagraphProperties(pPr);var builder = new ParagraphBuilder(this, output, props, mark, pPr);@@ -578,6 +604,7 @@ internal sealed partial class DocxReader{var field = new FieldState();field.Instruction.Append(Attr(element, "instr"));+ _budget.Fields(_fields.Count + 1);_fields.Add(field);StartFieldResult(field, builder, CharFormat(styleRun));ParagraphContent(element.Elements(), builder, styleRun);@@ -710,6 +737,7 @@ internal sealed partial class DocxReader/// </summary>private void Equations(XElement math, ParagraphBuilder builder, RunProps styleRun){+ _budget.Object();var rPr = math.Descendants(M + "r").Select(r => r.Element(W + "rPr")).FirstOrDefault(p => p is not null);var format = CharFormat(RunProperties(rPr, styleRun)) with { Italic = true };var lines = math.Name.LocalName == "oMathPara" ? math.Elements(M + "oMath").ToList() : [math];@@ -741,14 +769,25 @@ internal sealed partial class DocxReaderprivate HNote? Note(XElement reference, Dictionary<string, XElement> notes, Part? part, bool endnote){- if (part is null || !notes.TryGetValue(Attr(reference, "id") ?? "", out var element))+ if (_inNote || part is null || !notes.TryGetValue(Attr(reference, "id") ?? "", out var element))return null;var note = new HNote(endnote);- WithPart(part, () =>+ using (_budget.Enter()){- ReadBlocks(element.Elements(), note.Blocks);- return 0;- });+ _inNote = true;+ try+ {+ WithPart(part, () =>+ {+ ReadBlocks(element.Elements(), note.Blocks);+ return 0;+ });+ }+ finally+ {+ _inNote = false;+ }+ }// Word notes start with the reference mark and a space; 한글 writes its own number.if (note.Blocks.FirstOrDefault() is HParagraph first && first.Inlines.FirstOrDefault() is HText { Text: var text } leading){@@ -784,6 +823,8 @@ internal sealed partial class DocxReaderswitch (Attr(fldChar, "fldCharType")){case "begin":+ // ShowsText looks at every open field for every run, so their number is limited.+ _budget.Fields(_fields.Count + 1);_fields.Add(new FieldState());break;case "separate":diff --git a/src/Filee.Engines/Hwp/Hwpx/HwpxReader.Body.cs b/src/Filee.Engines/Hwp/Hwpx/HwpxReader.Body.csindex a74fc163..674bda48 100644--- a/src/Filee.Engines/Hwp/Hwpx/HwpxReader.Body.cs+++ b/src/Filee.Engines/Hwp/Hwpx/HwpxReader.Body.cs@@ -29,6 +29,7 @@ internal sealed partial class HwpxReadervar blocks = new List<HBlock>();if (subList is null)return blocks;+ using var level = _budget.Enter();// Fields, highlighting and tracked deletions do not reach into or out of a nested paragraph list.var (fields, markpen, deleted) = (_fields, _markpen, _deleted);(_fields, _markpen, _deleted) = ([], null, false);@@ -150,6 +151,7 @@ internal sealed partial class HwpxReaderprivate void ReadParagraph(XElement p, List<HBlock> output, SectionState? section){+ _budget.Paragraph();var shape = ParaShapeOf(Attr(p, "paraPrIDRef"));var (heading, list) = HeadingAndList(shape, Attr(p, "styleIDRef"));var builder = new ParagraphBuilder(output, _fields, shape.Format, heading, list, _forLayout) { MarkFormat = CharFormat(null) };@@ -432,7 +434,9 @@ internal sealed partial class HwpxReaderbuilder.Add(new HBookmark(name));break;}- // Other fields (click-here, date, file name, ...) keep their result text, which follows until fieldEnd.+ // Other fields (click-here, date, file name, ...) keep their result text, which follows until fieldEnd. Every+ // paragraph reopens the links of the open fields, so their number is limited.+ _budget.Fields(_fields.Count + 1);_fields.Add(new OpenField(Attr(control, "id"), target));if (target is not null)builder.OpenLink(target);@@ -490,9 +494,12 @@ internal sealed partial class HwpxReaderprivate HTable Table(XElement tbl){+ _budget.Object();+ // Column numbers and spans of damaged files stay within the limit, so they can't overflow.+ var maxColumns = _budget.Limits.MaxTableColumns;var table = new HTable{- ColumnCount = Math.Max(0, Int(tbl, "colCnt")),+ ColumnCount = Math.Clamp(Int(tbl, "colCnt"), 0, maxColumns),PageBreak = Attr(tbl, "pageBreak") switch{"NONE" => HTablePageBreak.None,@@ -535,8 +542,8 @@ internal sealed partial class HwpxReadervar subList = Child(tc, "subList");var cell = new HCell{- Column = address is null ? -1 : Math.Max(0, Int(address, "colAddr")),- ColSpan = Math.Max(1, Int(span, "colSpan", 1)),+ Column = address is null ? -1 : Math.Clamp(Int(address, "colAddr"), 0, maxColumns - 1),+ ColSpan = Math.Clamp(Int(span, "colSpan", 1), 1, maxColumns),RowSpan = Math.Max(1, Int(span, "rowSpan", 1)),VerticalAlign = VerticalAlign(Attr(subList, "vertAlign")),};@@ -566,13 +573,21 @@ internal sealed partial class HwpxReader// Cells with their own margins (hasMargin) set the table's padding when they all agree.if (cellMargins.Count == 1 && cellMargins.Single() is { } shared)table.CellMargin = shared;- table.ColumnCount = Math.Max(table.ColumnCount, widths.Select(w => w.Column + w.Span).DefaultIfEmpty(1).Max());+ // A cell can't span rows below the table (writers walk the rows it covers).+ for (var r = 0; r < table.Rows.Count; r++)+ {+ foreach (var cell in table.Rows[r].Cells)+ cell.RowSpan = Math.Min(cell.RowSpan, table.Rows.Count - r);+ }+ table.ColumnCount = Math.Min(maxColumns, Math.Max(table.ColumnCount, widths.Select(w => w.Column + w.Span).DefaultIfEmpty(1).Max()));table.ColumnWidths = ColumnWidths(widths, table.ColumnCount);if (Child(Child(tbl, "caption"), "subList") is { } caption)table.Caption.AddRange(ReadSubList(caption));return table;}+ private const long MaxCoveredColumns = 4_000_000;+/// <summary>Column widths from the cell widths: single cells first, then what spanning cells leave over.</summary>private static int[]? ColumnWidths(List<(int Column, int Span, int Width)> cells, int columns){@@ -584,7 +599,10 @@ internal sealed partial class HwpxReaderif (span == 1 && column < columns && width > 0)widths[column] ??= width;}- for (var changed = true; changed;)+ // Spanning cells are spread over the columns they cover; past a few million columns covered (a damaged or+ // hostile table, real ones cover thousands) only the single cells count.+ var covered = cells.Sum(c => c.Span > 1 ? (long)Math.Min(c.Span, columns) : 0);+ for (var changed = covered <= MaxCoveredColumns; changed;){changed = false;foreach (var (column, span, width) in cells.Where(c => c.Span > 1 && c.Width > 0))@@ -593,9 +611,9 @@ internal sealed partial class HwpxReadervar unknown = range.Where(i => widths[i] is null).ToList();if (unknown.Count == 0)continue;- var rest = width - range.Sum(i => widths[i] ?? 0);+ var rest = width - range.Sum(i => (long)(widths[i] ?? 0));foreach (var i in unknown)- widths[i] = Math.Max(1, rest / unknown.Count);+ widths[i] = (int)Math.Clamp(rest / unknown.Count, 1, int.MaxValue);changed = true;}}diff --git a/src/Filee.Engines/Hwp/Hwpx/HwpxReader.Objects.cs b/src/Filee.Engines/Hwp/Hwpx/HwpxReader.Objects.csindex 2a611296..23524320 100644--- a/src/Filee.Engines/Hwp/Hwpx/HwpxReader.Objects.cs+++ b/src/Filee.Engines/Hwp/Hwpx/HwpxReader.Objects.cs@@ -56,6 +56,7 @@ internal sealed partial class HwpxReader/// <summary>The model objects for one drawing object; groups give one object per member.</summary>private IEnumerable<HInline> Shapes(XElement element, Placement placement){+ _budget.Object();switch (element.Name.LocalName){case "pic":@@ -242,6 +243,7 @@ internal sealed partial class HwpxReader/// </summary>private IEnumerable<HInline> Group(XElement container, Placement placement){+ using var level = _budget.Enter();var anchor = placement.Anchor ?? new HAnchor("COLUMN", "LEFT", 0, "PARA", "TOP", 0, "IN_FRONT_OF_TEXT", true);foreach (var member in Elements(container)){diff --git a/src/Filee.Engines/Hwp/Hwpx/HwpxReader.cs b/src/Filee.Engines/Hwp/Hwpx/HwpxReader.csindex e88e25a1..3a7da3da 100644--- a/src/Filee.Engines/Hwp/Hwpx/HwpxReader.cs+++ b/src/Filee.Engines/Hwp/Hwpx/HwpxReader.cs@@ -14,9 +14,9 @@// and master pages are read. Both fill the rendering detail of formats and objects (HwpxModel.Layout.cs).using System.Globalization;-using System.IO.Compression;using System.Xml;using System.Xml.Linq;+using Filee.Engines.Documents;namespace Filee.Engines.Hwp.Hwpx;@@ -34,10 +34,13 @@ internal sealed partial class HwpxReader/// </summary>internal const string EstimatedLinesMeta = "FileeLineSegments";- private readonly ZipArchive? _zip;+ private readonly DocumentPackage? _package;private readonly string _mediaFolder;private readonly bool _forLayout;+ /// <summary>Nesting, paragraphs and objects read so far, against the package's limits.</summary>+ private readonly DocumentBudget _budget;+/// <summary>Manifest item id → zip entry (master page parts are found through it).</summary>private Dictionary<string, string> _items = new(StringComparer.Ordinal);@@ -53,14 +56,15 @@ internal sealed partial class HwpxReader/// <summary>Elements the model cannot hold, by local name, with the number of times they were skipped.</summary>private readonly Dictionary<string, int> _skipped = new(StringComparer.Ordinal);- private HwpxReader(ZipArchive? zip, string mediaFolder, bool forLayout)+ private HwpxReader(DocumentPackage? package, string mediaFolder, bool forLayout){- _zip = zip;+ _package = package;_mediaFolder = mediaFolder;_forLayout = forLayout;+ _budget = new DocumentBudget(package?.Limits ?? DocumentLimits.Default);}- private ZipArchive Zip => _zip ?? throw new InvalidOperationException("This reader only has a header.");+ private DocumentPackage Package => _package ?? throw new InvalidOperationException("This reader only has a header.");// ───────────────────────── Entry points ─────────────────────────@@ -69,14 +73,18 @@ internal sealed partial class HwpxReader/// <param name="skipped">Receives the elements that were skipped because the model has no equivalent (local name → count).</param>public static HDocument Read(string path, string mediaFolder, IDictionary<string, int>? skipped) =>- ReadPackage(path, mediaFolder, skipped, forLayout: false);+ ReadPackage(path, mediaFolder, skipped, forLayout: false, DocumentLimits.Default);++ /// <summary>Reads within other <paramref name="limits"/> than <see cref="DocumentLimits.Default"/> (for tests).</summary>+ internal static HDocument Read(string path, string mediaFolder, IDictionary<string, int>? skipped, DocumentLimits limits) =>+ ReadPackage(path, mediaFolder, skipped, forLayout: false, limits);/// <summary>/// Reads <paramref name="path"/> for the layout engine: like <see cref="Read(string, string, IDictionary{string, int}?)"/>,/// plus what only a renderer uses (runs apart by layout detail, polygons / curves / arcs, master pages)./// </summary>- public static HDocument ReadForLayout(string path, string mediaFolder, IDictionary<string, int>? skipped = null) =>- ReadPackage(path, mediaFolder, skipped, forLayout: true);+ public static HDocument ReadForLayout(string path, string mediaFolder, IDictionary<string, int>? skipped = null, DocumentLimits? limits = null) =>+ ReadPackage(path, mediaFolder, skipped, forLayout: true, limits ?? DocumentLimits.Default);/// <summary>A reader of header.xml alone, for its character and paragraph shapes (see <see cref="StyleResolver"/>).</summary>internal static HwpxReader ForHeader(XElement head)@@ -86,21 +94,11 @@ internal sealed partial class HwpxReaderreturn reader;}- private static HDocument ReadPackage(string path, string mediaFolder, IDictionary<string, int>? skipped, bool forLayout)+ private static HDocument ReadPackage(string path, string mediaFolder, IDictionary<string, int>? skipped, bool forLayout, DocumentLimits limits){- ZipArchive zip;- try- {- zip = ZipFile.OpenRead(path);- }- catch (InvalidDataException)- {- throw NotAPackage(path);- }-- using (zip)+ using (var package = DocumentPackage.Open(path, limits, () => NotAPackage(path))){- var reader = new HwpxReader(zip, mediaFolder, forLayout);+ var reader = new HwpxReader(package, mediaFolder, forLayout);var document = reader.ReadDocument();if (skipped is not null){@@ -200,7 +198,7 @@ internal sealed partial class HwpxReaderif (result.Count > 0)return result;- return Zip.Entries+ return Package.Entries.Where(e => e.FullName.StartsWith("Contents/", StringComparison.OrdinalIgnoreCase) && IsSection(e.FullName)).OrderBy(e => int.TryParse(Path.GetFileNameWithoutExtension(e.Name).AsSpan(7), NumberStyles.Integer, CultureInfo.InvariantCulture, out var n) ? n : int.MaxValue).Select(e => e.FullName)@@ -210,14 +208,14 @@ internal sealed partial class HwpxReaderprivate string? EntryPath(string href, string folder){var path = Uri.UnescapeDataString(href).Replace('\\', '/').TrimStart('/');- if (Zip.GetEntry(path) is not null)+ if (Package.GetEntry(path) is not null)return path;- return folder.Length > 0 && Zip.GetEntry(folder + path) is not null ? folder + path : path;+ return folder.Length > 0 && Package.GetEntry(folder + path) is not null ? folder + path : path;}private XDocument? LoadXml(string path, bool required){- var entry = Zip.GetEntry(path);+ var entry = Package.GetEntry(path);if (entry is null){return required@@ -226,9 +224,8 @@ internal sealed partial class HwpxReader}try{- using var stream = entry.Open();// Whitespace matters: <hp:t> </hp:t> is a real space.- return XDocument.Load(stream, LoadOptions.PreserveWhitespace);+ return Package.LoadXml(entry, preserveWhitespace: true);}catch (XmlException ex){@@ -247,16 +244,15 @@ internal sealed partial class HwpxReader// Without a manifest entry, 한글's naming (BinData/<id>.<ext>) still finds the file.var entryPath = _binaryItems.GetValueOrDefault(itemId)- ?? Zip.Entries.FirstOrDefault(e => e.FullName.StartsWith("BinData/", StringComparison.OrdinalIgnoreCase) &&+ ?? Package.Entries.FirstOrDefault(e => e.FullName.StartsWith("BinData/", StringComparison.OrdinalIgnoreCase) &&string.Equals(Path.GetFileNameWithoutExtension(e.Name), itemId, StringComparison.OrdinalIgnoreCase))?.FullName;string? path = null;- if (entryPath is not null && Zip.GetEntry(entryPath) is { } entry)+ if (entryPath is not null && Package.GetEntry(entryPath) is { } entry){Directory.CreateDirectory(_mediaFolder);- path = Path.Combine(_mediaFolder, $"hwpx-media-{_media.Count + 1}{Path.GetExtension(entry.Name).ToLowerInvariant()}");- using var source = entry.Open();- using var target = File.Create(path);- source.CopyTo(target);+ path = Path.Combine(_mediaFolder, $"hwpx-media-{_media.Count + 1}{DocumentPackage.SafeExtension(entry.Name)}");+ if (!Package.ExtractPicture(entry, path))+ path = null; // larger than any picture should be: left out}_media[itemId] = path;return path;diff --git a/src/Filee.Engines/Hwp/Hwpx/HwpxUnits.cs b/src/Filee.Engines/Hwp/Hwpx/HwpxUnits.csindex 5d095302..5d62baa9 100644--- a/src/Filee.Engines/Hwp/Hwpx/HwpxUnits.cs+++ b/src/Filee.Engines/Hwp/Hwpx/HwpxUnits.cs@@ -40,6 +40,9 @@ internal static class HwpxUnits"%" => number * 1.5, // % of a ~150 mm text width_ => number * 25.4 / 96, // px or unitless};- return (int)Math.Round(mm * PerMm);+ // Lengths beyond ±500 million (1.7 km) are cut there, so differences of two can't overflow.+ return (int)Math.Clamp(Math.Round(mm * PerMm), -MaxLength, MaxLength);}++ private const int MaxLength = 500_000_000;}diff --git a/src/Filee.Engines/Hwp/Hwpx/StyleResolver.cs b/src/Filee.Engines/Hwp/Hwpx/StyleResolver.csindex 7afdd824..831492b0 100644--- a/src/Filee.Engines/Hwp/Hwpx/StyleResolver.cs+++ b/src/Filee.Engines/Hwp/Hwpx/StyleResolver.cs@@ -8,8 +8,9 @@// coloured) and the per-language slots the writer fills from Font / EastAsianFont / Spacing. Rendering detail the// source set (HParaLayout, HCharLayout) wins over the style's.-using System.IO.Compression;+using System.Xml;using System.Xml.Linq;+using Filee.Engines.Documents;namespace Filee.Engines.Hwp.Hwpx;@@ -41,10 +42,18 @@ internal sealed class StyleResolvervar stream = template ?? HwpxWriter.OpenBuiltInTemplate();try{- using var zip = new ZipArchive(stream, ZipArchiveMode.Read, leaveOpen: true);- var entry = zip.GetEntry("Contents/header.xml") ?? throw new InvalidDataException("The template has no Contents/header.xml.");- using var header = entry.Open();- return new StyleResolver(XDocument.Load(header).Root ?? throw new InvalidDataException("header.xml has no root element."));+ using var package = DocumentPackage.Open(stream);+ var entry = package.GetEntry("Contents/header.xml") ?? throw new InvalidDataException("The template has no Contents/header.xml.");+ XDocument header;+ try+ {+ header = package.LoadXml(entry, preserveWhitespace: false);+ }+ catch (XmlException ex)+ {+ throw new InvalidDataException($"The template's header.xml is not valid XML ({ex.Message}).", ex);+ }+ return new StyleResolver(header.Root ?? throw new InvalidDataException("header.xml has no root element."));}finally{diff --git a/src/Filee.Engines/Geo/LimitedStream.cs b/src/Filee.Engines/Infrastructure/LimitedStream.cssimilarity index 51%rename from src/Filee.Engines/Geo/LimitedStream.csrename to src/Filee.Engines/Infrastructure/LimitedStream.csindex 57e7aa38..ed171a01 100644--- a/src/Filee.Engines/Geo/LimitedStream.cs+++ b/src/Filee.Engines/Infrastructure/LimitedStream.cs@@ -1,13 +1,31 @@// A read-only wrapper that counts the bytes read through it and fails once they pass a limit, so a ZIP entry that// unpacks to far more than its header says (a ZIP bomb) stops at a known size instead of filling the memory.-namespace Filee.Engines.Geo;+namespace Filee.Engines.Infrastructure;-/// <summary>Reads at most <c>limit</c> bytes of <paramref name="inner"/>, then throws <see cref="InvalidDataException"/>.</summary>-internal sealed class LimitedStream(Stream inner, long limit, string message) : Stream+/// <summary>Reads at most <c>limit</c> bytes of <c>inner</c>, then throws (<see cref="InvalidDataException"/> by default).</summary>+internal sealed class LimitedStream : Stream{+ private readonly Stream _inner;+ private readonly long _limit;+ private readonly Func<Exception> _over;+ private readonly Action<long>? _onRead;private long _read;+ public LimitedStream(Stream inner, long limit, string message) : this(inner, limit, () => new InvalidDataException(message))+ {+ }++ /// <param name="over">The exception thrown once more than <paramref name="limit"/> bytes were read.</param>+ /// <param name="onRead">Called after every read with the number of bytes read so far (for limits shared by several streams).</param>+ public LimitedStream(Stream inner, long limit, Func<Exception> over, Action<long>? onRead = null)+ {+ _inner = inner;+ _limit = limit;+ _over = over;+ _onRead = onRead;+ }+public override bool CanRead => true;public override bool CanSeek => false;public override bool CanWrite => false;@@ -23,17 +41,19 @@ internal sealed class LimitedStream(Stream inner, long limit, string message) :public override int Read(Span<byte> buffer){- var read = inner.Read(buffer);+ var read = _inner.Read(buffer);_read += read;- if (_read > limit)- throw new InvalidDataException(message);+ if (_read > _limit)+ throw _over();+ if (read > 0)+ _onRead?.Invoke(_read);return read;}protected override void Dispose(bool disposing){if (disposing)- inner.Dispose();+ _inner.Dispose();base.Dispose(disposing);}diff --git a/src/Filee.Engines/Infrastructure/SafeXml.cs b/src/Filee.Engines/Infrastructure/SafeXml.csnew file mode 100644index 00000000..f03b2622--- /dev/null+++ b/src/Filee.Engines/Infrastructure/SafeXml.cs@@ -0,0 +1,130 @@+// XML from files Filee did not write: no DTDs, no resolver (so nothing outside the file is ever read and no entity+// expands into gigabytes), and limits on depth, element count and size. The tree is built here from the reader rather+// than by XDocument.Load, which takes time quadratic in the depth (a 1.4 MB file nested 100,000 deep kept it busy for+// 48 seconds; the reader alone needs a quarter of a second) and so must never see such a file. Shared by the+// geospatial formats (KML, GPX) and the document readers (HWPX, DOCX, OFD).++using System.Globalization;+using System.Xml;+using System.Xml.Linq;++namespace Filee.Engines.Infrastructure;++/// <summary>What <see cref="SafeXml.Load(Stream, XmlLimits, Func{string, Exception}?)"/> accepts.</summary>+/// <param name="MaxDepth">Levels of elements (the root is level 1).</param>+/// <param name="MaxElements">Elements in the file.</param>+/// <param name="MaxCharacters">Characters in the file.</param>+/// <param name="PreserveWhitespace">Keep white space between elements (like <see cref="LoadOptions.PreserveWhitespace"/>).</param>+/// <param name="KeepNamespaceDeclarations">Keep xmlns attributes, so prefixes can be looked up as after XDocument.Load.</param>+/// <param name="CheckCharacters">Refuse characters XML 1.0 does not allow.</param>+/// <param name="ElementsBefore">Elements read before this file that count against <paramref name="MaxElements"/> (other parts of one package).</param>+internal readonly record struct XmlLimits(+ int MaxDepth,+ int MaxElements,+ long MaxCharacters,+ bool PreserveWhitespace = true,+ bool KeepNamespaceDeclarations = true,+ bool CheckCharacters = true,+ int ElementsBefore = 0);++/// <summary>Parses untrusted XML into an <see cref="XDocument"/> within <see cref="XmlLimits"/>.</summary>+internal static class SafeXml+{+ private const string XmlnsNamespace = "http://www.w3.org/2000/xmlns/";++ /// <summary>+ /// The document in <paramref name="stream"/>. Comments and processing instructions are left out. Throws+ /// <see cref="XmlException"/> for XML that is not well formed or has a DTD.+ /// </summary>+ /// <param name="limitExceeded">Makes the exception for a limit that was passed from a message saying which+ /// (<see cref="InvalidDataException"/> when null).</param>+ public static XDocument Load(Stream stream, XmlLimits limits, Func<string, Exception>? limitExceeded = null) =>+ Load(stream, limits, limitExceeded, out _);++ /// <param name="elements">The number of elements read.</param>+ public static XDocument Load(Stream stream, XmlLimits limits, Func<string, Exception>? limitExceeded, out int elements)+ {+ limitExceeded ??= message => new InvalidDataException(message);+ var settings = new XmlReaderSettings+ {+ DtdProcessing = DtdProcessing.Prohibit,+ XmlResolver = null,+ MaxCharactersFromEntities = 0,+ MaxCharactersInDocument = limits.MaxCharacters,+ CheckCharacters = limits.CheckCharacters,+ IgnoreWhitespace = !limits.PreserveWhitespace,+ IgnoreComments = true,+ IgnoreProcessingInstructions = true,+ };+ using var reader = XmlReader.Create(stream, settings);+ var document = new XDocument();+ XContainer current = document;+ var depth = 0;+ elements = 0;+ try+ {+ while (reader.Read())+ {+ switch (reader.NodeType)+ {+ case XmlNodeType.Element:+ if (++depth > limits.MaxDepth)+ throw limitExceeded($"The file nests elements more than {limits.MaxDepth} levels deep.");+ if (++elements > limits.MaxElements - limits.ElementsBefore)+ {+ throw limitExceeded((limits.ElementsBefore > 0 ? "The package has more than " : "The file has more than ")+ + limits.MaxElements.ToString("N0", CultureInfo.InvariantCulture) + " elements.");+ }+ var element = new XElement(XName.Get(reader.LocalName, reader.NamespaceURI));+ var empty = reader.IsEmptyElement;+ if (reader.HasAttributes)+ {+ while (reader.MoveToNextAttribute())+ {+ if (reader.NamespaceURI == XmlnsNamespace)+ {+ if (limits.KeepNamespaceDeclarations)+ element.Add(new XAttribute(reader.Prefix.Length == 0 ? XName.Get("xmlns") : XNamespace.Xmlns + reader.LocalName, reader.Value));+ }+ else+ {+ element.SetAttributeValue(XName.Get(reader.LocalName, reader.NamespaceURI), reader.Value);+ }+ }+ reader.MoveToElement();+ }+ current.Add(element);+ if (empty)+ depth--;+ else+ current = element;+ break;+ case XmlNodeType.EndElement:+ current = current.Parent ?? (XContainer)document;+ depth--;+ break;+ case XmlNodeType.CDATA:+ if (current is XElement)+ current.Add(new XCData(reader.Value));+ break;+ case XmlNodeType.Text or XmlNodeType.SignificantWhitespace or XmlNodeType.Whitespace:+ // Text outside the root element (white space between declarations) is no content. Text next+ // to text becomes one node, as XDocument.Load makes it.+ if (current is XElement parent)+ {+ if (parent.LastNode is XText last and not XCData)+ last.Value += reader.Value;+ else+ parent.Add(new XText(reader.Value));+ }+ break;+ }+ }+ }+ catch (XmlException ex) when (ex.Message.Contains("MaxCharactersInDocument", StringComparison.Ordinal))+ {+ throw limitExceeded("The file has more than " + limits.MaxCharacters.ToString("N0", CultureInfo.InvariantCulture) + " characters.");+ }+ return document;+ }+}diff --git a/src/Filee.Engines/Ofd/OfdPackage.cs b/src/Filee.Engines/Ofd/OfdPackage.csindex 2e74863b..2959f129 100644--- a/src/Filee.Engines/Ofd/OfdPackage.cs+++ b/src/Filee.Engines/Ofd/OfdPackage.cs@@ -1,69 +1,40 @@// The ZIP container of an OFD file, read defensively: parts are looked up by name inside the package and never// written out under their own names, references ("../Res/a.png", "/Doc_0/Document.xml") are resolved against the-// package root and cannot leave it, XML is read without DTDs, and every part is read with a size limit (also checked-// while decompressing, so a lying header does not help a zip bomb).+// package root and cannot leave it, and the package is read within DocumentLimits (Documents/DocumentPackage): XML+// without DTDs and with a depth limit, every part with a size limit that is also checked while decompressing, so a+// lying header does not help a zip bomb.using System.IO.Compression;using System.Xml;using System.Xml.Linq;+using Filee.Engines.Documents;namespace Filee.Engines.Ofd;/// <summary>An OFD package opened for reading.</summary>internal sealed class OfdPackage : IDisposable{- /// <summary>More parts than this are refused (real OFD files have a few per page).</summary>- private const int MaxEntries = 100_000;-- /// <summary>Largest XML part read (a page, a resource list).</summary>- private const long MaxXmlBytes = 64L * 1024 * 1024;-/// <summary>Largest picture taken; bigger ones are left out.</summary>public const long MaxPictureBytes = 32L * 1024 * 1024;- /// <summary>Everything read from one package together.</summary>- private const long MaxTotalBytes = 1024L * 1024 * 1024;-- private static readonly XmlReaderSettings XmlSettings = new()- {- DtdProcessing = DtdProcessing.Prohibit,- XmlResolver = null,- IgnoreComments = true,- IgnoreProcessingInstructions = true,- MaxCharactersInDocument = MaxXmlBytes,- };-- private readonly ZipArchive _zip;+ private readonly DocumentPackage _package;private readonly Dictionary<string, ZipArchiveEntry> _entries = new(StringComparer.OrdinalIgnoreCase);- private readonly long _maxTotal;- private long _read;- public OfdPackage(string path) : this(() => ZipFile.OpenRead(path), MaxTotalBytes)+ public OfdPackage(string path) : this(limits => DocumentPackage.Open(path, limits, NotAPackage), DocumentLimits.Default){}/// <summary>A package held in memory (the seal picture inside a signature), with its own limit on what it unpacks to.</summary>- public OfdPackage(byte[] bytes, long maxTotal) : this(() => new ZipArchive(new MemoryStream(bytes, writable: false), ZipArchiveMode.Read), maxTotal)+ public OfdPackage(byte[] bytes, long maxTotal) : this(+ limits => DocumentPackage.Open(new MemoryStream(bytes, writable: false), limits, NotAPackage),+ DocumentLimits.Default with { MaxPackageBytes = Math.Min(maxTotal, DocumentLimits.Default.MaxPackageBytes) }){}- private OfdPackage(Func<ZipArchive> open, long maxTotal)+ private OfdPackage(Func<DocumentLimits, DocumentPackage> open, DocumentLimits limits){- _maxTotal = Math.Min(maxTotal, MaxTotalBytes);- try- {- _zip = open();- }- catch (InvalidDataException ex)- {- throw new InvalidDataException("The file is not an OFD document (it is not a ZIP package). It may be damaged.", ex);- }- if (_zip.Entries.Count > MaxEntries)- {- _zip.Dispose();- throw new InvalidDataException($"The OFD package has more than {MaxEntries:N0} parts; Filee does not read it.");- }- foreach (var entry in _zip.Entries)+ _package = open(limits);+ foreach (var entry in _package.Entries){// Names that climb out of the package ("../x") are unreachable; of two parts with one name the first wins.if (!entry.FullName.EndsWith('/') && Resolve("", entry.FullName) is { } name)@@ -71,7 +42,13 @@ internal sealed class OfdPackage : IDisposable}}- public void Dispose() => _zip.Dispose();+ private static InvalidDataException NotAPackage() =>+ new("The file is not an OFD document (it is not a ZIP package). It may be damaged.");++ public void Dispose() => _package.Dispose();++ /// <summary>The limits the package is read with.</summary>+ public DocumentLimits Limits => _package.Limits;/// <summary>True when the package has a part of that (resolved) name.</summary>public bool Has(string? name) => name is not null && _entries.ContainsKey(name);@@ -120,14 +97,11 @@ internal sealed class OfdPackage : IDisposable/// <summary>An XML part, or null when the package has no such part. Throws for parts that are not XML.</summary>public XDocument? LoadXml(string? name){- if (name is null || !_entries.ContainsKey(name))+ if (name is null || !_entries.TryGetValue(name, out var entry))return null;- var bytes = Read(name, MaxXmlBytes)- ?? throw new InvalidDataException($"The OFD part {name} is larger than {MaxXmlBytes / (1024 * 1024)} MB; Filee does not read it.");try{- using var reader = XmlReader.Create(new MemoryStream(bytes), XmlSettings);- return XDocument.Load(reader);+ return _package.LoadXml(entry, preserveWhitespace: true);}catch (XmlException ex){@@ -136,23 +110,6 @@ internal sealed class OfdPackage : IDisposable}/// <summary>The bytes of a part; null when it is missing or larger than <paramref name="max"/>.</summary>- public byte[]? Read(string name, long max)- {- if (!_entries.TryGetValue(name, out var entry) || entry.Length > max)- return null;- using var stream = entry.Open();- using var copy = new MemoryStream((int)Math.Min(entry.Length, 1024 * 1024));- var buffer = new byte[81920];- int count;- while ((count = stream.Read(buffer)) > 0)- {- if (copy.Length + count > max)- return null;- _read += count;- if (_read > _maxTotal)- throw new InvalidDataException($"The OFD package unpacks to more than {_maxTotal / (1024 * 1024)} MB; Filee does not read it.");- copy.Write(buffer, 0, count);- }- return copy.ToArray();- }+ public byte[]? Read(string name, long max) =>+ _entries.TryGetValue(name, out var entry) ? _package.ReadBytes(entry, max) : null;}diff --git a/src/Filee.Engines/Ofd/OfdReader.cs b/src/Filee.Engines/Ofd/OfdReader.csindex 56ec9395..274843c3 100644--- a/src/Filee.Engines/Ofd/OfdReader.cs+++ b/src/Filee.Engines/Ofd/OfdReader.cs@@ -21,6 +21,7 @@using System.Globalization;using System.Xml.Linq;+using Filee.Engines.Documents;using Filee.Engines.Hwp.Hwpx;using ImageMagick;@@ -610,6 +611,9 @@ internal sealed partial class OfdReadervar name = Path.Combine(_mediaFolder, $"ofd-{Guid.NewGuid():N}");if (kind is "png" or "jpg" or "gif" or "bmp"){+ // Copied as they are; one that claims more pixels than any picture should have is left out.+ if (PictureSize.Read(new MemoryStream(bytes, writable: false)) is { } size && (long)size.Width * size.Height > _package.Limits.MaxPicturePixels)+ return null;File.WriteAllBytes($"{name}.{kind}", bytes);return $"{name}.{kind}";}diff --git a/tests/Filee.Engines.Tests/DocumentFuzzTests.cs b/tests/Filee.Engines.Tests/DocumentFuzzTests.csnew file mode 100644index 00000000..bef2303d--- /dev/null+++ b/tests/Filee.Engines.Tests/DocumentFuzzTests.cs@@ -0,0 +1,233 @@+// Mutation fuzzing of the document readers (HWPX for conversion and for layout, DOCX): seeds are written in code,+// DocumentFuzzer changes them, and every input must be read or refused with InvalidDataException /+// ConversionRefusedException, in time. In a normal test run each reader gets a short, fixed run (the same inputs every+// time); the nightly fuzz workflow sets FILEE_FUZZ_SECONDS for a longer run with a new random seed, and+// FILEE_FUZZ_OUT for a folder that keeps the inputs that failed. FILEE_FUZZ_SEED repeats a run.++using System.Globalization;+using Filee.Engines.Hwp.Hwpx;+using Filee.Engines.Hwp.Hwpx.Docx;+using static Filee.Engines.Tests.DocxBuilder;++namespace Filee.Engines.Tests;++[Trait("Category", "Fuzz")]+public class DocumentFuzzTests(EngineFixture fx) : IClassFixture<EngineFixture>+{+ /// <summary>Inputs per reader in a normal test run.</summary>+ private const int QuickIterations = 50;++ private static readonly TimeSpan ReadTimeout = TimeSpan.FromSeconds(30);++ [Fact]+ public void HwpxReader_reads_or_refuses_every_mutated_package() =>+ Fuzz("hwpx", "hwpx", HwpxSeeds(), (path, media) => HwpxReader.Read(path, media));++ [Fact]+ public void HwpxReader_for_layout_reads_or_refuses_every_mutated_package() =>+ Fuzz("hwpx-layout", "hwpx", HwpxSeeds(), (path, media) => HwpxReader.ReadForLayout(path, media));++ [Fact]+ public void DocxReader_reads_or_refuses_every_mutated_package() =>+ Fuzz("docx", "docx", DocxSeeds(), (path, media) => DocxReader.Read(path, media));++ // ───────────────────────── Seeds ─────────────────────────++ private List<byte[]> HwpxSeeds()+ {+ var folder = fx.NewFolder();+ var hangul = HwpxReaderTests.HangulStylePackage(folder);+ // DOCX read and written as HWPX: what Filee's own writer makes.+ var written = Path.Combine(folder, "written.hwpx");+ HwpxWriter.Write(DocxReader.Read(RichDocx(folder), Path.Combine(folder, "media-docx")), written);+ return [File.ReadAllBytes(hangul), File.ReadAllBytes(written)];+ }++ private List<byte[]> DocxSeeds()+ {+ var folder = fx.NewFolder();+ var rich = RichDocx(folder);+ // The 한글-style package written as DOCX: what Filee's own writer makes.+ var written = Path.Combine(folder, "written.docx");+ DocxWriter.Write(HwpxReader.Read(HwpxReaderTests.HangulStylePackage(folder), Path.Combine(folder, "media-hwpx")), written);+ return [File.ReadAllBytes(rich), File.ReadAllBytes(written)];+ }++ /// <summary>+ /// A DOCX with most of what the reader handles: styles based on styles, a numbered list, a nested table with+ /// merged cells, inline and floating pictures, a text box with a VML fallback, a group, fields, a hyperlink,+ /// notes, a header and footer, math, a content control, a VML picture and a symbol.+ /// </summary>+ internal static string RichDocx(string folder)+ {+ var builder = new DocxBuilder+ {+ Numbering = """+ <w:abstractNum w:abstractNumId="0"><w:lvl w:ilvl="0"><w:start w:val="1"/><w:numFmt w:val="decimal"/><w:lvlText w:val="%1."/><w:pPr><w:ind w:left="720" w:hanging="360"/></w:pPr></w:lvl>+ <w:lvl w:ilvl="1"><w:start w:val="1"/><w:numFmt w:val="bullet"/><w:lvlText w:val=""/><w:rPr><w:rFonts w:ascii="Symbol"/></w:rPr></w:lvl></w:abstractNum>+ <w:num w:numId="1"><w:abstractNumId w:val="0"/></w:num>+ """,+ Settings = "<w:evenAndOddHeaders/><w:noLineBreaksBefore w:lang=\"ko-KR\" w:val=\"!%),.:;?]}\"/>",+ };+ builder.Styles += """+ <w:style w:type="paragraph" w:styleId="Quote"><w:name w:val="Quote"/><w:basedOn w:val="Heading1"/><w:pPr><w:ind w:left="400"/><w:jc w:val="center"/></w:pPr><w:rPr><w:i/></w:rPr></w:style>+ <w:style w:type="paragraph" w:styleId="Loop"><w:name w:val="Loop"/><w:basedOn w:val="Loop2"/></w:style>+ <w:style w:type="paragraph" w:styleId="Loop2"><w:name w:val="Loop2"/><w:basedOn w:val="Loop"/></w:style>+ """;+ var png = builder.Image(TinyPng(4, 3));+ var link = builder.Hyperlink("https://example.invalid/page");+ var header = builder.Header(P(R("머리말 ") + Field("PAGE", "1")));+ var footer = builder.Footer(P(R("꼬리말 ") + Field("NUMPAGES", "3")));+ builder.Footnotes("<w:footnote w:id=\"1\"><w:p><w:r><w:footnoteRef/></w:r>" + R(" 각주 본문") + "</w:p></w:footnote>");+ builder.Endnotes("<w:endnote w:id=\"1\"><w:p><w:r><w:endnoteRef/></w:r>" + R(" 미주 본문") + "</w:p></w:endnote>");+ builder.SectionProperties = $"<w:headerReference w:type=\"default\" r:id=\"{header}\"/><w:footerReference w:type=\"default\" r:id=\"{footer}\"/>" ++ DefaultPage + "<w:cols w:num=\"2\" w:space=\"720\"/><w:docGrid w:type=\"lines\" w:linePitch=\"360\"/><w:titlePg/>";++ const string Cell = "<w:tc><w:tcPr><w:tcW w:w=\"2000\" w:type=\"dxa\"/></w:tcPr>{0}</w:tc>";+ var inner = "<w:tbl><w:tblPr><w:tblStyle w:val=\"TableGrid\"/></w:tblPr><w:tblGrid><w:gridCol w:w=\"1000\"/></w:tblGrid><w:tr>" ++ string.Format(CultureInfo.InvariantCulture, Cell, P(R("안쪽 표"))) + "</w:tr></w:tbl>";+ var table = "<w:tbl><w:tblPr><w:tblStyle w:val=\"TableGrid\"/><w:tblLook w:firstRow=\"1\"/></w:tblPr><w:tblGrid><w:gridCol w:w=\"2000\"/><w:gridCol w:w=\"2000\"/><w:gridCol w:w=\"2000\"/></w:tblGrid>" ++ "<w:tr><w:trPr><w:tblHeader/><w:trHeight w:val=\"400\"/></w:trPr>" ++ "<w:tc><w:tcPr><w:gridSpan w:val=\"2\"/><w:shd w:val=\"clear\" w:fill=\"FFFF00\"/></w:tcPr>" + P(R("병합")) + "</w:tc>" ++ "<w:tc><w:tcPr><w:vMerge w:val=\"restart\"/><w:vAlign w:val=\"center\"/></w:tcPr>" + P(R("세로")) + "</w:tc></w:tr>" ++ "<w:tr><w:trPr><w:cantSplit/></w:trPr>" + string.Format(CultureInfo.InvariantCulture, Cell, inner + P("")) ++ string.Format(CultureInfo.InvariantCulture, Cell, P(R("칸"))) + "<w:tc><w:tcPr><w:vMerge/></w:tcPr>" + P("") + "</w:tc></w:tr></w:tbl>";+ var textBox = "<w:r><mc:AlternateContent><mc:Choice Requires=\"wps\"><w:drawing><wp:anchor distT=\"0\" distB=\"0\" distL=\"0\" distR=\"0\" simplePos=\"0\" relativeHeight=\"3\" behindDoc=\"0\" locked=\"0\" layoutInCell=\"1\" allowOverlap=\"1\">" ++ "<wp:simplePos x=\"0\" y=\"0\"/><wp:positionH relativeFrom=\"column\"><wp:posOffset>100</wp:posOffset></wp:positionH><wp:positionV relativeFrom=\"paragraph\"><wp:align>top</wp:align></wp:positionV>" ++ "<wp:extent cx=\"914400\" cy=\"457200\"/><wp:wrapSquare wrapText=\"bothSides\"/><wp:docPr id=\"3\" name=\"tb\"/><a:graphic><a:graphicData uri=\"" + ShapeUri + "\">" ++ "<wps:wsp><wps:spPr><a:xfrm rot=\"5400000\"><a:off x=\"0\" y=\"0\"/><a:ext cx=\"914400\" cy=\"457200\"/></a:xfrm><a:prstGeom prst=\"roundRect\"/><a:solidFill><a:srgbClr val=\"DDEEFF\"/></a:solidFill><a:ln w=\"12700\"><a:solidFill><a:schemeClr val=\"accent1\"/></a:solidFill></a:ln></wps:spPr>" ++ "<wps:txbx><w:txbxContent>" + P(R("글상자")) + "</w:txbxContent></wps:txbx><wps:bodyPr lIns=\"0\" tIns=\"45720\" anchor=\"ctr\"/></wps:wsp></a:graphicData></a:graphic></wp:anchor></w:drawing></mc:Choice>" ++ "<mc:Fallback><w:pict><v:rect style=\"position:absolute;width:72pt;height:36pt;z-index:2\" fillcolor=\"#ddeeff\" strokecolor=\"red\" strokeweight=\"1pt\"><v:textbox><w:txbxContent>" + P(R("VML 글상자")) + "</w:txbxContent></v:textbox></v:rect></w:pict></mc:Fallback></mc:AlternateContent></w:r>";+ var group = Anchor("http://schemas.microsoft.com/office/word/2010/wordprocessingGroup",+ "<wpg:wgp><wpg:cNvGrpSpPr/><wpg:grpSpPr><a:xfrm><a:off x=\"0\" y=\"0\"/><a:ext cx=\"1828800\" cy=\"914400\"/><a:chOff x=\"0\" y=\"0\"/><a:chExt cx=\"1828800\" cy=\"914400\"/></a:xfrm></wpg:grpSpPr>" ++ Picture(png, 914400, 914400).Replace("<pic:pic>", "<pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">", StringComparison.Ordinal) ++ "<wps:wsp><wps:spPr><a:xfrm><a:off x=\"914400\" y=\"0\"/><a:ext cx=\"914400\" cy=\"914400\"/></a:xfrm><a:prstGeom prst=\"ellipse\"/></wps:spPr><wps:bodyPr/></wps:wsp>" ++ "<wpg:grpSp><wpg:grpSpPr><a:xfrm><a:off x=\"0\" y=\"457200\"/><a:ext cx=\"914400\" cy=\"457200\"/><a:chOff x=\"0\" y=\"0\"/><a:chExt cx=\"914400\" cy=\"457200\"/></a:xfrm></wpg:grpSpPr>" ++ "<wps:wsp><wps:spPr><a:xfrm><a:off x=\"0\" y=\"0\"/><a:ext cx=\"457200\" cy=\"457200\"/></a:xfrm><a:prstGeom prst=\"rect\"/></wps:spPr><wps:txbx><w:txbxContent>" + P(R("묶음 안")) + "</w:txbxContent></wps:txbx><wps:bodyPr/></wps:wsp></wpg:grpSp></wpg:wgp>",+ 1828800, 914400, "<wp:positionH relativeFrom=\"page\"><wp:align>center</wp:align></wp:positionH>", "<wp:positionV relativeFrom=\"page\"><wp:posOffset>914400</wp:posOffset></wp:positionV>");++ builder.Paragraph(P(R("제목") + "<w:bookmarkStart w:id=\"0\" w:name=\"top\"/><w:bookmarkEnd w:id=\"0\"/>", "<w:pStyle w:val=\"Heading1\"/>"))+ .Paragraph(P(R("인용", "<w:b/><w:color w:val=\"FF0000\"/><w:highlight w:val=\"yellow\"/>"), "<w:pStyle w:val=\"Quote\"/><w:spacing w:before=\"120\" w:after=\"240\" w:line=\"360\" w:lineRule=\"auto\"/>"))+ .Paragraph(P(R("고리"), "<w:pStyle w:val=\"Loop\"/>"))+ .Paragraph(P(R("첫째"), "<w:numPr><w:ilvl w:val=\"0\"/><w:numId w:val=\"1\"/></w:numPr>"))+ .Paragraph(P(R("둘째"), "<w:numPr><w:ilvl w:val=\"1\"/><w:numId w:val=\"1\"/></w:numPr>"))+ .Paragraph(table)+ .Paragraph(P(InlinePicture(png, 381000, 285750) + textBox + group))+ .Paragraph(P($"<w:hyperlink r:id=\"{link}\">{R("링크", "<w:rStyle w:val=\"Hyperlink\"/>")}</w:hyperlink>" ++ "<w:hyperlink w:anchor=\"top\">" + R("책갈피") + "</w:hyperlink>" ++ Field("HYPERLINK \"https://example.invalid/field\" \\o \"tip\"", "필드 링크") ++ "<w:fldSimple w:instr=\" PAGE \">" + R("1") + "</w:fldSimple>" ++ "<w:r><w:footnoteReference w:id=\"1\"/></w:r><w:r><w:endnoteReference w:id=\"1\"/></w:r>" ++ "<w:r><w:sym w:font=\"Wingdings\" w:char=\"F0FC\"/><w:tab/><w:br/><w:t>끝</w:t><w:br w:type=\"column\"/></w:r>"))+ .Paragraph(P("<m:oMath><m:f><m:num><m:r><m:t>x</m:t></m:r></m:num><m:den><m:sSup><m:e><m:r><m:t>y</m:t></m:r></m:e><m:sup><m:r><m:t>2</m:t></m:r></m:sup></m:sSup></m:den></m:f><m:rad><m:radPr><m:degHide m:val=\"1\"/></m:radPr><m:deg/><m:e><m:r><m:t>z</m:t></m:r></m:e></m:rad></m:oMath>"))+ .Paragraph("<w:sdt><w:sdtPr/><w:sdtContent>" + P(R("내용 컨트롤")) + "</w:sdtContent></w:sdt>")+ .Paragraph(P($"<w:r><w:pict><v:shape style=\"width:30pt;height:20pt\"><v:imagedata r:id=\"{png}\"/></v:shape><v:line from=\"0,0\" to=\"10pt,20pt\" strokecolor=\"#0000ff\"/></w:pict></w:r>" ++ "<w:r><w:br w:type=\"page\"/></w:r>" + R("다음 쪽", "<w:rFonts w:ascii=\"Arial\" w:eastAsia=\"바탕\"/><w:sz w:val=\"28\"/><w:spacing w:val=\"20\"/><w:w w:val=\"90\"/><w:vertAlign w:val=\"superscript\"/>")))+ .Paragraph("<w:p><w:pPr><w:sectPr>" + DefaultPage + "<w:type w:val=\"continuous\"/></w:sectPr></w:pPr></w:p>");+ return builder.Save(Path.Combine(folder, "rich.docx"));+ }++ /// <summary>A valid PNG of <paramref name="width"/> × <paramref name="height"/> grey pixels.</summary>+ internal static byte[] TinyPng(int width, int height)+ {+ using var output = new MemoryStream();+ output.Write([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]);+ void Chunk(string type, byte[] data)+ {+ var length = new byte[4];+ System.Buffers.Binary.BinaryPrimitives.WriteInt32BigEndian(length, data.Length);+ output.Write(length);+ var body = System.Text.Encoding.ASCII.GetBytes(type).Concat(data).ToArray();+ output.Write(body);+ var crc = new byte[4];+ System.Buffers.Binary.BinaryPrimitives.WriteUInt32BigEndian(crc, Filee.Engines.Archives.Crc32.Compute(body));+ output.Write(crc);+ }+ var header = new byte[13];+ System.Buffers.Binary.BinaryPrimitives.WriteInt32BigEndian(header, width);+ System.Buffers.Binary.BinaryPrimitives.WriteInt32BigEndian(header.AsSpan(4), height);+ header[8] = 8; // bit depth+ header[9] = 0; // greyscale+ Chunk("IHDR", header);+ using (var pixels = new MemoryStream())+ {+ using (var zlib = new System.IO.Compression.ZLibStream(pixels, System.IO.Compression.CompressionLevel.Optimal, leaveOpen: true))+ {+ for (var y = 0; y < height; y++)+ {+ zlib.WriteByte(0); // no filter+ for (var x = 0; x < width; x++)+ zlib.WriteByte(0x80);+ }+ }+ Chunk("IDAT", pixels.ToArray());+ }+ Chunk("IEND", []);+ return output.ToArray();+ }++ // ───────────────────────── Running ─────────────────────────++ /// <param name="name">Names the kept inputs.</param>+ private void Fuzz(string name, string extension, List<byte[]> seeds, Action<string, string> read)+ {+ var seconds = int.TryParse(Environment.GetEnvironmentVariable("FILEE_FUZZ_SECONDS"), out var s) && s > 0 ? s : 0;+ var seed = int.TryParse(Environment.GetEnvironmentVariable("FILEE_FUZZ_SEED"), out var fixedSeed) ? fixedSeed+ : seconds > 0 ? Environment.TickCount : 20261028;+ var outFolder = Environment.GetEnvironmentVariable("FILEE_FUZZ_OUT") is { Length: > 0 } o ? o : null;+ var keep = outFolder ?? fx.NewFolder();+ var output = TestContext.Current.TestOutputHelper;+ output?.WriteLine($"Seed {seed}, {(seconds > 0 ? seconds + " s" : QuickIterations + " inputs per seed package")}");++ var random = new Random(seed);+ var fuzzers = seeds.Select(bytes => new DocumentFuzzer(bytes, random)).ToList();+ var folder = fx.NewFolder();+ // With an output folder the input being read lives there: a crash that ends the process (a stack overflow)+ // leaves it behind for the report. A run that ends normally removes it.+ var input = Path.Combine(outFolder ?? folder, $"running-{name}.{extension}");+ var media = Path.Combine(folder, "media");++ // Every seed itself reads without trouble.+ foreach (var bytes in seeds)+ {+ File.WriteAllBytes(input, bytes);+ Assert.Equal(DocumentFuzzer.Outcome.Read, DocumentFuzzer.Run(() => read(input, media), ReadTimeout));+ }++ var failures = new Dictionary<string, string>();+ var (inputs, refused) = (0, 0);+ var stopwatch = System.Diagnostics.Stopwatch.StartNew();+ while (seconds > 0 ? stopwatch.Elapsed < TimeSpan.FromSeconds(seconds) : inputs < QuickIterations * seeds.Count)+ {+ var fuzzer = fuzzers[inputs % fuzzers.Count];+ var bytes = fuzzer.Next();+ inputs++;+ File.WriteAllBytes(input, bytes);+ if (Directory.Exists(media))+ Directory.Delete(media, recursive: true);+ try+ {+ if (DocumentFuzzer.Run(() => read(input, media), ReadTimeout) == DocumentFuzzer.Outcome.Refused)+ refused++;+ }+ catch (DocumentFuzzer.FuzzFailure failure)+ {+ // One input per kind of failure (exception type and the frame it came from).+ var frame = failure.InnerException?.StackTrace?.Split('\n').FirstOrDefault()?.Trim() ?? "";+ var kind = $"{failure.InnerException?.GetType().Name ?? "Timeout"} {frame}";+ if (!failures.ContainsKey(kind))+ {+ var kept = Path.Combine(keep, $"{name}-{seed}-{inputs}.{extension}");+ File.WriteAllBytes(kept, bytes);+ failures[kind] = $"{failure.Message} (input {kept})\n{failure.InnerException}";+ }+ if (failure.InnerException is null)+ break; // a read that hangs keeps running; don't pile them up+ }+ }+ output?.WriteLine($"{inputs} inputs, {refused} refused, {inputs - refused - failures.Count} read, {failures.Count} kinds of failure");+ if (failures.Count == 0 || failures.Keys.All(k => !k.StartsWith("Timeout", StringComparison.Ordinal)))+ File.Delete(input); // a read that still runs holds it open+ Assert.True(failures.Count == 0, $"Seed {seed}: " + string.Join("\n\n", failures.Values));+ }+}diff --git a/tests/Filee.Engines.Tests/DocumentFuzzer.cs b/tests/Filee.Engines.Tests/DocumentFuzzer.csnew file mode 100644index 00000000..727ad59a--- /dev/null+++ b/tests/Filee.Engines.Tests/DocumentFuzzer.cs@@ -0,0 +1,375 @@+// A mutation fuzzer for document packages (HWPX, DOCX): it takes a valid package written in code, changes it a little+// at random and hands it to a reader. Most changes edit the XML tree, so the file stays well-formed and reaches the+// reader's logic (attributes set to extreme or foreign values, elements removed, repeated, renamed or moved inside one+// another); some edit the text or the bytes, so the XML and ZIP error paths are covered too. A reader may refuse a+// file only with InvalidDataException or ConversionRefusedException; anything else, or a read that takes too long, is+// a failure, and the input that caused it is kept for the report.++using System.IO.Compression;+using System.Text;+using System.Xml;+using System.Xml.Linq;+using Filee.Core.Conversion;++namespace Filee.Engines.Tests;++internal sealed class DocumentFuzzer+{+ /// <summary>Attribute values that have broken readers before: bounds of every integer type, non-numbers, paths.</summary>+ private static readonly string[] Interesting =+ [+ "", "0", "1", "-1", "2", "255", "65535", "65536", "-32768", "2147483647", "-2147483648", "2147483648", "4294967295",+ "4294967296", "9223372036854775807", "-9223372036854775808", "18446744073709551615", "99999999999999999999999",+ "1e308", "-1e308", "1e-308", "NaN", "Infinity", "-Infinity", "0.5", "-0.0", "1.#INF", "0x7FFFFFFF", "#", "#FFF",+ "#GGGGGG", "#AARRGGBB", "#FF00FF00", "none", "auto", "true", "false", "x", " ", "\t", "%", "50%", "1in", "-5cm",+ "12pt", "999999999in", "../../../../../etc/passwd", "/word/document.xml", "Contents/header.xml", "BinData/image1.png",+ "C:\\Windows\\win.ini", "\\\\server\\share\\x", "file:///c:/x", "http://example.invalid/a", "rId1", "rId10", "0,0",+ "1,2,3", "-1,-1", ",,,", "a:b", "image.png:stream", "con", "nul.png", new string('9', 400), new string('a', 5000),+ ];++ /// <summary>Numbers at the edges of what readers parse and compute with.</summary>+ private static readonly string[] Numbers =+ [+ "0", "-1", "1", "2", "-2147483648", "2147483647", "2147483648", "4294967295", "4294967296", "-4294967296",+ "9223372036854775807", "-9223372036854775808", "1e308", "-1e308", "1e30", "-1e30", "100000000", "-100000000",+ "65536", "1073741824", "-1073741824", "0.0001", "NaN", "Infinity",+ ];++ private readonly Random _random;+ private readonly List<(string Name, byte[] Bytes)> _seed;+ private readonly List<string> _xmlParts;++ public DocumentFuzzer(byte[] seedPackage, Random random)+ {+ _random = random;+ _seed = [];+ using (var zip = new ZipArchive(new MemoryStream(seedPackage), ZipArchiveMode.Read))+ {+ foreach (var entry in zip.Entries)+ {+ using var stream = entry.Open();+ using var copy = new MemoryStream();+ stream.CopyTo(copy);+ _seed.Add((entry.FullName, copy.ToArray()));+ }+ }+ _xmlParts = _seed.Where(p => IsXml(p.Name)).Select(p => p.Name).ToList();+ }++ private static bool IsXml(string name) =>+ name.EndsWith(".xml", StringComparison.OrdinalIgnoreCase) || name.EndsWith(".rels", StringComparison.OrdinalIgnoreCase)+ || name.EndsWith(".hpf", StringComparison.OrdinalIgnoreCase);++ /// <summary>A mutated copy of the seed package.</summary>+ public byte[] Next()+ {+ var parts = _seed.Select(p => (p.Name, p.Bytes)).ToList();+ var rounds = 1 + _random.Next(4);+ for (var i = 0; i < rounds; i++)+ {+ var choice = _random.Next(100);+ if (choice < 70 && _xmlParts.Count > 0)+ MutateTree(parts);+ else if (choice < 82 && _xmlParts.Count > 0)+ MutateText(parts);+ else if (choice < 90)+ MutateBinary(parts);+ else if (choice < 95 && parts.Count > 1)+ parts.RemoveAt(_random.Next(parts.Count));+ else+ Splice(parts);+ }+ var package = Pack(parts);+ if (_random.Next(100) < 4)+ {+ // Bytes of the ZIP itself: headers, sizes, compressed data.+ for (var flips = 1 + _random.Next(8); flips > 0; flips--)+ package[_random.Next(package.Length)] = (byte)_random.Next(256);+ }+ return package;+ }++ private byte[] Pack(List<(string Name, byte[] Bytes)> parts)+ {+ using var output = new MemoryStream();+ using (var zip = new ZipArchive(output, ZipArchiveMode.Create, leaveOpen: true))+ {+ foreach (var (name, bytes) in parts)+ {+ using var stream = zip.CreateEntry(name, _random.Next(4) == 0 ? CompressionLevel.NoCompression : CompressionLevel.Fastest).Open();+ stream.Write(bytes);+ }+ }+ return output.ToArray();+ }++ /// <summary>An XML part, the larger ones (the body, the styles) more often.</summary>+ private int PickXml(List<(string Name, byte[] Bytes)> parts)+ {+ var candidates = Enumerable.Range(0, parts.Count).Where(i => _xmlParts.Contains(parts[i].Name)).ToList();+ if (candidates.Count == 0)+ return -1;+ if (_random.Next(4) == 0)+ return candidates[_random.Next(candidates.Count)];+ var total = candidates.Sum(i => (long)parts[i].Bytes.Length + 1);+ var pick = _random.NextInt64(total);+ foreach (var i in candidates)+ {+ pick -= parts[i].Bytes.Length + 1;+ if (pick < 0)+ return i;+ }+ return candidates[^1];+ }++ // ───────────────────────── Tree ─────────────────────────++ private void MutateTree(List<(string Name, byte[] Bytes)> parts)+ {+ var index = PickXml(parts);+ if (index < 0)+ return;+ XDocument document;+ try+ {+ document = XDocument.Parse(Encoding.UTF8.GetString(parts[index].Bytes), LoadOptions.PreserveWhitespace);+ }+ catch (XmlException)+ {+ return; // an earlier round broke it+ }+ var elements = document.Descendants().ToList();+ if (elements.Count == 0)+ return;+ for (var edits = 1 + _random.Next(3); edits > 0; edits--)+ {+ var element = elements[_random.Next(elements.Count)];+ switch (_random.Next(9))+ {+ case 0 or 1 or 2:+ SetAttribute(element, elements);+ break;+ case 3:+ if (element.Parent is not null)+ element.Remove();+ break;+ case 4:+ // The same element many times: counts, ids and caches.+ if (element.Parent is not null)+ {+ var copies = _random.Next(3) == 0 ? 1 + _random.Next(200) : 1 + _random.Next(3);+ for (var c = 0; c < copies; c++)+ element.AddAfterSelf(new XElement(element));+ }+ break;+ case 5:+ {+ // An element inside another: tables in cells in tables, notes in notes, groups in groups.+ var host = elements[_random.Next(elements.Count)];+ if (!host.AncestorsAndSelf().Contains(element) && !element.AncestorsAndSelf().Contains(host))+ host.Add(new XElement(element));+ else if (host == element)+ host.Add(new XElement(element));+ break;+ }+ case 6:+ {+ // The name of another element of the part.+ var other = elements[_random.Next(elements.Count)];+ element.Name = other.Name;+ break;+ }+ case 7:+ element.ReplaceNodes(_random.Next(3) switch+ {+ 0 => "",+ 1 => new string('가', 1 + _random.Next(2000)),+ _ => Interesting[_random.Next(Interesting.Length)],+ });+ break;+ default:+ {+ var attributes = element.Attributes().Where(a => !a.IsNamespaceDeclaration).ToList();+ if (attributes.Count > 0)+ attributes[_random.Next(attributes.Count)].Remove();+ break;+ }+ }+ elements = document.Descendants().ToList();+ if (elements.Count == 0)+ break;+ }+ var text = new StringBuilder();+ try+ {+ using var writer = XmlWriter.Create(text, new XmlWriterSettings { OmitXmlDeclaration = false, CheckCharacters = false });+ document.Save(writer);+ }+ catch (Exception ex) when (ex is ArgumentException or XmlException or InvalidOperationException)+ {+ return; // a tree XML can't hold (xml:space="", a prefix bound twice): this edit is dropped+ }+ parts[index] = (parts[index].Name, Encoding.UTF8.GetBytes(text.ToString().Replace("encoding=\"utf-16\"", "encoding=\"utf-8\"", StringComparison.Ordinal)));+ }++ private void SetAttribute(XElement element, List<XElement> elements)+ {+ var attributes = element.Attributes().Where(a => !a.IsNamespaceDeclaration).ToList();+ if (_random.Next(2) == 0)+ {+ // Any attribute of the part, so the few that matter (a table's column count) come up as often as any.+ attributes = [.. elements.SelectMany(e => e.Attributes()).Where(a => !a.IsNamespaceDeclaration)];+ }+ var target = attributes.Count > 0 ? attributes[_random.Next(attributes.Count)] : null;+ string value;+ if (_random.Next(3) == 0)+ {+ // A value from elsewhere in the part: ids that point at the wrong thing.+ var source = elements[_random.Next(elements.Count)].Attributes().Where(a => !a.IsNamespaceDeclaration).ToList();+ value = source.Count > 0 ? source[_random.Next(source.Count)].Value : Interesting[_random.Next(Interesting.Length)];+ }+ else if (target is not null && double.TryParse(target.Value, System.Globalization.CultureInfo.InvariantCulture, out _) && _random.Next(4) > 0)+ {+ // A number stays a number, of an extreme size.+ value = Numbers[_random.Next(Numbers.Length)];+ }+ else+ {+ value = Interesting[_random.Next(Interesting.Length)];+ }+ if (target is not null && _random.Next(5) > 0)+ {+ target.Value = value;+ }+ else+ {+ // An attribute this element does not have, named like one elsewhere.+ var named = elements[_random.Next(elements.Count)].Attributes().Where(a => !a.IsNamespaceDeclaration).ToList();+ if (named.Count > 0)+ element.SetAttributeValue(named[_random.Next(named.Count)].Name, value);+ }+ }++ // ───────────────────────── Text and bytes ─────────────────────────++ private void MutateText(List<(string Name, byte[] Bytes)> parts)+ {+ var index = PickXml(parts);+ if (index < 0)+ return;+ var bytes = parts[index].Bytes.ToList();+ if (bytes.Count == 0)+ return;+ switch (_random.Next(5))+ {+ case 0:+ for (var flips = 1 + _random.Next(4); flips > 0; flips--)+ bytes[_random.Next(bytes.Count)] = (byte)_random.Next(256);+ break;+ case 1:+ {+ var start = _random.Next(bytes.Count);+ bytes.RemoveRange(start, Math.Min(bytes.Count - start, 1 + _random.Next(64)));+ break;+ }+ case 2:+ {+ var start = _random.Next(bytes.Count);+ var span = bytes.GetRange(start, Math.Min(bytes.Count - start, 1 + _random.Next(256)));+ bytes.InsertRange(_random.Next(bytes.Count), span);+ break;+ }+ case 3:+ bytes = bytes.Take(_random.Next(bytes.Count)).ToList(); // cut short+ break;+ default:+ {+ // A DTD with entities in front of the root: must be refused, never expanded or resolved.+ var text = Encoding.UTF8.GetString([.. bytes]);+ var root = text.IndexOf('<', Math.Max(0, text.IndexOf("?>", StringComparison.Ordinal)));+ if (root < 0)+ root = 0;+ var dtd = _random.Next(2) == 0+ ? "<!DOCTYPE x [<!ENTITY a \"aaaaaaaaaa\"><!ENTITY b \"&a;&a;&a;&a;&a;&a;&a;&a;&a;&a;\"><!ENTITY c \"&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;\">]>"+ : "<!DOCTYPE x SYSTEM \"file:///c:/windows/win.ini\">";+ bytes = [.. Encoding.UTF8.GetBytes(text.Insert(root, dtd))];+ break;+ }+ }+ parts[index] = (parts[index].Name, [.. bytes]);+ }++ private void MutateBinary(List<(string Name, byte[] Bytes)> parts)+ {+ var binaries = Enumerable.Range(0, parts.Count).Where(i => !IsXml(parts[i].Name)).ToList();+ if (binaries.Count == 0)+ return;+ var index = binaries[_random.Next(binaries.Count)];+ var bytes = (byte[])parts[index].Bytes.Clone();+ if (bytes.Length >= 24 && bytes[0] == 0x89 && _random.Next(2) == 0)+ {+ // A PNG that claims an enormous size.+ bytes[16] = bytes[20] = 0x7F;+ bytes[17] = bytes[21] = 0xFF;+ }+ else if (bytes.Length > 0)+ {+ for (var flips = 1 + _random.Next(8); flips > 0; flips--)+ bytes[_random.Next(bytes.Length)] = (byte)_random.Next(256);+ if (_random.Next(4) == 0)+ bytes = bytes[.._random.Next(bytes.Length)];+ }+ parts[index] = (parts[index].Name, bytes);+ }++ /// <summary>A part under the name of another, or an XML part where a picture should be.</summary>+ private void Splice(List<(string Name, byte[] Bytes)> parts)+ {+ if (parts.Count < 2)+ return;+ var from = parts[_random.Next(parts.Count)];+ var to = _random.Next(parts.Count);+ parts[to] = (parts[to].Name, from.Bytes);+ }++ // ───────────────────────── Running ─────────────────────────++ /// <summary>What a reader did with one input.</summary>+ public enum Outcome+ {+ Read,+ Refused,+ }++ /// <summary>+ /// Runs <paramref name="read"/>; returns how it ended, or throws+ /// <see cref="FuzzFailure"/> for an exception a reader must not throw or a read longer than <paramref name="timeout"/>.+ /// </summary>+ public static Outcome Run(Action read, TimeSpan timeout)+ {+ Exception? error = null;+ var thread = new Thread(() =>+ {+ try+ {+ read();+ }+ catch (Exception ex)+ {+ error = ex;+ }+ }, 1024 * 1024); // the smallest stack a conversion runs on: what the limits allow must fit in it+ thread.IsBackground = true;+ thread.Start();+ if (!thread.Join(timeout))+ throw new FuzzFailure($"The read took longer than {timeout.TotalSeconds:0} s.", null);+ return error switch+ {+ null => Outcome.Read,+ InvalidDataException or ConversionRefusedException => Outcome.Refused,+ _ => throw new FuzzFailure($"{error.GetType().Name}: {error.Message}", error),+ };+ }++ public sealed class FuzzFailure(string message, Exception? inner) : Exception(message, inner);+}diff --git a/tests/Filee.Engines.Tests/DocumentHostileInputTests.cs b/tests/Filee.Engines.Tests/DocumentHostileInputTests.csnew file mode 100644index 00000000..07882465--- /dev/null+++ b/tests/Filee.Engines.Tests/DocumentHostileInputTests.cs@@ -0,0 +1,637 @@+// Hostile HWPX and DOCX packages for the document readers and the limits they share (Documents/DocumentLimits): ZIP+// bombs, XML nested or repeated far beyond real documents, DTDs, tables, groups, notes and fields nested without end,+// pictures that claim gigapixels, and the inputs the fuzzer (DocumentFuzzTests) found. Each must be refused or read+// within the limits, quickly and on the smallest stack a conversion runs on. The layout loop guards are tested here too.++using System.IO.Compression;+using System.Text;+using Filee.Core.Conversion;+using Filee.Engines.Documents;+using Filee.Engines.Hwp.Hwpx;+using Filee.Engines.Hwp.Hwpx.Docx;+using Filee.Engines.Infrastructure;+using static Filee.Engines.Tests.DocxBuilder;++namespace Filee.Engines.Tests;++public class DocumentHostileInputTests(EngineFixture fx) : IClassFixture<EngineFixture>+{+ private const string HwpNamespaces =+ "xmlns:hp=\"http://www.hancom.co.kr/hwpml/2011/paragraph\" xmlns:hs=\"http://www.hancom.co.kr/hwpml/2011/section\" xmlns:hc=\"http://www.hancom.co.kr/hwpml/2011/core\"";++ // ───────────────────────── Helpers ─────────────────────────++ /// <summary>+ /// Runs <paramref name="action"/> on a thread with a 1 MB stack (the smallest a conversion gets) and returns what+ /// it threw; fails when it takes longer than <paramref name="seconds"/>.+ /// </summary>+ private static Exception? OnSmallStack(Action action, int seconds = 60)+ {+ Exception? error = null;+ var thread = new Thread(() =>+ {+ try+ {+ action();+ }+ catch (Exception ex)+ {+ error = ex;+ }+ }, 1024 * 1024);+ thread.IsBackground = true;+ thread.Start();+ Assert.True(thread.Join(TimeSpan.FromSeconds(seconds)), $"Still running after {seconds} s");+ return error;+ }++ private static ConversionRefusedException Refused(Action action)+ {+ var refusal = Assert.IsType<ConversionRefusedException>(OnSmallStack(action));+ Assert.Equal(DocumentLimits.ErrorKey, refusal.ErrorKey);+ return refusal;+ }++ private static void Reads(Action action) => Assert.Null(OnSmallStack(action));++ /// <summary>A copy of a package with some parts replaced (written by the action) or added.</summary>+ private static string Rewrite(string source, string target, Dictionary<string, Action<Stream>> parts)+ {+ using (var input = ZipFile.OpenRead(source))+ using (var output = ZipFile.Open(target, ZipArchiveMode.Create))+ {+ foreach (var entry in input.Entries)+ {+ if (parts.ContainsKey(entry.FullName))+ continue;+ using var from = entry.Open();+ using var to = output.CreateEntry(entry.FullName).Open();+ from.CopyTo(to);+ }+ foreach (var (name, write) in parts)+ {+ using var to = output.CreateEntry(name).Open();+ write(to);+ }+ }+ return target;+ }++ private static Action<Stream> Text(string text) => stream => stream.Write(Encoding.UTF8.GetBytes(text));++ private string Docx(string body, Action<DocxBuilder>? setup = null)+ {+ var builder = new DocxBuilder();+ builder.Paragraph(body);+ setup?.Invoke(builder);+ return builder.Save(Path.Combine(fx.NewFolder(), "input.docx"));+ }++ /// <summary>An HWPX package written by Filee whose section0.xml holds <paramref name="paragraphs"/>.</summary>+ private string Hwpx(string paragraphs, Dictionary<string, Action<Stream>>? more = null)+ {+ var folder = fx.NewFolder();+ var document = new HDocument();+ document.Sections.Add(new HSection { Blocks = { Paragraph("seed") } });+ var seed = Path.Combine(folder, "seed.hwpx");+ HwpxWriter.Write(document, seed);+ var parts = new Dictionary<string, Action<Stream>>(more ?? [])+ {+ ["Contents/section0.xml"] = Text($"<?xml version=\"1.0\" encoding=\"UTF-8\"?><hs:sec {HwpNamespaces}>{paragraphs}</hs:sec>"),+ };+ return Rewrite(seed, Path.Combine(folder, "input.hwpx"), parts);+ }++ private static string Hp(string runContent) => $"<hp:p paraPrIDRef=\"0\" styleIDRef=\"0\"><hp:run charPrIDRef=\"0\">{runContent}</hp:run></hp:p>";++ private static HParagraph Paragraph(string text)+ {+ var paragraph = new HParagraph();+ paragraph.Inlines.Add(new HText(text, default));+ return paragraph;+ }++ /// <summary>A table with one cell, nested <paramref name="levels"/> deep, the innermost holding "inside".</summary>+ private static HTable NestedTables(int levels)+ {+ HBlock content = Paragraph("inside");+ HTable? table = null;+ for (var i = 0; i < levels; i++)+ {+ table = new HTable { ColumnCount = 1 };+ var cell = new HCell();+ cell.Blocks.Add(content);+ var row = new HRow();+ row.Cells.Add(cell);+ table.Rows.Add(row);+ content = table;+ }+ return table!;+ }++ private string Media() => Path.Combine(fx.NewFolder(), "media");++ private static int Depth(IEnumerable<HBlock> blocks) =>+ blocks.OfType<HTable>().Select(t => 1 + t.Rows.SelectMany(r => r.Cells).Select(c => Depth(c.Blocks)).DefaultIfEmpty(0).Max()).DefaultIfEmpty(0).Max();++ // ───────────────────────── The limits ─────────────────────────++ [Fact]+ public void The_limits_are_the_planned_ones()+ {+ var limits = DocumentLimits.Default;+ Assert.Equal(64L << 20, limits.MaxPartBytes);+ Assert.Equal(1L << 30, limits.MaxPackageBytes);+ Assert.Equal(32, limits.MaxNestingDepth);+ Assert.Equal(20_000, limits.MaxPages);+ Assert.Equal(16_384L * 16_384, limits.MaxPicturePixels);+ Assert.True(limits.MaxXmlDepth >= limits.MaxNestingDepth * 8, "tables nested to the limit must fit in the XML depth");+ }++ // ───────────────────────── ZIP bombs ─────────────────────────++ [Fact]+ public void A_part_larger_than_64_MB_is_refused_before_it_is_unpacked()+ {+ var path = Rewrite(Docx(P(R("x"))), Path.Combine(fx.NewFolder(), "big.docx"), new()+ {+ ["word/header9.xml"] = stream =>+ {+ var chunk = new byte[1 << 20];+ Array.Fill(chunk, (byte)' ');+ for (var i = 0; i < 65; i++)+ stream.Write(chunk);+ },+ });+ using var package = DocumentPackage.Open(path);+ var entry = package.GetEntry("word/header9.xml")!;+ var refusal = Assert.Throws<ConversionRefusedException>(() => package.Open(entry));+ Assert.Contains("more than 64 MB", refusal.Detail);+ Assert.Equal(0, package.BytesRead);+ }++ [Fact]+ public void A_part_that_inflates_like_a_bomb_is_refused_while_it_is_unpacked()+ {+ // 6 MB of spaces packs into a few kilobytes: far beyond what any word processor's XML compresses to.+ var body = "<w:p><w:r><w:t>" + new string(' ', 6 << 20) + "</w:t></w:r></w:p>";+ var refusal = Refused(() => DocxReader.Read(Docx(body), Media()));+ Assert.Contains("times its packed size", refusal.Detail);+ }++ [Fact]+ public void A_part_whose_header_understates_its_size_is_counted_as_it_unpacks()+ {+ var docx = Docx(P(R(new string('x', 2 << 20))));+ var bytes = File.ReadAllBytes(docx);+ UnderstateSize(bytes, "word/document.xml", 100);+ var lying = Path.Combine(fx.NewFolder(), "lying.docx");+ File.WriteAllBytes(lying, bytes);+ // What .NET makes of the size is its business; Filee must not take 2 MB for a part of 1 MB at the most.+ var error = OnSmallStack(() => DocxReader.Read(lying, Media(), DocumentLimits.Default with { MaxPartBytes = 1 << 20 }));+ Assert.True(error is InvalidDataException or ConversionRefusedException, error?.ToString() ?? "read");+ }++ /// <summary>Writes <paramref name="size"/> as the unpacked size of <paramref name="name"/> in its local and central headers.</summary>+ private static void UnderstateSize(byte[] zip, string name, uint size)+ {+ var nameBytes = Encoding.UTF8.GetBytes(name);+ for (var i = 0; i + 46 < zip.Length; i++)+ {+ var signature = BitConverter.ToUInt32(zip, i);+ if (signature == 0x04034B50 && zip.AsSpan(i + 30, nameBytes.Length).SequenceEqual(nameBytes))+ BitConverter.TryWriteBytes(zip.AsSpan(i + 22), size);+ else if (signature == 0x02014B50 && zip.AsSpan(i + 46, nameBytes.Length).SequenceEqual(nameBytes))+ BitConverter.TryWriteBytes(zip.AsSpan(i + 24), size);+ }+ }++ [Fact]+ public void A_package_whose_parts_together_pass_the_limit_is_refused()+ {+ var limits = DocumentLimits.Default with { MaxPackageBytes = 3 << 20 };+ var docx = Docx(P(R("x")), builder =>+ {+ var references = string.Concat(Enumerable.Range(0, 4).Select(i =>+ $"<w:headerReference w:type=\"{(i % 2 == 0 ? "default" : "even")}\" r:id=\"{builder.Header(P(R(new string('x', 1 << 20))))}\"/>"));+ builder.SectionProperties = references + DefaultPage;+ });+ var refusal = Refused(() => DocxReader.Read(docx, Media(), limits));+ Assert.Contains("The package unpacks to more than 3 MB", refusal.Detail);+ Reads(() => DocxReader.Read(docx, Media()));+ }++ [Fact]+ public void A_package_of_too_many_parts_is_refused()+ {+ var docx = Docx(P(R("x")));+ var refusal = Refused(() => DocxReader.Read(docx, Media(), DocumentLimits.Default with { MaxPackageEntries = 3 }));+ Assert.Contains("more than 3 parts", refusal.Detail);+ }++ // ───────────────────────── XML ─────────────────────────++ [Theory]+ [InlineData("docx")]+ [InlineData("hwpx")]+ public void Xml_nested_a_hundred_thousand_levels_deep_is_refused_at_once(string format)+ {+ var nested = string.Concat(Enumerable.Repeat("<x>", 100_000)) + string.Concat(Enumerable.Repeat("</x>", 100_000));+ var refusal = format == "docx"+ ? Refused(() => DocxReader.Read(Docx(P("<w:r>" + nested + "</w:r>")), Media()))+ : Refused(() => HwpxReader.Read(Hwpx(Hp(nested)), Media()));+ Assert.Contains("512 levels deep", refusal.Detail);+ }++ [Fact]+ public void Xml_of_countless_tiny_elements_is_refused_before_it_fills_the_memory()+ {+ var limits = DocumentLimits.Default with { MaxXmlElements = 10_000 };+ var docx = Docx(P(string.Concat(Enumerable.Repeat("<w:r/>", 20_000))));+ var refusal = Refused(() => DocxReader.Read(docx, Media(), limits));+ Assert.Contains("more than 10,000 elements", refusal.Detail);+ Reads(() => DocxReader.Read(docx, Media()));+ }++ [Theory]+ [InlineData("<!DOCTYPE w:document [<!ENTITY a \"aaaaaaaaaa\"><!ENTITY b \"&a;&a;&a;&a;&a;&a;&a;&a;&a;&a;\"><!ENTITY c \"&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;\"><!ENTITY d \"&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;\">]>", "&d;")]+ [InlineData("<!DOCTYPE w:document [<!ENTITY secret SYSTEM \"file:///C:/Windows/win.ini\">]>", "&secret;")]+ [InlineData("<!DOCTYPE w:document SYSTEM \"http://example.invalid/word.dtd\">", "x")]+ public void A_document_with_a_DTD_is_refused_without_expanding_or_fetching_anything(string doctype, string text)+ {+ var docx = Docx(P(R("x")));+ var path = Rewrite(docx, Path.Combine(fx.NewFolder(), "dtd.docx"), new()+ {+ ["word/document.xml"] = Text($"<?xml version=\"1.0\" encoding=\"UTF-8\"?>{doctype}<w:document {Namespaces}><w:body><w:p><w:r><w:t>{text}</w:t></w:r></w:p></w:body></w:document>"),+ });+ var error = Assert.IsType<InvalidDataException>(OnSmallStack(() => DocxReader.Read(path, Media())));+ Assert.Contains("DTD", error.Message);+ }++ [Fact]+ public void Geo_xml_still_reads_through_the_shared_loader()+ {+ using var stream = new MemoryStream("<a xmlns:p=\"urn:p\"><p:b>1 & 2</p:b><![CDATA[x]]></a>"u8.ToArray());+ var root = Filee.Engines.Geo.GeoXml.Load(stream).Root!;+ Assert.Equal("1 & 2", root.Element(XmlNamespace("urn:p") + "b")!.Value);+ Assert.Empty(root.Attributes()); // namespace declarations are not kept for the geospatial formats+ }++ private static System.Xml.Linq.XNamespace XmlNamespace(string uri) => System.Xml.Linq.XNamespace.Get(uri);++ [Fact]+ public void The_shared_loader_keeps_what_the_readers_look_up_like_XDocument_Load()+ {+ const string xml = "<?xml version=\"1.0\"?><r xmlns=\"urn:d\" xmlns:p=\"urn:p\" p:a=\"1\"><t xml:space=\"preserve\"> a <![CDATA[<b>]]> c </t><!-- note --><p:e/>\n</r>";+ using var stream = new MemoryStream(Encoding.UTF8.GetBytes(xml));+ var safe = SafeXml.Load(stream, new XmlLimits(10, 100, 10_000));+ var reference = System.Xml.Linq.XDocument.Parse(xml, System.Xml.Linq.LoadOptions.PreserveWhitespace);+ Assert.Equal(reference.Root!.Value, safe.Root!.Value);+ Assert.Equal("urn:p", safe.Root.GetNamespaceOfPrefix("p")?.NamespaceName);+ Assert.Equal(reference.Root.Attributes().Select(a => a.ToString()), safe.Root.Attributes().Select(a => a.ToString()));+ Assert.Equal(reference.Root.Elements().Select(e => e.Name), safe.Root.Elements().Select(e => e.Name));+ }++ // ───────────────────────── Nesting ─────────────────────────++ [Fact]+ public void Hwpx_tables_nested_to_the_limit_are_read_and_one_deeper_refused()+ {+ string Write(int levels)+ {+ var document = new HDocument();+ document.Sections.Add(new HSection { Blocks = { NestedTables(levels) } });+ var path = Path.Combine(fx.NewFolder(), $"nested-{levels}.hwpx");+ HwpxWriter.Write(document, path);+ return path;+ }+ HDocument? read = null;+ Reads(() => read = HwpxReader.Read(Write(32), Media()));+ Assert.Equal(32, Depth(read!.Sections[0].Blocks));+ var refusal = Refused(() => HwpxReader.ReadForLayout(Write(33), Media()));+ Assert.Contains("nested more than 32 levels deep", refusal.Detail);+ }++ [Fact]+ public void Docx_tables_nested_to_the_limit_are_read_and_one_deeper_refused()+ {+ string Write(int levels)+ {+ var document = new HDocument();+ document.Sections.Add(new HSection { Blocks = { NestedTables(levels) } });+ var path = Path.Combine(fx.NewFolder(), $"nested-{levels}.docx");+ DocxWriter.Write(document, path);+ return path;+ }+ HDocument? read = null;+ Reads(() => read = DocxReader.Read(Write(32), Media()));+ Assert.Equal(32, Depth(read!.Sections[0].Blocks));+ Refused(() => DocxReader.Read(Write(33), Media()));+ }++ [Fact]+ public void Groups_nested_without_end_are_refused()+ {+ var hwpx = Hwpx(Hp(string.Concat(Enumerable.Repeat("<hp:container>", 60)) + "<hp:rect/>" + string.Concat(Enumerable.Repeat("</hp:container>", 60))));+ Refused(() => HwpxReader.Read(hwpx, Media()));++ var group = string.Concat(Enumerable.Repeat("<wpg:grpSp>", 60)) + "<wps:wsp><wps:bodyPr/></wps:wsp>" + string.Concat(Enumerable.Repeat("</wpg:grpSp>", 60));+ var docx = Docx(P(Anchor("http://schemas.microsoft.com/office/word/2010/wordprocessingGroup", "<wpg:wgp>" + group + "</wpg:wgp>", 914400, 914400,+ "<wp:positionH relativeFrom=\"page\"><wp:posOffset>0</wp:posOffset></wp:positionH>", "<wp:positionV relativeFrom=\"page\"><wp:posOffset>0</wp:posOffset></wp:positionV>")));+ Refused(() => DocxReader.Read(docx, Media()));++ var vml = Docx(P("<w:r><w:pict>" + string.Concat(Enumerable.Repeat("<v:group>", 60)) + "<v:rect style=\"width:10pt;height:10pt\"/>" + string.Concat(Enumerable.Repeat("</v:group>", 60)) + "</w:pict></w:r>"));+ Refused(() => DocxReader.Read(vml, Media()));+ }++ [Fact]+ public void A_footnote_that_refers_to_itself_is_read_once()+ {+ // Before, reading the note read its reference, which read the note again until the stack overflowed.+ var docx = Docx(P(R("본문") + "<w:r><w:footnoteReference w:id=\"1\"/></w:r>"), builder =>+ builder.Footnotes("<w:footnote w:id=\"1\"><w:p>" + R("각주") + "<w:r><w:footnoteReference w:id=\"1\"/></w:r></w:p></w:footnote>"));+ HDocument? read = null;+ Reads(() => read = DocxReader.Read(docx, Media()));+ var note = Assert.Single(read!.Sections[0].Blocks.OfType<HParagraph>().SelectMany(p => p.Inlines).OfType<HNote>());+ Assert.Empty(note.Blocks.OfType<HParagraph>().SelectMany(p => p.Inlines).OfType<HNote>());+ }++ [Fact]+ public void Fields_that_never_end_are_refused_instead_of_slowing_every_run()+ {+ var docx = Docx(P(string.Concat(Enumerable.Repeat("<w:r><w:fldChar w:fldCharType=\"begin\"/></w:r>", 100_000)) + R("x")));+ var refusal = Refused(() => DocxReader.Read(docx, Media()));+ Assert.Contains("Fields are nested", refusal.Detail);++ // Every paragraph reopened the links of every open HWPX field: links nested as deep as the fields.+ const string Begin = "<hp:ctrl><hp:fieldBegin type=\"HYPERLINK\"><hp:parameters><hp:stringParam name=\"Command\">https\\://example.invalid;1;0;0;</hp:stringParam></hp:parameters></hp:fieldBegin></hp:ctrl>";+ var hwpx = Hwpx(Hp(string.Concat(Enumerable.Repeat(Begin, 1000))) + Hp("<hp:t>x</hp:t>"));+ Refused(() => HwpxReader.Read(hwpx, Media()));+ }++ [Fact]+ public void A_style_chain_of_a_hundred_thousand_styles_is_read_without_overflowing_the_stack()+ {+ var styles = new StringBuilder();+ for (var i = 0; i < 100_000; i++)+ styles.Append($"<w:style w:type=\"paragraph\" w:styleId=\"S{i}\"><w:basedOn w:val=\"S{i + 1}\"/><w:pPr><w:jc w:val=\"center\"/></w:pPr></w:style>");+ var docx = Docx(P(R("x"), "<w:pStyle w:val=\"S0\"/>"), builder => builder.Styles += styles.ToString());+ HDocument? read = null;+ Reads(() => read = DocxReader.Read(docx, Media()));+ Assert.Equal(HAlign.Center, read!.Sections[0].Blocks.OfType<HParagraph>().First().Format.Align);+ }++ // ───────────────────────── Counts ─────────────────────────++ [Fact]+ public void Paragraphs_and_objects_beyond_the_limits_are_refused()+ {+ var limits = DocumentLimits.Default with { MaxParagraphs = 100, MaxObjects = 10 };+ var paragraphs = Docx(string.Concat(Enumerable.Repeat(P(R("x")), 101)));+ Assert.Contains("more than 100 paragraphs", Refused(() => DocxReader.Read(paragraphs, Media(), limits)).Detail);+ var hwpxParagraphs = Hwpx(string.Concat(Enumerable.Repeat(Hp("<hp:t>x</hp:t>"), 101)));+ Assert.Contains("more than 100 paragraphs", Refused(() => HwpxReader.Read(hwpxParagraphs, Media(), null, limits)).Detail);++ var shapes = Hwpx(Hp(string.Concat(Enumerable.Repeat("<hp:rect/>", 11))));+ Assert.Contains("more than 10 tables, pictures and drawing objects", Refused(() => HwpxReader.Read(shapes, Media(), null, limits)).Detail);+ Reads(() => HwpxReader.Read(shapes, Media()));+ }++ // ───────────────────────── Pictures ─────────────────────────++ /// <summary>A PNG of 4 × 3 pixels whose header claims <paramref name="width"/> × <paramref name="height"/>.</summary>+ private static byte[] ClaimingPng(int width, int height)+ {+ var png = DocumentFuzzTests.TinyPng(4, 3);+ System.Buffers.Binary.BinaryPrimitives.WriteInt32BigEndian(png.AsSpan(16), width);+ System.Buffers.Binary.BinaryPrimitives.WriteInt32BigEndian(png.AsSpan(20), height);+ return png;+ }++ [Fact]+ public void A_picture_that_claims_ten_gigapixels_is_left_out_and_the_document_still_reads()+ {+ var huge = new DocxBuilder();+ var big = huge.Image(ClaimingPng(100_000, 100_000));+ var small = huge.Image(DocumentFuzzTests.TinyPng(4, 3));+ huge.Paragraph(P(InlinePicture(big, 914400, 914400) + InlinePicture(small, 914400, 914400) + R("글")));+ var docx = huge.Save(Path.Combine(fx.NewFolder(), "huge.docx"));+ HDocument? read = null;+ var media = Media();+ Reads(() => read = DocxReader.Read(docx, media));+ var image = Assert.Single(read!.Sections[0].Blocks.OfType<HParagraph>().SelectMany(p => p.Inlines).OfType<HImage>());+ Assert.Equal((4, 3), PictureSize.Read(image.Path));+ Assert.Single(Directory.GetFiles(media)); // the huge one was not kept on disk either++ // HWPX: the same picture through the HWPX writer, then its header made huge.+ var document = new HDocument();+ var paragraph = new HParagraph();+ var picture = Path.Combine(fx.NewFolder(), "p.png");+ File.WriteAllBytes(picture, DocumentFuzzTests.TinyPng(4, 3));+ paragraph.Inlines.Add(new HImage(picture) { Width = 1000, Height = 750 });+ document.Sections.Add(new HSection { Blocks = { paragraph } });+ var seed = Path.Combine(fx.NewFolder(), "picture.hwpx");+ HwpxWriter.Write(document, seed);+ string binData;+ using (var zip = ZipFile.OpenRead(seed))+ binData = zip.Entries.Single(e => e.FullName.StartsWith("BinData/", StringComparison.Ordinal)).FullName;+ var hwpx = Rewrite(seed, Path.Combine(fx.NewFolder(), "huge.hwpx"), new() { [binData] = s => s.Write(ClaimingPng(100_000, 100_000)) });+ Reads(() => read = HwpxReader.Read(hwpx, Media()));+ Assert.Empty(read!.Sections[0].Blocks.OfType<HParagraph>().SelectMany(p => p.Inlines).OfType<HImage>());+ }++ [Fact]+ public void Picture_sizes_are_read_from_the_headers_of_every_common_format()+ {+ (int, int)? Size(byte[] bytes) => PictureSize.Read(new MemoryStream(bytes));+ Assert.Equal((4, 3), Size(DocumentFuzzTests.TinyPng(4, 3)));+ Assert.Equal((100_000, 70_000), Size(ClaimingPng(100_000, 70_000)));+ Assert.Equal((640, 480), Size([.. "GIF89a"u8, 0x80, 0x02, 0xE0, 0x01, 0, 0, 0]));+ // BMP: BITMAPINFOHEADER with a negative (top-down) height.+ var bmp = new byte[54];+ "BM"u8.CopyTo(bmp);+ BitConverter.TryWriteBytes(bmp.AsSpan(14), 40);+ BitConverter.TryWriteBytes(bmp.AsSpan(18), 300);+ BitConverter.TryWriteBytes(bmp.AsSpan(22), -200);+ Assert.Equal((300, 200), Size(bmp));+ // JPEG: an APP0 segment, then the frame header (SOF0) with height 1080 and width 1920.+ byte[] jpeg = [0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, .. new byte[14], 0xFF, 0xC0, 0x00, 0x11, 0x08, 0x04, 0x38, 0x07, 0x80, 0x03, .. new byte[9], 0xFF, 0xD9];+ Assert.Equal((1920, 1080), Size(jpeg));+ // WebP: VP8X with a canvas of 5000 × 4000.+ var webp = new byte[30];+ "RIFF"u8.CopyTo(webp);+ "WEBPVP8X"u8.CopyTo(webp.AsSpan(8));+ webp[24] = (5000 - 1) & 0xFF;+ webp[25] = (5000 - 1) >> 8;+ webp[27] = (4000 - 1) & 0xFF;+ webp[28] = (4000 - 1) >> 8;+ Assert.Equal((5000, 4000), Size(webp));+ // Unknown, cut short or endless headers give no size.+ Assert.Null(Size("%PDF-1.7"u8.ToArray()));+ Assert.Null(Size([0x89, (byte)'P', (byte)'N', (byte)'G']));+ Assert.Null(Size([0xFF, 0xD8, .. Enumerable.Repeat<byte>(0xFF, 5000)]));+ }++ [Fact]+ public void Part_names_never_become_file_names()+ {+ Assert.Equal(".png", DocumentPackage.SafeExtension("word/media/image1.PNG"));+ Assert.Equal("", DocumentPackage.SafeExtension("media/image.png:stream"));+ Assert.Equal("", DocumentPackage.SafeExtension("../../evil"));+ Assert.Equal("", DocumentPackage.SafeExtension("x.p/n\\g"));+ Assert.Equal("", DocumentPackage.SafeExtension("x." + new string('a', 9)));+ Assert.Equal("", DocumentPackage.SafeExtension("noextension"));++ // A relationship target with a stream name and a path still lands as docx-media-1 in the media folder.+ var builder = new DocxBuilder();+ var id = builder.Image(DocumentFuzzTests.TinyPng(4, 3));+ builder.Paragraph(P(InlinePicture(id, 914400, 914400)));+ var docx = builder.Save(Path.Combine(fx.NewFolder(), "names.docx"));+ var renamed = Path.Combine(fx.NewFolder(), "renamed.docx");+ using (var input = ZipFile.OpenRead(docx))+ using (var output = ZipFile.Open(renamed, ZipArchiveMode.Create))+ {+ foreach (var entry in input.Entries)+ {+ var name = entry.FullName == "word/media/image1.png" ? "word/media/image1.png:evil" : entry.FullName;+ using var from = entry.Open();+ var text = name.EndsWith(".rels", StringComparison.Ordinal) ? new StreamReader(from).ReadToEnd().Replace("media/image1.png", "media/image1.png:evil", StringComparison.Ordinal) : null;+ using var to = output.CreateEntry(name).Open();+ if (text is null)+ from.CopyTo(to);+ else+ to.Write(Encoding.UTF8.GetBytes(text));+ }+ }+ var media = Media();+ Reads(() => DocxReader.Read(renamed, media));+ Assert.Equal(["docx-media-1"], Directory.GetFiles(media).Select(Path.GetFileName));+ }++ [Fact]+ public void Embedded_fonts_are_not_read()+ {+ var docx = Docx(P(R("x", "<w:rFonts w:ascii=\"Hostile\"/>")));+ var withFont = Rewrite(docx, Path.Combine(fx.NewFolder(), "font.docx"), new()+ {+ ["word/fonts/font1.odttf"] = s => s.Write(new byte[4096]),+ ["word/fontTable.xml"] = Text($"<w:fonts {Namespaces}><w:font w:name=\"Hostile\"><w:embedRegular r:id=\"rIdFont\" w:fontKey=\"{{00000000-0000-0000-0000-000000000000}}\"/></w:font></w:fonts>"),+ });+ var media = Media();+ Reads(() => DocxReader.Read(withFont, media));+ Assert.False(Directory.Exists(media) && Directory.EnumerateFiles(media).Any());+ }++ // ───────────────────────── What the fuzzer found ─────────────────────────++ [Fact]+ public void Fuzz_finding_two_numberings_with_one_id_read_with_the_first()+ {+ // ArgumentException: an item with the same key has already been added (DocxReader.LoadNumbering).+ var docx = Docx(P(R("항목"), "<w:numPr><w:ilvl w:val=\"0\"/><w:numId w:val=\"1\"/></w:numPr>"), builder => builder.Numbering =+ "<w:abstractNum w:abstractNumId=\"0\"><w:lvl w:ilvl=\"0\"><w:numFmt w:val=\"decimal\"/><w:lvlText w:val=\"%1)\"/></w:lvl></w:abstractNum>" ++ "<w:abstractNum w:abstractNumId=\"0\"><w:lvl w:ilvl=\"0\"><w:numFmt w:val=\"bullet\"/><w:lvlText w:val=\"*\"/></w:lvl></w:abstractNum>" ++ "<w:num w:numId=\"1\"><w:abstractNumId w:val=\"0\"/></w:num>");+ HDocument? read = null;+ Reads(() => read = DocxReader.Read(docx, Media()));+ var list = read!.Sections[0].Blocks.OfType<HParagraph>().First().List;+ Assert.Equal("^1)", list!.Numbering.Levels[0].Text);+ }++ [Theory]+ [InlineData("-1e308")]+ [InlineData("-2147483648")]+ [InlineData("-9223372036854775808")]+ public void Fuzz_finding_page_sizes_at_the_end_of_the_number_range_read(string value)+ {+ // OverflowException: negating the minimum value of a twos complement number (Math.Abs in DocxReader.Page).+ var docx = Docx(P(R("x")), builder => builder.SectionProperties =+ $"<w:pgSz w:w=\"{value}\" w:h=\"16838\"/><w:pgMar w:top=\"{value}\" w:right=\"1440\" w:bottom=\"1440\" w:left=\"1440\" w:header=\"720\" w:footer=\"720\" w:gutter=\"0\"/>");+ Reads(() => DocxReader.Read(docx, Media()));+ // VML line ends at both ends of the range (Math.Abs of their difference).+ var line = Docx(P("<w:r><w:pict><v:line from=\"-99999999999in,0\" to=\"0,0\"/></w:pict></w:r>"));+ Reads(() => DocxReader.Read(line, Media()));+ }++ [Fact]+ public void Fuzz_finding_damaged_xml_in_a_docx_is_invalid_data()+ {+ // XmlException escaped DocxReader for every part (document, relationships, core properties).+ foreach (var part in new[] { "word/document.xml", "word/_rels/document.xml.rels", "docProps/core.xml" })+ {+ var path = Rewrite(Docx(P(R("x"))), Path.Combine(fx.NewFolder(), "damaged.docx"), new() { [part] = Text("<a><b></a>") });+ var error = Assert.IsType<InvalidDataException>(OnSmallStack(() => DocxReader.Read(path, Media())));+ Assert.Contains(part, error.Message);+ }+ }++ [Theory]+ [InlineData("colCnt=\"2147483647\"", "colAddr=\"0\"", "colSpan=\"1\"")] // fuzzer: OutOfMemoryException, array dimensions exceeded+ [InlineData("colCnt=\"2\"", "colAddr=\"2147483647\"", "colSpan=\"2147483647\"")] // fuzzer: ArgumentOutOfRangeException in ColumnWidths+ [InlineData("colCnt=\"3\"", "colAddr=\"0\"", "colSpan=\"3\"")] // found with them: the widths of the cells below overflowed their sum+ public void Fuzz_finding_hwpx_tables_with_column_numbers_at_the_end_of_the_range_read(string count, string address, string span)+ {+ static string Cell(string text, int column, int row, string width) =>+ $"<hp:tc><hp:subList><hp:p><hp:run><hp:t>{text}</hp:t></hp:run></hp:p></hp:subList><hp:cellAddr colAddr=\"{column}\" rowAddr=\"{row}\"/>" ++ $"<hp:cellSpan colSpan=\"1\" rowSpan=\"1\"/><hp:cellSz width=\"{width}\" height=\"1000\"/></hp:tc>";+ var cell = $"<hp:tc><hp:subList><hp:p><hp:run><hp:t>칸</hp:t></hp:run></hp:p></hp:subList><hp:cellAddr {address} rowAddr=\"0\"/>" ++ $"<hp:cellSpan {span} rowSpan=\"2147483647\"/><hp:cellSz width=\"2147483647\" height=\"1000\"/></hp:tc>";+ var below = Cell("둘", 0, 1, "2147483647") + Cell("셋", 1, 1, "2147483647");+ var hwpx = Hwpx(Hp($"<hp:tbl {count} rowCnt=\"2\"><hp:tr>{cell}</hp:tr><hp:tr>{below}</hp:tr></hp:tbl>"));+ HDocument? read = null;+ Reads(() => read = HwpxReader.Read(hwpx, Media()));+ var table = read!.Sections[0].Blocks.OfType<HTable>().Single();+ Assert.InRange(table.ColumnCount, 1, DocumentLimits.Default.MaxTableColumns);+ Assert.Equal(2, table.Rows[0].Cells[0].RowSpan); // no further than the last row+ }++ // ───────────────────────── Layout guards ─────────────────────────++ [Fact]+ public void The_layout_stops_at_the_page_limit_and_after_too_many_empty_pages()+ {+ var limits = DocumentLimits.Default with { MaxPages = 5, MaxConsecutiveEmptyPages = 2 };+ var guard = new LayoutGuard(limits, CancellationToken.None);+ for (var i = 0; i < 5; i++)+ guard.PageFinished(empty: false);+ Assert.Contains("more than 5 pages", Assert.Throws<ConversionRefusedException>(() => guard.PageFinished(empty: false)).Detail);++ var stuck = new LayoutGuard(limits with { MaxPages = 100 }, CancellationToken.None);+ stuck.PageFinished(empty: true);+ stuck.PageFinished(empty: true);+ stuck.PageFinished(empty: false); // something placed: the count starts again+ stuck.PageFinished(empty: true);+ stuck.PageFinished(empty: true);+ Assert.Contains("more than 2 empty pages in a row", Assert.Throws<ConversionRefusedException>(() => stuck.PageFinished(empty: true)).Detail);+ }++ [Fact]+ public void Every_paragraph_checks_cancellation_and_the_time_budget()+ {+ var elapsed = TimeSpan.Zero;+ var guard = new LayoutGuard(DocumentLimits.Default with { LayoutTimeBudget = TimeSpan.FromMinutes(1) }, CancellationToken.None, () => elapsed);+ guard.Paragraph();+ elapsed = TimeSpan.FromSeconds(61);+ var refusal = Assert.Throws<ConversionRefusedException>(guard.Paragraph);+ Assert.Equal(DocumentLimits.ErrorKey, refusal.ErrorKey);+ Assert.Contains("longer than 1 minute.", refusal.Detail);++ using var cancelled = new CancellationTokenSource();+ var cancellable = new LayoutGuard(DocumentLimits.Default, cancelled.Token);+ cancellable.Paragraph();+ cancelled.Cancel();+ Assert.Throws<OperationCanceledException>(cancellable.Paragraph);+ }++ [Theory]+ [InlineData(100, 50, 200, 300, 100, 50, false)] // fits+ [InlineData(500, 50, 200, 300, 200, 50, true)] // too wide: clipped to the column+ [InlineData(100, 9e9, 200, 300, 100, 300, true)] // taller than any page: clipped and placed, not moved on+ [InlineData(double.NaN, double.PositiveInfinity, 200, 300, 0, 0, false)] // sizes that are no numbers+ [InlineData(-5, 10, 200, 300, 0, 10, false)]+ public void An_object_that_fits_no_page_is_clipped_and_placed(double width, double height, double areaWidth, double areaHeight,+ double placedWidth, double placedHeight, bool clipped)+ {+ Assert.Equal((placedWidth, placedHeight, clipped), LayoutGuard.Fit(width, height, areaWidth, areaHeight));+ }+}diff --git a/tests/Filee.Engines.Tests/GeoHostileInputTests.cs b/tests/Filee.Engines.Tests/GeoHostileInputTests.csindex 95daaea9..8b141e41 100644--- a/tests/Filee.Engines.Tests/GeoHostileInputTests.cs+++ b/tests/Filee.Engines.Tests/GeoHostileInputTests.cs@@ -7,6 +7,7 @@ using System.IO.Compression;using System.Text;using Filee.Core.Conversion;using Filee.Engines.Geo;+using Filee.Engines.Infrastructure;namespace Filee.Engines.Tests;diff --git a/tests/Filee.Engines.Tests/HwpxReaderTests.cs b/tests/Filee.Engines.Tests/HwpxReaderTests.csindex 9b44da77..e9a83e84 100644--- a/tests/Filee.Engines.Tests/HwpxReaderTests.cs+++ b/tests/Filee.Engines.Tests/HwpxReaderTests.cs@@ -636,7 +636,7 @@ public class HwpxReaderTests(EngineFixture fx) : IClassFixture<EngineFixture>/// one missing), a group, equation, compose, ruby text, notes, a chart with an OLE fallback, and in the second/// section other namespace prefixes./// </summary>- private static string HangulStylePackage(string folder)+ internal static string HangulStylePackage(string folder){const string Namespaces ="xmlns:ha=\"http://www.hancom.co.kr/hwpml/2011/app\" xmlns:hp=\"http://www.hancom.co.kr/hwpml/2011/paragraph\" " +