Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(SDK-4716): Resolve thrown exception when enumerating device cookies that include non-string keys/names #739

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
]]);
Loading