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

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:
Cauê Faleiros
2026-10-27 11:17:20 -03:00
parent 494925dff4
commit bba55a83f3
6 changed files with 121 additions and 21 deletions

View File

@@ -2,7 +2,7 @@
import unittest
from app.printjobs import whole_sheet
from app.scanning import format_matches
from app.scanning import check_large, format_matches, real_name
ROW = {'id': 'u1', 'name': 'folha.png', 'size': 3 * 1024 ** 3, 'scan_state': 'clean',
'purged_at': None, 'expired': False}
@@ -37,6 +37,20 @@ class FormatTest(unittest.TestCase):
self.assertFalse(format_matches('a.png', b'%PDF-1.4'))
self.assertFalse(format_matches('semextensao', b'\x89PNG\r\n\x1a\n'))
def test_the_content_names_the_file(self):
# Dropstar's rule D-13: a ".cdr" that is a PDF is a PDF, a JPG called
# ".png" is a JPG; a name its content agrees with stays (".ai" is a PDF inside).
self.assertEqual(real_name('folha.cdr', b'%PDF-1.7'), 'folha.pdf')
self.assertEqual(real_name('arte.png', b'\xff\xd8\xff\xe0'), 'arte.jpg')
self.assertEqual(real_name('semextensao', b'%PDF-1.4'), 'semextensao.pdf')
for name, head in (('folha.cdr', b'RIFF\0\0\0\0CDR9'), ('arte.ai', b'%PDF-1.5'), ('arte.ai', b'%!PS-Adobe'),
('foto.jpeg', b'\xff\xd8\xff'), ('arte.png', b'GIF89a')):
self.assertEqual(real_name(name, head), name, (name, head))
def test_a_large_file_passes_on_its_content(self):
self.assertEqual(check_large(b'%PDF-1.7', {'name': 'folha.cdr'})[0], 'clean')
self.assertEqual(check_large(b'MZ\x90\x00', {'name': 'folha.cdr'})[0], 'rejected')
if __name__ == '__main__':
unittest.main()