fix: a hostile PDF cannot hold the scanner: bounded DPI reading and a stuck scan is reported
All checks were successful
Build and deploy / Validate source (push) Successful in 8s
Build and deploy / Integration suite on a real stack (push) Successful in 8m38s
Build and deploy / Secret scan and release gate (push) Successful in 7s
Build and deploy / Publish images (push) Successful in 1m19s

The DPI of a stored PDF is read in a subprocess killed after
PDF_MAX_SECONDS + 5; inside it, only image draws count toward
PDF_MAX_DRAWS (heavy vector art still reads), content streams are parsed
once per object, and the deadline is checked every 1000 operators. A scan
busy for more than SCAN_STUCK_SECONDS marks the scanner unhealthy and logs
one error to Sentry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-11-30 10:50:14 -03:00
parent 37d747697a
commit 7e33e6fb1d
4 changed files with 191 additions and 37 deletions

View File

@@ -105,7 +105,7 @@ class GradeCheckTests(unittest.TestCase):
storage = CountingStorage({'k.pdf': b'%PDF-1.4 test', 'k.png': encoded('PNG', (100, 50))})
files = grade_check.Files(storage)
pdf_row = {'object_key': 'k.pdf', 'size': 15, 'name': 'a.pdf'}
with mock.patch.object(grade_check, 'pdf_dpi', return_value=300):
with mock.patch.object(grade_check, 'bounded_pdf_dpi', return_value=300):
self.assertEqual(files.pdf_dpi(pdf_row), 300)
self.assertEqual(files.pdf_dpi(pdf_row), 300)
self.assertEqual(storage.downloads, 1)
@@ -158,6 +158,62 @@ class GradeCheckTests(unittest.TestCase):
self.assertIn('confirme a ressalva', issue(199.4))
def nested_forms_pdf(path, fanout=10, levels=8):
"""A 2 KB PDF whose page draws form F0 ten times, F0 draws F1 ten times,
eight deep: 10^8 draws for a reader that walks them."""
pdf = pikepdf.new()
pdf.add_blank_page(page_size=(200, 200))
forms = []
for level in reversed(range(levels)):
if forms:
content = b''.join(b'q /F%d Do Q\n' % (level + 1) for _ in range(fanout))
resources = pikepdf.Dictionary(XObject=pikepdf.Dictionary({'/F%d' % (level + 1): forms[0]}))
else:
content, resources = b'0 0 1 1 re f\n', pikepdf.Dictionary()
form = pikepdf.Stream(pdf, content)
form.Type, form.Subtype, form.BBox, form.Resources = (pikepdf.Name.XObject, pikepdf.Name.Form,
[0, 0, 1, 1], resources)
forms.insert(0, pdf.make_indirect(form))
page = pdf.pages[0]
page.obj.Resources = pikepdf.Dictionary(XObject=pikepdf.Dictionary({'/F0': forms[0]}))
page.obj.Contents = pdf.make_indirect(pikepdf.Stream(pdf, b''.join(b'q /F0 Do Q\n' for _ in range(fanout))))
pdf.save(path)
class HostilePdfTests(unittest.TestCase):
"""A tiny PDF must not hold the scanner's one thread (security review,
2026-11-27): it held it for hours, and every upload behind it."""
def test_nested_forms_end_unknown_in_seconds(self):
import time
with tempfile.NamedTemporaryFile(suffix='.pdf') as handle:
nested_forms_pdf(handle.name)
start = time.monotonic()
self.assertIsNone(grade_check.pdf_dpi(handle.name))
self.assertLess(time.monotonic() - start, grade_check.PDF_MAX_SECONDS + 2)
start = time.monotonic()
self.assertIsNone(grade_check.bounded_pdf_dpi(handle.name))
self.assertLess(time.monotonic() - start, grade_check.PDF_MAX_SECONDS + 10)
def test_a_heavy_vector_page_is_still_read(self):
# Vector art has many paths and few draws: only draws are counted.
pdf = pikepdf.new()
pdf.add_blank_page(page_size=(200, 200))
pdf.pages[0].obj.Contents = pdf.make_indirect(pikepdf.Stream(pdf, b'0 0 1 1 re f\n' * 200000))
with tempfile.NamedTemporaryFile(suffix='.pdf') as handle:
pdf.save(handle.name)
self.assertEqual(grade_check.pdf_dpi(handle.name), grade_check.PDF_VECTOR_DPI)
def test_a_scan_held_on_one_file_is_reported(self):
from app import scanning
import time
self.assertFalse(scanning.stuck())
with mock.patch.object(scanning, 'busy_since', time.monotonic() - scanning.SCAN_STUCK_SECONDS - 1):
self.assertTrue(scanning.stuck())
with mock.patch.object(scanning, 'busy_since', time.monotonic()):
self.assertFalse(scanning.stuck())
class ProportionTests(unittest.TestCase):
"""A file laid out at proportions it does not have is refused before
payment, not found at print time (a 5 m sheet sent as 10 cm)."""