Skip to content

Commit bf46682

Browse files
committed
Added check in agentzero::parse() to check that all the characters in the input UA string are printable.
Changed `preg_replace()`call for normalising spaces to a `str_replace()`. Updated `index.php` to correctly check when the value could not be parsed. Updated some test strings that had invalid characters in them.
1 parent ad28d2b commit bf46682

File tree

6 files changed

+18
-15
lines changed

6 files changed

+18
-15
lines changed

composer.lock

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@
153153
<input type="submit" class="form__submit" value="Parse User Agent String" />
154154
</div>
155155
</form>
156-
<?php if ($output !== null) { ?>
156+
<?php if ($output !== false) { ?>
157157
<pre><?= htmlspecialchars(\strval(\print_r(\array_filter((array) $output, fn(mixed $value) => $value !== null), true))); ?></pre>
158158
<p>Generated in <?= \number_format($total, 5); ?></p>
159159
<?php } ?>

src/agentzero.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,17 @@ protected static function getTokens(string $ua, array $single, array $ignore) :
195195
* @return agentzero|false An agentzero object containing the parsed values of the input UA, or false if it could not be parsed
196196
*/
197197
public static function parse(string $ua, array $hints = [], array $config = []) : agentzero|false {
198-
$ua = \preg_replace('/\s{2,}/', ' ', $ua);
198+
$ua = \str_replace(' ', ' ', $ua);
199199

200200
// parse client hints
201201
$hinted = $ua;
202202
$browser = hints::parse($hinted, $hints);
203203

204+
// check all printable characters
205+
if (!\ctype_print($ua)) {
206+
204207
// get config
205-
if (($config = config::get($config)) === null) {
208+
} elseif (($config = config::get($config)) === null) {
206209

207210
// get tokens
208211
} elseif (($tokens = self::getTokens(\trim($hinted, ' "\''), $config['single'], $config['ignore'])) !== false) {

tests/browsersTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ public function testBrave() : void {
446446
'engineversion' => '601.1.46',
447447
'browser' => 'Brave',
448448
'browserversion' => '1.2.11',
449-
'browserreleased' => '2025-03-26'
449+
'browserreleased' => '2025-04-23'
450450
],
451451
'Mozilla/5.0 (Macintosh; Intel Mac OS X 13_4_1) AppleWebKit/605.1.15 (KHTML, like Gecko) Brave/115.0.0.0 Safari/605.1.15' => [
452452
'string' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 13_4_1) AppleWebKit/605.1.15 (KHTML, like Gecko) Brave/115.0.0.0 Safari/605.1.15',

tests/crawlersTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,8 @@ public function testSearch() : void {
264264
'engineversion' => '103.0.5060.134',
265265
'browserreleased' => '2022-07-19'
266266
],
267-
'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.345.0Mobile Safari/537.36 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)' => [
268-
'string' => 'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.345.0Mobile Safari/537.36 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)',
267+
'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.345.0 Mobile Safari/537.36 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)' => [
268+
'string' => 'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.345.0 Mobile Safari/537.36 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)',
269269
'type' => 'robot',
270270
'category' => 'search',
271271
'app' => 'Bing Bot',

tests/profile.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@
255255
'Googlebot-Video/1.0',
256256
'Mozilla/5.0 (Linux; Android 8.0; Pixel 2 Build/OPD3.170816.012; Storebot-Google/1.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Mobile Safari/537.36',
257257
'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm) Chrome/103.0.5060.134 Safari/537.36',
258-
'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.345.0Mobile Safari/537.36 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)',
258+
'Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.345.0 Mobile Safari/537.36 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)',
259259
'Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)',
260260
'Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53 (compatible; adidxbot/2.0; +http://www.bing.com/bingbot.htm)',
261261
'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm) Chrome/80.0.345.0 Safari/537.36',

0 commit comments

Comments
 (0)