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

Abierto Apto para principiantes
#341 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
88/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
javascript
Área
frontend

Línea de trabajo

Empieza en src/shared/glb-magic.js, donde isValidGlbMagic comprueba el magic y la versión, pero nunca lee la longitud declarada en el desplazamiento de bytes 8. Usa la comprobación del servidor en api/_lib/glb-inspect.js como referencia: rechaza cuando la longitud declarada es menor que 20 o mayor que file.size, leyendo aún solo file.slice(0, 12). Ejecuta primero la prueba del issue; una vez corregido, debe devolver false para ambas cabeceras. Está terminado cuando el nuevo tests/glb-magic.test.js pasa con npx vitest run y se cumplen los criterios de aceptación.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
JavaScript
Estrellas
230
Forks
63
Merge medio
15 h 43 min
PR fusionados (30 d)
225

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de nirholas/three.ws

Todos los issues de nirholas/three.ws

Issues similares

Más issues de JavaScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.