Skip to content

Commit f919926

Browse files
authored
fix(SDK-4716): Resolve thrown exception when enumerating device cookies that include non-string keys/names (#739)
### Changes This PR resolves an issue wherein the SDK erroneously throws an exception when enumerating cookies from a device with integer-type keys. ### References See internal ticket SDK-4716. ### Testing Tests have been added to cover the code changes. Coverage remains 100%. ### Contributor Checklist - [x] I agree to adhere to the [Auth0 General Contribution Guidelines](https://github.com/auth0/open-source-template/blob/master/GENERAL-CONTRIBUTING.md). - [x] I agree to uphold the [Auth0 Code of Conduct](https://github.com/auth0/open-source-template/blob/master/CODE-OF-CONDUCT.md).
1 parent ab133e2 commit f919926

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/Store/CookieStore.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ public function setEncrypted(bool $encrypt = true): self
438438
/**
439439
* Push our storage state to the source for persistence.
440440
*
441-
* @psalm-suppress UnusedFunctionCall
441+
* @psalm-suppress UnusedFunctionCall,DocblockTypeContradiction
442442
*
443443
* @param bool $force
444444
*/
@@ -458,6 +458,10 @@ public function setState(
458458
foreach (array_keys($_COOKIE) as $cookieName) {
459459
$cookieBeginsWith = $this->namespace . self::KEY_SEPARATOR;
460460

461+
if (is_int($cookieName)) {
462+
$cookieName = (string) $cookieName;
463+
}
464+
461465
if (mb_strlen($cookieName) >= mb_strlen($cookieBeginsWith)
462466
&& mb_substr($cookieName, 0, mb_strlen($cookieBeginsWith)) === $cookieBeginsWith) {
463467
$existing[] = $cookieName;

tests/Unit/Store/CookieStoreTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,21 @@
249249
$this->store->setEncrypted(false);
250250
expect($this->store->encrypt($state, ['encoded1' => false]))->toEqual('');
251251
});
252+
253+
it('enumerates $_COOKIE with non-string keys', function(array $state): void {
254+
$cookieNamespace = $this->store->getNamespace() . '_0';
255+
256+
$encrypted = MockCrypto::cookieCompatibleEncrypt($this->cookieSecret, [$this->exampleKey => $state]);
257+
258+
$_COOKIE[$cookieNamespace] = $encrypted;
259+
$_COOKIE['123'] = uniqid();
260+
$_COOKIE[456] = uniqid();
261+
$_COOKIE['abc'] = uniqid();
262+
263+
$this->store->getState();
264+
$this->store->setState(true);
265+
266+
expect($this->store->get($this->exampleKey))->toEqual($state);
267+
})->with(['mocked state' => [
268+
fn() => MockDataset::state()
269+
]]);

0 commit comments

Comments
 (0)