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