feat: a file's type comes from its first bytes, not its name (Dropstar's rule D-13)
All checks were successful
Build and deploy / Validate source (push) Successful in 7s
Build and deploy / Integration suite on a real stack (push) Successful in 3m37s
Build and deploy / Secret scan and release gate (push) Successful in 7s
Build and deploy / Publish images (push) Successful in 1m11s
All checks were successful
Build and deploy / Validate source (push) Successful in 7s
Build and deploy / Integration suite on a real stack (push) Successful in 3m37s
Build and deploy / Secret scan and release gate (push) Successful in 7s
Build and deploy / Publish images (push) Successful in 1m11s
The Site reads the first bytes when a file is chosen and renames it to its real extension: a .cdr that is a PDF is measured and priced as a PDF, without the manual charge; a JPG called .png is refused, naming the file. The scan does the same on the server and stores the real name, so every decision that reads the name follows the content. A name its content agrees with stays (an .ai is a PDF inside but is still handled by hand). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -75,6 +75,37 @@ RIFF_FORMS = {'cdr': re.compile(rb'^RIFF....CDR', re.S), 'webp': re.compile(rb'^
|
||||
HEAD_BYTES = 64
|
||||
|
||||
|
||||
# Dropstar's rule D-13 (2026-10-26): the content decides a file's type, never
|
||||
# its extension. A third of their ".cdr" files are PDFs renamed.
|
||||
def detected_format(head):
|
||||
"""The format a file's first bytes show, as an extension, or None."""
|
||||
for ext in ('pdf', 'png', 'jpg', 'tif', 'psd'):
|
||||
if any(head.startswith(sig) for sig in SIGNATURES[ext]):
|
||||
return ext
|
||||
for ext, form in RIFF_FORMS.items():
|
||||
if form.match(head):
|
||||
return ext
|
||||
return None
|
||||
|
||||
|
||||
def real_name(name, head):
|
||||
"""The file's name with the extension of what it really is: a ".cdr" that
|
||||
is a PDF is "x.pdf", a JPG called ".png" is "x.jpg". A name its content
|
||||
agrees with stays, so an Illustrator file, which is a PDF inside, is still
|
||||
".ai" and handled by hand; unknown content keeps the name too."""
|
||||
found = detected_format(head)
|
||||
if not found or format_matches(name, head):
|
||||
return name
|
||||
stem, dot, _ = name.rpartition('.')
|
||||
return (stem if dot else name) + '.' + found
|
||||
|
||||
|
||||
def head_of(storage, row):
|
||||
"""A stored file's first bytes."""
|
||||
return storage.client.get_object(Bucket=storage.bucket, Key=row['object_key'],
|
||||
Range=f'bytes=0-{HEAD_BYTES - 1}')['Body'].read()
|
||||
|
||||
|
||||
def format_matches(name, head):
|
||||
"""Whether a file's first bytes are those of the format its name claims."""
|
||||
ext = name.rsplit('.', 1)[-1].lower() if '.' in name else ''
|
||||
@@ -83,11 +114,10 @@ def format_matches(name, head):
|
||||
return any(head.startswith(sig) for sig in SIGNATURES.get(ext, ()))
|
||||
|
||||
|
||||
def check_large(storage, row):
|
||||
def check_large(head, row):
|
||||
"""Release decision for a file above the antivirus limit."""
|
||||
head = storage.client.get_object(Bucket=storage.bucket, Key=row['object_key'],
|
||||
Range=f'bytes=0-{HEAD_BYTES - 1}')['Body'].read()
|
||||
if format_matches(row['name'], head):
|
||||
# The content decides (D-13): a ".cdr" that is a PDF is still a PDF.
|
||||
if format_matches(row['name'], head) or detected_format(head):
|
||||
return 'clean', 'Acima do limite do antivírus; formato do arquivo conferido'
|
||||
return 'rejected', 'O conteúdo do arquivo não corresponde ao formato do nome'
|
||||
|
||||
@@ -99,19 +129,25 @@ def scan_one(storage, scanner=None):
|
||||
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']
|
||||
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(storage,row)
|
||||
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'
|
||||
c.execute("""UPDATE dtf_local.uploads SET scan_state=%s,scan_reason=%s,scanned_at=now(),
|
||||
c.execute("""UPDATE dtf_local.uploads SET name=%s,scan_state=%s,scan_reason=%s,scanned_at=now(),
|
||||
scan_after=now()+interval '1 minute',
|
||||
expires_at=CASE WHEN %s IN ('rejected','error') THEN LEAST(expires_at,now()+interval '3 days') ELSE expires_at END
|
||||
WHERE id=%s""",(state,reason,state,row['id']))
|
||||
WHERE id=%s""",(name,state,reason,state,row['id']))
|
||||
audit('artwork_scan', upload=str(row['id']), result=state)
|
||||
return True
|
||||
|
||||
|
||||
Reference in New Issue
Block a user