viewer: pin three.js CDN URLs with Subresource Integrity (OLD-8618) #133

Merged
ItsMrDubey merged 1 commit from upstream-pr/133 into main 2026-11-24 03:09:16 +00:00
ItsMrDubey commented 2026-11-24 02:24:59 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by ItsMrDubey on 2026-11-24T02:24:59Z.


Claimed in the coordination issue as: OLD-8618 (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7119646964)

What and why

Old #8618: the viewer loaded three.js from CDNs with no integrity check, so a compromised or changed CDN file would run in the page.

  • viewer/index.html: the import map now has an "integrity" table (Chromium 127+, Firefox 138+, Safari 18+) for every file of the module graph: three.module.js, GLTFLoader.js and the utils/BufferGeometryUtils.js it imports. The same hashes are on <link rel="modulepreload" crossorigin="anonymous" integrity=...> links after the import map, so browsers without import-map integrity still check them (the imports reuse the preloaded fetch). Version stays pinned at [email protected].
  • viewer/studio.html: the three r128 classic scripts carry integrity + crossorigin="anonymous". The hash covers both the /vendor/ copy that tools/gallery.py serves and the CDN fallback.
  • Also fixed the studio fallback itself. It set this.src in onerror, and a script element that already ran never fetches again (checked in headless Chrome: the fallback script never loaded). It now document.writes a new <script> with the CDN URL and this.integrity, so it still runs before the scripts that follow it.
  • New tests/test_viewer_cdn_integrity.py (standard library). It checks that every remote URL in viewer/*.html has an exact version and a sha384/sha512 hash, that crossorigin is set, that fallbacks reuse the tag's hash, that the import map and modulepreload hashes agree, and that every module import resolves to a hashed URL. It also has checker cases built from hand-written HTML.

Hashes are sha384 of the files as served. jsdelivr and unpkg serve identical bytes for every 0.160.0 file, and cdnjs and jsdelivr serve identical bytes for r128 three.min.js. jsdelivr and unpkg also match for the r128 example files, so one hash per file is enough. No JS is vendored, since the repo doesn't vendor any.

Tested

Windows 11, Python 3.11.9, Node 22.23.2, no GPU needed.

  • python -I tests/test_viewer_cdn_integrity.py: 10 tests OK. With viewer/ from origin/main it fails (2 failures, 1 error).
  • python -I tests/test_viewer_serve.py: 39 OK (1 skipped). python -I tests/test_gallery.py: 29 OK.
  • node --test tests/test_viewer_streaming.cjs tests/test_viewer_studio.cjs: 67/68. The one failure ("Shift speeds the camera up…", AbortController is not defined) also fails on origin/main and is addressed by #13.
  • Headless Chrome 158, pages served from 127.0.0.1:
    • index.html's import map + preload block loads three r160 and GLTFLoader. A wrong hash in the map alone blocks the load, and so does a wrong hash in a preload link alone.
    • studio.html with no /vendor/ loads r128, OrbitControls and GLTFLoader from the CDN. With identical /vendor/ copies it loads them locally. With a tampered hash nothing loads.
  • python -I scripts/check_no_game_data.py viewer/index.html viewer/studio.html tests/test_viewer_cdn_integrity.py: clean.

AI-assisted: yes

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • No new translator rule, so nothing to add to the contract table of docs/design.md.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5464901959 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/133). Originally posted by [ItsMrDubey](https://github.com/ItsMrDubey) on 2026-11-24T02:24:59Z. --- Claimed in the coordination issue as: OLD-8618 (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7119646964) ### What and why Old #8618: the viewer loaded three.js from CDNs with no integrity check, so a compromised or changed CDN file would run in the page. - `viewer/index.html`: the import map now has an `"integrity"` table (Chromium 127+, Firefox 138+, Safari 18+) for every file of the module graph: `three.module.js`, `GLTFLoader.js` and the `utils/BufferGeometryUtils.js` it imports. The same hashes are on `<link rel="modulepreload" crossorigin="anonymous" integrity=...>` links after the import map, so browsers without import-map integrity still check them (the imports reuse the preloaded fetch). Version stays pinned at `[email protected]`. - `viewer/studio.html`: the three r128 classic scripts carry `integrity` + `crossorigin="anonymous"`. The hash covers both the `/vendor/` copy that `tools/gallery.py` serves and the CDN fallback. - Also fixed the studio fallback itself. It set `this.src` in `onerror`, and a script element that already ran never fetches again (checked in headless Chrome: the fallback script never loaded). It now `document.write`s a new `<script>` with the CDN URL and `this.integrity`, so it still runs before the scripts that follow it. - New `tests/test_viewer_cdn_integrity.py` (standard library). It checks that every remote URL in `viewer/*.html` has an exact version and a sha384/sha512 hash, that `crossorigin` is set, that fallbacks reuse the tag's hash, that the import map and modulepreload hashes agree, and that every module import resolves to a hashed URL. It also has checker cases built from hand-written HTML. Hashes are sha384 of the files as served. jsdelivr and unpkg serve identical bytes for every 0.160.0 file, and cdnjs and jsdelivr serve identical bytes for r128 `three.min.js`. jsdelivr and unpkg also match for the r128 example files, so one hash per file is enough. No JS is vendored, since the repo doesn't vendor any. ### Tested Windows 11, Python 3.11.9, Node 22.23.2, no GPU needed. - `python -I tests/test_viewer_cdn_integrity.py`: 10 tests OK. With `viewer/` from origin/main it fails (2 failures, 1 error). - `python -I tests/test_viewer_serve.py`: 39 OK (1 skipped). `python -I tests/test_gallery.py`: 29 OK. - `node --test tests/test_viewer_streaming.cjs tests/test_viewer_studio.cjs`: 67/68. The one failure ("Shift speeds the camera up…", `AbortController is not defined`) also fails on origin/main and is addressed by #13. - Headless Chrome 158, pages served from 127.0.0.1: - index.html's import map + preload block loads `three` r160 and `GLTFLoader`. A wrong hash in the map alone blocks the load, and so does a wrong hash in a preload link alone. - studio.html with no `/vendor/` loads r128, `OrbitControls` and `GLTFLoader` from the CDN. With identical `/vendor/` copies it loads them locally. With a tampered hash nothing loads. - `python -I scripts/check_no_game_data.py viewer/index.html viewer/studio.html tests/test_viewer_cdn_integrity.py`: clean. AI-assisted: yes ### Checklist - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [x] No new translator rule, so nothing to add to the contract table of `docs/design.md`.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!133
No description provided.