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

オープン 初心者向け
#341 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
88/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
javascript
領域
frontend

調査の方向性

src/shared/glb-magic.js から始めてください。isValidGlbMagic はマジックとバージョンを検証していますが、バイトオフセット 8 にある宣言された長さを読んでいません。参照として api/_lib/glb-inspect.js のサーバー側チェックを使ってください。宣言された長さが 20 未満、または file.size より大きい場合は拒否します。読み取るのは引き続き file.slice(0, 12) のみです。まず issue の probe を実行してください。修正後は、両方のヘッダーに対して false を返すはずです。新しい tests/glb-magic.test.js が npx vitest run で通り、受け入れ基準を満たせば完了です。

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
JavaScript
スター
230
フォーク
63
平均マージ
15時間 43分
マージ済み PR(30日)
225

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

nirholas/three.ws のほかの issue

nirholas/three.ws の issue をすべて見る

似ている issue

JavaScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。