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
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:
@@ -124,46 +124,64 @@ def check_large(head, row):
|
||||
return 'rejected', 'O conteúdo do arquivo não corresponde ao formato do nome'
|
||||
|
||||
|
||||
# When the scan of the file now in hand began (None between files): the
|
||||
# worker's health reports a scan held on one file for longer than
|
||||
# SCAN_STUCK_SECONDS, since every upload waits behind it.
|
||||
busy_since=None
|
||||
SCAN_STUCK_SECONDS=900
|
||||
|
||||
def stuck():
|
||||
"""Whether the scan has been on the same file for SCAN_STUCK_SECONDS."""
|
||||
return busy_since is not None and time.monotonic()-busy_since>SCAN_STUCK_SECONDS
|
||||
|
||||
def scan_one(storage, scanner=None):
|
||||
global busy_since
|
||||
scanner=scanner or ClamAV()
|
||||
with connect() as c:
|
||||
row=c.execute('''SELECT * FROM dtf_local.uploads WHERE complete AND purged_at IS NULL
|
||||
AND expires_at>now() AND scan_state IN ('pending','error') AND scan_after<=now()
|
||||
ORDER BY created_at FOR UPDATE SKIP LOCKED LIMIT 1''').fetchone()
|
||||
if not row:return False
|
||||
name=row['name']
|
||||
busy_since=time.monotonic()
|
||||
try:
|
||||
# Every later decision reads the stored name (the manual charge,
|
||||
# the JPG refusal, the grade, the print file, the folders), so the
|
||||
# name is made to say what the file is (D-13).
|
||||
head=head_of(storage,row)
|
||||
name=real_name(name,head)
|
||||
if row['size'] > scan_limit_bytes():
|
||||
state,reason=check_large(head,row)
|
||||
else:
|
||||
stream=storage.client.get_object(Bucket=storage.bucket,Key=row['object_key'])['Body']
|
||||
try:state,reason=scanner.scan(stream,row['size'])
|
||||
finally:stream.close()
|
||||
except Exception:
|
||||
state,reason='error','Malware scanner unavailable; file remains blocked'
|
||||
# A PDF's resolution is worked out here, once, so checkout never reads
|
||||
# it (Critical Corrections 2). A failure leaves it for the fallback.
|
||||
checked,pdf_dpi_value=False,None
|
||||
if state=='clean' and name.lower().endswith('.pdf'):
|
||||
try:
|
||||
pdf_dpi_value=stored_pdf_dpi(storage,row)
|
||||
checked=True
|
||||
except Exception:
|
||||
logging.exception('Could not work out the PDF DPI of upload %s',row['id'])
|
||||
c.execute("""UPDATE dtf_local.uploads SET name=%s,scan_state=%s,scan_reason=%s,scanned_at=now(),
|
||||
scan_after=now()+interval '1 minute',
|
||||
pdf_checked=CASE WHEN %s THEN true ELSE pdf_checked END,
|
||||
pdf_dpi=CASE WHEN %s THEN %s ELSE pdf_dpi END,
|
||||
expires_at=CASE WHEN %s IN ('rejected','error') THEN LEAST(expires_at,now()+interval '3 days') ELSE expires_at END
|
||||
WHERE id=%s""",(name,state,reason,checked,checked,pdf_dpi_value,state,row['id']))
|
||||
audit('artwork_scan', upload=str(row['id']), result=state, c=c)
|
||||
_scan_row(c, storage, scanner, row)
|
||||
finally:
|
||||
busy_since=None
|
||||
return True
|
||||
|
||||
def _scan_row(c, storage, scanner, row):
|
||||
name=row['name']
|
||||
try:
|
||||
# Every later decision reads the stored name (the manual charge,
|
||||
# the JPG refusal, the grade, the print file, the folders), so the
|
||||
# name is made to say what the file is (D-13).
|
||||
head=head_of(storage,row)
|
||||
name=real_name(name,head)
|
||||
if row['size'] > scan_limit_bytes():
|
||||
state,reason=check_large(head,row)
|
||||
else:
|
||||
stream=storage.client.get_object(Bucket=storage.bucket,Key=row['object_key'])['Body']
|
||||
try:state,reason=scanner.scan(stream,row['size'])
|
||||
finally:stream.close()
|
||||
except Exception:
|
||||
state,reason='error','Malware scanner unavailable; file remains blocked'
|
||||
# A PDF's resolution is worked out here, once, so checkout never reads
|
||||
# it (Critical Corrections 2). A failure leaves it for the fallback.
|
||||
checked,pdf_dpi_value=False,None
|
||||
if state=='clean' and name.lower().endswith('.pdf'):
|
||||
try:
|
||||
pdf_dpi_value=stored_pdf_dpi(storage,row)
|
||||
checked=True
|
||||
except Exception:
|
||||
logging.exception('Could not work out the PDF DPI of upload %s',row['id'])
|
||||
c.execute("""UPDATE dtf_local.uploads SET name=%s,scan_state=%s,scan_reason=%s,scanned_at=now(),
|
||||
scan_after=now()+interval '1 minute',
|
||||
pdf_checked=CASE WHEN %s THEN true ELSE pdf_checked END,
|
||||
pdf_dpi=CASE WHEN %s THEN %s ELSE pdf_dpi END,
|
||||
expires_at=CASE WHEN %s IN ('rejected','error') THEN LEAST(expires_at,now()+interval '3 days') ELSE expires_at END
|
||||
WHERE id=%s""",(name,state,reason,checked,checked,pdf_dpi_value,state,row['id']))
|
||||
audit('artwork_scan', upload=str(row['id']), result=state, c=c)
|
||||
|
||||
def scan_loop(storage):
|
||||
while True:
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user