Skip to content

Commit

Permalink
Fix possible error if an incorrect version list array is provided (#7350
Browse files Browse the repository at this point in the history
)

* Fix possible error if an incorrect version list array is provided

* add array check
  • Loading branch information
sgiehl authored Feb 14, 2023
1 parent c12bcb6 commit 76d26eb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
12 changes: 7 additions & 5 deletions ClientHints.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,13 @@ public function getOperatingSystemVersion(): string
*/
public function getBrandList(): array
{
if (\count($this->fullVersionList)) {
return \array_combine(
\array_column($this->fullVersionList, 'brand'),
\array_column($this->fullVersionList, 'version')
) ?: [];
if (\is_array($this->fullVersionList) && \count($this->fullVersionList)) {
$brands = \array_column($this->fullVersionList, 'brand');
$versions = \array_column($this->fullVersionList, 'version');

if (\count($brands) === \count($versions)) {
return \array_combine($brands, $versions);
}
}

return [];
Expand Down
14 changes: 14 additions & 0 deletions Tests/ClientHintsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,18 @@ public function testHeadersJavascript(): void
], $ch->getBrandList());
self::assertSame('', $ch->getModel());
}

public function testIncorrectVersionListIsDiscarded(): void
{
$headers = [
'fullVersionList' => [
['brand' => ' Not A;Brand', 'version' => '99.0.0.0'],
['brand' => 'Chromium', 'version' => '99.0.4844.51'],
['version' => '99.0.4844.51'], // this entry lags a brand
],
];

$ch = ClientHints::factory($headers);
self::assertSame([], $ch->getBrandList());
}
}

0 comments on commit 76d26eb

Please sign in to comment.