Skip to content

Commit

Permalink
Expand session reader to cover more data
Browse files Browse the repository at this point in the history
  • Loading branch information
mbabker committed Aug 27, 2024
1 parent 8f56e00 commit d407dc0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 34 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"phpunit/phpunit": "11.3.1",
"psr/container": "^1.0 || ^2.0",
"psr/event-dispatcher": "^1.0",
"rector/rector": "1.2.4"
"rector/rector": "1.2.4",
"symfony/security-core": "^6.4 || ^7.0"
},
"conflict": {
"psr/container": ">=3.0",
Expand Down
11 changes: 8 additions & 3 deletions src/Session/Reader/PhpReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,19 @@ private function getSerializedSegmentLength(string $data): int|false
// Add characters for the closing quote and semicolon
return \strlen($matches[0]) + (int) substr($matches[0], 2, -2) + 2;

// Array value
// Array or object value
case 'a':
if (!preg_match('/^a:\d+:\{/', $data, $matches)) {
case 'O':
$isArray = 'a' === $data[0];

$pattern = $isArray ? '/^a:\d+:\{/' : '/^O:\d+:"[^"]+":(\d+):\{/';

if (!preg_match($pattern, $data, $matches)) {
return false;
}

$start = \strlen($matches[0]);
$count = (int) substr($matches[0], 2, -2);
$count = $isArray ? (int) substr($matches[0], 2, -2) : (int) $matches[1];
$offset = $start;
$length = \strlen($data);

Expand Down
33 changes: 18 additions & 15 deletions tests/Session/Reader/PhpReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,30 @@
namespace BabDev\WebSocket\Server\Tests\Session\Reader;

use BabDev\WebSocket\Server\Session\Reader\PhpReader;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

final class PhpReaderTest extends TestCase
{
public function testReadsData(): void
/**
* @return \Generator<string, array{non-empty-string}>
*/
public static function dataRead(): \Generator
{
$input = '_sf2_attributes|a:3:{s:3:"foo";s:3:"bar";s:12:"messages.foo";s:3:"bar";s:4:"data";a:1:{s:3:"foo";s:3:"bar";}}_sf2_meta|a:3:{s:1:"u";i:1653958488;s:1:"c";i:1653958488;s:1:"l";i:0;}';
yield 'Basic Symfony session payload' => ['_sf2_attributes|a:3:{s:3:"foo";s:3:"bar";s:12:"messages.foo";s:3:"bar";s:4:"data";a:1:{s:3:"foo";s:3:"bar";}}_sf2_meta|a:3:{s:1:"u";i:1653958488;s:1:"c";i:1653958488;s:1:"l";i:0;}'];

$expected = [
'_sf2_attributes' => [
'foo' => 'bar',
'messages.foo' => 'bar',
'data' => ['foo' => 'bar'],
],
'_sf2_meta' => [
'u' => 1653958488,
'c' => 1653958488,
'l' => 0,
],
];
yield 'Symfony application session payload' => ['_sf2_attributes|a:3:{s:14:"_security_main";s:340:"O:74:"Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken":3:{i:0;N;i:1;s:4:"main";i:2;a:5:{i:0;O:15:"App\Entity\User":3:{s:2:"id";i:123456;s:5:"email";s:17:"xxxxxxx@xxxxx.xxx";s:8:"password";s:60:"$2y$13$9klfX3owVXOQ4jjoqTMi8.71kW5i6rHixw5E2kRE1KW7yPELxmSHi";}i:1;b:1;i:2;N;i:3;a:0:{}i:4;a:1:{i:0;s:9:"ROLE_USER";}}}";s:23:"_security.last_username";s:17:"xxxxxxx@xxxxx.xxx";s:20:"_security.last_error";O:67:"Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException":5:{i:0;N;i:1;i:0;i:2;s:19:"Invalid CSRF token.";i:3;s:83:"/var/www/html/vendor/symfony/security-http/EventListener/CsrfProtectionListener.php";i:4;i:51;}}_sf2_meta|a:3:{s:1:"u";i:1724707524;s:1:"c";i:1724705819;s:1:"l";i:0;}'];
}

/**
* @param non-empty-string $input
*/
#[DataProvider('dataRead')]
public function testReadsData(string $input): void
{
$output = (new PhpReader())->read($input);

$this->assertSame($expected, (new PhpReader())->read($input));
$this->assertArrayHasKey('_sf2_attributes', $output);
$this->assertArrayHasKey('_sf2_meta', $output);
}
}
33 changes: 18 additions & 15 deletions tests/Session/Reader/PhpSerializeReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,30 @@
namespace BabDev\WebSocket\Server\Tests\Session\Reader;

use BabDev\WebSocket\Server\Session\Reader\PhpSerializeReader;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

final class PhpSerializeReaderTest extends TestCase
{
public function testReadsData(): void
/**
* @return \Generator<string, array{non-empty-string}>
*/
public static function dataRead(): \Generator
{
$input = 'a:2:{s:15:"_sf2_attributes";a:3:{s:3:"foo";s:3:"bar";s:12:"messages.foo";s:3:"bar";s:4:"data";a:1:{s:3:"foo";s:3:"bar";}}s:9:"_sf2_meta";a:3:{s:1:"u";i:1653958488;s:1:"c";i:1653958488;s:1:"l";i:0;}}';
yield 'Basic Symfony session payload' => ['a:2:{s:15:"_sf2_attributes";a:3:{s:3:"foo";s:3:"bar";s:12:"messages.foo";s:3:"bar";s:4:"data";a:1:{s:3:"foo";s:3:"bar";}}s:9:"_sf2_meta";a:3:{s:1:"u";i:1653958488;s:1:"c";i:1653958488;s:1:"l";i:0;}}'];

$expected = [
'_sf2_attributes' => [
'foo' => 'bar',
'messages.foo' => 'bar',
'data' => ['foo' => 'bar'],
],
'_sf2_meta' => [
'u' => 1653958488,
'c' => 1653958488,
'l' => 0,
],
];
yield 'Symfony application session payload' => ['a:2:{s:15:"_sf2_attributes";a:3:{s:14:"_security_main";s:340:"O:74:"Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken":3:{i:0;N;i:1;s:4:"main";i:2;a:5:{i:0;O:15:"App\Entity\User":3:{s:2:"id";i:123456;s:5:"email";s:17:"xxxxxxx@xxxxx.xxx";s:8:"password";s:60:"$2y$13$9klfX3owVXOQ4jjoqTMi8.71kW5i6rHixw5E2kRE1KW7yPELxmSHi";}i:1;b:1;i:2;N;i:3;a:0:{}i:4;a:1:{i:0;s:9:"ROLE_USER";}}}";s:23:"_security.last_username";s:17:"xxxxxxx@xxxxx.xxx";s:20:"_security.last_error";O:67:"Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException":5:{i:0;N;i:1;i:0;i:2;s:19:"Invalid CSRF token.";i:3;s:83:"/var/www/html/vendor/symfony/security-http/EventListener/CsrfProtectionListener.php";i:4;i:51;}}s:9:"_sf2_meta";a:3:{s:1:"u";i:1724707524;s:1:"c";i:1724705819;s:1:"l";i:0;}}'];
}

/**
* @param non-empty-string $input
*/
#[DataProvider('dataRead')]
public function testReadsData(string $input): void
{
$output = (new PhpSerializeReader())->read($input);

$this->assertSame($expected, (new PhpSerializeReader())->read($input));
$this->assertArrayHasKey('_sf2_attributes', $output);
$this->assertArrayHasKey('_sf2_meta', $output);
}
}

0 comments on commit d407dc0

Please sign in to comment.