@@ -873,13 +873,21 @@ shared memory setup.
873873Restricting Lua access to Python objects
874874----------------------------------------
875875
876+ .. note ::
877+ Any Lupa deployment that allows untrusted Lua code to be executed
878+ should disable the access to Python's builtin functions,
879+ as shown below. This includes functions like ``eval() ``, ``exec() ``
880+ or ``__import__() `` that allow arbitrary code execution,
881+ but also seemingly innocent helpers like ``getattr() ``
882+ which provides unrestricted access to arbitrary Python attributes
883+ that is not guarded by the attribute access control described below.
884+
876885..
877886 >>> try: unicode = unicode
878887 ... except NameError: unicode = str
879888
880889Lupa provides a simple mechanism to control access to Python objects.
881- Each attribute access can be passed through a filter function as
882- follows:
890+ Each attribute access can be passed through a filter function as follows:
883891
884892.. code :: python
885893
@@ -890,16 +898,16 @@ follows:
890898 ... raise AttributeError (' access denied' )
891899
892900 >> > lua = lupa.LuaRuntime(
893- ... register_eval = False ,
894- ... attribute_filter = filter_attribute_access)
901+ ... register_eval = False , # disallow python.eval('...')
902+ ... register_builtins = False , # disallow python.builtins.*
903+ ... attribute_filter = filter_attribute_access)
895904 >> > func = lua.eval(' function(x) return x.__class__ end' )
896905 >> > func(lua)
897906 Traceback (most recent call last):
898907 ...
899908 AttributeError : access denied
900909
901- The ``is_setting `` flag indicates whether the attribute is being read
902- or set.
910+ The ``is_setting `` flag indicates whether the attribute is being read or set.
903911
904912Note that the attributes of Python functions provide access to the
905913current ``globals() `` and therefore to the builtins etc. If you want
@@ -934,7 +942,10 @@ setter function implementations for a ``LuaRuntime``:
934942
935943 >> > x = X()
936944
937- >> > lua = lupa.LuaRuntime(attribute_handlers = (getter, setter))
945+ >> > lua = lupa.LuaRuntime(
946+ ... register_eval = False , # disallow python.eval('...')
947+ ... register_builtins = False , # disallow python.builtins.*
948+ ... attribute_handlers = (getter, setter))
938949 >> > func = lua.eval(' function(x) return x.yes end' )
939950 >> > func(x) # getting 'yes'
940951 123
0 commit comments