Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable10] Fixes #32090 - browser extension urls in origin header do … #32189

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 26 additions & 2 deletions apps/dav/lib/Connector/Sabre/CorsPlugin.php
Expand Up @@ -95,8 +95,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 @@ -145,4 +152,21 @@ public function setOptionsRequestHeaders(RequestInterface $request, ResponseInte
return false;
}
}

/**
* in addition to schemas used by extensions we ignore empty origin header
* values as well as 'null' which is not valid by the specification but used
* by some clients.
* @link https://github.com/owncloud/core/pull/32120#issuecomment-407008243
*
* @param string $originHeader
* @return bool
*/
public function ignoreOriginHeader($originHeader) {
if (\in_array($originHeader, ['', null, 'null'], true)) {
return true;
}
$schema = \parse_url($originHeader, PHP_URL_SCHEME);
return \in_array(\strtolower($schema), ['moz-extension', 'chrome-extension']);
}
}
29 changes: 28 additions & 1 deletion apps/dav/tests/unit/Connector/Sabre/CorsPluginTest.php
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');

Expand Down Expand Up @@ -262,8 +261,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 @@ -298,4 +304,25 @@ 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 string Origin' => [true, 'null'],
'Null Origin' => [true, null],
'plain http' => [false, 'http://example.net/'],
];
}
}