Upload check: `isValidGlbMagic` skips the declared-length check the server enforces

Aperta Adatta ai principianti
#341 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
88/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
javascript
Ambito
frontend

Direzione di ricerca

Inizia da src/shared/glb-magic.js, dove isValidGlbMagic verifica il magic e la versione, ma non legge mai la lunghezza dichiarata all'offset di byte 8. Usa il controllo lato server in api/_lib/glb-inspect.js come riferimento: rifiuta quando la lunghezza dichiarata è inferiore a 20 o superiore a file.size, leggendo comunque solo file.slice(0, 12). Esegui prima la probe nell'issue; dopo la correzione deve restituire false per entrambi gli header. È completato quando il nuovo tests/glb-magic.test.js passa con npx vitest run e i criteri di accettazione sono soddisfatti.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug good first issue

Context

Before a user's 3D model is uploaded, the browser checks its 12-byte GLB header so a misnamed JPEG is rejected instantly instead of after a full upload. That check is isValidGlbMagic in src/shared/glb-magic.js, used by src/create-agent.js and src/agent-edit.js.

Its header comment says it "mirrors the server-side isValidGlbHeader() in api/_lib/glb-inspect.js" and that it checks "that the declared file length field is at least the minimum GLB size". It does not check the length field at all. It checks the magic bytes and the version, then returns true.

The server version in api/_lib/glb-inspect.js does more:

const declaredLen = view.getUint32(8, true);
if (declaredLen > buf.length || declaredLen < 20) return false;

So a truncated or corrupted GLB (a valid 12-byte header whose declared length is larger than the file, or smaller than 20) passes the browser check and is only rejected after it has been uploaded. The two checks are supposed to agree, and today they do not. There is also no test for glb-magic.js.

Verified on main:

node --input-type=module -e "
import { isValidGlbMagic } from './src/shared/glb-magic.js';
const header = (declared) => { const b = new ArrayBuffer(12); const v = new DataView(b); v.setUint32(0, 0x46546c67, true); v.setUint32(4, 2, true); v.setUint32(8, declared, true); return b; };
console.log('declares 5000 bytes, file is 12:', await isValidGlbMagic(new File([header(5000)], 'a.glb')));
console.log('declares 8 bytes:', await isValidGlbMagic(new File([header(8)], 'a.glb')));
"
declares 5000 bytes, file is 12: true
declares 8 bytes: true

Both should be false, because the server rejects both.

What to change

In isValidGlbMagic, read the declared length (little-endian uint32 at byte offset 8) and return false when it is below 20 or larger than file.size, matching the server. Keep reading only the first 12 bytes; the point of the function is that it never buffers the whole file. Make the header comment match what the code does.

Add tests/glb-magic.test.js. Node 20 and later have a global File, so tests can build headers the way the probe above does.

Acceptance criteria

  • A valid header whose declared length equals the file size returns true.
  • Declared length larger than file.size returns false.
  • Declared length below 20 returns false.
  • Wrong magic, version 1, a file under 12 bytes, and a missing file all return false (these already do; lock them in).
  • The function still reads only file.slice(0, 12).
  • The header comment describes exactly the checks the code performs.

How to test

npx vitest run tests/glb-magic.test.js
npm test

Good first issue: a small, well-bounded correctness fix with a clear reference implementation next door. Comment "taking this" before you start.

Lingua principale
JavaScript
Stelle
230
Fork
63
Merge medio
15h 43m
PR unite (30g)
225

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di nirholas/three.ws

Tutte le issue di nirholas/three.ws

Issue simili

Altre issue su JavaScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.