Skip to content

fix(lite): validate sort state on client writes and restrict persist restore - #2162

Merged
luanfreitasdev merged 2 commits into
Power-Components:7.xfrom
0z3:fix/lite-sort-persist-hardening
Nov 17, 2026
Merged

luanfreitasdev merged 2 commits into
Power-Components:7.xfrom
0z3:fix/lite-sort-persist-hardening

Conversation

@0z3

@0z3 0z3 commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

⚡ PowerGrid - Pull Request

  • Bug fix
  • Enhancement
  • New feature
  • Breaking change

Description

Two hardening fixes for the Lite traits. Neither is a SQL injection: Laravel still quotes the column and rejects a bad direction in orderBy(). They close gaps that the main grid already guards.

WithSorting

sortField, sortDirection and sortArray are public, so a client could set them directly and skip the field-name check and the sortableFields allowlist in sortBy(). Client writes now go through the same rules:

  • an invalid or non-allowlisted sortField is reset to ''
  • sortDirection is forced to asc/desc
  • sortArray drops non-sortable keys and forces each direction to asc/desc

This uses the trait-level updatedWithSorting hook, so a component that defines its own updatedSortField does not replace the guard. Invalid writes are corrected silently, as sortBy() already does.

WithPersist

restorePersistedState() assigned every key of the stored JSON to any property that exists on the component. With the cookie driver that was safe only because Laravel encrypts cookies. It now restores only the keys persistState() writes for the configured persist items, then re-validates the restored sort state.

Tests

Three tests added to tests/Feature/Lite/LitePersistTest.php: direct property writes are rejected, restore ignores keys it does not persist, and a valid persisted sort still restores. Full suite, PHPStan and Pint pass.

Related Issue(s):

Documentation

This PR requires Documentation update?

  • Yes
  • No
  • I have already submitted a Documentation pull request.

…restore

Sort properties in the Lite WithSorting trait are public, so a client could
set sortField, sortDirection and sortArray directly and skip the checks in
sortBy(). Client writes now go through the same field and direction rules.

WithPersist restored every key of the stored state onto any matching
property. It now restores only the keys persistState() writes, then
re-validates the restored sort state.

@luanfreitasdev luanfreitasdev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for this PR!

Note: isSortableField() reads $this->sortableFields, which in the tests (and the obvious usage) is a public property, so it is client-writable like the sort props this PR guards. Could we read the allowlist from a sortableFields(): array method when present, falling back to the property, and mark the test properties #[Locked]? Keeps BC and makes the allowlist server-only.

Minor: could the test values be neutral (e.g. 'sideways', 'bad field') instead of SQL-looking strings?

A public $sortableFields property is client-writable unless locked, so
prefer a sortableFields() method and fall back to the property for BC.
Lock the test allowlist properties and use neutral invalid test values.
@0z3

0z3 commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for this PR!

Note: isSortableField() reads $this->sortableFields, which in the tests (and the obvious usage) is a public property, so it is client-writable like the sort props this PR guards. Could we read the allowlist from a sortableFields(): array method when present, falling back to the property, and mark the test properties #[Locked]? Keeps BC and makes the allowlist server-only.

Minor: could the test values be neutral (e.g. 'sideways', 'bad field') instead of SQL-looking strings?

hey :) yes I adjusted it :)

@luanfreitasdev
luanfreitasdev self-requested a review November 17, 2026 18:09
@luanfreitasdev
luanfreitasdev merged commit 1aad757 into Power-Components:7.x Nov 17, 2026
21 checks passed
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.

2 participants