2016-10-24 14:31:06 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Security\CSP;
|
|
|
|
|
2016-10-26 10:07:10 +00:00
|
|
|
use OC\AppFramework\Http\Request;
|
2016-10-24 14:31:06 +00:00
|
|
|
use OC\Security\CSRF\CsrfTokenManager;
|
2016-10-25 19:36:17 +00:00
|
|
|
use OCP\IRequest;
|
2016-10-24 14:31:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @package OC\Security\CSP
|
|
|
|
*/
|
|
|
|
class ContentSecurityPolicyNonceManager {
|
|
|
|
/** @var CsrfTokenManager */
|
|
|
|
private $csrfTokenManager;
|
2016-10-25 19:36:17 +00:00
|
|
|
/** @var IRequest */
|
|
|
|
private $request;
|
2016-10-24 14:31:06 +00:00
|
|
|
/** @var string */
|
|
|
|
private $nonce = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param CsrfTokenManager $csrfTokenManager
|
2016-10-25 19:36:17 +00:00
|
|
|
* @param IRequest $request
|
2016-10-24 14:31:06 +00:00
|
|
|
*/
|
2016-10-25 19:36:17 +00:00
|
|
|
public function __construct(CsrfTokenManager $csrfTokenManager,
|
|
|
|
IRequest $request) {
|
2016-10-24 14:31:06 +00:00
|
|
|
$this->csrfTokenManager = $csrfTokenManager;
|
2016-10-25 19:36:17 +00:00
|
|
|
$this->request = $request;
|
2016-10-24 14:31:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current CSP nounce
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getNonce() {
|
|
|
|
if($this->nonce === '') {
|
|
|
|
$this->nonce = base64_encode($this->csrfTokenManager->getToken()->getEncryptedValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->nonce;
|
|
|
|
}
|
2016-10-25 19:36:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the browser supports CSP v3
|
2016-10-25 20:13:09 +00:00
|
|
|
*
|
2016-10-25 19:36:17 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function browserSupportsCspV3() {
|
|
|
|
$browserWhitelist = [
|
2016-10-26 10:07:10 +00:00
|
|
|
Request::USER_AGENT_CHROME,
|
2016-10-25 19:36:17 +00:00
|
|
|
// Firefox 45+
|
|
|
|
'/^Mozilla\/5\.0 \([^)]+\) Gecko\/[0-9.]+ Firefox\/(4[5-9]|[5-9][0-9])\.[0-9.]+$/',
|
|
|
|
];
|
|
|
|
|
|
|
|
if($this->request->isUserAgent($browserWhitelist)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-24 14:31:06 +00:00
|
|
|
}
|