Skip to content

Commit

Permalink
Test invoking websocket with invalid utf-8
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Jan 18, 2025
1 parent 7139ed6 commit 4e47393
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/it/php/web/unittest/IntegrationTest.class.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace web\unittest;

use test\{Assert, After, Test, Values};
use peer\ProtocolException;
use test\{Assert, After, Expect, Test, Values};
use util\Bytes;
use websocket\WebSocket;

Expand Down Expand Up @@ -178,6 +179,18 @@ public function websocket_message($input, $output) {
Assert::equals($output, $result);
}

#[Test, Expect(class: ProtocolException::class, message: 'Connection closed (#1007): Not valid utf-8')]
public function invalid_utf8_passed_to_websocket_text_message() {
try {
$ws= new WebSocket($this->server->connection, '/ws');
$ws->connect();
$ws->send("\xfc");
$ws->receive();
} finally {
$ws->close();
}
}

#[After]
public function shutdown() {
$this->server->shutdown();
Expand Down
2 changes: 1 addition & 1 deletion src/main/php/xp/web/srv/WsProtocol.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function handleData($socket) {
switch ($opcode) {
case Opcodes::TEXT:
if (!preg_match('//u', $payload)) {
$conn->answer(Opcodes::CLOSE, pack('n', 1007));
$conn->answer(Opcodes::CLOSE, pack('na*', 1007, 'Not valid utf-8'));
$hints= ['error' => new FormatException('Malformed payload')];
$socket->close();
break;
Expand Down

0 comments on commit 4e47393

Please sign in to comment.