@@ -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' ,
@@ -563,6 +563,50 @@ def test_repr(self):
563563 r'Set-Cookie: key=coded_val; '
564564 r'expires=\w+, \d+ \w+ \d+ \d+:\d+:\d+ \w+' )
565565
566+ def test_control_characters (self ):
567+ for c0 in support .control_characters_c0 ():
568+ morsel = cookies .Morsel ()
569+
570+ # .__setitem__()
571+ with self .assertRaises (cookies .CookieError ):
572+ morsel [c0 ] = "val"
573+ with self .assertRaises (cookies .CookieError ):
574+ morsel ["path" ] = c0
575+
576+ # .setdefault()
577+ with self .assertRaises (cookies .CookieError ):
578+ morsel .setdefault ("path" , c0 )
579+ with self .assertRaises (cookies .CookieError ):
580+ morsel .setdefault (c0 , "val" )
581+
582+ # .set()
583+ with self .assertRaises (cookies .CookieError ):
584+ morsel .set (c0 , "val" , "coded-value" )
585+ with self .assertRaises (cookies .CookieError ):
586+ morsel .set ("path" , c0 , "coded-value" )
587+ with self .assertRaises (cookies .CookieError ):
588+ morsel .set ("path" , "val" , c0 )
589+
590+ def test_control_characters_output (self ):
591+ # Tests that even if the internals of Morsel are modified
592+ # that a call to .output() has control character safeguards.
593+ for c0 in support .control_characters_c0 ():
594+ morsel = cookies .Morsel ()
595+ morsel .set ("key" , "value" , "coded-value" )
596+ morsel ._key = c0 # Override private variable.
597+ cookie = cookies .SimpleCookie ()
598+ cookie ["cookie" ] = morsel
599+ with self .assertRaises (cookies .CookieError ):
600+ cookie .output ()
601+
602+ morsel = cookies .Morsel ()
603+ morsel .set ("key" , "value" , "coded-value" )
604+ morsel ._coded_value = c0 # Override private variable.
605+ cookie = cookies .SimpleCookie ()
606+ cookie ["cookie" ] = morsel
607+ with self .assertRaises (cookies .CookieError ):
608+ cookie .output ()
609+
566610
567611def load_tests (loader , tests , pattern ):
568612 tests .addTest (doctest .DocTestSuite (cookies ))
0 commit comments