Skip to content

Commit

Permalink
Fixes #32090 - browser extension urls in origin header do not trigger…
Browse files Browse the repository at this point in the history
… CORS verification
  • Loading branch information
DeepDiver1975 committed Jul 23, 2018
1 parent 1a7539c commit 97ac510
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
23 changes: 21 additions & 2 deletions apps/dav/lib/Connector/Sabre/CorsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,15 @@ public function initialize(\Sabre\DAV\Server $server) {
$this->server = $server;

$request = $this->server->httpRequest;
if (!$request->hasHeader('Origin') || Util::isSameDomain($request->getHeader('Origin'), $request->getAbsoluteUrl())) {
return false;
if (!$request->hasHeader('Origin')) {
return;
}
$originHeader = $request->getHeader('Origin');
if ($this->ignoreOriginHeader($originHeader)) {
return;
}
if (Util::isSameDomain($originHeader, $request->getAbsoluteUrl())) {
return;
}

$this->server->on('beforeMethod', [$this, 'setCorsHeaders']);
Expand Down Expand Up @@ -147,4 +154,16 @@ public function setOptionsRequestHeaders(RequestInterface $request, ResponseInte
return false;
}
}

/**
* @param string $originHeader
* @return bool
*/
public function ignoreOriginHeader($originHeader) {
if (empty($originHeader)) {
return true;
}
$schema = \parse_url($originHeader, PHP_URL_SCHEME);
return \in_array(\strtolower($schema), ['moz-extension', 'chrome-extension']);
}
}
28 changes: 27 additions & 1 deletion apps/dav/tests/unit/Connector/Sabre/CorsPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public function setUp() {
$this->server->sapi = $this->getMockBuilder(\stdClass::class)
->setMethods(['sendResponse'])
->getMock();
$this->server->sapi->expects($this->once())->method('sendResponse')->with($this->server->httpResponse);

$this->server->httpRequest->setMethod('OPTIONS');
$this->server->httpRequest->setUrl('/owncloud/remote.php/dav/files/user1/target/path');
Expand Down Expand Up @@ -263,8 +262,15 @@ public function optionsCases() {

/**
* @dataProvider optionsCases
* @param $allowedDomains
* @param $hasUser
* @param $requestHeaders
* @param $expectedStatus
* @param array $expectedHeaders
* @param bool $expectDavHeaders
*/
public function testOptionsHeaders($allowedDomains, $hasUser, $requestHeaders, $expectedStatus, array $expectedHeaders, $expectDavHeaders = false) {
$this->server->sapi->expects($this->once())->method('sendResponse')->with($this->server->httpResponse);
$user = $this->createMock(IUser::class);
$user->method('getUID')->willReturn('someuser');

Expand Down Expand Up @@ -299,4 +305,24 @@ public function testOptionsHeaders($allowedDomains, $hasUser, $requestHeaders, $
// if it has DAV headers, it means we did not bypass further processing
$this->assertEquals($expectDavHeaders, $this->server->httpResponse->hasHeader('DAV'));
}

/**
* @dataProvider providesOriginUrls
* @param $expectedValue
* @param $url
*/
public function testExtensionRequests($expectedValue, $url) {
$plugin = new CorsPlugin($this->createMock(IUserSession::class));
self::assertEquals($expectedValue, $plugin->ignoreOriginHeader($url));
}

public function providesOriginUrls() {
return [
'Firefox extension' => [true, 'moz-extension://mgmnhfbjphngabcpbpmapnnaabhnchmi/'],
'Chrome extension' => [true, 'chrome-extension://mgmnhfbjphngabcpbpmapnnaabhnchmi/'],
'Empty Origin' => [true, ''],
'Null Origin' => [true, null],
'plain http' => [false, 'http://example.net/'],
];
}
}

0 comments on commit 97ac510

Please sign in to comment.