Created
May 8, 2014 16:40
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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