2016-10-24 16:31:06 +02:00
|
|
|
<?php
|
2019-12-03 19:57:53 +01:00
|
|
|
|
2018-03-05 15:27:05 +01:00
|
|
|
declare(strict_types=1);
|
2019-12-03 19:57:53 +01:00
|
|
|
|
2016-10-24 16:31:06 +02:00
|
|
|
/**
|
2024-05-23 09:26:56 +02:00
|
|
|
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2016-10-24 16:31:06 +02:00
|
|
|
*/
|
|
|
|
namespace OC\Security\CSP;
|
|
|
|
|
2016-10-26 12:07:10 +02:00
|
|
|
use OC\AppFramework\Http\Request;
|
2016-10-24 16:31:06 +02:00
|
|
|
use OC\Security\CSRF\CsrfTokenManager;
|
2016-10-25 21:36:17 +02:00
|
|
|
use OCP\IRequest;
|
2016-10-24 16:31:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @package OC\Security\CSP
|
|
|
|
*/
|
|
|
|
class ContentSecurityPolicyNonceManager {
|
2023-06-26 13:28:09 +03:30
|
|
|
private string $nonce = '';
|
2016-10-24 16:31:06 +02:00
|
|
|
|
2023-06-26 13:28:09 +03:30
|
|
|
public function __construct(
|
|
|
|
private CsrfTokenManager $csrfTokenManager,
|
|
|
|
private IRequest $request,
|
|
|
|
) {
|
2016-10-24 16:31:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-06-26 13:28:09 +03:30
|
|
|
* Returns the current CSP nonce
|
2016-10-24 16:31:06 +02:00
|
|
|
*/
|
2018-03-05 15:27:05 +01:00
|
|
|
public function getNonce(): string {
|
2016-10-24 16:31:06 +02:00
|
|
|
if ($this->nonce === '') {
|
2019-07-08 20:54:45 +01:00
|
|
|
if (empty($this->request->server['CSP_NONCE'])) {
|
2024-08-01 23:06:23 +02:00
|
|
|
// Get the token from the CSRF token, we only use the "shared secret" part
|
|
|
|
// as the first part does not add any security / entropy to the token
|
|
|
|
// so it can be ignored to keep the nonce short while keeping the same randomness
|
2024-08-01 23:06:55 +02:00
|
|
|
$csrfSecret = explode(':', ($this->csrfTokenManager->getToken()->getEncryptedValue()));
|
|
|
|
$this->nonce = end($csrfSecret);
|
2019-07-08 20:54:45 +01:00
|
|
|
} else {
|
|
|
|
$this->nonce = $this->request->server['CSP_NONCE'];
|
|
|
|
}
|
2016-10-24 16:31:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->nonce;
|
|
|
|
}
|
2016-10-25 21:36:17 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the browser supports CSP v3
|
|
|
|
*/
|
2018-03-05 15:27:05 +01:00
|
|
|
public function browserSupportsCspV3(): bool {
|
2024-03-22 16:03:29 +01:00
|
|
|
$browserBlocklist = [
|
|
|
|
Request::USER_AGENT_IE,
|
2016-10-25 21:36:17 +02:00
|
|
|
];
|
|
|
|
|
2024-03-22 16:03:29 +01:00
|
|
|
if ($this->request->isUserAgent($browserBlocklist)) {
|
|
|
|
return false;
|
2016-10-25 21:36:17 +02:00
|
|
|
}
|
|
|
|
|
2024-03-22 16:03:29 +01:00
|
|
|
return true;
|
2016-10-25 21:36:17 +02:00
|
|
|
}
|
2016-10-24 16:31:06 +02:00
|
|
|
}
|