@@ -17,10 +17,10 @@ def test_basic(self):
1717 'repr' : "<SimpleCookie: chips='ahoy' vienna='finger'>" ,
1818 'output' : 'Set-Cookie: chips=ahoy\n Set-Cookie: vienna=finger' },
1919
20- {'data' : 'keebler="E=mc2; L=\\ "Loves\\ "; fudge=\\ 012 ;"' ,
21- 'dict' : {'keebler' : 'E=mc2; L="Loves"; fudge=\012 ;' },
22- 'repr' : '''<SimpleCookie: keebler='E=mc2; L="Loves"; fudge=\\ n ;'>''' ,
23- 'output' : 'Set-Cookie: keebler="E=mc2; L=\\ "Loves\\ "; fudge=\\ 012 ;"' },
20+ {'data' : 'keebler="E=mc2; L=\\ "Loves\\ "; fudge=;"' ,
21+ 'dict' : {'keebler' : 'E=mc2; L="Loves"; fudge=;' },
22+ 'repr' : '''<SimpleCookie: keebler='E=mc2; L="Loves"; fudge=;'>''' ,
23+ 'output' : 'Set-Cookie: keebler="E=mc2; L=\\ "Loves\\ "; fudge=;"' },
2424
2525 # Check illegal cookies that have an '=' char in an unquoted value
2626 {'data' : 'keebler=E=mc2' ,
@@ -594,6 +594,51 @@ def test_repr(self):
594594 r'Set-Cookie: key=coded_val; '
595595 r'expires=\w+, \d+ \w+ \d+ \d+:\d+:\d+ \w+' )
596596
597+ def test_control_characters (self ):
598+ for c0 in support .control_characters_c0 ():
599+ morsel = cookies .Morsel ()
600+
601+ # .__setitem__()
602+ with self .assertRaises (cookies .CookieError ):
603+ morsel [c0 ] = "val"
604+ with self .assertRaises (cookies .CookieError ):
605+ morsel ["path" ] = c0
606+
607+ # .setdefault()
608+ with self .assertRaises (cookies .CookieError ):
609+ morsel .setdefault ("path" , c0 )
610+ with self .assertRaises (cookies .CookieError ):
611+ morsel .setdefault (c0 , "val" )
612+
613+ # .set()
614+ with self .assertRaises (cookies .CookieError ):
615+ morsel .set (c0 , "val" , "coded-value" )
616+ with self .assertRaises (cookies .CookieError ):
617+ morsel .set ("path" , c0 , "coded-value" )
618+ with self .assertRaises (cookies .CookieError ):
619+ morsel .set ("path" , "val" , c0 )
620+
621+ def test_control_characters_output (self ):
622+ # Tests that even if the internals of Morsel are modified
623+ # that a call to .output() has control character safeguards.
624+ for c0 in support .control_characters_c0 ():
625+ morsel = cookies .Morsel ()
626+ morsel .set ("key" , "value" , "coded-value" )
627+ morsel ._key = c0 # Override private variable.
628+ cookie = cookies .SimpleCookie ()
629+ cookie ["cookie" ] = morsel
630+ with self .assertRaises (cookies .CookieError ):
631+ cookie .output ()
632+
633+ morsel = cookies .Morsel ()
634+ morsel .set ("key" , "value" , "coded-value" )
635+ morsel ._coded_value = c0 # Override private variable.
636+ cookie = cookies .SimpleCookie ()
637+ cookie ["cookie" ] = morsel
638+ with self .assertRaises (cookies .CookieError ):
639+ cookie .output ()
640+
641+
597642
598643def load_tests (loader , tests , pattern ):
599644 tests .addTest (doctest .DocTestSuite (cookies ))
0 commit comments