Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Lite/Traits/WithPersist.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,18 @@ private function restorePersistedState(): void

$state = (array) json_decode(strval($storage), true);

// Restore only what persistState() writes, never an arbitrary property.
$persistable = array_keys($this->gatherPersistableState());

foreach ($state as $property => $value) {
if (property_exists($this, $property)) {
if (in_array($property, $persistable, true) && property_exists($this, $property)) {
$this->{$property} = $value;
}
}

if (method_exists($this, 'normalizeSortState')) {
$this->normalizeSortState();
}
}

private function getPersistStorageKey(): string
Expand Down
51 changes: 48 additions & 3 deletions src/Lite/Traits/WithSorting.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ trait WithSorting

public function sortBy(string $field, string $direction = 'asc'): void
{
$allowed = property_exists($this, 'sortableFields') ? (array) $this->sortableFields : null;

if (preg_match('/^[A-Za-z_][\w.]*$/', $field) !== 1 || ($allowed !== null && ! in_array($field, $allowed, true))) {
if (! $this->isSortableField($field)) {
return;
}

Expand All @@ -44,6 +42,53 @@ public function sortBy(string $field, string $direction = 'asc'): void
$this->afterSortBy();
}

/**
* The sort properties are public, so a client can write them without going
* through sortBy(). Hold those writes to the same rules.
*/
public function updatedWithSorting(string $name): void
{
if (in_array(explode('.', $name)[0], ['sortField', 'sortDirection', 'sortArray'], true)) {
$this->normalizeSortState();
}
}

private function normalizeSortState(): void
{
if (! $this->isSortableField($this->sortField)) {
$this->sortField = '';
}

$this->sortDirection = Sql::sanitizeSortDirection($this->sortDirection);

$sortArray = [];

foreach ($this->sortArray as $field => $direction) {
if (is_string($field) && $this->isSortableField($field)) {
$sortArray[$field] = Sql::sanitizeSortDirection(is_string($direction) ? $direction : null);
}
}

$this->sortArray = $sortArray;
$this->sortCache = [];
}

/**
* Prefer a sortableFields() method: a public property is client-writable
* unless marked #[Locked].
*/
private function isSortableField(string $field): bool
{
$allowed = match (true) {
method_exists($this, 'sortableFields') => (array) $this->sortableFields(),
property_exists($this, 'sortableFields') => (array) $this->sortableFields,
default => null,
};

return preg_match('/^[A-Za-z_][\w.]*$/', $field) === 1
&& ($allowed === null || in_array($field, $allowed, true));
}

public function isSorted(string $field): bool
{
return $this->sortCache["sorted:{$field}"] ??= $this->multiSort
Expand Down
74 changes: 74 additions & 0 deletions tests/Feature/Lite/LitePersistTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Illuminate\Auth\GenericUser;
use Illuminate\Support\Facades\{Cache, Cookie};
use Livewire\Attributes\Locked;
use Livewire\{Component, Livewire};
use PowerComponents\LivewirePowerGrid\Lite\Traits\{WithPersist, WithSorting};

Expand Down Expand Up @@ -51,6 +52,7 @@ class LiteAllowlistSortComponent extends LitePersistComponent
{
public array $persist = [];

#[Locked]
public array $sortableFields = ['name'];
}

Expand All @@ -64,3 +66,75 @@ class LiteAllowlistSortComponent extends LitePersistComponent
->assertSet('sortField', 'name')
->assertSet('sortDirection', 'asc');
});

it('rejects lite sort state written straight to the properties', function () {
Livewire::test(LiteAllowlistSortComponent::class)
->set('sortField', 'password')
->assertSet('sortField', '')
->set('sortField', 'name')
->assertSet('sortField', 'name')
->set('sortDirection', 'downwards')
->assertSet('sortDirection', 'asc')
->set('sortArray', ['name' => 'sideways', 'password' => 'desc', 'bad field' => 'asc'])
->assertSet('sortArray', ['name' => 'asc'])
->set('sortArray.password', 'desc')
->assertSet('sortArray', ['name' => 'asc']);
});

class LiteMethodAllowlistSortComponent extends LitePersistComponent
{
public array $persist = [];

public function sortableFields(): array
{
return ['name'];
}
}

it('reads the lite sort allowlist from a sortableFields() method', function () {
Livewire::test(LiteMethodAllowlistSortComponent::class)
->call('sortBy', 'password')
->assertSet('sortField', '')
->set('sortField', 'password')
->assertSet('sortField', '')
->call('sortBy', 'name')
->assertSet('sortField', 'name');
});

class LitePersistWithSecretComponent extends LitePersistComponent
{
#[Locked]
public array $sortableFields = ['name'];

public string $tenant = 'mine';
}

it('restores only the lite state it persists', function () {
config()->set('livewire-powergrid.persist_driver', 'session');

session()->put('pg_lite:LitePersistWithSecretComponent', json_encode([
'sortField' => 'password',
'sortDirection' => 'desc',
'tenant' => 'theirs',
'persist' => ['sorting', 'checkbox'],
]));

Livewire::test(LitePersistWithSecretComponent::class)
->assertSet('tenant', 'mine')
->assertSet('persist', ['sorting'])
->assertSet('sortField', '')
->assertSet('sortDirection', 'desc');
});

it('still restores a valid lite sort from persisted state', function () {
config()->set('livewire-powergrid.persist_driver', 'session');

session()->put('pg_lite:LitePersistWithSecretComponent', json_encode([
'sortField' => 'name',
'sortDirection' => 'desc',
]));

Livewire::test(LitePersistWithSecretComponent::class)
->assertSet('sortField', 'name')
->assertSet('sortDirection', 'desc');
});
Loading