Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
codejail
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
codejail
Commits
1d961007
Commit
1d961007
authored
Mar 01, 2013
by
Ned Batchelder
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Have to make the globals json-safe before sending them to the sandbox.
parent
2e7f7d3c
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
25 deletions
+26
-25
codejail/safe_exec.py
+26
-25
No files found.
codejail/safe_exec.py
View file @
1d961007
...
@@ -101,7 +101,7 @@ def safe_exec(code, globals_dict, assumed_imports=None, files=None, python_path=
...
@@ -101,7 +101,7 @@ def safe_exec(code, globals_dict, assumed_imports=None, files=None, python_path=
json.dump(g_dict, sys.__stdout__)
json.dump(g_dict, sys.__stdout__)
"""
))
"""
))
stdin
=
json
.
dumps
([
code
,
globals_dict
])
stdin
=
json
.
dumps
([
code
,
json_safe
(
globals_dict
)
])
jailed_code
=
""
.
join
(
the_code
)
jailed_code
=
""
.
join
(
the_code
)
# Turn this on to see what's being executed.
# Turn this on to see what's being executed.
...
@@ -117,6 +117,29 @@ def safe_exec(code, globals_dict, assumed_imports=None, files=None, python_path=
...
@@ -117,6 +117,29 @@ def safe_exec(code, globals_dict, assumed_imports=None, files=None, python_path=
globals_dict
.
update
(
json
.
loads
(
res
.
stdout
))
globals_dict
.
update
(
json
.
loads
(
res
.
stdout
))
def
json_safe
(
d
):
"""Return only the JSON-safe part of d.
Used to emulate reading data through a serialization straw.
"""
ok_types
=
(
type
(
None
),
int
,
long
,
float
,
str
,
unicode
,
list
,
tuple
,
dict
)
bad_keys
=
(
"__builtins__"
,)
jd
=
{}
for
k
,
v
in
d
.
iteritems
():
if
not
isinstance
(
v
,
ok_types
):
continue
if
k
in
bad_keys
:
continue
try
:
json
.
dumps
(
v
)
except
TypeError
:
continue
else
:
jd
[
k
]
=
v
return
json
.
loads
(
json
.
dumps
(
jd
))
def
not_safe_exec
(
code
,
globals_dict
,
assumed_imports
=
None
,
files
=
None
,
python_path
=
None
):
def
not_safe_exec
(
code
,
globals_dict
,
assumed_imports
=
None
,
files
=
None
,
python_path
=
None
):
"""Another implementation of `safe_exec`, but not safe.
"""Another implementation of `safe_exec`, but not safe.
...
@@ -126,29 +149,7 @@ def not_safe_exec(code, globals_dict, assumed_imports=None, files=None, python_p
...
@@ -126,29 +149,7 @@ def not_safe_exec(code, globals_dict, assumed_imports=None, files=None, python_p
and modifying sys.path.
and modifying sys.path.
"""
"""
def
straw
(
d
):
g_dict
=
json_safe
(
globals_dict
)
"""Return only the JSON-safe part of d.
Used to emulate reading data through a serialization straw.
"""
ok_types
=
(
type
(
None
),
int
,
long
,
float
,
str
,
unicode
,
list
,
tuple
,
dict
)
bad_keys
=
(
"__builtins__"
,)
jd
=
{}
for
k
,
v
in
d
.
iteritems
():
if
not
isinstance
(
v
,
ok_types
):
continue
if
k
in
bad_keys
:
continue
try
:
json
.
dumps
(
v
)
except
TypeError
:
continue
else
:
jd
[
k
]
=
v
return
json
.
loads
(
json
.
dumps
(
jd
))
g_dict
=
straw
(
globals_dict
)
for
name
,
modname
in
names_and_modules
(
assumed_imports
or
()):
for
name
,
modname
in
names_and_modules
(
assumed_imports
or
()):
g_dict
[
name
]
=
lazymod
.
LazyModule
(
modname
)
g_dict
[
name
]
=
lazymod
.
LazyModule
(
modname
)
...
@@ -168,7 +169,7 @@ def not_safe_exec(code, globals_dict, assumed_imports=None, files=None, python_p
...
@@ -168,7 +169,7 @@ def not_safe_exec(code, globals_dict, assumed_imports=None, files=None, python_p
finally
:
finally
:
sys
.
path
=
original_path
sys
.
path
=
original_path
globals_dict
.
update
(
straw
(
g_dict
))
globals_dict
.
update
(
json_safe
(
g_dict
))
# Running Python code in the sandbox makes it difficult to debug.
# Running Python code in the sandbox makes it difficult to debug.
# Change 0 to 1 to run the code directly.
# Change 0 to 1 to run the code directly.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment