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

@@ -8,7 +8,7 @@ from http.server import BaseHTTPRequestHandler, HTTPServer
import psycopg
from botocore.exceptions import ClientError
from psycopg.types.json import Jsonb
from . import mail, observability, unpaid
from . import mail, observability, scanning, unpaid
from .adapters import FakeTiny, LocalS3Storage, require_runtime
from .core.secrets import load as load_secret_files
from .core.db import connect
@@ -205,13 +205,25 @@ def loop():
delay = 1
time.sleep(1)
scan_stuck_reported = False
class Health(BaseHTTPRequestHandler):
def do_GET(self):
global scan_stuck_reported
scanner = False
try:
scanner = bool(scan_thread and scan_thread.is_alive() and ClamAV().ping())
except Exception:
pass
# Alive but held on one file: every upload waits behind it.
if scanning.stuck():
scanner = False
if not scan_stuck_reported:
scan_stuck_reported = True
logging.error('The upload scan has been on one file for over %s min: every upload waits',
scanning.SCAN_STUCK_SECONDS // 60)
else:
scan_stuck_reported = False
renderer = bool(render_thread and render_thread.is_alive())
healthy = time.monotonic()-last_tick < 15 and scanner and renderer
self.send_response(200 if self.path == '/health' and healthy else 503)