Skip to content

Commit

Permalink
Simplify the generation of HTML node identifiers
Browse files Browse the repository at this point in the history
We do not to generate completely random identifiers, the original intention was to prevent collisions with existing tag names.

Using a per-request random prefix together with a counter is sufficient to generate unique tag names without paying the CSPRNG tax for ever node.
  • Loading branch information
dtdesign committed Jun 16, 2024
1 parent 72016a9 commit 3b9604d
Showing 1 changed file with 9 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -370,18 +370,17 @@ protected function invokeNodeHandlers($classNamePattern, array $skipTags = [], ?
*/
public function getWcfNodeIdentifer(): array
{
static $engine = null;

if ($engine === null) {
if (\class_exists(\Random\Engine\Xoshiro256StarStar::class, false)) {
$randomizer = new \Random\Randomizer(new \Random\Engine\Xoshiro256StarStar());
$engine = static fn () => \bin2hex($randomizer->getBytes(16));
} else {
$engine = static fn () => \bin2hex(\random_bytes(16));
}
static $counter = 0;
static $prefix = null;

if ($prefix === null) {
// The `x` is appended to visually separate the prefix and the
// counter to aid in debugging in case the random prefix ends with
// one or more numeric characters.
$prefix = \bin2hex(\random_bytes(16)) . 'x';
}

$identifier = $engine();
$identifier = $prefix . $counter++;

return [$identifier, "wcfNode-{$identifier}"];
}
Expand Down

0 comments on commit 3b9604d

Please sign in to comment.