From a06fe2e546a06bb8c2639d6823d5250b2efb3209 Mon Sep 17 00:00:00 2001 From: Serban Ghita Date: Tue, 10 Dec 2024 17:32:06 +0200 Subject: [PATCH] sha1` is now the default fn for encoding cache keys. Signed-off-by: Serban Ghita --- CHANGELOG.md | 6 +++++- MobileDetect.json | 2 +- src/MobileDetect.php | 7 ++++--- tests/MobileDetectWithCacheTest.php | 20 ++++++++++---------- 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e993397..8cfc3648 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,12 +1,16 @@ # Change log +# 4.8.09 + +- [x] `sha1` is now the default fn for encoding cache keys. Using `base64` [was causing problems](https://github.com/serbanghita/Mobile-Detect/issues/974#issuecomment-2531597903) in Laravel. + # 4.8.08 - [x] fix for missing psr/cache prod dependency - [bug] latest 4.8.07 cause site error Call to a member function get() on false #974 - [x] fix for Docker build not installing dev dependencies -# 4.8.07 (broken in composer) +# 4.8.07 (broken in composer, please skip) - [x] fix cache and generate short cache key (#971) - [x] Added configuration cacheKeyFn which allows for using a custom cache key creation fn. diff --git a/MobileDetect.json b/MobileDetect.json index 6aad225e..c641602f 100644 --- a/MobileDetect.json +++ b/MobileDetect.json @@ -1,5 +1,5 @@ { - "version": "4.8.08", + "version": "4.8.09", "headerMatch": { "HTTP_ACCEPT": { "matches": [ diff --git a/src/MobileDetect.php b/src/MobileDetect.php index be742388..cbfa0949 100644 --- a/src/MobileDetect.php +++ b/src/MobileDetect.php @@ -19,7 +19,7 @@ * @author Nick Ilyin * @author: Victor Stanciu (original author) * - * @version 4.8.08 + * @version 4.8.09 */ declare(strict_types=1); @@ -241,7 +241,7 @@ class MobileDetect /** * Stores the version number of the current release. */ - protected string $VERSION = '4.8.08'; + protected string $VERSION = '4.8.09'; protected array $config = [ // Auto-initialization on HTTP headers from $_SERVER['HTTP...'] @@ -253,7 +253,8 @@ class MobileDetect // @var int 'maximumUserAgentLength' => 500, // Function that creates the cache key. e.g. (base64, sha1, custom fn). - 'cacheKeyFn' => 'base64_encode', + // Note: used to be base64 but we went with sha1 because of fixed length. + 'cacheKeyFn' => 'sha1', // Cache TTL // @var null|int|\DateInterval 'cacheTtl' => 86400, diff --git a/tests/MobileDetectWithCacheTest.php b/tests/MobileDetectWithCacheTest.php index e0e7b5ab..eea11a05 100644 --- a/tests/MobileDetectWithCacheTest.php +++ b/tests/MobileDetectWithCacheTest.php @@ -43,8 +43,8 @@ public function testDefaultCacheClassCreatesACacheRecord() $this->assertTrue($isMobile); $this->assertEquals(1, $detect->getCache()->count()); $this->assertSame( - "mobile:Some iPhone user agent:", - base64_decode($detect->getCache()->getKeys()[0]) + sha1("mobile:Some iPhone user agent:"), + $detect->getCache()->getKeys()[0] ); } @@ -98,20 +98,20 @@ public function testDefaultCacheClassCreatesMultipleCacheRecordsForAllCalls() $this->assertEquals(4, $detect->getCache()->count()); $this->assertSame( - "mobile:iPad; AppleWebKit/533.17.9 Version/5.0.2 Mobile/8C148 Safari/6533.18.5:", - base64_decode($detect->getCache()->getKeys()[0]) + sha1("mobile:iPad; AppleWebKit/533.17.9 Version/5.0.2 Mobile/8C148 Safari/6533.18.5:"), + $detect->getCache()->getKeys()[0] ); $this->assertSame( - "tablet:iPad; AppleWebKit/533.17.9 Version/5.0.2 Mobile/8C148 Safari/6533.18.5:", - base64_decode($detect->getCache()->getKeys()[1]) + sha1("tablet:iPad; AppleWebKit/533.17.9 Version/5.0.2 Mobile/8C148 Safari/6533.18.5:"), + $detect->getCache()->getKeys()[1] ); $this->assertSame( - "iPad:iPad; AppleWebKit/533.17.9 Version/5.0.2 Mobile/8C148 Safari/6533.18.5:", - base64_decode($detect->getCache()->getKeys()[2]) + sha1("iPad:iPad; AppleWebKit/533.17.9 Version/5.0.2 Mobile/8C148 Safari/6533.18.5:"), + $detect->getCache()->getKeys()[2] ); $this->assertSame( - "iOS:iPad; AppleWebKit/533.17.9 Version/5.0.2 Mobile/8C148 Safari/6533.18.5:", - base64_decode($detect->getCache()->getKeys()[3]) + sha1("iOS:iPad; AppleWebKit/533.17.9 Version/5.0.2 Mobile/8C148 Safari/6533.18.5:"), + $detect->getCache()->getKeys()[3] ); }