Skip to content

Commit 996b723

Browse files
committed
Handle close codes correctly
1 parent 32c5eef commit 996b723

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

lib/Rfc6455Connection.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,17 +200,30 @@ private function onParsedControlFrame(int $opcode, string $data) {
200200
if ($this->closedAt) {
201201
$this->unloadServer();
202202
} else {
203-
if (\strlen($data) < 2) {
204-
return; // invalid close reason
203+
$length = \strlen($data);
204+
if ($length === 0) {
205+
$this->close();
206+
return;
207+
} elseif ($length < 2) {
208+
$this->close(Code::PROTOCOL_ERROR, 'Close code must be two bytes');
209+
return;
205210
}
206211
$code = \current(\unpack('S', \substr($data, 0, 2)));
207212
$reason = \substr($data, 2);
208213

209214
$this->serverInitiatedClose = true;
215+
216+
// Note: There's a test for 1004, but only 1005 and 1006 must not be used for closing by an endpoint
217+
if ($code < 1000 || $code === 1005 || $code === 1006 || $code === 1015) {
218+
$this->close(Code::PROTOCOL_ERROR, 'Invalid close code');
219+
220+
return;
221+
}
222+
210223
if ($this->options->isValidateUtf8() && !\preg_match('//u', $reason)) {
211224
$this->close(Code::INCONSISTENT_FRAME_DATA_TYPE, 'Close reason must be valid UTF-8');
212225
} else {
213-
$this->close($code, $reason);
226+
$this->close();
214227
}
215228
}
216229
break;

0 commit comments

Comments
 (0)