Skip to content

fix(sections-editor): add bounds checks to array field operations - #6109

Open
pedrofrxncx wants to merge 1 commit into
mainfrom
fix/array-field-bounds-checks-w4
Open

fix(sections-editor): add bounds checks to array field operations#6109
pedrofrxncx wants to merge 1 commit into
mainfrom
fix/array-field-bounds-checks-w4

Conversation

@pedrofrxncx

@pedrofrxncx pedrofrxncx commented Aug 14, 2026

Copy link
Copy Markdown
Collaborator

Summary

Adds defensive bounds checks to array field operations (openItem, duplicateItem, toggleItemHidden, removeItem, updateItem) to prevent undefined item access if entry indices ever become desynchronized with items.

Why This Matters

The rendering loop already has a defensive bounds check (line 551-552: if (item === undefined) return null;), but the action handlers were missing equivalent checks. If entry indices ever become out-of-bounds due to a bug in resizeArrayEntries, removeEntryAt, or insertEntryAfter, these handlers would operate on undefined items, leading to crashes or silent failures in label computation, item cloning, or hidden-item toggling.

This fix ensures all array operations fail safely on invalid indices instead of propagating undefined values.

Changes

  • openItem(): Guard against index < 0 || index >= items.length
  • duplicateItem(): Guard against index < 0 || index >= items.length
  • toggleItemHidden(): Guard against index < 0 || index >= items.length
  • removeItem(): Guard against index < 0 || index >= items.length
  • updateItem(): Guard against index < 0 || index >= items.length

Verification

  • All 628 sections-editor tests pass
  • TypeScript (bunx tsc --noEmit) green in apps/web
  • Linter (bunx oxlint) clean on modified file
  • Formatting verified with bun run fmt

Testing Command

bun test apps/web/src/components/sections-editor/
bunx tsc --noEmit --cwd apps/web
bunx oxlint apps/web/src/components/sections-editor/fields/array-field.tsx

Summary by cubic

Prevents undefined item access in the sections editor by adding bounds checks to array field handlers. Previously these handlers acted on out-of-range indices; now they return early to avoid crashes and unintended mutations.

Details for review

  • Add index < 0 || index >= items.length guards to openItem, duplicateItem, toggleItemHidden, removeItem, and updateItem in apps/web/src/components/sections-editor/fields/array-field.tsx; openItem also preserves the suppressClickRef check.
  • Aligns handler behavior with the existing render-time guard, containing issues from desynchronized entries produced by resizeArrayEntries, removeEntryAt, or insertEntryAfter.

Rollout

  • No migration required.

Written for commit e91b8da. Summary will update on new commits.

Review in cubic

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.

1 participant