Skip to content

Commit c285cf9

Browse files
authored
Merge pull request #1109 from clue-labs/undefined-origin
Fix warning when handling HTTP request with missing `Origin` header
2 parents d66c645 + 883c07c commit c285cf9

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/Ratchet/Http/OriginCheck.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function __construct(MessageComponentInterface $component, array $allowed
3333
*/
3434
#[HackSupportForPHP8] public function onOpen(ConnectionInterface $conn, ?RequestInterface $request = null) { /*
3535
public function onOpen(ConnectionInterface $conn, RequestInterface $request = null) { /**/
36-
$header = (string)$request->getHeader('Origin')[0];
36+
$header = $request->getHeaderLine('Origin');
3737
$origin = parse_url($header, PHP_URL_HOST) ?: $header;
3838

3939
if (!in_array($origin, $this->allowedOrigins)) {

tests/unit/Http/OriginCheckTest.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class OriginCheckTest extends AbstractMessageComponentTestCase {
1313
*/
1414
public function setUpConnection() {
1515
$this->_reqStub = $this->getMockBuilder('Psr\Http\Message\RequestInterface')->getMock();
16-
$this->_reqStub->expects($this->any())->method('getHeader')->will($this->returnValue(['localhost']));
16+
$this->_reqStub->expects($this->any())->method('getHeaderLine')->with('Origin')->willReturn('localhost');
1717

1818
parent::setUpConnection();
1919

@@ -44,6 +44,24 @@ public function testCloseOnNonMatchingOrigin() {
4444
$this->_serv->onOpen($this->_conn, $this->_reqStub);
4545
}
4646

47+
public function testCloseOnMissingOrigin() {
48+
$this->_serv->allowedOrigins = ['socketo.me'];
49+
$this->_conn->expects($this->once())->method('close');
50+
51+
$this->_reqStub->expects($this->once())->method('getHeaderLine')->with('Origin')->willReturn('');
52+
53+
$this->_serv->onOpen($this->_conn, $this->_reqStub);
54+
}
55+
56+
public function testCloseOnDuplicateOrigin() {
57+
$this->_serv->allowedOrigins = ['socketo.me'];
58+
$this->_conn->expects($this->once())->method('close');
59+
60+
$this->_reqStub->expects($this->once())->method('getHeaderLine')->with('Origin')->willReturn('http://socketo.me,https://socketo.me');
61+
62+
$this->_serv->onOpen($this->_conn, $this->_reqStub);
63+
}
64+
4765
public function testOnMessage() {
4866
$this->passthroughMessageTest('Hello World!');
4967
}

0 commit comments

Comments
 (0)