Skip to content

Commit 688eecc

Browse files
committed
add nonce generation
1 parent 4e9ce41 commit 688eecc

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

Ajax/JsUtils.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* JQuery PHP library
1515
*
1616
* @author jcheron
17-
* @version 1.004
17+
* @version 1.0.5
1818
* @license Apache 2 http://www.apache.org/licenses/
1919
*/
2020
/**
@@ -213,7 +213,8 @@ public function __construct($params = array(), $injected = NULL) {
213213
'defer' => false,
214214
'ajax' => $ajaxDefault,
215215
'historize' => true,
216-
'autoActiveLinks' => true
216+
'autoActiveLinks' => true,
217+
'nonce' => true
217218
];
218219
foreach ($defaults as $key => $val) {
219220
if (isset($params[$key]) === false || $params[$key] === "") {

Ajax/common/traits/JsUtilsInternalTrait.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ trait JsUtilsInternalTrait {
88
protected $jquery_code_for_compile = array();
99

1010
protected $jquery_code_for_compile_at_last = array();
11+
12+
protected $nonce;
1113

1214
protected function _addToCompile($jsScript) {
1315
$this->jquery_code_for_compile[] = $jsScript;
@@ -71,9 +73,26 @@ protected function minify($input) {
7173
*/
7274
protected function _open_script($src = '') {
7375
$str = '<script ';
76+
if(isset($this->params['nonce'])){
77+
$str.=' nonce="'.$this->generateNonce($this->params['nonce']).'" ';
78+
$this->onNonce();
79+
}
7480
$str .= ($src == '') ? '>' : ' src="' . $src . '">';
7581
return $str;
7682
}
83+
84+
protected function onNonce(){
85+
86+
}
87+
88+
protected function generateNonce($value=null): string {
89+
$bytes = \random_bytes((int) ($value ?? 32));
90+
return $this->nonce=\base64_encode($bytes);
91+
}
92+
93+
public function getNonce(){
94+
return $this->nonce;
95+
}
7796

7897
/**
7998
* Outputs an closing </script>

0 commit comments

Comments
 (0)