File tree Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Original file line number Diff line number Diff line change @@ -194,7 +194,9 @@ function doctype(string $type = 'html5'): string
194
194
*/
195
195
function script_tag ($ src = '' , bool $ indexPage = false ): string
196
196
{
197
- $ script = '<script ' ;
197
+ $ cspNonce = csp_script_nonce ();
198
+ $ cspNonce = $ cspNonce ? ' ' . $ cspNonce : $ cspNonce ;
199
+ $ script = '<script ' . $ cspNonce . ' ' ;
198
200
if (! is_array ($ src )) {
199
201
$ src = ['src ' => $ src ];
200
202
}
Original file line number Diff line number Diff line change 11
11
12
12
namespace CodeIgniter \Helpers ;
13
13
14
+ use CodeIgniter \Config \Factories ;
14
15
use CodeIgniter \Files \Exceptions \FileNotFoundException ;
15
16
use CodeIgniter \Test \CIUnitTestCase ;
17
+ use Config \App ;
16
18
17
19
/**
18
20
* @internal
@@ -269,6 +271,28 @@ public function testScriptTagWithSrcAndAttributes()
269
271
$ this ->assertSame ($ expected , script_tag ($ target ));
270
272
}
271
273
274
+ public function testScriptTagWithCsp ()
275
+ {
276
+ // Reset CSP object
277
+ $ this ->resetServices ();
278
+
279
+ $ config = new App ();
280
+ $ config ->CSPEnabled = true ;
281
+ Factories::injectMock ('config ' , 'App ' , $ config );
282
+
283
+ $ target = 'http://site.com/js/mystyles.js ' ;
284
+ $ html = script_tag ($ target );
285
+
286
+ $ this ->assertMatchesRegularExpression (
287
+ '!<script nonce="\w+?" src="http://site.com/js/mystyles.js".*?>!u ' ,
288
+ $ html
289
+ );
290
+
291
+ // Reset CSP object
292
+ $ this ->resetFactories ();
293
+ $ this ->resetServices ();
294
+ }
295
+
272
296
/**
273
297
* This test has probably no real-world value but may help detecting
274
298
* a change in the default behaviour.
You can’t perform that action at this time.
0 commit comments