Skip to content

viewer: harden parseCamHash and formatCamHash for multi-param and non-decimal hashes - #4912

Open
DenRakEiw wants to merge 2 commits into
ItsNotRexed:mainfrom
DenRakEiw:fix/viewer-cam-hash-validation
Open

DenRakEiw wants to merge 2 commits into
ItsNotRexed:mainfrom
DenRakEiw:fix/viewer-cam-hash-validation

Conversation

@DenRakEiw

Copy link
Copy Markdown
Contributor

Hardens parseCamHash() and formatCamHash() in viewer/index.html:

  • Accepts cam= after another &-delimited hash parameter (e.g. #foo=1&cam=1,2,3,4,5&bar=2) while still rejecting unrelated parameter names like #notcam=....
  • Rejects non-decimal JavaScript numeric literals (0x10, 0b10, 0o10) and overflow literals (1e309) instead of accepting them via Number().
  • Normalizes -0.0 to +0 in both parseCamHash() and formatCamHash().
  • Returns "" from formatCamHash() on non-finite inputs (NaN, Infinity) and skips history.replaceState when empty.
  • Extended camera hash round-trips and malformed hashes are ignored in tests/test_viewer_streaming.cjs.

Claim

Claimed in #4816: #4816 (comment)

Testing

  • Environment: Windows 11, Node.js v24.18.0 (no browser, no GPU, no game files).
  • node --test tests/test_viewer_streaming.cjs: 51/51 pass.
  • git diff --check: clean.

AI-assisted (Gemini 4 Neon).

@dcastell1

Copy link
Copy Markdown
Contributor

Code read + CPU tests, 788a7f9: pass (test_viewer_serve: 39).

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

The P key copies a stale camera link when the current position is invalid. At viewer/index.html:241, the clipboard call runs even when formatCamHash returns an empty string.

I ran the head's keydown handler with a clipboard spy on Windows (Node.js v24.19.0). To reproduce:

  1. Set yaw and pitch to 0. Set the camera position to [6, 7, 8]. Send {code: "KeyP", repeat: false}.
  2. Set the camera position to [NaN, 7, 8]. Send the same key event.
  3. Inspect the calls to navigator.clipboard.writeText. Both calls copy a URL ending in #cam=6.0,7.0,8.0,0.000,0.000.

Expected: the second key press leaves the clipboard unchanged because there is no link for the current camera. Return early when nextHash is empty. A handler test can check that the clipboard spy receives only one call.

All 51 tests in node --test tests/test_viewer_streaming.cjs pass on 788a7f9. The same handler probe on merge base d974c88 copies a URL containing NaN on the second press. I used the actual handler with simulated history and clipboard objects. Browser integration remains untested.

AI-assisted.

@whattheflop whattheflop left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent Node VM check, AI-assisted. The head restored existing camera links unchanged and newly restored #foo=1&cam=1,2,3,4,5&bar=2. Two thousand generated valid poses round-tripped at the existing precision on both revisions.

One inherited behavior becomes more visible now that the reader accepts other fragment parameters: pressing P on that multi-parameter link replaces the whole fragment with #cam=…, so foo and bar are dropped on both parent and head. Preserving those parameters, or documenting that P writes a camera-only link, would make the read/write behavior explicit. This is separate from the stale clipboard result already reported.

The dry merge with #4874 at e1e5600d was clean (tree 7c24fe5b); both focused camera and empty-scene tests passed in the combined snapshot. No browser or GPU run.

@DenRakEiw

Copy link
Copy Markdown
Contributor Author

Good catch, @shdann — updated in bdc93207 so KeyP only calls
avigator.clipboard?.writeText(location.href) when
extHash is non-empty (
ode --test tests/test_viewer_streaming.cjs: 51/51 pass). AI-assisted (Gemini 4 Neon).

@memorypressure

Copy link
Copy Markdown

Merged with current main (3faac55): node --test tests/test_viewer_streaming.cjs passes 51/51 (re-run on node v26), and the extended assertions fail against main's viewer/index.html — a 0x10 x-coordinate parses as 16, and 1e309 becomes Infinity that the old length-only check let through. The decimal-only gate, the &cam= alternative and the -0 normalization are all correct, and returning empty from formatCamHash for a non-finite camera plus the KeyP guard is the right way to keep a NaN camera out of the URL and the clipboard. Two notes: the KeyP path is only covered indirectly via formatCamHash (no DOM in the harness), and CI reported no checks on this branch. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run).

@whattheflop

Copy link
Copy Markdown
Contributor

T3 Preview integration check, AI-assisted. #4912 at bdc93207 and the asset viewer #4763 at 66c88956 merged cleanly onto 3faac557 as tree 5d3948ca. All 60 Node viewer tests passed. On the generated test terrain, P sent the expected rounded camera pose to a clipboard stub after loading a multi-parameter fragment. The copied link contains only cam; transport-only fragment parameters are dropped. Following Assets and then Map loads the default map URL with an empty fragment. The new parser and both navigation links worked in the same preview. Synthetic terrain only.

@kapitche

Copy link
Copy Markdown
Contributor

Tested bdc9320 merged with main 3faac55 on Linux (WSL2), CPython 3.12.16, Node v22.23.5, CPU only: pass

  • node --test tests/test_viewer_streaming.cjs: 51 run, 51 passed, 0 failed.
  • Negative control: viewer/index.html restored from main, same test file: 51 run, 50 passed, 1 failed.
  • By hand against the PR's parseCamHash/formatCamHash: #foo=1&cam=1,2,3,0,0 and #cam=1,2,3,0,0&x=1 parse; #cam=0x10,..., #cam=Infinity,..., a 4-value list, #xcam=... and #a=b&xcam=... return null; -0 and a value that rounds to zero print as 0.0/0.000; a NaN position gives "", so P leaves the hash and clipboard alone.
  • tests/test_viewer_serve.py (not touched by this PR) errors here with ConnectionRefused (110 errors, same on current main in this WSL setup), so it says nothing about this change.

@Atomic258

Copy link
Copy Markdown
Contributor

Tested bdc9320 on Windows Server 2022, CPython 3.13.12: pass

  • node tests/test_viewer_streaming.cjs -> 51 passed, 0 failed
  • viewer/index.html change; suite green.

git diff --check clean; python -m compileall -q exit 0. No game files, no GPU. AI-assisted (Devin).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants