Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6101395

Browse files
committed
Remove deprecated SmartCookie and SerialCookie classes.
1 parent b17acad commit 6101395

4 files changed

Lines changed: 16 additions & 212 deletions

File tree

Doc/library/http.cookies.rst

Lines changed: 5 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -39,36 +39,6 @@ result, the parsing rules used are a bit less strict.
3939
and :meth:`value_encode` to be the identity and :func:`str` respectively.
4040

4141

42-
.. class:: SerialCookie([input])
43-
44-
This class derives from :class:`BaseCookie` and overrides :meth:`value_decode`
45-
and :meth:`value_encode` to be the :func:`pickle.loads` and
46-
:func:`pickle.dumps`.
47-
48-
.. deprecated:: 2.3
49-
Reading pickled values from untrusted cookie data is a huge security hole, as
50-
pickle strings can be crafted to cause arbitrary code to execute on your server.
51-
It is supported for backwards compatibility only, and may eventually go away.
52-
53-
54-
.. class:: SmartCookie([input])
55-
56-
This class derives from :class:`BaseCookie`. It overrides :meth:`value_decode`
57-
to be :func:`pickle.loads` if it is a valid pickle, and otherwise the value
58-
itself. It overrides :meth:`value_encode` to be :func:`pickle.dumps` unless it
59-
is a string, in which case it returns the value itself.
60-
61-
.. deprecated:: 2.3
62-
The same security warning from :class:`SerialCookie` applies here.
63-
64-
A further security note is warranted. For backwards compatibility, the
65-
:mod:`http.cookies` module exports a class named :class:`Cookie` which is just an
66-
alias for :class:`SmartCookie`. This is probably a mistake and will likely be
67-
removed in a future version. You should not use the :class:`Cookie` class in
68-
your applications, for the same reason why you should not use the
69-
:class:`SerialCookie` class.
70-
71-
7242
.. seealso::
7343

7444
Module :mod:`http.cookiejar`
@@ -212,8 +182,6 @@ The following example demonstrates how to use the :mod:`http.cookies` module.
212182

213183
>>> from http import cookies
214184
>>> C = cookies.SimpleCookie()
215-
>>> C = cookies.SerialCookie()
216-
>>> C = cookies.SmartCookie()
217185
>>> C["fig"] = "newton"
218186
>>> C["sugar"] = "wafer"
219187
>>> print(C) # generate HTTP headers
@@ -222,28 +190,28 @@ The following example demonstrates how to use the :mod:`http.cookies` module.
222190
>>> print(C.output()) # same thing
223191
Set-Cookie: fig=newton
224192
Set-Cookie: sugar=wafer
225-
>>> C = cookies.SmartCookie()
193+
>>> C = cookies.SimpleCookie()
226194
>>> C["rocky"] = "road"
227195
>>> C["rocky"]["path"] = "/cookie"
228196
>>> print(C.output(header="Cookie:"))
229197
Cookie: rocky=road; Path=/cookie
230198
>>> print(C.output(attrs=[], header="Cookie:"))
231199
Cookie: rocky=road
232-
>>> C = cookies.SmartCookie()
200+
>>> C = cookies.SimpleCookie()
233201
>>> C.load("chips=ahoy; vienna=finger") # load from a string (HTTP header)
234202
>>> print(C)
235203
Set-Cookie: chips=ahoy
236204
Set-Cookie: vienna=finger
237-
>>> C = cookies.SmartCookie()
205+
>>> C = cookies.SimpleCookie()
238206
>>> C.load('keebler="E=everybody; L=\\"Loves\\"; fudge=\\012;";')
239207
>>> print(C)
240208
Set-Cookie: keebler="E=everybody; L=\"Loves\"; fudge=\012;"
241-
>>> C = cookies.SmartCookie()
209+
>>> C = cookies.SimpleCookie()
242210
>>> C["oreo"] = "doublestuff"
243211
>>> C["oreo"]["path"] = "/"
244212
>>> print(C)
245213
Set-Cookie: oreo=doublestuff; Path=/
246-
>>> C = cookies.SmartCookie()
214+
>>> C = cookies.SimpleCookie()
247215
>>> C["twix"] = "none for you"
248216
>>> C["twix"].value
249217
'none for you'
@@ -257,24 +225,3 @@ The following example demonstrates how to use the :mod:`http.cookies` module.
257225
>>> print(C)
258226
Set-Cookie: number=7
259227
Set-Cookie: string=seven
260-
>>> C = cookies.SerialCookie()
261-
>>> C["number"] = 7
262-
>>> C["string"] = "seven"
263-
>>> C["number"].value
264-
7
265-
>>> C["string"].value
266-
'seven'
267-
>>> print(C)
268-
Set-Cookie: number="I7\012."
269-
Set-Cookie: string="S'seven'\012p1\012."
270-
>>> C = cookies.SmartCookie()
271-
>>> C["number"] = 7
272-
>>> C["string"] = "seven"
273-
>>> C["number"].value
274-
7
275-
>>> C["string"].value
276-
'seven'
277-
>>> print(C)
278-
Set-Cookie: number="I7\012."
279-
Set-Cookie: string=seven
280-

Lib/http/cookies.py

Lines changed: 8 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,14 @@
5050
5151
>>> from http import cookies
5252
53-
Most of the time you start by creating a cookie. Cookies come in
54-
three flavors, each with slightly different encoding semantics, but
55-
more on that later.
53+
Most of the time you start by creating a cookie.
5654
5755
>>> C = cookies.SimpleCookie()
58-
>>> C = cookies.SerialCookie()
59-
>>> C = cookies.SmartCookie()
60-
61-
[Note: Long-time users of cookies.py will remember using
62-
cookies.Cookie() to create an Cookie object. Although deprecated, it
63-
is still supported by the code. See the Backward Compatibility notes
64-
for more information.]
6556
6657
Once you've created your Cookie, you can add values just as if it were
6758
a dictionary.
6859
69-
>>> C = cookies.SmartCookie()
60+
>>> C = cookies.SimpleCookie()
7061
>>> C["fig"] = "newton"
7162
>>> C["sugar"] = "wafer"
7263
>>> C.output()
@@ -77,7 +68,7 @@
7768
default behavior. You can change the header and printed
7869
attributes by using the .output() function
7970
80-
>>> C = cookies.SmartCookie()
71+
>>> C = cookies.SimpleCookie()
8172
>>> C["rocky"] = "road"
8273
>>> C["rocky"]["path"] = "/cookie"
8374
>>> print(C.output(header="Cookie:"))
@@ -89,7 +80,7 @@
8980
CGI script, you would use this method to extract the cookies from the
9081
HTTP_COOKIE environment variable.
9182
92-
>>> C = cookies.SmartCookie()
83+
>>> C = cookies.SimpleCookie()
9384
>>> C.load("chips=ahoy; vienna=finger")
9485
>>> C.output()
9586
'Set-Cookie: chips=ahoy\r\nSet-Cookie: vienna=finger'
@@ -98,7 +89,7 @@
9889
within a string. Escaped quotation marks, nested semicolons, and other
9990
such trickeries do not confuse it.
10091
101-
>>> C = cookies.SmartCookie()
92+
>>> C = cookies.SimpleCookie()
10293
>>> C.load('keebler="E=everybody; L=\\"Loves\\"; fudge=\\012;";')
10394
>>> print(C)
10495
Set-Cookie: keebler="E=everybody; L=\"Loves\"; fudge=\012;"
@@ -107,7 +98,7 @@
10798
Cookie attributes. Here's an example which sets the Path
10899
attribute.
109100
110-
>>> C = cookies.SmartCookie()
101+
>>> C = cookies.SimpleCookie()
111102
>>> C["oreo"] = "doublestuff"
112103
>>> C["oreo"]["path"] = "/"
113104
>>> print(C)
@@ -116,21 +107,11 @@
116107
Each dictionary element has a 'value' attribute, which gives you
117108
back the value associated with the key.
118109
119-
>>> C = cookies.SmartCookie()
110+
>>> C = cookies.SimpleCookie()
120111
>>> C["twix"] = "none for you"
121112
>>> C["twix"].value
122113
'none for you'
123114
124-
125-
A Bit More Advanced
126-
-------------------
127-
128-
As mentioned before, there are three different flavors of Cookie
129-
objects, each with different encoding/decoding semantics. This
130-
section briefly discusses the differences.
131-
132-
SimpleCookie
133-
134115
The SimpleCookie expects that all values should be standard strings.
135116
Just to be sure, SimpleCookie invokes the str() builtin to convert
136117
the value to a string, when the values are set dictionary-style.
@@ -145,62 +126,6 @@
145126
>>> C.output()
146127
'Set-Cookie: number=7\r\nSet-Cookie: string=seven'
147128
148-
149-
SerialCookie
150-
151-
The SerialCookie expects that all values should be serialized using
152-
pickle. As a result of serializing, SerialCookie can save almost any
153-
Python object to a value, and recover the exact same object when the
154-
cookie has been returned. (SerialCookie can yield some
155-
strange-looking cookie values, however.)
156-
157-
>>> C = cookies.SerialCookie()
158-
>>> C["number"] = 7
159-
>>> C["string"] = "seven"
160-
>>> C["number"].value
161-
7
162-
>>> C["string"].value
163-
'seven'
164-
>>> C.output()
165-
'Set-Cookie: number="L7\\012."\r\nSet-Cookie: string="Vseven\\012p0\\012."'
166-
167-
Be warned, however, if SerialCookie cannot de-serialize a value (because
168-
it isn't a valid pickle'd object), IT WILL RAISE AN EXCEPTION.
169-
170-
171-
SmartCookie
172-
173-
The SmartCookie combines aspects of each of the other two flavors.
174-
When setting a value in a dictionary-fashion, the SmartCookie will
175-
serialize (ala pickle) the value *if and only if* it isn't a
176-
Python string. String objects are *not* serialized. Similarly,
177-
when the load() method parses out values, it attempts to de-serialize
178-
the value. If it fails, then it fallsback to treating the value
179-
as a string.
180-
181-
>>> C = cookies.SmartCookie()
182-
>>> C["number"] = 7
183-
>>> C["string"] = "seven"
184-
>>> C["number"].value
185-
7
186-
>>> C["string"].value
187-
'seven'
188-
>>> C.output()
189-
'Set-Cookie: number="L7\\012."\r\nSet-Cookie: string=seven'
190-
191-
192-
Backwards Compatibility
193-
-----------------------
194-
195-
In order to keep compatibilty with earlier versions of Cookie.py,
196-
it is still possible to use cookies.Cookie() to create a Cookie. In
197-
fact, this simply returns a SmartCookie.
198-
199-
>>> C = cookies.Cookie()
200-
>>> print(C.__class__.__name__)
201-
SmartCookie
202-
203-
204129
Finis.
205130
""" #"
206131
# ^
@@ -215,8 +140,7 @@
215140

216141
import re, warnings
217142

218-
__all__ = ["CookieError","BaseCookie","SimpleCookie","SerialCookie",
219-
"SmartCookie","Cookie"]
143+
__all__ = ["CookieError", "BaseCookie", "SimpleCookie"]
220144

221145
_nulljoin = ''.join
222146
_semispacejoin = '; '.join
@@ -653,70 +577,6 @@ def value_encode(self, val):
653577
return strval, _quote( strval )
654578
# end SimpleCookie
655579

656-
class SerialCookie(BaseCookie):
657-
"""SerialCookie
658-
SerialCookie supports arbitrary objects as cookie values. All
659-
values are serialized (using pickle) before being sent to the
660-
client. All incoming values are assumed to be valid Pickle
661-
representations. IF AN INCOMING VALUE IS NOT IN A VALID PICKLE
662-
FORMAT, THEN AN EXCEPTION WILL BE RAISED.
663-
664-
Note: Large cookie values add overhead because they must be
665-
retransmitted on every HTTP transaction.
666-
667-
Note: HTTP has a 2k limit on the size of a cookie. This class
668-
does not check for this limit, so be careful!!!
669-
"""
670-
def __init__(self, input=None):
671-
warnings.warn("SerialCookie class is insecure; do not use it",
672-
DeprecationWarning)
673-
BaseCookie.__init__(self, input)
674-
# end __init__
675-
def value_decode(self, val):
676-
# This could raise an exception!
677-
return loads( _unquote(val).encode('latin-1') ), val
678-
def value_encode(self, val):
679-
return val, _quote( dumps(val, 0).decode('latin-1') )
680-
# end SerialCookie
681-
682-
class SmartCookie(BaseCookie):
683-
"""SmartCookie
684-
SmartCookie supports arbitrary objects as cookie values. If the
685-
object is a string, then it is quoted. If the object is not a
686-
string, however, then SmartCookie will use pickle to serialize
687-
the object into a string representation.
688-
689-
Note: Large cookie values add overhead because they must be
690-
retransmitted on every HTTP transaction.
691-
692-
Note: HTTP has a 2k limit on the size of a cookie. This class
693-
does not check for this limit, so be careful!!!
694-
"""
695-
def __init__(self, input=None):
696-
warnings.warn("Cookie/SmartCookie class is insecure; do not use it",
697-
DeprecationWarning)
698-
BaseCookie.__init__(self, input)
699-
# end __init__
700-
def value_decode(self, val):
701-
strval = _unquote(val)
702-
try:
703-
return loads(strval.encode('latin-1')), val
704-
except:
705-
return strval, val
706-
def value_encode(self, val):
707-
if isinstance(val, str):
708-
return val, _quote(val)
709-
else:
710-
return val, _quote( dumps(val, 0).decode('latin-1') )
711-
# end SmartCookie
712-
713-
714-
###########################################################
715-
# Backwards Compatibility: Don't break any existing code!
716-
717-
# We provide Cookie() as an alias for SmartCookie()
718-
Cookie = SmartCookie
719-
720580
#
721581
###########################################################
722582

@@ -726,8 +586,3 @@ def _test():
726586

727587
if __name__ == "__main__":
728588
_test()
729-
730-
731-
#Local Variables:
732-
#tab-width: 4
733-
#end:

Lib/test/test_http_cookies.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
DeprecationWarning)
1111

1212
class CookieTests(unittest.TestCase):
13-
# Currently this only tests SimpleCookie
1413
def test_basic(self):
1514
cases = [
1615
{ 'data': 'chips=ahoy; vienna=finger',

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ Extension Modules
6060
Library
6161
-------
6262

63+
- The deprecated ``SmartCookie`` and ``SimpleCookie`` classes have
64+
been removed from ``http.cookies``.
65+
6366
- The ``commands`` module has been removed. Its getoutput() and
6467
getstatusoutput() functions have been moved to the ``subprocess`` module.
6568

0 commit comments

Comments
 (0)