@@ -15,6 +15,8 @@ class Panel extends \Magento\Framework\View\Element\Template
1515
1616 protected $ qdbHelperRegister ;
1717
18+ private $ cspNonceProvider ;
19+
1820
1921 public function __construct (
2022 \Magento \Framework \View \Element \Template \Context $ context ,
@@ -27,6 +29,10 @@ public function __construct(
2729
2830 $ this ->helper = $ helper ;
2931 $ this ->qdbHelperRegister = $ qdbHelperRegister ;
32+
33+ if (class_exists (\Magento \Csp \Helper \CspNonceProvider::class)) {
34+ $ this ->cspNonceProvider = ObjectManager::getInstance ()->get (\Magento \Csp \Helper \CspNonceProvider::class);
35+ }
3036 }
3137
3238 /**
@@ -190,7 +196,9 @@ protected function _toHtml()
190196 {
191197 try {
192198 $ buffer = parent ::_toHtml ();
193- return $ this ->sanitizeOutput ($ buffer );
199+ $ buffer = $ this ->sanitizeOutput ($ buffer );
200+ $ buffer = $ this ->addNonceOnScript ($ buffer );
201+ return $ buffer ;
194202 } catch (\Exception $ e ) {
195203 return $ e ->getMessage ();
196204 }
@@ -225,6 +233,20 @@ protected function sanitizeOutput($buffer)
225233 return $ buffer ;
226234 }
227235
236+ private function addNonceOnScript ($ buffer )
237+ {
238+ //$this->setAutoNonceOnSrciptTag(true);
239+ if ($ this ->getAutoNonceOnSrciptTag () && $ this ->cspNonceProvider ) {
240+ $ openscriptPattern = '/<(script)(.*)>/ ' ;
241+ $ nonceTiInject = 'nonce=" ' . $ this ->cspNonceProvider ->generateNonce () .'" ' ;
242+ if (preg_match ($ openscriptPattern , $ buffer , $ matches ) ) {
243+ $ buffer = preg_replace ($ openscriptPattern , '<${1} ' . $ nonceTiInject . '${2}> ' , $ buffer );
244+ }
245+ }
246+
247+ return $ buffer ;
248+ }
249+
228250 public function htmlFormatClass ($ class )
229251 {
230252 return $ this ->helper ->getIDELinkForClass ($ class );
@@ -245,4 +267,6 @@ public function getQdbConfig($key, $scopeType = ScopeConfigInterface::SCOPE_TYPE
245267 return $ this ->helper ->getQdbConfig ($ key , $ scopeType , $ scopeCode );
246268 }
247269
270+
271+
248272}
0 commit comments