Skip to content

Commit 1b6feb3

Browse files
mschmitzertcalmant
authored andcommitted
jsonrpc: Don't proxy "special" methods
Code that expects ServerProxy objects to be regular python objects can trigger bogus rpc calls by accessing "special" methods (i.e. methods starting and ending with two underscores). Avoid this by raising AttributeError for such names instead of returning a method proxy. A real life example for this is the "raven" sentry client calling `__getattribute__('__sentry__')` on a ServerProxy instance when reporting an exception.
1 parent 5f53fcd commit 1b6feb3

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

jsonrpclib/jsonrpc.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,9 @@ def __getattr__(self, name):
583583
"""
584584
Returns a callable object to call the remote service
585585
"""
586+
if name.startswith("__") and name.endswith("__"):
587+
# Don't proxy special methods.
588+
raise AttributeError("ServerProxy has no attribute '%s'" % name)
586589
# Same as original, just with new _Method reference
587590
return _Method(self._request, name)
588591

tests/test_compatibility.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ def test_non_existent_method(self):
113113
self.assertTrue(request == verify_request)
114114
self.assertTrue(response == verify_response)
115115

116+
def test_special_method(self):
117+
self.assertRaises(AttributeError, getattr, self.client, '__special_method__')
118+
self.assertIsNone(self.history.request)
119+
116120
def test_invalid_json(self):
117121
invalid_json = '{"jsonrpc": "2.0", "method": "foobar, ' + \
118122
'"params": "bar", "baz]'

0 commit comments

Comments
 (0)