@@ -260,3 +260,54 @@ Deno.test("CSP - useNonce replaces unsafe-inline in default-src", async () => {
260260 // default-src should have nonce, not unsafe-inline
261261 expect ( cspHeader ) . toMatch ( / d e f a u l t - s r c ' s e l f ' ' n o n c e - [ a - f 0 - 9 ] + ' / ) ;
262262} ) ;
263+
264+ Deno . test ( "CSP - useNonce with insecureUnsafeInline keeps both" , async ( ) => {
265+ const app = new App ( )
266+ . use ( csp ( { useNonce : true , insecureUnsafeInline : true } ) )
267+ . get ( "/" , ( ctx ) => {
268+ return ctx . render (
269+ < html >
270+ < head >
271+ < style > { "body { color: red; }" } </ style >
272+ </ head >
273+ < body >
274+ < h1 > hello</ h1 >
275+ </ body >
276+ </ html > ,
277+ ) ;
278+ } ) ;
279+
280+ const server = new FakeServer ( app . handler ( ) ) ;
281+ const res = await server . get ( "/" ) ;
282+ const html = await res . text ( ) ;
283+ const cspHeader = res . headers . get ( "Content-Security-Policy" ) ! ;
284+
285+ // Should contain both unsafe-inline and nonce
286+ expect ( cspHeader ) . toContain ( "'unsafe-inline'" ) ;
287+ expect ( cspHeader ) . toMatch (
288+ / s c r i p t - s r c ' s e l f ' ' u n s a f e - i n l i n e ' ' n o n c e - [ a - f 0 - 9 ] + ' / ,
289+ ) ;
290+ expect ( cspHeader ) . toMatch (
291+ / s t y l e - s r c ' s e l f ' ' u n s a f e - i n l i n e ' ' n o n c e - [ a - f 0 - 9 ] + ' / ,
292+ ) ;
293+
294+ // HTML should still have nonce on the style tag
295+ const nonceMatch = cspHeader . match ( / n o n c e - ( [ a - f 0 - 9 ] + ) / ) ;
296+ expect ( nonceMatch ) . not . toBeNull ( ) ;
297+ const nonce = nonceMatch ! [ 1 ] ;
298+ expect ( html ) . toContain ( `nonce="${ nonce } "` ) ;
299+ } ) ;
300+
301+ Deno . test ( "CSP - insecureUnsafeInline without useNonce has no effect" , async ( ) => {
302+ const handler = new App ( )
303+ . use ( csp ( { insecureUnsafeInline : true } ) )
304+ . get ( "/" , ( ) => new Response ( "ok" ) )
305+ . handler ( ) ;
306+
307+ const res = await handler ( new Request ( "https://localhost/" ) ) ;
308+ const cspHeader = res . headers . get ( "Content-Security-Policy" ) ! ;
309+
310+ // Without useNonce, insecureUnsafeInline should not change anything
311+ expect ( cspHeader ) . toContain ( "'unsafe-inline'" ) ;
312+ expect ( cspHeader ) . not . toContain ( "'nonce-" ) ;
313+ } ) ;
0 commit comments