@@ -30,17 +30,46 @@ Deno.test("CSP - GET with override options", async () => {
3030 . handler ( ) ;
3131
3232 const res = await handler ( new Request ( "https://localhost/" ) ) ;
33+ const header = res . headers . get ( "Content-Security-Policy" ) ! ;
3334
3435 expect ( res . status ) . toBe ( 200 ) ;
35- expect ( res . headers . get ( "Content-Security-Policy" ) ) . toContain (
36- "font-src 'self' 'https://fonts.gstatic.com'; style-src 'self' 'https://fonts.googleapis.com' " ,
36+ expect ( header ) . toContain (
37+ "font-src 'self' 'https://fonts.gstatic.com'" ,
3738 ) ;
38- expect ( res . headers . get ( "Content-Security-Policy" ) ) . toContain (
39- "report-uri /api/csp-reports " ,
39+ expect ( header ) . toContain (
40+ "style-src 'self' 'https://fonts.googleapis.com' " ,
4041 ) ;
42+ expect ( header ) . toContain ( "report-uri /api/csp-reports" ) ;
4143 expect ( res . headers . get ( "Reporting-Endpoints" ) ) . toBe (
4244 'csp-endpoint="/api/csp-reports"' ,
4345 ) ;
46+
47+ // Overrides should replace defaults, not duplicate them
48+ const fontSrcCount = header . split ( "font-src" ) . length - 1 ;
49+ expect ( fontSrcCount ) . toBe ( 1 ) ;
50+ const styleSrcCount = header . split ( "style-src" ) . length - 1 ;
51+ expect ( styleSrcCount ) . toBe ( 1 ) ;
52+ } ) ;
53+
54+ Deno . test ( "CSP - user directives override defaults" , async ( ) => {
55+ const handler = new App ( )
56+ . use ( csp ( {
57+ csp : [
58+ "img-src 'self' https://example.com data:" ,
59+ ] ,
60+ } ) )
61+ . get ( "/" , ( ) => new Response ( "ok" ) )
62+ . handler ( ) ;
63+
64+ const res = await handler ( new Request ( "https://localhost/" ) ) ;
65+ const header = res . headers . get ( "Content-Security-Policy" ) ! ;
66+
67+ // Should contain the user's img-src, not the default
68+ expect ( header ) . toContain ( "img-src 'self' https://example.com data:" ) ;
69+
70+ // Should not duplicate img-src
71+ const imgSrcCount = header . split ( "img-src" ) . length - 1 ;
72+ expect ( imgSrcCount ) . toBe ( 1 ) ;
4473} ) ;
4574
4675Deno . test ( "CSP - GET report only" , async ( ) => {
0 commit comments