fix(data-table): harden sort and selection helpers#46
Merged
Conversation
Whitelist sort columns via sort_columns, ignore invalid sort_by without crashing, and only accept row ids that exist in the current rows assign.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.