Skip to content
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

PacketEncoder: add minimal validation of fcgi packets on decode #78

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
16 changes: 14 additions & 2 deletions src/Encoders/PacketEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
namespace hollodotme\FastCGI\Encoders;

use hollodotme\FastCGI\Interfaces\EncodesPacket;
use hollodotme\FastCGI\Exceptions\ReadFailedException;
use function chr;
use function ord;
use function strlen;
Expand Down Expand Up @@ -58,9 +59,20 @@ public function encodePacket( int $type, string $content, int $requestId ) : str
*/
public function decodeHeader( string $data ) : array
{
if (strlen($data) < 7) {
throw new ReadFailedException( 'FCGI packet too short' );
}
$version = ord( $data[0] );
if ($version != 1) {
throw new ReadFailedException( 'Unsupported FCGI Version: ' . $version );
}
$type = ord( $data[1] );
if ($type > 11) { // FCGI_MAXTYPE
throw new ReadFailedException( 'Unsupported FCGI Packet type: ' . $type );
}
return [
'version' => ord( $data[0] ),
'type' => ord( $data[1] ),
'version' => $version,
'type' => $type,
'requestId' => (ord( $data[2] ) << 8) + ord( $data[3] ),
'contentLength' => (ord( $data[4] ) << 8) + ord( $data[5] ),
'paddingLength' => ord( $data[6] ),
Expand Down