Skip to content

bugfix: Fix eventstream partial read #3165

New issue

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

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

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions .changes/nextrelease/fix-eventstream-partial-read.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"type": "bugfix",
"category": "",
"description": "Fixed an issue in NonSeekableStreamDecodingEventStreamIterator where partial reads from non-seekable streams could result in truncated payloads and CRC mismatches."
}
]
15 changes: 13 additions & 2 deletions src/Api/Parser/NonSeekableStreamDecodingEventStreamIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,21 @@ protected function readAndHashBytes($num): string
while (!empty($this->tempBuffer) && $num > 0) {
$byte = array_shift($this->tempBuffer);
$bytes .= $byte;
$num = $num - 1;
$num -= 1;
}

// Loop until we've read the expected number of bytes
while ($num > 0 && !$this->stream->eof()) {
$chunk = $this->stream->read($num);
$chunkLen = strlen($chunk);
$bytes .= $chunk;
$num -= $chunkLen;

if ($chunkLen === 0) {
break; // Prevent infinite loop on unexpected EOF
}
}

$bytes = $bytes . $this->stream->read($num);
hash_update($this->hashContext, $bytes);

return $bytes;
Expand Down
129 changes: 129 additions & 0 deletions tests/Api/Parser/NonSeekableStreamDecodingEventStreamIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Aws\Api\Parser\NonSeekableStreamDecodingEventStreamIterator;
use GuzzleHttp\Psr7\NoSeekStream;
use GuzzleHttp\Psr7\Utils;
use Psr\Http\Message\StreamInterface;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;

class NonSeekableStreamDecodingEventStreamIteratorTest extends TestCase
Expand Down Expand Up @@ -67,4 +68,132 @@ public function testValidReturnsTrueOnEOF()
$iterator->next();
$this->assertFalse($iterator->valid());
}

public function testReadAndHashBytesHandlesPartialReads()
{
$payload = str_repeat('A', 1024);
$stream = new NoSeekStream(new PartialReadStream($payload));

$iterator = new NonSeekableStreamDecodingEventStreamIterator($stream);

$reflect = new \ReflectionClass($iterator);
$hashContextProperty = $reflect->getProperty('hashContext');
$hashContextProperty->setAccessible(true);
$hashContextProperty->setValue($iterator, hash_init('crc32c'));

$method = $reflect->getMethod('readAndHashBytes');
$method->setAccessible(true);

$result = $method->invoke($iterator, 1024);

$this->assertEquals($payload, $result);
}
}
/**
* Simulates partial reads by limiting each read() call to a maximum number of bytes,
* regardless of what is requested.
*/
class PartialReadStream implements StreamInterface
{
private string $data;
private int $position = 0;
private int $maxBytesPerRead;

public function __construct(string $data, int $maxBytesPerRead = 100)
{
$this->data = $data;
$this->maxBytesPerRead = $maxBytesPerRead;
}

public function __toString(): string
{
return $this->data;
}

public function close(): void
{
// No resources to close
}

public function detach()
{
return null;
}

public function getSize(): ?int
{
return strlen($this->data);
}

public function tell(): int
{
return $this->position;
}

public function eof(): bool
{
return $this->position >= strlen($this->data);
}

public function isSeekable(): bool
{
return false;
}

public function seek($offset, $whence = SEEK_SET): void
{
throw new \RuntimeException("Stream is not seekable");
}

public function rewind(): void
{
throw new \RuntimeException("Stream is not seekable");
}

public function isWritable(): bool
{
return false;
}

public function write($string): int
{
throw new \RuntimeException("Stream is not writable");
}

public function isReadable(): bool
{
return true;
}

public function read($length): string
{
if ($this->eof()) {
return '';
}

// Read only up to maxBytesPerRead
$readLength = min($length, $this->maxBytesPerRead);
$chunk = substr($this->data, $this->position, $readLength);
$this->position += strlen($chunk);

return $chunk;
}

public function getContents(): string
{
if ($this->eof()) {
return '';
}

$contents = substr($this->data, $this->position);
$this->position = strlen($this->data);

return $contents;
}

public function getMetadata($key = null): mixed
{
return null;
}
}