Skip to content

Commit 0c9a9b5

Browse files
committed
add nonce for Ubiquity JsUtils
1 parent db96c71 commit 0c9a9b5

File tree

3 files changed

+18
-21
lines changed

3 files changed

+18
-21
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
.settings/*
44
doxygeneJquery
55
doc/*
6+
.idea/

Ajax/common/traits/JsUtilsInternalTrait.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -73,27 +73,10 @@ protected function minify($input) {
7373
*/
7474
protected function _open_script($src = '') {
7575
$str = '<script ';
76-
if (! $this->isAjax() && isset($this->params['nonce'])) {
77-
$nonce = $this->nonce ?? $this->generateNonce($this->params['nonce']);
78-
$str .= ' nonce="' . $nonce . '" ';
79-
}
8076
$str .= ($src == '') ? '>' : ' src="' . $src . '">';
8177
return $str;
8278
}
8379

84-
protected function onNonce() {}
85-
86-
protected function generateNonce($value = null): string {
87-
$bytes = \random_bytes((int) ($value ?? 32));
88-
$this->nonce = \base64_encode($bytes);
89-
$this->onNonce();
90-
return $this->nonce;
91-
}
92-
93-
public function getNonce() {
94-
return $this->nonce;
95-
}
96-
9780
/**
9881
* Outputs an closing </script>
9982
*
@@ -111,8 +94,4 @@ protected function conflict() {
11194
public function addToCompile($jsScript) {
11295
$this->_addToCompile($jsScript);
11396
}
114-
115-
public function isAjax(): bool {
116-
return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ! empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
117-
}
11897
}

Ajax/php/ubiquity/JsUtils.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,26 @@
33

44
use Ubiquity\controllers\Startup;
55
use Ubiquity\utils\http\URequest;
6+
use Ubiquity\security\csp\ContentSecurityManager;
67

78
class JsUtils extends \Ajax\JsUtils {
89

10+
/**
11+
* Outputs an opening <script>
12+
*
13+
* @param string $src
14+
* @return string
15+
*/
16+
protected function _open_script($src = '') {
17+
$str = '<script ';
18+
if (isset($this->params['nonce']) && ContentSecurityManager::isStarted()) {
19+
$nonce = ContentSecurityManager::getNonce('jsUtils');
20+
$str .= ' nonce="' . $nonce . '" ';
21+
}
22+
$str .= ($src == '') ? '>' : ' src="' . $src . '">';
23+
return $str;
24+
}
25+
926
public function getUrl($url) {
1027
return URequest::getUrl($url);
1128
}

0 commit comments

Comments
 (0)