Skip to content

Commit 2266c4a

Browse files
committed
fix(http): detect Safari version from Version token
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
1 parent d6cc89f commit 2266c4a

3 files changed

Lines changed: 38 additions & 2 deletions

File tree

system/HTTP/UserAgent.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,15 @@ protected function setBrowser(): bool
317317
if (is_array($this->config->browsers) && $this->config->browsers !== []) {
318318
foreach ($this->config->browsers as $key => $val) {
319319
if (preg_match('|' . $key . '.*?([0-9\.]+)|i', $this->agent, $match)) {
320+
$version = $match[1];
321+
322+
// Safari's browser version is reported in the Version token.
323+
if ($val === 'Safari' && preg_match('|Version/([0-9\.]+).*?Safari|i', $this->agent, $safariMatch)) {
324+
$version = $safariMatch[1];
325+
}
326+
320327
$this->isBrowser = true;
321-
$this->version = $match[1];
328+
$this->version = $version;
322329
$this->browser = $val;
323330
$this->setMobile();
324331

tests/system/HTTP/UserAgentTest.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,38 @@ public function testBrowserInfo(): void
7474
{
7575
$this->assertSame('Mac OS X', $this->agent->getPlatform());
7676
$this->assertSame('Safari', $this->agent->getBrowser());
77-
$this->assertSame('533.20.27', $this->agent->getVersion());
77+
$this->assertSame('5.0.4', $this->agent->getVersion());
7878
$this->assertSame('', $this->agent->getRobot());
7979
}
8080

81+
public function testParseModernSafariUsesVersionToken(): void
82+
{
83+
$this->agent->parse('Mozilla/5.0 (Macintosh; Intel Mac OS X 13_2) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.3 Safari/605.1.15');
84+
85+
$this->assertSame('Mac OS X', $this->agent->getPlatform());
86+
$this->assertSame('Safari', $this->agent->getBrowser());
87+
$this->assertSame('16.3', $this->agent->getVersion());
88+
}
89+
90+
public function testParseMobileSafariUsesVersionToken(): void
91+
{
92+
$this->agent->parse($this->_mobile_ua);
93+
94+
$this->assertSame('iOS', $this->agent->getPlatform());
95+
$this->assertSame('Safari', $this->agent->getBrowser());
96+
$this->assertSame('4.0.5', $this->agent->getVersion());
97+
$this->assertSame('Apple iPhone', $this->agent->getMobile());
98+
}
99+
100+
public function testParseChromeWithSafariTokenUsesChromeVersion(): void
101+
{
102+
$this->agent->parse('Mozilla/5.0 (Macintosh; Intel Mac OS X 13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36');
103+
104+
$this->assertSame('Mac OS X', $this->agent->getPlatform());
105+
$this->assertSame('Chrome', $this->agent->getBrowser());
106+
$this->assertSame('110.0.0.0', $this->agent->getVersion());
107+
}
108+
81109
public function testParse(): void
82110
{
83111
$newAgent = 'Mozilla/5.0 (Android; Mobile; rv:13.0) Gecko/13.0 Firefox/13.0';

user_guide_src/source/changelogs/v4.7.4.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Bugs Fixed
3131
**********
3232

3333
- **Database:** Fixed a bug where ``updateBatch()`` could be called after Query Builder ``where()`` conditions, even though it's not supported. In this situation, now the ``DatabaseException`` is thrown.
34+
- **HTTP:** Fixed a bug where the User Agent library reported Safari's WebKit version instead of the browser version from the ``Version`` token.
3435

3536
See the repo's
3637
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_

0 commit comments

Comments
 (0)