Skip to content

Commit

Permalink
bug #798 [Live] Fix checksum calculation for deeply nested data (1ed)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.x branch.

Discussion
----------

[Live] Fix checksum calculation for deeply nested data

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| Tickets       | -
| License       | MIT

For a consistent checksum we need to sort the whole input.

Commits
-------

5b0b862 [Live] Fix checksum calculation for deeply nested data
  • Loading branch information
weaverryan committed Jul 31, 2023
2 parents eb7023a + 5b0b862 commit 3c13a1b
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/LiveComponent/src/LiveComponentHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ private static function coerceStringValue(string $value, string $type, bool $all
private function calculateChecksum(array $dehydratedPropsData): ?string
{
// sort so it is always consistent (frontend could have re-ordered data)
ksort($dehydratedPropsData);
$this->recursiveKeySort($dehydratedPropsData);

return base64_encode(hash_hmac('sha256', json_encode($dehydratedPropsData), $this->secret, true));
}
Expand Down Expand Up @@ -549,4 +549,14 @@ private function combineAndValidateProps(array $props, array $updatedPropsFromPa

return $dehydratedOriginalProps;
}

private function recursiveKeySort(array &$data): void
{
foreach ($data as &$value) {
if (\is_array($value)) {
$this->recursiveKeySort($value);
}
}
ksort($data);
}
}

0 comments on commit 3c13a1b

Please sign in to comment.