fix(sandbox): reject non-finite/non-integer express ids in toRef()

Merged
PE
3mo ago
+45-2

Summary

toRef() in packages/sandbox/src/bridge-helpers.ts checked typeof ref.expressId === 'number' but never that the number was a finite positive integer. typeof is true of NaN, Infinity, negative numbers, and fractions, so any of those shapes passed the boundary and were handed to bim.mutate.* / bim.store.* / bim.query.* as if they named a real entity.

For bim.mutate.setProperty / setAttribute / deleteProperty, this meant a mutation got recorded under a key no real entity has and no export path ever emits — with no exception raised. The sandbox script reports success; the edit goes nowhere. This matters because joining uploaded CSV data to a model is a documented sandbox use case, and a CSV column parsed to a number is exactly where a NaN or fractional value comes from.

Sibling that already got this right: bridge-store.ts's requireStoreyId (packages/sandbox/src/bridge-store.ts:319-323) enforces Number.isInteger(id) && id > 0 and throws bim.store.${op}: storeyExpressId must be a positive integer, got ${id} when it doesn't hold. The shared toRef boundary is the one that never got the same treatment.

Found and deliberately deferred while fixing a different, unrelated defect in #3470 (see the PR description and comments there).

Behaviour chosen

toRef now returns null for a non-finite/non-integer/non-positive expressId, matching its own existing convention (return null for any unusable shape). No new throw was added to toRef itself — every one of its 30+ call sites across bridge-mutate.ts, bridge-store.ts, and bridge-query.ts already does if (!ref) throw new Error('bim.X.method: invalid entity reference') on a null result, so the fix flows straight into the existing throw path. This keeps toRef consistent with itself (it already returns null, never throws, for a bad modelId type, a missing field, etc.) while landing on the same externally observable behaviour as requireStoreyId — a bad id throws — via the existing call-site wiring rather than inventing a second validation shape inside toRef.

Observable behaviour change: a script that previously computed a bad express id and appeared to "succeed" (the mutation was silently discarded) will now throw. A script that only ever passes valid express ids is unaffected. Since a sandbox script is user-authored code with the user present, silent no-op was the one clearly wrong prior behaviour to preserve.

Evidence

RED (before the fix, pnpm exec vitest run src/bridge-helpers.test.ts):

FAIL  src/bridge-helpers.test.ts > toRef > rejects an expressId that is not a finite positive integer
AssertionError: expected { modelId: 'm1', expressId: NaN } to be null
+ Received: { "expressId": NaN, "modelId": "m1" }

GREEN (after): 14 passed (14) in bridge-helpers.test.ts; full package suite 206 passed (206).

Each invalid shape is covered as its own assertion (NaN, Infinity, -Infinity, -1, 1.5), plus a regression guard that expressId: 1 still round-trips unchanged.

Existing test coverage

bridge-helpers.test.ts already existed and tests toRef's shape validation thoroughly (wrong types, missing fields, nullish input) but had no case for a numerically-typed-but-invalid expressId — this PR adds that case rather than editing an assertion that was asserting the buggy behaviour.

Other unvalidated boundaries found (not fixed here, out of scope)

  • packages/sandbox/src/bridge-query.ts:61-63 — bim.query.entity(modelId, expressId) takes args[1] as number directly and builds a ref without going through toRef or any integer check at all. Lower risk than the mutate path since it's a read lookup (an unknown id most likely just returns null from sdk.entity, not silent corruption), but it's the same class of gap and worth a follow-up.
  • packages/sandbox/src/bridge-create-schedule.ts and bridge-create.ts:473 (setColor) take raw numeric handles/element ids for the in-memory creator subsystem (not real STEP express ids in a parsed document) without an integer/positivity check. Different subsystem, flagging for completeness.

Module size

node scripts/check-module-size.mjs → OK (2021 files measured, 307 allowlisted, 0 new over 400).

Changeset

@ifc-lite/sandbox is not private (published), so .changeset/toref-express-id-validation.md is included (patch bump).