diff --git a/src/Ratchet/Http/OriginCheck.php b/src/Ratchet/Http/OriginCheck.php index 257c847b..a4ca8400 100644 --- a/src/Ratchet/Http/OriginCheck.php +++ b/src/Ratchet/Http/OriginCheck.php @@ -33,7 +33,7 @@ public function __construct(MessageComponentInterface $component, array $allowed */ #[HackSupportForPHP8] public function onOpen(ConnectionInterface $conn, ?RequestInterface $request = null) { /* public function onOpen(ConnectionInterface $conn, RequestInterface $request = null) { /**/ - $header = (string)$request->getHeader('Origin')[0]; + $header = $request->getHeaderLine('Origin'); $origin = parse_url($header, PHP_URL_HOST) ?: $header; if (!in_array($origin, $this->allowedOrigins)) { diff --git a/tests/unit/Http/OriginCheckTest.php b/tests/unit/Http/OriginCheckTest.php index 3f804a04..42321518 100644 --- a/tests/unit/Http/OriginCheckTest.php +++ b/tests/unit/Http/OriginCheckTest.php @@ -13,7 +13,7 @@ class OriginCheckTest extends AbstractMessageComponentTestCase { */ public function setUpConnection() { $this->_reqStub = $this->getMockBuilder('Psr\Http\Message\RequestInterface')->getMock(); - $this->_reqStub->expects($this->any())->method('getHeader')->will($this->returnValue(['localhost'])); + $this->_reqStub->expects($this->any())->method('getHeaderLine')->with('Origin')->willReturn('localhost'); parent::setUpConnection(); @@ -44,6 +44,24 @@ public function testCloseOnNonMatchingOrigin() { $this->_serv->onOpen($this->_conn, $this->_reqStub); } + public function testCloseOnMissingOrigin() { + $this->_serv->allowedOrigins = ['socketo.me']; + $this->_conn->expects($this->once())->method('close'); + + $this->_reqStub->expects($this->once())->method('getHeaderLine')->with('Origin')->willReturn(''); + + $this->_serv->onOpen($this->_conn, $this->_reqStub); + } + + public function testCloseOnDuplicateOrigin() { + $this->_serv->allowedOrigins = ['socketo.me']; + $this->_conn->expects($this->once())->method('close'); + + $this->_reqStub->expects($this->once())->method('getHeaderLine')->with('Origin')->willReturn('http://socketo.me,https://socketo.me'); + + $this->_serv->onOpen($this->_conn, $this->_reqStub); + } + public function testOnMessage() { $this->passthroughMessageTest('Hello World!'); }