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