Skip to content

fix(data-table): harden sort and selection helpers#46

Merged
karim-semmoud merged 2 commits into
mainfrom
fix/datatable-helper-hardening
Jun 1, 2026
Merged

fix(data-table): harden sort and selection helpers#46
karim-semmoud merged 2 commits into
mainfrom
fix/datatable-helper-hardening

Conversation

@karim-semmoud

Copy link
Copy Markdown
Member

Summary

Hardens Corex.DataTable.Sort and Corex.DataTable.Selection against client-forged LiveView event params.

Previously, a malicious or malformed "sort_by" could crash the LiveView via String.to_existing_atom/1, or drive sorting on arbitrary fields. Forged row checkbox ids could be added to "selected" and used for bulk actions without server validation.

Changes

lib/components/data_table/sort.ex
New :sort_columns option on assign_for_sort/3 (stored as :sort_columns assign)
handle_sort/3 accepts only whitelisted columns when :sort_columns is set
Invalid or unknown "sort_by" values are ignored (socket unchanged, no crash)
safe_existing_atom/1 rescues ArgumentError from String.to_existing_atom/1
lib/components/data_table/selection.ex
handle_select/3 validates row ids against current rows via selection_row_id
Forged ids are ignored when checking a row
:selected is filtered to valid ids only; select-all state uses valid ids

Docs

Updated moduledoc examples in sort.ex, selection.ex, and data_table.ex to show sort_columns: [:id, :name]

Tests

test/components/data_table_sort_test.exs: unknown atom, whitelist rejection
test/components/data_table_selection_test.exs: forged row id, stale selection cleanup
Consumer migration
Add sort_columns when using sortable tables:

|> Corex.DataTable.Sort.assign_for_sort(:users,
default_sort_by: :id,
default_sort_order: :asc,
sort_columns: [:id, :name]
)
Selection API is unchanged; validation is automatic when using handle_select/3.

Whitelist sort columns via sort_columns, ignore invalid sort_by without
crashing, and only accept row ids that exist in the current rows assign.
@karim-semmoud karim-semmoud self-assigned this Jun 1, 2026
@karim-semmoud karim-semmoud changed the title fix(data-table): harden sort and selection helpers against forged client params fix(data-table): harden sort and selection helpers Jun 1, 2026
@karim-semmoud karim-semmoud merged commit 7641fb4 into main Jun 1, 2026
14 checks passed
@karim-semmoud karim-semmoud deleted the fix/datatable-helper-hardening branch June 1, 2026 11:57
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