From cec713c50a19425fd484071e81eaecd98f02ddaf Mon Sep 17 00:00:00 2001 From: Evan Sims Date: Sun, 19 Nov 2023 21:24:59 -0600 Subject: [PATCH] fix(SDK-4716): Resolve thrown exception when enumerating device cookies that include non-string keys/names --- src/Store/CookieStore.php | 6 +++++- tests/Unit/Store/CookieStoreTest.php | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Store/CookieStore.php b/src/Store/CookieStore.php index 4bd8e9b0..debe74f6 100644 --- a/src/Store/CookieStore.php +++ b/src/Store/CookieStore.php @@ -438,7 +438,7 @@ public function setEncrypted(bool $encrypt = true): self /** * Push our storage state to the source for persistence. * - * @psalm-suppress UnusedFunctionCall + * @psalm-suppress UnusedFunctionCall,DocblockTypeContradiction * * @param bool $force */ @@ -458,6 +458,10 @@ public function setState( foreach (array_keys($_COOKIE) as $cookieName) { $cookieBeginsWith = $this->namespace . self::KEY_SEPARATOR; + if (is_int($cookieName)) { + $cookieName = (string) $cookieName; + } + if (mb_strlen($cookieName) >= mb_strlen($cookieBeginsWith) && mb_substr($cookieName, 0, mb_strlen($cookieBeginsWith)) === $cookieBeginsWith) { $existing[] = $cookieName; diff --git a/tests/Unit/Store/CookieStoreTest.php b/tests/Unit/Store/CookieStoreTest.php index ac2ba766..d1699642 100644 --- a/tests/Unit/Store/CookieStoreTest.php +++ b/tests/Unit/Store/CookieStoreTest.php @@ -249,3 +249,21 @@ $this->store->setEncrypted(false); expect($this->store->encrypt($state, ['encoded1' => false]))->toEqual(''); }); + +it('enumerates $_COOKIE with non-string keys', function(array $state): void { + $cookieNamespace = $this->store->getNamespace() . '_0'; + + $encrypted = MockCrypto::cookieCompatibleEncrypt($this->cookieSecret, [$this->exampleKey => $state]); + + $_COOKIE[$cookieNamespace] = $encrypted; + $_COOKIE['123'] = uniqid(); + $_COOKIE[456] = uniqid(); + $_COOKIE['abc'] = uniqid(); + + $this->store->getState(); + $this->store->setState(true); + + expect($this->store->get($this->exampleKey))->toEqual($state); +})->with(['mocked state' => [ + fn() => MockDataset::state() +]]);