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

Skip to content

Commit eca72d4

Browse files
committed
merge 3.3 (#16043)
2 parents f990e7f + 81b7374 commit eca72d4

3 files changed

Lines changed: 37 additions & 4 deletions

File tree

Lib/test/test_xmlrpc.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ def test_bad_gzip_request(self):
863863
p.pow(6, 8)
864864
p("close")()
865865

866-
def test_gsip_response(self):
866+
def test_gzip_response(self):
867867
t = self.Transport()
868868
p = xmlrpclib.ServerProxy(URL, transport=t)
869869
old = self.requestHandler.encode_threshold
@@ -877,6 +877,27 @@ def test_gsip_response(self):
877877
self.requestHandler.encode_threshold = old
878878
self.assertTrue(a>b)
879879

880+
881+
@unittest.skipIf(gzip is None, 'requires gzip')
882+
class GzipUtilTestCase(unittest.TestCase):
883+
884+
def test_gzip_decode_limit(self):
885+
max_gzip_decode = 20 * 1024 * 1024
886+
data = b'\0' * max_gzip_decode
887+
encoded = xmlrpclib.gzip_encode(data)
888+
decoded = xmlrpclib.gzip_decode(encoded)
889+
self.assertEqual(len(decoded), max_gzip_decode)
890+
891+
data = b'\0' * (max_gzip_decode + 1)
892+
encoded = xmlrpclib.gzip_encode(data)
893+
894+
with self.assertRaisesRegexp(ValueError,
895+
"max gzipped payload length exceeded"):
896+
xmlrpclib.gzip_decode(encoded)
897+
898+
xmlrpclib.gzip_decode(encoded, max_decode=-1)
899+
900+
880901
#Test special attributes of the ServerProxy object
881902
class ServerProxyTestCase(unittest.TestCase):
882903
def setUp(self):
@@ -1105,7 +1126,7 @@ def test_main():
11051126
support.run_unittest(XMLRPCTestCase, HelperTestCase, DateTimeTestCase,
11061127
BinaryTestCase, FaultTestCase, UseBuiltinTypesTestCase,
11071128
SimpleServerTestCase, KeepaliveServerTestCase1,
1108-
KeepaliveServerTestCase2, GzipServerTestCase,
1129+
KeepaliveServerTestCase2, GzipServerTestCase, GzipUtilTestCase,
11091130
MultiPathServerTestCase, ServerProxyTestCase, FailingServerTestCase,
11101131
CGIHandlerTestCase)
11111132

Lib/xmlrpc/client.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
# 2003-07-12 gp Correct marshalling of Faults
5050
# 2003-10-31 mvl Add multicall support
5151
# 2004-08-20 mvl Bump minimum supported Python version to 2.1
52+
# 2014-12-02 ch/doko Add workaround for gzip bomb vulnerability
5253
#
5354
# Copyright (c) 1999-2002 by Secret Labs AB.
5455
# Copyright (c) 1999-2002 by Fredrik Lundh.
@@ -1030,10 +1031,13 @@ def gzip_encode(data):
10301031
# in the HTTP header, as described in RFC 1952
10311032
#
10321033
# @param data The encoded data
1034+
# @keyparam max_decode Maximum bytes to decode (20MB default), use negative
1035+
# values for unlimited decoding
10331036
# @return the unencoded data
10341037
# @raises ValueError if data is not correctly coded.
1038+
# @raises ValueError if max gzipped payload length exceeded
10351039

1036-
def gzip_decode(data):
1040+
def gzip_decode(data, max_decode=20971520):
10371041
"""gzip encoded data -> unencoded data
10381042
10391043
Decode data using the gzip content encoding as described in RFC 1952
@@ -1043,11 +1047,16 @@ def gzip_decode(data):
10431047
f = BytesIO(data)
10441048
gzf = gzip.GzipFile(mode="rb", fileobj=f)
10451049
try:
1046-
decoded = gzf.read()
1050+
if max_decode < 0: # no limit
1051+
decoded = gzf.read()
1052+
else:
1053+
decoded = gzf.read(max_decode + 1)
10471054
except OSError:
10481055
raise ValueError("invalid data")
10491056
f.close()
10501057
gzf.close()
1058+
if max_decode >= 0 and len(decoded) > max_decode:
1059+
raise ValueError("max gzipped payload length exceeded")
10511060
return decoded
10521061

10531062
##

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ Core and Builtins
3939
Library
4040
-------
4141

42+
- Issue #16043: Add a default limit for the amount of data xmlrpclib.gzip_decode
43+
will return. This resolves CVE-2013-1753.
44+
4245
- Issue #14099: ZipFile.open() no longer reopen the underlying file. Objects
4346
returned by ZipFile.open() can now operate independently of the ZipFile even
4447
if the ZipFile was created by passing in a file-like object as the first

0 commit comments

Comments
 (0)