From 458abd03eb5129a5561ba1874d8acc1bd0759478 Mon Sep 17 00:00:00 2001 From: Stephen Masters Date: Tue, 3 Nov 2026 16:36:59 +0000 Subject: [PATCH] feat: a policy on a TIFF's tags, and on tags that contradict each other Two fields on Policy, both allow-by-default so that no file that passed before is refused: - tiff_tags: which kinds of tag a TIFF's directories may hold. The reader sorts every tag into standard (the TIFF 6.0 set and the few later additions every writer uses), metadata (XMP, IPTC, Photoshop, Exif, ICC, GPS, GeoTIFF, GDAL and the like) or private (everything else: vendor and DNG tags). A program that saves a file back as TIFF with its tag table carries every tag into libtiff's encoder, where a crafted private tag copied process memory into the output (CVE-2026-112902); such a program allows standard and metadata only. - allow_inconsistent_tiff_tags: the reader now reads ColorMap's count, Predictor and SampleFormat, and reports a ColorMap with the wrong number of entries for the bit depth (CVE-2026-112896) or a floating-point Predictor on integer samples (CVE-2026-112396); a policy for files from outside refuses them. Facts and Header carry `tags` and `inconsistencies`; the command line has --tiff-tags and --no-inconsistent-tiff-tags; README and the API page document both. The three B proposals that asked for this are promoted into the record with their tests, which empties the backfill's proposals. Co-authored-by: Yvette Mankerian Co-Authored-By: Claude Fable 5.1 --- README.md | 8 +- docs/advisories/proposed/CVE-2026-112396.json | 30 ----- docs/advisories/proposed/CVE-2026-112896.json | 30 ----- docs/advisories/proposed/CVE-2026-112902.json | 29 ----- docs/advisories/record.json | 113 +++++++++++++++++- docs/advisories/record/CVE-2026-112396.json | 36 ++++++ docs/advisories/record/CVE-2026-112896.json | 36 ++++++ docs/advisories/record/CVE-2026-112902.json | 35 ++++++ docs/api.md | 2 + src/sillybos/__init__.py | 2 + src/sillybos/check.py | 20 ++++ src/sillybos/cli.py | 18 +++ src/sillybos/headers.py | 85 +++++++++++++ src/sillybos/policy.py | 29 +++++ tests/test_check.py | 49 ++++++++ tests/test_cli.py | 14 +++ tests/test_headers.py | 49 ++++++++ 17 files changed, 491 insertions(+), 94 deletions(-) delete mode 100644 docs/advisories/proposed/CVE-2026-112396.json delete mode 100644 docs/advisories/proposed/CVE-2026-112896.json delete mode 100644 docs/advisories/proposed/CVE-2026-112902.json create mode 100644 docs/advisories/record/CVE-2026-112396.json create mode 100644 docs/advisories/record/CVE-2026-112896.json create mode 100644 docs/advisories/record/CVE-2026-112902.json diff --git a/README.md b/README.md index 4fed9a3..62773fc 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,8 @@ check(path, Policy(formats={"tiff", "pdf"}, max_pages=50)) | `tiff_subsamplings` | 1x1, 2x1, 1x2, 2x2 | the same | Which chroma subsamplings a YCbCr TIFF may declare. The factor-of-four paths are where libtiff's most recent heap write was found (CVE-2026-4775) | | `tiff_compressions` | none, ccitt, lzw, jpeg, deflate, packbits | the same | How a TIFF may be compressed, by name. The other codecs in libtiff are where most of its flaws have been found (PixarLog most recently, CVE-2026-12912), and a scan never needs them. A code the library does not know is refused whatever the policy says, and every frame must use the same scheme | | `jpeg_frames` | baseline, extended, progressive | the same | How a JPEG's frame may be coded. Lossless, hierarchical and arithmetic frames are legal JPEG that no scanner writes and the decoder's least-exercised code; a lossless frame was the way into a heap overflow in Pillow's JPEG decoder (CVE-2026-112403) | +| `tiff_tags` | standard, metadata, private | the same | Which kinds of tag a TIFF's directories may hold. Standard is the TIFF 6.0 set; metadata the registered blocks (XMP, IPTC, Photoshop, Exif, ICC, GPS, GeoTIFF); private everything else, the vendor and DNG tags a scan never carries. A read is not harmed by a private tag, so the default allows all three; a program that saves a file back as TIFF with its tag table carries every tag into libtiff's encoder, where a crafted one copied process memory into the output (CVE-2026-112902), and should allow standard and metadata only | +| `allow_inconsistent_tiff_tags` | yes | yes | Pass a TIFF whose tags contradict each other: a ColorMap of the wrong size for the bit depth (CVE-2026-112896), a floating-point Predictor on integer samples (CVE-2026-112396). No scanner writes either; both reach an encoder that trusts the tag. Allowed by default because refusing a file that passed before is breaking; a policy for files from outside should refuse | | `allow_encrypted_pdf` | no | no | A scanned book is never encrypted | | `pdf_features` | none | none | Which of JavaScript, forms, attached files and launch actions a PDF may carry. Each makes a viewer do something other than draw the page, and a scanned book has none. Forms are read from `pdfinfo`'s report and attached files from `pdfdetach`'s; a launch action is found by its name in the file, since no poppler tool reports one; JavaScript is looked for both ways | | `pdf_image_codecs` | jpeg, jpx, jbig2, ccitt | the same | Which codecs a PDF's images may be coded with: JPEG, JPEG 2000 (OpenJPEG), JBIG2 and CCITT fax. Each hands the stream to a decoder of its own inside poppler, and the JBIG2 decoder is where several of its flaws have been (CVE-2025-32365, CVE-2026-93312, CVE-2026-93313). Scanners and the archives that derive PDFs from scans write all four, so both presets allow them; refuse one only when you know your source never writes it. Found by the filter's name in the file, not by `pdfimages -list`, which opens a JPX stream with OpenJPEG just to describe it | @@ -180,8 +182,10 @@ prints one object per line for a pipeline; `-q` prints only refusals. layout (photometric interpretation and, for YCbCr, chroma subsampling, both of which the policy names; samples per pixel; bits per sample), all of which must be the same in every frame, since the decoder sizes - its buffers from the first, its tile size if it is tiled, and every - tag's data checked to lie inside the file; the chain is walked with a limit and a memory of where it has + its buffers from the first, its tile size if it is tiled, every + tag's data checked to lie inside the file, every tag sorted into + standard, metadata or private, and its ColorMap and Predictor checked + against its samples; the chain is walked with a limit and a memory of where it has been, so that one that loops or points off the end of the file is a refusal, not a hang. Every read is bounded and every offset is checked against the file's size. For a PDF, diff --git a/docs/advisories/proposed/CVE-2026-112396.json b/docs/advisories/proposed/CVE-2026-112396.json deleted file mode 100644 index e913631..0000000 --- a/docs/advisories/proposed/CVE-2026-112396.json +++ /dev/null @@ -1,30 +0,0 @@ -{ - "key": "CVE-2026-112396", - "aliases": [], - "library": "Pillow", - "package": "pillow", - "ecosystem": "PyPI", - "title": "Encoding a TIFF to memory uses a freed buffer when libtiff rejects a tag", - "summary": "Pillow: Heap use-after-free when encoding TIFF to an in-memory buffer", - "published": "2026-10-19", - "severity": 5.9, - "affected": "< 12.3.0", - "fixed": "12.3.0", - "reached_by": "save", - "trigger": "Image.save(format=\"TIFF\") to an in-memory buffer with a tag libtiff rejects.", - "bucket": "B", - "finding": "The rejected tag, a floating-point Predictor on integer samples in the report's example, rides into the save when the program keeps the opened file's tag table, and is visible in the directory the reader walks. A policy on a TIFF's tags, refusing private and unknown tags by name and a ColorMap or Predictor that does not fit the samples, would keep the file from the encoder; it is not built, and these three proposals are its ticket.", - "refusal": null, - "verified_by": [], - "protect": { - "general": "Upgrade to Pillow 12.3.0.", - "specific": "Do not save a file from outside back to TIFF with its tag table, and save to a file rather than memory; upgrade to Pillow 12.3.0." - }, - "sources": [ - "https://osv.dev/vulnerability/GHSA-4rr5-89j8-mwwm", - "https://osv.dev/vulnerability/PYSEC-2026-4328" - ], - "triaged": "2026-11-01", - "checked": "2026-11-01", - "notes": "Proposed from the advisory's text on 2026-11-01, as part of folding the backfill into the record; not yet confirmed by a person. Re-read on 2026-11-03 against the question of what rides from an opened file into a save: the file is the input. Bucket and finding confirmed by a person on 2026-11-03; waits for the policy on a TIFF's tags and its test before it can be promoted." -} diff --git a/docs/advisories/proposed/CVE-2026-112896.json b/docs/advisories/proposed/CVE-2026-112896.json deleted file mode 100644 index 680cf1b..0000000 --- a/docs/advisories/proposed/CVE-2026-112896.json +++ /dev/null @@ -1,30 +0,0 @@ -{ - "key": "CVE-2026-112896", - "aliases": [], - "library": "Pillow", - "package": "pillow", - "ecosystem": "PyPI", - "title": "Saving a TIFF with a retained ColorMap over-reads in Pillow's encoder", - "summary": "Pillow: TIFF encoder ColorMap plane-size mismatch causes a heap out-of-bounds read", - "published": "2026-10-19", - "severity": null, - "affected": "< 12.3.0", - "fixed": "12.3.0", - "reached_by": "save", - "trigger": "Opening a 16-bit palette TIFF with a 768-entry ColorMap and saving it back as TIFF with its metadata.", - "bucket": "B", - "finding": "The ColorMap rides from the opened file's tag table into a save that keeps its metadata. The tag is in the directory the reader already walks, and a ColorMap whose 768 entries disagree with 16-bit samples, which need 65,536 per plane, is visible there. A policy on a TIFF's tags, refusing private and unknown tags by name and a ColorMap or Predictor that does not fit the samples, would keep the file from the encoder; it is not built, and these three proposals are its ticket.", - "refusal": null, - "verified_by": [], - "protect": { - "general": "Upgrade to Pillow 12.3.0.", - "specific": "Do not save a file from outside back to TIFF with its tag table; upgrade to Pillow 12.3.0." - }, - "sources": [ - "https://osv.dev/vulnerability/GHSA-pchr-8j38-3x4p", - "https://osv.dev/vulnerability/PYSEC-2026-4334" - ], - "triaged": "2026-11-01", - "checked": "2026-11-01", - "notes": "Proposed from the advisory's text on 2026-11-01, as part of folding the backfill into the record; not yet confirmed by a person. Re-read on 2026-11-03 against the question of what rides from an opened file into a save: the file is the input. Bucket and finding confirmed by a person on 2026-11-03; waits for the policy on a TIFF's tags and its test before it can be promoted." -} diff --git a/docs/advisories/proposed/CVE-2026-112902.json b/docs/advisories/proposed/CVE-2026-112902.json deleted file mode 100644 index a533aaf..0000000 --- a/docs/advisories/proposed/CVE-2026-112902.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "key": "CVE-2026-112902", - "aliases": [], - "library": "Pillow", - "package": "pillow", - "ecosystem": "PyPI", - "title": "Crafted TIFF tags make Pillow's libtiff encoder copy process memory into the output", - "summary": "Pillow: Memory disclosure and segfault in libtiff encoder via crafted TIFF tags", - "published": "2026-10-21", - "severity": null, - "affected": "< 12.3.0", - "fixed": "12.3.0", - "reached_by": "save", - "trigger": "Opening a crafted TIFF and saving it back as TIFF with its tags.", - "bucket": "B", - "finding": "The crafted tags (DNGPrivateData and ActiveArea, private and DNG tags a scan never carries) ride from the opened file's tag table into a bare save, through libtiff whenever the file is compressed, with no option from the program. The tags are in the directory the reader already walks. A policy on a TIFF's tags, refusing private and unknown tags by name and a ColorMap or Predictor that does not fit the samples, would keep the file from the encoder; it is not built, and these three proposals are its ticket. Tetrak OCR copies each frame before saving, which drops the tag table, so it is not reached there.", - "refusal": null, - "verified_by": [], - "protect": { - "general": "Upgrade to Pillow 12.3.0.", - "specific": "Do not save a file from outside back to TIFF with its tag table; upgrade to Pillow 12.3.0. Until the tag policy exists, a boxed process bounds the damage." - }, - "sources": [ - "https://osv.dev/vulnerability/GHSA-xvh5-g6gv-559v" - ], - "triaged": "2026-11-01", - "checked": "2026-11-01", - "notes": "Proposed from the advisory's text on 2026-11-01, as part of folding the backfill into the record; not yet confirmed by a person. Re-read on 2026-11-03 against the question of what rides from an opened file into a save: the file is the input. Bucket and finding confirmed by a person on 2026-11-03; waits for the policy on a TIFF's tags and its test before it can be promoted." -} diff --git a/docs/advisories/record.json b/docs/advisories/record.json index 787a819..4cec002 100644 --- a/docs/advisories/record.json +++ b/docs/advisories/record.json @@ -17,11 +17,11 @@ "other": "some other way; the finding says" }, "counts": { - "records": 120, + "records": 123, "refused_by_default": 44, "by_bucket": { "A": 39, - "B": 16, + "B": 19, "B2": 3, "C": 19, "D": 3, @@ -31,7 +31,7 @@ "by_library": { "FreeType": 4, "OpenJPEG": 6, - "Pillow": 58, + "Pillow": 61, "libpng": 15, "libtiff": 18, "poppler": 16, @@ -528,6 +528,41 @@ "checked": "2026-11-03", "notes": "Proposed from the advisory's text on 2026-11-01, as part of folding the backfill into the record; confirmed by a person on 2026-11-03. Re-read on 2026-11-03: no file carries the trigger." }, + { + "key": "CVE-2026-112902", + "aliases": [], + "library": "Pillow", + "package": "pillow", + "ecosystem": "PyPI", + "title": "Crafted TIFF tags make Pillow's libtiff encoder copy process memory into the output", + "summary": "Pillow: Memory disclosure and segfault in libtiff encoder via crafted TIFF tags", + "published": "2026-10-21", + "severity": null, + "affected": "< 12.3.0", + "fixed": "12.3.0", + "reached_by": "save", + "trigger": "Opening a crafted TIFF and saving it back as TIFF with its tags.", + "bucket": "B", + "finding": "The crafted tags (DNGPrivateData and ActiveArea, private and DNG tags a scan never carries) ride from the opened file's tag table into a bare save, through libtiff whenever the file is compressed, with no option from the program. The tags are in the directory the reader walks, and every tag is sorted into standard, metadata or private: a policy allowing standard and metadata tags refuses the file before Pillow sees it (tiff_tags). The default allows private tags, since a vendor's own tag in a scanner's output is harmless on a read, so the setting is what closes this. Tetrak OCR copies each frame before saving, which drops the tag table, so it is not reached there.", + "refusal": { + "check": "tiff_tags", + "default": false, + "setting": "Policy(tiff_tags={\"standard\", \"metadata\"})" + }, + "verified_by": [ + "tests/test_check.py::TestHostileFiles::test_a_tiff_with_private_tags_under_a_policy_without_them" + ], + "protect": { + "general": "Upgrade to Pillow 12.3.0.", + "specific": "Allow standard and metadata tags only (tiff_tags) wherever a file from outside is saved back as TIFF with its tag table; upgrade to Pillow 12.3.0." + }, + "sources": [ + "https://osv.dev/vulnerability/GHSA-xvh5-g6gv-559v" + ], + "triaged": "2026-11-03", + "checked": "2026-11-03", + "notes": "Proposed from the advisory's text on 2026-11-01, as part of folding the backfill into the record; confirmed by a person on 2026-11-03. Re-read on 2026-11-03 against the question of what rides from an opened file into a save: the file is the input. Bucket and finding confirmed by a person on 2026-11-03; waits for the policy on a TIFF's tags and its test before it can be promoted. The policy was built on 2026-11-03 (sillybos 4.2.0)." + }, { "triaged": "2026-11-01", "checked": "2026-11-01", @@ -1225,6 +1260,42 @@ "https://github.com/advisories/GHSA-462x-pgg6-55fp" ] }, + { + "key": "CVE-2026-112896", + "aliases": [], + "library": "Pillow", + "package": "pillow", + "ecosystem": "PyPI", + "title": "Saving a TIFF with a retained ColorMap over-reads in Pillow's encoder", + "summary": "Pillow: TIFF encoder ColorMap plane-size mismatch causes a heap out-of-bounds read", + "published": "2026-10-19", + "severity": null, + "affected": "< 12.3.0", + "fixed": "12.3.0", + "reached_by": "save", + "trigger": "Opening a 16-bit palette TIFF with a 768-entry ColorMap and saving it back as TIFF with its metadata.", + "bucket": "B", + "finding": "The ColorMap rides from the opened file's tag table into a save that keeps its metadata. The tag is in the directory the reader walks, and its 768 entries disagree with 16-bit samples, which need 196,608: the reader reports the contradiction, and a policy that refuses inconsistent tags refuses the file before Pillow sees it (allow_inconsistent_tiff_tags). The default allows it, because refusing a file that passed before is breaking, so the setting is what closes this.", + "refusal": { + "check": "allow_inconsistent_tiff_tags", + "default": false, + "setting": "Policy(allow_inconsistent_tiff_tags=False)" + }, + "verified_by": [ + "tests/test_check.py::TestHostileFiles::test_a_tiff_whose_colormap_does_not_fit_its_depth" + ], + "protect": { + "general": "Upgrade to Pillow 12.3.0.", + "specific": "Refuse inconsistent TIFF tags (allow_inconsistent_tiff_tags off) for files from outside; upgrade to Pillow 12.3.0." + }, + "sources": [ + "https://osv.dev/vulnerability/GHSA-pchr-8j38-3x4p", + "https://osv.dev/vulnerability/PYSEC-2026-4334" + ], + "triaged": "2026-11-03", + "checked": "2026-11-03", + "notes": "Proposed from the advisory's text on 2026-11-01, as part of folding the backfill into the record; confirmed by a person on 2026-11-03. Re-read on 2026-11-03 against the question of what rides from an opened file into a save: the file is the input. Bucket and finding confirmed by a person on 2026-11-03; waits for the policy on a TIFF's tags and its test before it can be promoted. The policy was built on 2026-11-03 (sillybos 4.2.0)." + }, { "key": "CVE-2026-112405", "aliases": [], @@ -1328,6 +1399,42 @@ "checked": "2026-11-03", "notes": "Proposed from the advisory's text on 2026-11-01, as part of folding the backfill into the record; confirmed by a person on 2026-11-03." }, + { + "key": "CVE-2026-112396", + "aliases": [], + "library": "Pillow", + "package": "pillow", + "ecosystem": "PyPI", + "title": "Encoding a TIFF to memory uses a freed buffer when libtiff rejects a tag", + "summary": "Pillow: Heap use-after-free when encoding TIFF to an in-memory buffer", + "published": "2026-10-19", + "severity": 5.9, + "affected": "< 12.3.0", + "fixed": "12.3.0", + "reached_by": "save", + "trigger": "Image.save(format=\"TIFF\") to an in-memory buffer with a tag libtiff rejects.", + "bucket": "B", + "finding": "The rejected tag, a floating-point Predictor on integer samples in the report's example, rides into the save when the program keeps the opened file's tag table. The reader reads the Predictor and the SampleFormat and reports the contradiction, and a policy that refuses inconsistent tags refuses the file before Pillow sees it (allow_inconsistent_tiff_tags). The default allows it, because refusing a file that passed before is breaking, so the setting is what closes this.", + "refusal": { + "check": "allow_inconsistent_tiff_tags", + "default": false, + "setting": "Policy(allow_inconsistent_tiff_tags=False)" + }, + "verified_by": [ + "tests/test_check.py::TestHostileFiles::test_a_tiff_with_a_floating_point_predictor_on_integer_samples" + ], + "protect": { + "general": "Upgrade to Pillow 12.3.0.", + "specific": "Refuse inconsistent TIFF tags (allow_inconsistent_tiff_tags off) for files from outside, and save to a file rather than memory; upgrade to Pillow 12.3.0." + }, + "sources": [ + "https://osv.dev/vulnerability/GHSA-4rr5-89j8-mwwm", + "https://osv.dev/vulnerability/PYSEC-2026-4328" + ], + "triaged": "2026-11-03", + "checked": "2026-11-03", + "notes": "Proposed from the advisory's text on 2026-11-01, as part of folding the backfill into the record; confirmed by a person on 2026-11-03. Re-read on 2026-11-03 against the question of what rides from an opened file into a save: the file is the input. Bucket and finding confirmed by a person on 2026-11-03; waits for the policy on a TIFF's tags and its test before it can be promoted. The policy was built on 2026-11-03 (sillybos 4.2.0)." + }, { "key": "CVE-2026-112394", "aliases": [], diff --git a/docs/advisories/record/CVE-2026-112396.json b/docs/advisories/record/CVE-2026-112396.json new file mode 100644 index 0000000..e995e29 --- /dev/null +++ b/docs/advisories/record/CVE-2026-112396.json @@ -0,0 +1,36 @@ +{ + "key": "CVE-2026-112396", + "aliases": [], + "library": "Pillow", + "package": "pillow", + "ecosystem": "PyPI", + "title": "Encoding a TIFF to memory uses a freed buffer when libtiff rejects a tag", + "summary": "Pillow: Heap use-after-free when encoding TIFF to an in-memory buffer", + "published": "2026-10-19", + "severity": 5.9, + "affected": "< 12.3.0", + "fixed": "12.3.0", + "reached_by": "save", + "trigger": "Image.save(format=\"TIFF\") to an in-memory buffer with a tag libtiff rejects.", + "bucket": "B", + "finding": "The rejected tag, a floating-point Predictor on integer samples in the report's example, rides into the save when the program keeps the opened file's tag table. The reader reads the Predictor and the SampleFormat and reports the contradiction, and a policy that refuses inconsistent tags refuses the file before Pillow sees it (allow_inconsistent_tiff_tags). The default allows it, because refusing a file that passed before is breaking, so the setting is what closes this.", + "refusal": { + "check": "allow_inconsistent_tiff_tags", + "default": false, + "setting": "Policy(allow_inconsistent_tiff_tags=False)" + }, + "verified_by": [ + "tests/test_check.py::TestHostileFiles::test_a_tiff_with_a_floating_point_predictor_on_integer_samples" + ], + "protect": { + "general": "Upgrade to Pillow 12.3.0.", + "specific": "Refuse inconsistent TIFF tags (allow_inconsistent_tiff_tags off) for files from outside, and save to a file rather than memory; upgrade to Pillow 12.3.0." + }, + "sources": [ + "https://osv.dev/vulnerability/GHSA-4rr5-89j8-mwwm", + "https://osv.dev/vulnerability/PYSEC-2026-4328" + ], + "triaged": "2026-11-03", + "checked": "2026-11-03", + "notes": "Proposed from the advisory's text on 2026-11-01, as part of folding the backfill into the record; confirmed by a person on 2026-11-03. Re-read on 2026-11-03 against the question of what rides from an opened file into a save: the file is the input. Bucket and finding confirmed by a person on 2026-11-03; waits for the policy on a TIFF's tags and its test before it can be promoted. The policy was built on 2026-11-03 (sillybos 4.2.0)." +} diff --git a/docs/advisories/record/CVE-2026-112896.json b/docs/advisories/record/CVE-2026-112896.json new file mode 100644 index 0000000..cc37bc7 --- /dev/null +++ b/docs/advisories/record/CVE-2026-112896.json @@ -0,0 +1,36 @@ +{ + "key": "CVE-2026-112896", + "aliases": [], + "library": "Pillow", + "package": "pillow", + "ecosystem": "PyPI", + "title": "Saving a TIFF with a retained ColorMap over-reads in Pillow's encoder", + "summary": "Pillow: TIFF encoder ColorMap plane-size mismatch causes a heap out-of-bounds read", + "published": "2026-10-19", + "severity": null, + "affected": "< 12.3.0", + "fixed": "12.3.0", + "reached_by": "save", + "trigger": "Opening a 16-bit palette TIFF with a 768-entry ColorMap and saving it back as TIFF with its metadata.", + "bucket": "B", + "finding": "The ColorMap rides from the opened file's tag table into a save that keeps its metadata. The tag is in the directory the reader walks, and its 768 entries disagree with 16-bit samples, which need 196,608: the reader reports the contradiction, and a policy that refuses inconsistent tags refuses the file before Pillow sees it (allow_inconsistent_tiff_tags). The default allows it, because refusing a file that passed before is breaking, so the setting is what closes this.", + "refusal": { + "check": "allow_inconsistent_tiff_tags", + "default": false, + "setting": "Policy(allow_inconsistent_tiff_tags=False)" + }, + "verified_by": [ + "tests/test_check.py::TestHostileFiles::test_a_tiff_whose_colormap_does_not_fit_its_depth" + ], + "protect": { + "general": "Upgrade to Pillow 12.3.0.", + "specific": "Refuse inconsistent TIFF tags (allow_inconsistent_tiff_tags off) for files from outside; upgrade to Pillow 12.3.0." + }, + "sources": [ + "https://osv.dev/vulnerability/GHSA-pchr-8j38-3x4p", + "https://osv.dev/vulnerability/PYSEC-2026-4334" + ], + "triaged": "2026-11-03", + "checked": "2026-11-03", + "notes": "Proposed from the advisory's text on 2026-11-01, as part of folding the backfill into the record; confirmed by a person on 2026-11-03. Re-read on 2026-11-03 against the question of what rides from an opened file into a save: the file is the input. Bucket and finding confirmed by a person on 2026-11-03; waits for the policy on a TIFF's tags and its test before it can be promoted. The policy was built on 2026-11-03 (sillybos 4.2.0)." +} diff --git a/docs/advisories/record/CVE-2026-112902.json b/docs/advisories/record/CVE-2026-112902.json new file mode 100644 index 0000000..e870daa --- /dev/null +++ b/docs/advisories/record/CVE-2026-112902.json @@ -0,0 +1,35 @@ +{ + "key": "CVE-2026-112902", + "aliases": [], + "library": "Pillow", + "package": "pillow", + "ecosystem": "PyPI", + "title": "Crafted TIFF tags make Pillow's libtiff encoder copy process memory into the output", + "summary": "Pillow: Memory disclosure and segfault in libtiff encoder via crafted TIFF tags", + "published": "2026-10-21", + "severity": null, + "affected": "< 12.3.0", + "fixed": "12.3.0", + "reached_by": "save", + "trigger": "Opening a crafted TIFF and saving it back as TIFF with its tags.", + "bucket": "B", + "finding": "The crafted tags (DNGPrivateData and ActiveArea, private and DNG tags a scan never carries) ride from the opened file's tag table into a bare save, through libtiff whenever the file is compressed, with no option from the program. The tags are in the directory the reader walks, and every tag is sorted into standard, metadata or private: a policy allowing standard and metadata tags refuses the file before Pillow sees it (tiff_tags). The default allows private tags, since a vendor's own tag in a scanner's output is harmless on a read, so the setting is what closes this. Tetrak OCR copies each frame before saving, which drops the tag table, so it is not reached there.", + "refusal": { + "check": "tiff_tags", + "default": false, + "setting": "Policy(tiff_tags={\"standard\", \"metadata\"})" + }, + "verified_by": [ + "tests/test_check.py::TestHostileFiles::test_a_tiff_with_private_tags_under_a_policy_without_them" + ], + "protect": { + "general": "Upgrade to Pillow 12.3.0.", + "specific": "Allow standard and metadata tags only (tiff_tags) wherever a file from outside is saved back as TIFF with its tag table; upgrade to Pillow 12.3.0." + }, + "sources": [ + "https://osv.dev/vulnerability/GHSA-xvh5-g6gv-559v" + ], + "triaged": "2026-11-03", + "checked": "2026-11-03", + "notes": "Proposed from the advisory's text on 2026-11-01, as part of folding the backfill into the record; confirmed by a person on 2026-11-03. Re-read on 2026-11-03 against the question of what rides from an opened file into a save: the file is the input. Bucket and finding confirmed by a person on 2026-11-03; waits for the policy on a TIFF's tags and its test before it can be promoted. The policy was built on 2026-11-03 (sillybos 4.2.0)." +} diff --git a/docs/api.md b/docs/api.md index d923184..d3eada3 100644 --- a/docs/api.md +++ b/docs/api.md @@ -56,6 +56,8 @@ chooses from it. ::: sillybos.TIFF_SUBSAMPLINGS +::: sillybos.TIFF_TAGS + ## With Pillow ::: sillybos.pillow diff --git a/src/sillybos/__init__.py b/src/sillybos/__init__.py index 3d58f20..2f87588 100644 --- a/src/sillybos/__init__.py +++ b/src/sillybos/__init__.py @@ -42,6 +42,7 @@ TIFF_COMPRESSIONS, TIFF_PHOTOMETRICS, TIFF_SUBSAMPLINGS, + TIFF_TAGS, Policy, ) @@ -61,6 +62,7 @@ "TIFF_COMPRESSIONS", "TIFF_PHOTOMETRICS", "TIFF_SUBSAMPLINGS", + "TIFF_TAGS", "Facts", "Policy", "Refused", diff --git a/src/sillybos/check.py b/src/sillybos/check.py index 3616aa8..1531593 100644 --- a/src/sillybos/check.py +++ b/src/sillybos/check.py @@ -36,6 +36,7 @@ TIFF_COMPRESSIONS, TIFF_PHOTOMETRICS, TIFF_SUBSAMPLINGS, + TIFF_TAGS, Policy, ) @@ -84,6 +85,12 @@ class Facts: codecs: Which of [sillybos.PDF_IMAGE_CODECS][] a PDF's images are coded with, by the filters named in the file. Empty for an image and for a PDF whose images use none of them. + tags: A TIFF's tags outside the standard set, as (kind, number) + with the kind a name from [sillybos.TIFF_TAGS][] + (`"metadata"` or `"private"`). Empty for the other formats + and for a TIFF of standard tags only. + inconsistencies: Each way a TIFF's tags contradict each other, in + a sentence. Empty when they agree, and for the other formats. """ bytes: int @@ -104,6 +111,8 @@ class Facts: subsampling: str | None = None features: tuple[str, ...] = () codecs: tuple[str, ...] = () + tags: tuple[tuple[str, int], ...] = () + inconsistencies: tuple[str, ...] = () @dataclass(frozen=True) @@ -257,6 +266,8 @@ def _facts(path: Path, fmt: str, f: headers.Reader, size: int, policy: Policy) - interlaced=header.interlaced, tile=header.tile, subsampling=header.subsampling, + tags=header.tags, + inconsistencies=header.inconsistencies, ) @@ -329,6 +340,15 @@ def _against_limits(facts: Facts, policy: Policy, fmt: str) -> list[str]: reasons.append( f"a {facts.compression}-compressed TIFF; the policy allows {_listed(allowed)}" ) + refused_tags = [(kind, number) for kind, number in facts.tags if kind not in policy.tiff_tags] + if refused_tags: + found = _listed([f"{kind} tag {headers.tag_name(number)}" for kind, number in refused_tags]) + allowed = [name for name in TIFF_TAGS if name in policy.tiff_tags] + reasons.append(f"a TIFF with {found}; the policy allows {_listed(allowed)} tags") + if facts.inconsistencies and not policy.allow_inconsistent_tiff_tags: + reasons.append( + "a TIFF whose tags contradict each other: " + "; ".join(facts.inconsistencies) + ) return reasons diff --git a/src/sillybos/cli.py b/src/sillybos/cli.py index 75be4ef..515e891 100644 --- a/src/sillybos/cli.py +++ b/src/sillybos/cli.py @@ -54,6 +54,10 @@ def _policy(args: argparse.Namespace) -> Policy: changes["tiff_photometrics"] = frozenset(args.tiff_photometrics.split(",")) if args.tiff_subsamplings: changes["tiff_subsamplings"] = frozenset(args.tiff_subsamplings.split(",")) + if args.tiff_tags: + changes["tiff_tags"] = frozenset(args.tiff_tags.split(",")) + if args.no_inconsistent_tiff_tags: + changes["allow_inconsistent_tiff_tags"] = False if args.pdf_features: changes["pdf_features"] = frozenset(args.pdf_features.split(",")) if args.pdf_image_codecs: @@ -145,6 +149,20 @@ def build_parser() -> argparse.ArgumentParser: "(default: none,ccitt,lzw,jpeg,deflate,packbits)" ), ) + parser.add_argument( + "--tiff-tags", + metavar="LIST", + help=( + "comma-separated kinds of TIFF tag to allow: standard, metadata, private " + "(default: all three)" + ), + ) + parser.add_argument( + "--no-inconsistent-tiff-tags", + action="store_true", + help="refuse a TIFF whose tags contradict each other (a ColorMap of the wrong size, " + "a floating-point Predictor on integer samples)", + ) parser.add_argument( "--pdf-features", metavar="LIST", diff --git a/src/sillybos/headers.py b/src/sillybos/headers.py index fd7cdc0..3284578 100644 --- a/src/sillybos/headers.py +++ b/src/sillybos/headers.py @@ -79,6 +79,15 @@ class Header: tile: For a tiled TIFF, the (width, height) of its largest tile, which is what the decoder allocates whole. `None` for a TIFF in strips and for the other formats. + tags: For a TIFF, every tag outside the standard set, as (kind, + number) with the kind a name from [sillybos.TIFF_TAGS][] + (`"metadata"` or `"private"`), each number once, in order of + first appearance. Empty for a TIFF of standard tags only and + for the other formats. + inconsistencies: For a TIFF, each way its tags contradict each + other, in a sentence: a ColorMap with the wrong number of + entries for the bit depth, a floating-point Predictor on + integer samples. Empty when they agree. """ width: int @@ -95,6 +104,8 @@ class Header: interlaced: bool = False tile: tuple[int, int] | None = None subsampling: str | None = None + tags: tuple[tuple[str, int], ...] = () + inconsistencies: tuple[str, ...] = () def _read_exactly(f: Reader, n: int, what: str) -> bytes: @@ -413,8 +424,14 @@ def png(f: Reader, size: int, limit: int) -> Header: _TILE_WIDTH = 322 _TILE_LENGTH = 323 _YCBCR_SUBSAMPLING = 530 +_PREDICTOR = 317 +_COLORMAP = 320 +_SAMPLE_FORMAT = 339 _YCBCR = 6 +_FLOATING_POINT_PREDICTOR = 3 +_FLOATING_POINT_SAMPLES = 3 _SINGLE_VALUE_TAGS = ( + _PREDICTOR, _IMAGE_WIDTH, _IMAGE_LENGTH, _COMPRESSION, @@ -426,6 +443,7 @@ def png(f: Reader, size: int, limit: int) -> Header: _TAG_NAMES = { _IMAGE_WIDTH: "a dimension tag", _IMAGE_LENGTH: "a dimension tag", + _PREDICTOR: "a predictor tag", _COMPRESSION: "a compression tag", _PHOTOMETRIC: "a photometric tag", _SAMPLES_PER_PIXEL: "a samples-per-pixel tag", @@ -440,14 +458,49 @@ def png(f: Reader, size: int, limit: int) -> Header: 270: "ImageDescription", 273: "StripOffsets", 279: "StripByteCounts", + 317: "Predictor", + 320: "ColorMap", 324: "TileOffsets", 325: "TileByteCounts", + 339: "SampleFormat", 700: "XMP", 33723: "IPTC", 34377: "Photoshop", 34665: "Exif", 34675: "ICC profile", + 34853: "GPS", + 50706: "DNGVersion", + 50740: "DNGPrivateData", + 50829: "ActiveArea", } +# The TIFF 6.0 tags, baseline and extensions (254 to 532), with the few +# later additions every writer uses: TIFF-FX's global parameters (400 to +# 405), the JPEG tables and YCbCr tags, Copyright (33432), and TIFF/EP's +# CFA and sensor tags (33421 to 33423, 37399, 37400). Everything a scanner +# or an editor writes that is not here is in _METADATA_TAGS; what is in +# neither is a private tag, as TIFF calls 32,768 and above, or an +# unassigned number, and [sillybos.TIFF_TAGS][] names both "private". +_STANDARD_TAGS = ( + frozenset(range(254, 348)) + | frozenset(range(400, 406)) + | frozenset(range(512, 522)) + | frozenset({529, 530, 531, 532, 33421, 33422, 33423, 33432, 37399, 37400}) +) +# The registered metadata blocks: XMP, Microsoft's ratings, GeoTIFF, IPTC, +# Photoshop, the Exif, GPS and interoperability IFDs, the ICC profile, +# Photoshop's image source data, GDAL's two, the PrintIM block, and the +# padding and offset-schema tags Microsoft's writers leave. +_METADATA_TAGS = frozenset( + {700, 18246, 18247, 18248, 18249, 33550, 33723, 33922, 34377, 34665, 34675, 34735} +) | frozenset({34736, 34737, 34853, 37724, 40965, 42112, 42113, 50341, 59932, 59933}) + + +def tag_name(number: int) -> str: + """A TIFF tag's number, with its name where the library knows one: `50,740 (DNGPrivateData)`.""" + label = _TAG_LABELS.get(number) + return f"{number:,}" + (f" ({label})" if label else "") + + MAX_SAMPLES = 64 """The most samples per pixel a BitsPerSample tag may list. A scan has one, three or four; the bound keeps the out-of-line read small.""" @@ -537,6 +590,8 @@ def tiff(f: Reader, size: int, limit: int) -> Header: metadata: tuple[str, int] | None = None tile: tuple[int, int] | None = None subsampling: str | None = None + tags: dict[int, str] = {} # every tag outside the standard set, by number + inconsistencies: list[str] = [] max_pixels = 0 max_side = 0 pages = 0 @@ -558,6 +613,8 @@ def tiff(f: Reader, size: int, limit: int) -> Header: colour=layout[0], depth=layout[2], subsampling=subsampling, + tags=tuple((kind, number) for number, kind in tags.items()), + inconsistencies=tuple(inconsistencies), ) if offset in seen: raise HeaderError("the frame chain loops back on itself") @@ -579,10 +636,22 @@ def tiff(f: Reader, size: int, limit: int) -> Header: } bits: tuple[int, bytes] | None = None # (count, field) to resolve after the directory factors = (2, 2) # the subsampling tag's default + colormap_entries: int | None = None + predictor = 1 # the tag's default: none + sample_format = 1 # the tag's default: unsigned integer for _ in range(count): tag, kind, n, raw = struct.unpack( endian + "HHI4s", _read_exactly(f, 12, "frame directory") ) + if tag not in _STANDARD_TAGS and tag not in tags: + tags[tag] = "metadata" if tag in _METADATA_TAGS else "private" + if tag == _COLORMAP: + colormap_entries = n + elif tag == _SAMPLE_FORMAT and kind == _TIFF_SHORT and 1 <= n <= 2: + # One value per sample; a tag whose values sit out of line + # is left as the default, since nothing here depends on + # reading it, and a wrong guess would refuse a sound file. + (sample_format,) = struct.unpack(endian + "H", raw[:2]) data_size = n * _TYPE_SIZES.get(kind, 1) if data_size > 4: # The field holds an offset to the data. A count that would @@ -629,6 +698,8 @@ def tiff(f: Reader, size: int, limit: int) -> Header: width = value elif tag == _IMAGE_LENGTH: height = value + elif tag == _PREDICTOR: + predictor = value else: values[tag] = value if width is None or height is None: @@ -643,6 +714,18 @@ def tiff(f: Reader, size: int, limit: int) -> Header: if tile is None or tile_width * tile_length > tile[0] * tile[1]: tile = (tile_width, tile_length) depth = _bits_per_sample(f, size, endian, bits, pages + 1) + if colormap_entries is not None and (depth > 16 or colormap_entries != 3 * (1 << depth)): + # A ColorMap holds 3 * 2**depth entries, one plane per channel; + # past 16 bits no map fits, and the shift is not worth taking. + needed = f"which need {3 * (1 << depth):,}" if depth <= 16 else "which no map fits" + inconsistencies.append( + f"frame {pages + 1} has a ColorMap of {colormap_entries:,} entries for " + f"{depth}-bit samples, {needed}" + ) + if predictor == _FLOATING_POINT_PREDICTOR and sample_format != _FLOATING_POINT_SAMPLES: + inconsistencies.append( + f"frame {pages + 1} has a floating-point Predictor on integer samples" + ) photometric, samples = values[_PHOTOMETRIC], values[_SAMPLES_PER_PIXEL] assert photometric is not None and samples is not None # noqa: S101 - defaults above colour = _PHOTOMETRICS.get(photometric) @@ -703,6 +786,8 @@ def tiff(f: Reader, size: int, limit: int) -> Header: colour=layout[0], depth=layout[2], subsampling=subsampling, + tags=tuple((kind, number) for number, kind in tags.items()), + inconsistencies=tuple(inconsistencies), ) diff --git a/src/sillybos/policy.py b/src/sillybos/policy.py index ee3da04..c1e8877 100644 --- a/src/sillybos/policy.py +++ b/src/sillybos/policy.py @@ -89,6 +89,15 @@ a launch action that runs a program. A scanned book has none of them, so the default policy refuses them all; a policy names the ones it will pass.""" +TIFF_TAGS: tuple[str, ...] = ("standard", "metadata", "private") +"""The three kinds of tag a TIFF directory can hold, by name. `standard` is +the TIFF 6.0 set (baseline and extensions) with the few later additions +every writer uses; `metadata` is the registered blocks a scanner or an +editor attaches (XMP, IPTC, Photoshop, Exif, ICC, GPS, GeoTIFF, GDAL); +`private` is everything else, the vendor and DNG tags a scan never +carries, which is where a crafted tag goes when it is meant to reach the +encoder on a save (CVE-2026-112902).""" + PDF_IMAGE_CODECS: tuple[str, ...] = ("jpeg", "jpx", "jbig2", "ccitt") """Every codec a PDF's images can be coded with that hands the stream to a decoder of its own inside poppler, by name: DCT (JPEG), JPX (JPEG 2000, @@ -182,6 +191,23 @@ class Policy: libtiff's flaws have been found (PixarLog most recently, CVE-2026-12912), and a scan never needs them. Every frame of a file must use the same scheme. + tiff_tags: Which of [TIFF_TAGS][sillybos.TIFF_TAGS] a TIFF's directories + may hold: standard, metadata, private. The default is all + three, since a vendor's own tag in a scanner's output is + harmless on a read. A program that saves a file back as TIFF + with its tag table carries every tag into libtiff's encoder, + where a crafted private tag copied process memory into the + output (CVE-2026-112902); such a program should allow standard + and metadata tags only. + allow_inconsistent_tiff_tags: Pass a TIFF whose tags contradict + each other: a ColorMap with the wrong number of entries for + the bit depth (768 for 16-bit samples, which need 196,608; + CVE-2026-112896), or a floating-point Predictor on integer + samples (CVE-2026-112396). No scanner writes either; both are + what a crafted file carries to reach an encoder that trusts + the tag. The default allows them, because refusing a file + that passed before is a breaking change; a policy for files + from outside should not. allow_encrypted_pdf: Pass a PDF that `pdfinfo` reports as encrypted. A scanned book never is, so the default refuses. pdf_features: Which of [PDF_FEATURES][sillybos.PDF_FEATURES] a PDF may carry: @@ -234,6 +260,8 @@ class Policy: tiff_compressions: frozenset[str] = field( default_factory=lambda: frozenset({"none", "ccitt", "lzw", "jpeg", "deflate", "packbits"}) ) + tiff_tags: frozenset[str] = field(default_factory=lambda: frozenset(TIFF_TAGS)) + allow_inconsistent_tiff_tags: bool = True allow_encrypted_pdf: bool = False pdf_features: frozenset[str] = frozenset() pdf_image_codecs: frozenset[str] = field(default_factory=lambda: frozenset(PDF_IMAGE_CODECS)) @@ -269,6 +297,7 @@ def __post_init__(self) -> None: ("tiff_compressions", TIFF_COMPRESSIONS, "TIFF compression"), ("tiff_photometrics", TIFF_PHOTOMETRICS, "TIFF photometric interpretation"), ("tiff_subsamplings", TIFF_SUBSAMPLINGS, "TIFF subsampling"), + ("tiff_tags", TIFF_TAGS, "TIFF tag kind"), ): chosen = getattr(self, field_name) unknown = set(chosen) - set(known) diff --git a/tests/test_check.py b/tests/test_check.py index 1473e99..fdde1af 100644 --- a/tests/test_check.py +++ b/tests/test_check.py @@ -192,6 +192,53 @@ def test_a_jpeg_compressed_tiff_under_a_policy_without_jpeg(self, tmp_path: Path verdict = check(path, Policy(tiff_compressions=frozenset({"none", "ccitt", "lzw"}))) assert verdict.reasons == ("a jpeg-compressed TIFF; the policy allows none, ccitt and lzw",) + def test_a_tiff_with_private_tags_under_a_policy_without_them(self, tmp_path: Path) -> None: + # CVE-2026-112902: DNGPrivateData and ActiveArea, carried from the + # opened file into libtiff's encoder on a save with the tag table. + data = corpus.tiff( + [(10, 10)], tags=[(700, 1, b""), (50740, 1, b"\x01"), (50829, 4, b"\x00" * 16)] + ) + path = corpus.write(tmp_path / "scan.tif", data) + assert check(path).ok + verdict = check(path, Policy(tiff_tags=frozenset({"standard", "metadata"}))) + assert verdict.reasons == ( + "a TIFF with private tag 50,740 (DNGPrivateData) and private tag 50,829 (ActiveArea); " + "the policy allows standard and metadata tags", + ) + verdict = check(path, Policy(tiff_tags=frozenset({"standard"}))) + assert verdict.reasons[0].startswith("a TIFF with metadata tag 700 (XMP), private tag") + assert verdict.facts is not None and verdict.facts.tags == ( + ("metadata", 700), + ("private", 50740), + ("private", 50829), + ) + + def test_a_tiff_whose_colormap_does_not_fit_its_depth(self, tmp_path: Path) -> None: + # CVE-2026-112896: a 768-entry ColorMap on 16-bit samples over-reads + # in Pillow's encoder when the file is saved back with its metadata. + data = corpus.tiff([(10, 10)], layout=(1, 1, 16), tags=[(320, 3, b"\x00" * 1536)]) + path = corpus.write(tmp_path / "scan.tif", data) + assert check(path).ok + verdict = check(path, Policy(allow_inconsistent_tiff_tags=False)) + assert verdict.reasons == ( + "a TIFF whose tags contradict each other: frame 1 has a ColorMap of 768 entries " + "for 16-bit samples, which need 196,608", + ) + + def test_a_tiff_with_a_floating_point_predictor_on_integer_samples( + self, tmp_path: Path + ) -> None: + # CVE-2026-112396: libtiff rejects the tag at encode time, and Pillow + # then wrote into a freed buffer when encoding to memory. + data = corpus.tiff([(10, 10)], compression=5, tags=[(317, 3, b"\x03\x00")]) + path = corpus.write(tmp_path / "scan.tif", data) + assert check(path).ok + verdict = check(path, Policy(allow_inconsistent_tiff_tags=False)) + assert verdict.reasons == ( + "a TIFF whose tags contradict each other: frame 1 has a floating-point Predictor " + "on integer samples", + ) + def test_a_cielab_tiff(self, tmp_path: Path) -> None: path = corpus.write(tmp_path / "scan.tif", corpus.tiff([(10, 10)], layout=(8, 3, 8))) verdict = check(path) @@ -555,6 +602,8 @@ def test_presets(self) -> None: ({"tiff_photometrics": frozenset()}, "at least one TIFF photometric"), ({"tiff_subsamplings": frozenset({"3x3"})}, "unknown TIFF subsampling"), ({"tiff_subsamplings": frozenset()}, "at least one TIFF subsampling"), + ({"tiff_tags": frozenset({"standard", "exotic"})}, "unknown TIFF tag kind"), + ({"tiff_tags": frozenset()}, "at least one TIFF tag kind"), ({"max_pages": 0}, "max_pages must be at least 1"), ({"max_side": 0}, "max_side must be at least 1"), ({"max_metadata_bytes": 0}, "max_metadata_bytes must be at least 1"), diff --git a/tests/test_cli.py b/tests/test_cli.py index 20d69d0..b31b47e 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -108,6 +108,20 @@ def test_pdf_features_can_be_allowed(tmp_path: Path) -> None: @pytest.mark.skipif(shutil.which("pdfinfo") is None, reason="pdfinfo (poppler)") +def test_tiff_tags_can_be_refused(tmp_path: Path) -> None: + scan = corpus.write(tmp_path / "scan.tif", corpus.tiff([(10, 10)], tags=[(50740, 1, b"\x01")])) + assert main([str(scan)]) == 0 + assert main(["--tiff-tags", "standard,metadata", str(scan)]) == 1 + assert main(["--tiff-tags", "private", str(scan)]) == 0 + + +def test_inconsistent_tiff_tags_can_be_refused(tmp_path: Path) -> None: + data = corpus.tiff([(10, 10)], compression=5, tags=[(317, 3, b"\x03\x00")]) + scan = corpus.write(tmp_path / "scan.tif", data) + assert main([str(scan)]) == 0 + assert main(["--no-inconsistent-tiff-tags", str(scan)]) == 1 + + def test_pdf_image_codecs_can_be_refused(tmp_path: Path) -> None: scan = corpus.write(tmp_path / "scan.pdf", corpus.pdf_with("jbig2")) assert main([str(scan)]) == 0 diff --git a/tests/test_headers.py b/tests/test_headers.py index 9dc35a2..fed181e 100644 --- a/tests/test_headers.py +++ b/tests/test_headers.py @@ -210,6 +210,55 @@ def test_refuses_a_malformed_header(self, data: bytes, reason: str) -> None: read("tiff", data) +class TestTiffTags: + def test_standard_tags_are_not_listed(self) -> None: + data = corpus.tiff([(10, 10)], compression=5, tags=[(270, 2, b"a scan\x00")]) + header = read("tiff", data) + assert header.tags == () + assert header.inconsistencies == () + + def test_metadata_and_private_tags_are_sorted_by_kind(self) -> None: + data = corpus.tiff( + [(10, 10)] * 2, + tags=[(700, 1, b""), (50740, 1, b"\x01\x02"), (50829, 4, b"\x00" * 16)], + ) + header = read("tiff", data) + assert header.tags == (("metadata", 700), ("private", 50740), ("private", 50829)) + + def test_a_colormap_that_fits_its_depth_is_consistent(self) -> None: + data = corpus.tiff([(10, 10)], layout=(3, 1, 8), tags=[(320, 3, b"\x00" * (2 * 3 * 256))]) + assert read("tiff", data).inconsistencies == () + + def test_a_colormap_too_small_for_its_depth_is_inconsistent(self) -> None: + # CVE-2026-112896: 768 entries on 16-bit greyscale samples + data = corpus.tiff([(10, 10)], layout=(1, 1, 16), tags=[(320, 3, b"\x00" * (2 * 768))]) + assert read("tiff", data).inconsistencies == ( + "frame 1 has a ColorMap of 768 entries for 16-bit samples, which need 196,608", + ) + + def test_a_colormap_on_samples_too_deep_for_any_map_is_inconsistent(self) -> None: + data = corpus.tiff([(10, 10)], layout=(1, 1, 32), tags=[(320, 3, b"\x00" * 6)]) + assert read("tiff", data).inconsistencies == ( + "frame 1 has a ColorMap of 3 entries for 32-bit samples, which no map fits", + ) + + def test_a_floating_point_predictor_on_integer_samples_is_inconsistent(self) -> None: + # CVE-2026-112396: Predictor 3 with no SampleFormat (unsigned integer by default) + data = corpus.tiff([(10, 10)], compression=5, tags=[(317, 3, b"\x03\x00")]) + assert read("tiff", data).inconsistencies == ( + "frame 1 has a floating-point Predictor on integer samples", + ) + + def test_a_floating_point_predictor_on_floating_point_samples_is_fine(self) -> None: + data = corpus.tiff( + [(10, 10)], + compression=5, + layout=(1, 1, 32), + tags=[(317, 3, b"\x03\x00"), (339, 3, b"\x03\x00")], + ) + assert read("tiff", data).inconsistencies == () + + class TestTiffCompression: def test_no_tag_means_uncompressed(self) -> None: assert read("tiff", corpus.tiff([(10, 10)])).compression == "none"