Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../usr/lib64/python2....
File: Bastion.py
"""Bastionification utility.
[0] Fix | Delete
[1] Fix | Delete
A bastion (for another object -- the 'original') is an object that has
[2] Fix | Delete
the same methods as the original but does not give access to its
[3] Fix | Delete
instance variables. Bastions have a number of uses, but the most
[4] Fix | Delete
obvious one is to provide code executing in restricted mode with a
[5] Fix | Delete
safe interface to an object implemented in unrestricted mode.
[6] Fix | Delete
[7] Fix | Delete
The bastionification routine has an optional second argument which is
[8] Fix | Delete
a filter function. Only those methods for which the filter method
[9] Fix | Delete
(called with the method name as argument) returns true are accessible.
[10] Fix | Delete
The default filter method returns true unless the method name begins
[11] Fix | Delete
with an underscore.
[12] Fix | Delete
[13] Fix | Delete
There are a number of possible implementations of bastions. We use a
[14] Fix | Delete
'lazy' approach where the bastion's __getattr__() discipline does all
[15] Fix | Delete
the work for a particular method the first time it is used. This is
[16] Fix | Delete
usually fastest, especially if the user doesn't call all available
[17] Fix | Delete
methods. The retrieved methods are stored as instance variables of
[18] Fix | Delete
the bastion, so the overhead is only occurred on the first use of each
[19] Fix | Delete
method.
[20] Fix | Delete
[21] Fix | Delete
Detail: the bastion class has a __repr__() discipline which includes
[22] Fix | Delete
the repr() of the original object. This is precomputed when the
[23] Fix | Delete
bastion is created.
[24] Fix | Delete
[25] Fix | Delete
"""
[26] Fix | Delete
from warnings import warnpy3k
[27] Fix | Delete
warnpy3k("the Bastion module has been removed in Python 3.0", stacklevel=2)
[28] Fix | Delete
del warnpy3k
[29] Fix | Delete
[30] Fix | Delete
__all__ = ["BastionClass", "Bastion"]
[31] Fix | Delete
[32] Fix | Delete
from types import MethodType
[33] Fix | Delete
[34] Fix | Delete
[35] Fix | Delete
class BastionClass:
[36] Fix | Delete
[37] Fix | Delete
"""Helper class used by the Bastion() function.
[38] Fix | Delete
[39] Fix | Delete
You could subclass this and pass the subclass as the bastionclass
[40] Fix | Delete
argument to the Bastion() function, as long as the constructor has
[41] Fix | Delete
the same signature (a get() function and a name for the object).
[42] Fix | Delete
[43] Fix | Delete
"""
[44] Fix | Delete
[45] Fix | Delete
def __init__(self, get, name):
[46] Fix | Delete
"""Constructor.
[47] Fix | Delete
[48] Fix | Delete
Arguments:
[49] Fix | Delete
[50] Fix | Delete
get - a function that gets the attribute value (by name)
[51] Fix | Delete
name - a human-readable name for the original object
[52] Fix | Delete
(suggestion: use repr(object))
[53] Fix | Delete
[54] Fix | Delete
"""
[55] Fix | Delete
self._get_ = get
[56] Fix | Delete
self._name_ = name
[57] Fix | Delete
[58] Fix | Delete
def __repr__(self):
[59] Fix | Delete
"""Return a representation string.
[60] Fix | Delete
[61] Fix | Delete
This includes the name passed in to the constructor, so that
[62] Fix | Delete
if you print the bastion during debugging, at least you have
[63] Fix | Delete
some idea of what it is.
[64] Fix | Delete
[65] Fix | Delete
"""
[66] Fix | Delete
return "<Bastion for %s>" % self._name_
[67] Fix | Delete
[68] Fix | Delete
def __getattr__(self, name):
[69] Fix | Delete
"""Get an as-yet undefined attribute value.
[70] Fix | Delete
[71] Fix | Delete
This calls the get() function that was passed to the
[72] Fix | Delete
constructor. The result is stored as an instance variable so
[73] Fix | Delete
that the next time the same attribute is requested,
[74] Fix | Delete
__getattr__() won't be invoked.
[75] Fix | Delete
[76] Fix | Delete
If the get() function raises an exception, this is simply
[77] Fix | Delete
passed on -- exceptions are not cached.
[78] Fix | Delete
[79] Fix | Delete
"""
[80] Fix | Delete
attribute = self._get_(name)
[81] Fix | Delete
self.__dict__[name] = attribute
[82] Fix | Delete
return attribute
[83] Fix | Delete
[84] Fix | Delete
[85] Fix | Delete
def Bastion(object, filter = lambda name: name[:1] != '_',
[86] Fix | Delete
name=None, bastionclass=BastionClass):
[87] Fix | Delete
"""Create a bastion for an object, using an optional filter.
[88] Fix | Delete
[89] Fix | Delete
See the Bastion module's documentation for background.
[90] Fix | Delete
[91] Fix | Delete
Arguments:
[92] Fix | Delete
[93] Fix | Delete
object - the original object
[94] Fix | Delete
filter - a predicate that decides whether a function name is OK;
[95] Fix | Delete
by default all names are OK that don't start with '_'
[96] Fix | Delete
name - the name of the object; default repr(object)
[97] Fix | Delete
bastionclass - class used to create the bastion; default BastionClass
[98] Fix | Delete
[99] Fix | Delete
"""
[100] Fix | Delete
[101] Fix | Delete
raise RuntimeError, "This code is not secure in Python 2.2 and later"
[102] Fix | Delete
[103] Fix | Delete
# Note: we define *two* ad-hoc functions here, get1 and get2.
[104] Fix | Delete
# Both are intended to be called in the same way: get(name).
[105] Fix | Delete
# It is clear that the real work (getting the attribute
[106] Fix | Delete
# from the object and calling the filter) is done in get1.
[107] Fix | Delete
# Why can't we pass get1 to the bastion? Because the user
[108] Fix | Delete
# would be able to override the filter argument! With get2,
[109] Fix | Delete
# overriding the default argument is no security loophole:
[110] Fix | Delete
# all it does is call it.
[111] Fix | Delete
# Also notice that we can't place the object and filter as
[112] Fix | Delete
# instance variables on the bastion object itself, since
[113] Fix | Delete
# the user has full access to all instance variables!
[114] Fix | Delete
[115] Fix | Delete
def get1(name, object=object, filter=filter):
[116] Fix | Delete
"""Internal function for Bastion(). See source comments."""
[117] Fix | Delete
if filter(name):
[118] Fix | Delete
attribute = getattr(object, name)
[119] Fix | Delete
if type(attribute) == MethodType:
[120] Fix | Delete
return attribute
[121] Fix | Delete
raise AttributeError, name
[122] Fix | Delete
[123] Fix | Delete
def get2(name, get1=get1):
[124] Fix | Delete
"""Internal function for Bastion(). See source comments."""
[125] Fix | Delete
return get1(name)
[126] Fix | Delete
[127] Fix | Delete
if name is None:
[128] Fix | Delete
name = repr(object)
[129] Fix | Delete
return bastionclass(get2, name)
[130] Fix | Delete
[131] Fix | Delete
[132] Fix | Delete
def _test():
[133] Fix | Delete
"""Test the Bastion() function."""
[134] Fix | Delete
class Original:
[135] Fix | Delete
def __init__(self):
[136] Fix | Delete
self.sum = 0
[137] Fix | Delete
def add(self, n):
[138] Fix | Delete
self._add(n)
[139] Fix | Delete
def _add(self, n):
[140] Fix | Delete
self.sum = self.sum + n
[141] Fix | Delete
def total(self):
[142] Fix | Delete
return self.sum
[143] Fix | Delete
o = Original()
[144] Fix | Delete
b = Bastion(o)
[145] Fix | Delete
testcode = """if 1:
[146] Fix | Delete
b.add(81)
[147] Fix | Delete
b.add(18)
[148] Fix | Delete
print "b.total() =", b.total()
[149] Fix | Delete
try:
[150] Fix | Delete
print "b.sum =", b.sum,
[151] Fix | Delete
except:
[152] Fix | Delete
print "inaccessible"
[153] Fix | Delete
else:
[154] Fix | Delete
print "accessible"
[155] Fix | Delete
try:
[156] Fix | Delete
print "b._add =", b._add,
[157] Fix | Delete
except:
[158] Fix | Delete
print "inaccessible"
[159] Fix | Delete
else:
[160] Fix | Delete
print "accessible"
[161] Fix | Delete
try:
[162] Fix | Delete
print "b._get_.func_defaults =", map(type, b._get_.func_defaults),
[163] Fix | Delete
except:
[164] Fix | Delete
print "inaccessible"
[165] Fix | Delete
else:
[166] Fix | Delete
print "accessible"
[167] Fix | Delete
\n"""
[168] Fix | Delete
exec testcode
[169] Fix | Delete
print '='*20, "Using rexec:", '='*20
[170] Fix | Delete
import rexec
[171] Fix | Delete
r = rexec.RExec()
[172] Fix | Delete
m = r.add_module('__main__')
[173] Fix | Delete
m.b = b
[174] Fix | Delete
r.r_exec(testcode)
[175] Fix | Delete
[176] Fix | Delete
[177] Fix | Delete
if __name__ == '__main__':
[178] Fix | Delete
_test()
[179] Fix | Delete
[180] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function