Skip to content

Commit

Permalink
fix(SDK-4716): Resolve thrown exception when enumerating device cooki…
Browse files Browse the repository at this point in the history
…es that include non-string keys/names
  • Loading branch information
evansims committed Nov 20, 2023
1 parent ab133e2 commit cec713c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Store/CookieStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions tests/Unit/Store/CookieStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
]]);

0 comments on commit cec713c

Please # to comment.