diff options
author | Ferdinand Thiessen <opensource@fthiessen.de> | 2024-08-01 23:06:55 +0200 |
---|---|---|
committer | Ferdinand Thiessen <opensource@fthiessen.de> | 2024-08-13 10:32:44 +0200 |
commit | 2916e5df7e08fc588e752beaf486d907112a34ee (patch) | |
tree | 968c83adcd9a70717bda5d1d1a5e06f23a158097 /lib | |
parent | 009761be58c4485f29a8d3382e51fb4e1bfbeec4 (diff) | |
download | nextcloud-server-2916e5df7e08fc588e752beaf486d907112a34ee.tar.gz nextcloud-server-2916e5df7e08fc588e752beaf486d907112a34ee.zip |
feat: Provide CSP nonce as `<meta>` element
This way we use the CSP nonce for dynamically loaded scripts.
Important to notice: The CSP nonce must NOT be injected in `content` as
this can lead to value exfiltration using e.g. side-channel attacts (CSS selectors).
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/Security/CSP/ContentSecurityPolicyNonceManager.php | 3 | ||||
-rw-r--r-- | lib/private/Template/Base.php | 9 | ||||
-rw-r--r-- | lib/private/legacy/OC_Template.php | 9 |
3 files changed, 16 insertions, 5 deletions
diff --git a/lib/private/Security/CSP/ContentSecurityPolicyNonceManager.php b/lib/private/Security/CSP/ContentSecurityPolicyNonceManager.php index 2046c240291..993f74ae0e4 100644 --- a/lib/private/Security/CSP/ContentSecurityPolicyNonceManager.php +++ b/lib/private/Security/CSP/ContentSecurityPolicyNonceManager.php @@ -33,7 +33,8 @@ class ContentSecurityPolicyNonceManager { // 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 - $this->nonce = end(explode(':', ($this->csrfTokenManager->getToken()->getEncryptedValue()))); + $csrfSecret = explode(':', ($this->csrfTokenManager->getToken()->getEncryptedValue())); + $this->nonce = end($csrfSecret); } else { $this->nonce = $this->request->server['CSP_NONCE']; } diff --git a/lib/private/Template/Base.php b/lib/private/Template/Base.php index 2adf9172f6b..b48c10143cf 100644 --- a/lib/private/Template/Base.php +++ b/lib/private/Template/Base.php @@ -24,11 +24,14 @@ class Base { * @param string $template * @param string $requestToken * @param \OCP\IL10N $l10n + * @param string $cspNonce * @param Defaults $theme */ - public function __construct($template, $requestToken, $l10n, $theme) { - $this->vars = []; - $this->vars['requesttoken'] = $requestToken; + public function __construct($template, $requestToken, $l10n, $theme, $cspNonce) { + $this->vars = [ + 'cspNonce' => $cspNonce, + 'requesttoken' => $requestToken, + ]; $this->l10n = $l10n; $this->template = $template; $this->theme = $theme; diff --git a/lib/private/legacy/OC_Template.php b/lib/private/legacy/OC_Template.php index 5caa733b115..e3e9a7abc5f 100644 --- a/lib/private/legacy/OC_Template.php +++ b/lib/private/legacy/OC_Template.php @@ -43,6 +43,7 @@ class OC_Template extends \OC\Template\Base { $theme = OC_Util::getTheme(); $requestToken = (OC::$server->getSession() && $registerCall) ? \OCP\Util::callRegister() : ''; + $cspNonce = \OCP\Server::get(\OC\Security\CSP\ContentSecurityPolicyNonceManager::class)->getNonce(); $parts = explode('/', $app); // fix translation when app is something like core/lostpassword $l10n = \OC::$server->getL10N($parts[0]); @@ -56,7 +57,13 @@ class OC_Template extends \OC\Template\Base { $this->path = $path; $this->app = $app; - parent::__construct($template, $requestToken, $l10n, $themeDefaults); + parent::__construct( + $template, + $requestToken, + $l10n, + $themeDefaults, + $cspNonce, + ); } |