Skip to content

Commit fbfa4db

Browse files
committed
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>
1 parent 4b7ce9e commit fbfa4db

File tree

8 files changed

+23
-8
lines changed

8 files changed

+23
-8
lines changed

core/templates/layout.base.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
</title>
1515
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
1616
<meta name="theme-color" content="<?php p($theme->getColorPrimary()); ?>">
17+
<meta name="csp-nonce" nonce="<?php p($_['cspNonce']); /* Do not pass into "content" to prevent exfiltration */ ?>">
1718
<link rel="icon" href="<?php print_unescaped(image_path('core', 'favicon.ico')); /* IE11+ supports png */ ?>">
1819
<link rel="apple-touch-icon" href="<?php print_unescaped(image_path('core', 'favicon-touch.png')); ?>">
1920
<link rel="mask-icon" sizes="any" href="<?php print_unescaped(image_path('core', 'favicon-mask.svg')); ?>" color="<?php p($theme->getColorPrimary()); ?>">

core/templates/layout.guest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
p($theme->getTitle());
2020
?>
2121
</title>
22+
<meta name="csp-nonce" nonce="<?php p($_['cspNonce']); /* Do not pass into "content" to prevent exfiltration */ ?>">
2223
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
2324
<?php if ($theme->getiTunesAppId() !== '') { ?>
2425
<meta name="apple-itunes-app" content="app-id=<?php p($theme->getiTunesAppId()); ?>">

core/templates/layout.initial-state.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
?>
77
<div id="initial-state-container" style="display: none;">
8-
<?php foreach ($_['initialStates'] as $app => $initialState) { ?>
9-
<input type="hidden" id="initial-state-<?php p($app); ?>" value="<?php p(base64_encode($initialState)); ?>">
10-
<?php }?>
8+
<?php foreach ($_['initialStates'] as $app => $initialState) { ?>
9+
<input type="hidden" id="initial-state-<?php p($app); ?>" value="<?php p(base64_encode($initialState)); ?>">
10+
<?php }?>
1111
</div>

core/templates/layout.public.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
p($theme->getTitle());
1515
?>
1616
</title>
17+
<meta name="csp-nonce" nonce="<?php p($_['cspNonce']); /* Do not pass into "content" to prevent exfiltration */ ?>">
1718
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
1819
<?php if ($theme->getiTunesAppId() !== '') { ?>
1920
<meta name="apple-itunes-app" content="app-id=<?php p($theme->getiTunesAppId()); ?>">

core/templates/layout.user.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
p($theme->getTitle());
3030
?>
3131
</title>
32+
<meta name="csp-nonce" nonce="<?php p($_['cspNonce']); /* Do not pass into "content" to prevent exfiltration */ ?>">
3233
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
3334

3435
<?php if ($theme->getiTunesAppId() !== '') { ?>

lib/private/Security/CSP/ContentSecurityPolicyNonceManager.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public function getNonce(): string {
3333
// Get the token from the CSRF token, we only use the "shared secret" part
3434
// as the first part does not add any security / entropy to the token
3535
// so it can be ignored to keep the nonce short while keeping the same randomness
36-
$this->nonce = end(explode(':', ($this->csrfTokenManager->getToken()->getEncryptedValue())));
36+
$csrfSecret = explode(':', ($this->csrfTokenManager->getToken()->getEncryptedValue()));
37+
$this->nonce = end($csrfSecret);
3738
} else {
3839
$this->nonce = $this->request->server['CSP_NONCE'];
3940
}

lib/private/Template/Base.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@ class Base {
2424
* @param string $template
2525
* @param string $requestToken
2626
* @param \OCP\IL10N $l10n
27+
* @param string $cspNonce
2728
* @param Defaults $theme
2829
*/
29-
public function __construct($template, $requestToken, $l10n, $theme) {
30-
$this->vars = [];
31-
$this->vars['requesttoken'] = $requestToken;
30+
public function __construct($template, $requestToken, $l10n, $theme, $cspNonce) {
31+
$this->vars = [
32+
'cspNonce' => $cspNonce,
33+
'requesttoken' => $requestToken,
34+
];
3235
$this->l10n = $l10n;
3336
$this->template = $template;
3437
$this->theme = $theme;

lib/private/legacy/OC_Template.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function __construct($app, $name, $renderAs = TemplateResponse::RENDER_AS
4343
$theme = OC_Util::getTheme();
4444

4545
$requestToken = (OC::$server->getSession() && $registerCall) ? \OCP\Util::callRegister() : '';
46+
$cspNonce = \OCP\Server::get(\OC\Security\CSP\ContentSecurityPolicyNonceManager::class)->getNonce();
4647

4748
$parts = explode('/', $app); // fix translation when app is something like core/lostpassword
4849
$l10n = \OC::$server->getL10N($parts[0]);
@@ -56,7 +57,13 @@ public function __construct($app, $name, $renderAs = TemplateResponse::RENDER_AS
5657
$this->path = $path;
5758
$this->app = $app;
5859

59-
parent::__construct($template, $requestToken, $l10n, $themeDefaults);
60+
parent::__construct(
61+
$template,
62+
$requestToken,
63+
$l10n,
64+
$themeDefaults,
65+
$cspNonce,
66+
);
6067
}
6168

6269

0 commit comments

Comments
 (0)