Skip to content

Commit

Permalink
Namespace-wrap the createvirtualnamespace call
Browse files Browse the repository at this point in the history
Also fix `ut_repyv2api_virtualnamespacecontextsafety.py` to test the
proper functionality. (Following the namespace-wrap, it exhibited
an issue in in `namespace.py` which we need to discuss separately,
see SeattleTestbed#126)

Details about namespace wrapping:
* In `repy.py`, remove the addition of the non-wrapped `getresources`
  call to `usercontext`. (The properly wrapped definition was implemented
  in `namespace.py`'s `USERCONTEXT_WRAPPER_INFO` already, but `repy.py`
  overrode it.)
* In `namespace.py`, change the `SafeDict*` processors to inherit from
  class `ValueProcessor` rather than `ObjectProcessor`. This follows
  what the `Dict` processor does, and fixes the `virtualnamespace-eval`
  and `virtualnamespacecontextsafety` unit tests that failed with
  unwrapping errors when supplying a `dict` / `SafeDict` to the
  `VirtualNamespace` to be evaluated. The particular unit test
  errors were artifacts of how the `SafeDict*` processors inherited
  from `ObjectProcessor`, and looked like this:

"""
	Running: ut_repyv2api_virtualnamespace-eval.py              [ FAIL ]
--------------------------------------------------------------------------------
Standard error :
..............................Produced..............................
Internal Error
---
Uncaught exception!
---
Following is a full traceback, and a user traceback.
The user traceback excludes non-user modules. The most recent call is displayed last.

Full debugging traceback:
  "/private/tmp/repy_v2/RUNNABLE/namespace.py", line 1194, in wrapped_function
  "/private/tmp/repy_v2/RUNNABLE/namespace.py", line 1095, in _process_args
  "/private/tmp/repy_v2/RUNNABLE/namespace.py", line 283, in unwrap

User traceback:

Exception (with type 'exceptions.AttributeError'): SafeDict instance has no attribute '_wrapped__object'
---

..............................Expected..............................
None
--------------------------------------------------------------------------------
	Running: ut_repyv2api_virtualnamespacecontextsafety.py      [ FAIL ]
--------------------------------------------------------------------------------
Standard error :
..............................Produced..............................
Internal Error
---
Uncaught exception!
---
Following is a full traceback, and a user traceback.
The user traceback excludes non-user modules. The most recent call is displayed last.

Full debugging traceback:
  "/private/tmp/repy_v2/RUNNABLE/namespace.py", line 1194, in wrapped_function
  "/private/tmp/repy_v2/RUNNABLE/namespace.py", line 1095, in _process_args
  "/private/tmp/repy_v2/RUNNABLE/namespace.py", line 283, in unwrap

User traceback:

Exception (with type 'exceptions.AttributeError'): 'dict' object has no attribute '_wrapped__object'
---

..............................Expected..............................
None
--------------------------------------------------------------------------------
"""
  • Loading branch information
aaaaalbert committed Aug 11, 2016
1 parent 48aa0a0 commit 7222b4b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
4 changes: 2 additions & 2 deletions namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ def wrap(self, val):



class SafeDict(ObjectProcessor):
class SafeDict(ValueProcessor):
"""Allows SafeDict objects."""

# TODO: provide a copy function that won't actually copy so that
Expand All @@ -585,7 +585,7 @@ def check(self, val):



class DictOrSafeDict(ObjectProcessor):
class DictOrSafeDict(ValueProcessor):
"""Allows SafeDict objects or regular dict objects."""

# TODO: provide a copy function that won't actually copy so that
Expand Down
1 change: 0 additions & 1 deletion repy.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ def get_safe_context(args):
#usercontext["createthread"] = emultimer.createthread
#usercontext["sleep"] = emultimer.sleep
#usercontext["getthreadname"] = emulmisc.getthreadname
usercontext["createvirtualnamespace"] = virtual_namespace.createvirtualnamespace

# call the initialize function
usercontext['callfunc'] = 'initialize'
Expand Down
26 changes: 17 additions & 9 deletions testsV2/ut_repyv2api_virtualnamespacecontextsafety.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
"""
This unit test checks that VirtualNamespace.evaluate() forbids an unsafe context.
Verify that VirtualNamespace.evaluate() forbids an unsafe `context`.
Specifically, the `context` that a VirtualNamespace object receives
must be a `SafeDict`, or parseable as one. This means that the
context's keys must be of type `str` (which we try to violate in this
test), avoid particular names / prefixes / components, etc.
Using an unsafe context should raise a `ContextUnsafeError` when
evaluatinig the VirtualNamespace.
See `safe.py` for what exactly a `SafeDict` is and isn't allowed to
do and to contain.
"""

#pragma repy

# Small code snippet
code = "log('Bad!')\n"

# Get a VN
VN = createvirtualnamespace(code, "Bad VN")
# Create a virtual namespace with an empty piece of code and a telling name
vn = createvirtualnamespace("", "Bad VN")

# Create a malicious context
context = {"__metaclass__":str}
# Create a malicious context. (Its key should be `str`, not `int`.)
context = {123: 456}

# Try to evaluate this
try:
VN.evaluate(context)
vn.evaluate(context)
except ContextUnsafeError:
pass
else:
Expand Down

0 comments on commit 7222b4b

Please sign in to comment.