From 882fc2257ce960764b5e2b0e6c653e359b61b289 Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Wed, 10 Jan 2018 15:43:59 -0800 Subject: [PATCH] Add --max-image-mpixels argument to support Pillow 5.0 --- docs/release_notes.rst | 9 +++++++++ ocrmypdf/__main__.py | 16 ++++++++++++++++ tests/test_main.py | 22 +++++++++++++++++++++- 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/docs/release_notes.rst b/docs/release_notes.rst index 63325328..2b832b4a 100644 --- a/docs/release_notes.rst +++ b/docs/release_notes.rst @@ -5,6 +5,15 @@ OCRmyPDF uses `semantic versioning `_ for its command line i The OCRmyPDF package itself does not contain a public API, although it is fairly stable and breaking changes are usually timed with a major release. A future release will clearly define the stable public API. +v5.5 +---- + +- Add new argument ``--max-image-mpixels``. Pillow 5.0 now raises an exception when images may be decompression bombs. This argument can be used to override the limit Pillow sets. +- Fix output page cropped when using the sandwich renderer and OCR is skipped on a rotated and image-processed page +- A warning is now issued when old versions of Ghostscript are used in cases known to cause issues with non-Latin characters +- Fix a few parameter validation checks for ``-output-type pdfa-1`` and ``pdfa-2`` + + v5.4.4 ------ diff --git a/ocrmypdf/__main__.py b/ocrmypdf/__main__.py index 8ca9c7d8..16b7f662 100755 --- a/ocrmypdf/__main__.py +++ b/ocrmypdf/__main__.py @@ -14,6 +14,7 @@ import logging import argparse import PyPDF2 as pypdf +import PIL import ruffus.ruffus_exceptions as ruffus_exceptions import ruffus.cmdline as cmdline @@ -241,6 +242,11 @@ ocrsettings.add_argument( advanced = parser.add_argument_group( "Advanced", "Advanced options to control Tesseract's OCR behavior") +advanced.add_argument( + '--max-image-mpixels', action='store', type=float, metavar='MPixels', + help="Set maximum number of pixels to unpack before treating an image as a " + "decompression bomb", + default=128.0) advanced.add_argument( '--tesseract-config', action='append', metavar='CFG', default=[], help="Additional Tesseract configuration files -- see documentation") @@ -598,6 +604,12 @@ def do_ruffus_exception(ruffus_five_tuple, options, log): """)) exit_code = ExitCode.encrypted_pdf + elif exc_name == 'PIL.Image.DecompressionBombError': + msg = cleanup_ruffus_error_message(exc_value) + msg += ("\nUse the --max-image-mpixels argument to set increase the " + "maximum number of megapixels to accept.") + log.error(msg) + exit_code = ExitCode.input_file if exit_code is not None: return exit_code @@ -697,6 +709,10 @@ def run_pipeline(): check_options(options, _log) + PIL.Image.MAX_IMAGE_PIXELS = int(options.max_image_mpixels * 1000000) + if PIL.Image.MAX_IMAGE_PIXELS == 0: + PIL.Image.MAX_IMAGE_PIXELS = None + # Complain about qpdf version < 7.0.0 # Suppress the warning if in the test suite, since there are no PPAs # for qpdf 7.0.0 for Ubuntu trusty (i.e. Travis) diff --git a/tests/test_main.py b/tests/test_main.py index b6c652b0..ca5c02b6 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -14,6 +14,8 @@ from ocrmypdf.exec import ghostscript, tesseract, qpdf import logging from math import isclose +import PIL + check_ocrmypdf = pytest.helpers.check_ocrmypdf run_ocrmypdf = pytest.helpers.run_ocrmypdf @@ -1059,4 +1061,22 @@ def test_rotate_deskew_timeout(resources, outdir): test_pageno=1) # Confirm that the page still got deskewed - assert correlation > 0.50 \ No newline at end of file + assert correlation > 0.50 + + +@pytest.mark.skipif( + PIL.PILLOW_VERSION < '5.0.0', + reason="Pillow < 5.0.0 doesn't raise the exception") +def test_decompression_bomb(resources, outpdf): + p, out, err = run_ocrmypdf( + resources / 'hugemono.pdf', + outpdf + ) + assert 'decompression bomb' in err + + p, out, err = run_ocrmypdf( + resources / 'hugemono.pdf', + outpdf, + '--max-image-mpixels', '2000' + ) + assert p.returncode == 0 \ No newline at end of file