Skip to content

Instantly share code, notes, and snippets.

@ThiefMaster
Created May 8, 2014 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThiefMaster/cc226a9b578789071f09 to your computer and use it in GitHub Desktop.
Save ThiefMaster/cc226a9b578789071f09 to your computer and use it in GitHub Desktop.
You can pass an instance of this class to the `evalex` argument of `DebuggedApplication` to conditionally enable it. Feel free to use it under the WTFPL.
class RestrictedEvalex(object):
def __init__(self, whitelist=None):
self.whitelist = whitelist
def __nonzero__(self):
if not self.whitelist:
return False
elif self.whitelist is True:
return True
# Now the hard part becomes: We need to inspect the stack to get the user's IP :(
# First of all, let's find the frame with the WSGI __call__. We need to iterate
# multiple frames because the console and the actual debugger are on different depths.
for frame in (x[0] for x in inspect.stack()):
if frame.f_code.co_name in ('debug_application', '__call__') and 'environ' in frame.f_locals:
break
else:
print 'Did not find a stack frame containing a WSGI environment'
return False
try:
client_ip = frame.f_locals['environ']['REMOTE_ADDR']
except Exception as e:
print 'Could not get user ip from stack: {}'.format(e)
return False
return client_ip in self.whitelist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment