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

@@ -167,14 +167,14 @@ try{
assert.deepEqual(await evaluate(`({shown:!$('tipoEnvio').hidden,
on:[...document.querySelectorAll('#tipoEnvio .cam.on')].map(b=>b.dataset.tipo)})`),
{shown:true,on:['folha']});
await evaluate(`(()=>{const f=new File(['x'],'oversize.cdr');Object.defineProperty(f,'size',{value:1073741824+1});sel([f]);})()`);
await evaluate(`(()=>{const f=new File(['x'],'oversize.cdr');Object.defineProperty(f,'size',{value:1073741824+1});return sel([f]);})()`);
assert.equal(await evaluate(`folhas.length===0 && $('recusa').textContent.includes('1 GB')`),true,
'files beyond the 1 GB upload limit are rejected before browser analysis');
// A large sheet (here 900 MB) is the normal order: graded from the pixel size in its
// header, never decoded. 6732 x 35433 px across 57 cm is 3 m at 300 DPI.
await evaluate(`(()=>{const h=new Uint8Array(33);h.set([0x89,0x50,0x4E,0x47,0x0D,0x0A,0x1A,0x0A,0,0,0,13,0x49,0x48,0x44,0x52]);
const v=new DataView(h.buffer);v.setUint32(16,6732);v.setUint32(20,35433);h.set([8,6,0,0,0],24);
const f=new File([h],'folha-grande.png');Object.defineProperty(f,'size',{value:900*1048576});sel([f]);})()`);
const f=new File([h],'folha-grande.png');Object.defineProperty(f,'size',{value:900*1048576});return sel([f]);})()`);
await waitFor(()=>evaluate(`folhas.length===1 && !!folhas[0].an`),'large sheet graded from its header');
assert.deepEqual(await evaluate(`({grande:folhas[0].an.grande,dpi:folhas[0].an.dpi,alt:folhas[0].med.alt,preview:!!folhas[0].previewSrc})`),
{grande:true,dpi:300,alt:300,preview:false});
@@ -185,11 +185,14 @@ try{
// 2026-10-23), with the reason, before anything is read.
await evaluate(`sel([new File(['x'],'arte.jpg',{type:'image/jpeg'})])`);
assert.equal(await evaluate(`artes.length===0 && $('recusa').textContent.includes('JPG não dá para enviar')`),true);
// The content decides, not the name (D-13): a JPG called ".png" is refused as a JPG.
await evaluate(`sel([new File([new Uint8Array([0xFF,0xD8,0xFF,0xE0])],'disfarce.png',{type:'image/png'})])`);
assert.equal(await evaluate(`artes.length===0 && $('recusa').textContent.includes('disfarce.png é JPG')`),true);
// A large WebP artwork cannot be read in parts: it is measured from its
// header and placed as a full box, with its grade, instead of refused.
await evaluate(`(()=>{const h=new Uint8Array(30);h.set([0x52,0x49,0x46,0x46,0,0,0,0,0x57,0x45,0x42,0x50,0x56,0x50,0x38,0x58]);
h.set([0x39,0x09,0x00,0x39,0x09,0x00],24);
const f=new File([h],'arte-grande.webp');Object.defineProperty(f,'size',{value:400*1048576});sel([f]);})()`);
const f=new File([h],'arte-grande.webp');Object.defineProperty(f,'size',{value:400*1048576});return sel([f]);})()`);
await waitFor(()=>evaluate(`artes.length===1 && artes[0].semPrevia===true`),'large WebP artwork measured from its header');
await fill('[data-cm]',20);
await waitFor(()=>evaluate(`!!itemAtual?.production?.sources?.length`),'large WebP artwork packed');
@@ -375,6 +378,12 @@ try{
assert.equal(await evaluate(`avaliar().pronto`),false);
assert.equal(await evaluate(`cartPodeEnviar()`),false);
assert.match(await evaluate(`$('lista').textContent`),/não pode ser orçado/);
// A ".cdr" that is a PDF (D-13) is renamed and measured as a PDF, not handled by hand.
await click('#bVoltar');await click('[data-modo="file"]');
await evaluate(`sel([${pdfFile(pdfData(),'folha.cdr')}])`);
await waitFor(()=>evaluate(`folhas.length===1 && !!(folhas[0].med || folhas[0].measurementError)`),'renamed PDF measured');
assert.deepEqual(await evaluate(`({name:folhas[0].f.name,measured:!!folhas[0].med,manual:folhaManual()})`),
{name:'folha.pdf',measured:true,manual:false});
await click('#bVoltar');await click('[data-modo="avulsa"]');
await evaluate(`sel([new File(['not an image'],'broken.png',{type:'image/png'})])`);
await waitFor(()=>evaluate('artes.length===1 && artes[0].decodeError===true'),'failed image decode');

View File

@@ -52,7 +52,7 @@ try{
await waitFor(()=>site.eval('typeof window.dtfCheckout === "function"'),'checkout bridge');
// Prove escaping itself, independently of the CSP's second line of defense.
await site.call('Page.setBypassCSP',{enabled:true});
await site.eval(`(()=>{window.xssProbe=0;abrir('file');sel([new File(['test'],'<img src=x onerror=window.xssProbe=1>.cdr')]);})()`);
await site.eval(`(()=>{window.xssProbe=0;abrir('file');return sel([new File(['test'],'<img src=x onerror=window.xssProbe=1>.cdr')]);})()`);
await pause(500);
assert.equal(await site.eval('window.xssProbe'),0);
assert.equal(await site.eval('document.querySelectorAll("#lista img[onerror]").length'),0);

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()