@@ -55,6 +55,7 @@ def test_default_src() -> None:
55
55
policy = build_policy ()
56
56
policy_eq ("default-src example.com example2.com" , policy )
57
57
58
+
58
59
@override_settings (CONTENT_SECURITY_POLICY = {"DIRECTIVES" : {"default-src" : {"example.com" , "example2.com" }}})
59
60
def test_default_src_is_set () -> None :
60
61
policy = build_policy ()
@@ -300,7 +301,7 @@ def test_require_trusted_types_for() -> None:
300
301
def test_trusted_types () -> None :
301
302
policy = build_policy ()
302
303
policy_eq (
303
- "default-src 'self'; trusted-types strictPolicy laxPolicy default 'allow-duplicates'" ,
304
+ "default-src 'self'; trusted-types 'allow-duplicates' default laxPolicy strictPolicy " ,
304
305
policy ,
305
306
)
306
307
@@ -319,14 +320,14 @@ def test_block_all_mixed_content() -> None:
319
320
320
321
def test_nonce () -> None :
321
322
policy = build_policy (nonce = "abc123" )
322
- policy_eq ("default-src 'self' ' nonce-abc123'" , policy )
323
+ policy_eq ("default-src 'nonce-abc123' 'self '" , policy )
323
324
324
325
325
326
@override_settings (CONTENT_SECURITY_POLICY = {"DIRECTIVES" : {"default-src" : [SELF ], "script-src" : [SELF , NONCE ], "style-src" : [SELF , NONCE ]}})
326
327
def test_nonce_in_value () -> None :
327
328
policy = build_policy (nonce = "abc123" )
328
329
policy_eq (
329
- "default -src 'self'; script -src 'self' 'nonce-abc123' ; style-src 'self' ' nonce-abc123'" ,
330
+ "script -src 'nonce-abc123' ' self'; default -src 'self'; style-src 'nonce-abc123' 'self '" ,
330
331
policy ,
331
332
)
332
333
@@ -337,6 +338,35 @@ def test_only_nonce_in_value() -> None:
337
338
policy_eq ("default-src 'nonce-abc123'" , policy )
338
339
339
340
341
+ @override_settings (CONTENT_SECURITY_POLICY = {"DIRECTIVES" : {"img-src" : ["example.com" , "example.com" ]}})
342
+ def test_deduplicate_values () -> None :
343
+ """
344
+ GitHub issue #40 - given project settings as a tuple, and
345
+ an update/replace with a string, concatenate correctly.
346
+ """
347
+ policy = build_policy ()
348
+ policy_eq ("default-src 'self'; img-src example.com" , policy )
349
+
350
+
351
+ @override_settings (CONTENT_SECURITY_POLICY = {"DIRECTIVES" : {"img-src" : ["example.com" , "example.com" ]}})
352
+ def test_deduplicate_values_update () -> None :
353
+ """
354
+ GitHub issue #40 - given project settings as a tuple, and
355
+ an update/replace with a string, concatenate correctly.
356
+ """
357
+ policy = build_policy (update = {"img-src" : "example.com" })
358
+ policy_eq ("default-src 'self'; img-src example.com" , policy )
359
+
360
+
361
+ @override_settings (CONTENT_SECURITY_POLICY = {"DIRECTIVES" : {"img-src" : ("example.com" ,)}})
362
+ def test_deduplicate_values_replace () -> None :
363
+ """
364
+ Demonstrate that GitHub issue #40 doesn't affect replacements
365
+ """
366
+ policy = build_policy (replace = {"img-src" : ["example2.com" , "example2.com" ]})
367
+ policy_eq ("default-src 'self'; img-src example2.com" , policy )
368
+
369
+
340
370
def test_boolean_directives () -> None :
341
371
for directive in ["upgrade-insecure-requests" , "block-all-mixed-content" ]:
342
372
with override_settings (CONTENT_SECURITY_POLICY = {"DIRECTIVES" : {directive : True }}):
0 commit comments