@@ -335,9 +335,16 @@ def update(self, values):
335335 key = key .lower ()
336336 if key not in self ._reserved :
337337 raise CookieError ("Invalid attribute %r" % (key ,))
338+ if _has_control_character (key , val ):
339+ raise CookieError ("Control characters are not allowed in "
340+ f"cookies { key !r} { val !r} " )
338341 data [key ] = val
339342 dict .update (self , data )
340343
344+ def __ior__ (self , values ):
345+ self .update (values )
346+ return self
347+
341348 def isReservedKey (self , K ):
342349 return K .lower () in self ._reserved
343350
@@ -363,9 +370,15 @@ def __getstate__(self):
363370 }
364371
365372 def __setstate__ (self , state ):
366- self ._key = state ['key' ]
367- self ._value = state ['value' ]
368- self ._coded_value = state ['coded_value' ]
373+ key = state ['key' ]
374+ value = state ['value' ]
375+ coded_value = state ['coded_value' ]
376+ if _has_control_character (key , value , coded_value ):
377+ raise CookieError ("Control characters are not allowed in cookies "
378+ f"{ key !r} { value !r} { coded_value !r} " )
379+ self ._key = key
380+ self ._value = value
381+ self ._coded_value = coded_value
369382
370383 def output (self , attrs = None , header = "Set-Cookie:" ):
371384 return "%s %s" % (header , self .OutputString (attrs ))
@@ -377,13 +390,16 @@ def __repr__(self):
377390
378391 def js_output (self , attrs = None ):
379392 # Print javascript
393+ output_string = self .OutputString (attrs )
394+ if _has_control_character (output_string ):
395+ raise CookieError ("Control characters are not allowed in cookies" )
380396 return """
381397 <script type="text/javascript">
382398 <!-- begin hiding
383399 document.cookie = \" %s\" ;
384400 // end hiding -->
385401 </script>
386- """ % (self . OutputString ( attrs ) .replace ('"' , r'\"' ))
402+ """ % (output_string .replace ('"' , r'\"' ))
387403
388404 def OutputString (self , attrs = None ):
389405 # Build up our result
0 commit comments