aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar ToxicKidz <[email protected]>2021-05-17 12:47:23 -0400
committerGravatar ToxicKidz <[email protected]>2021-05-17 12:47:23 -0400
commitd2ce374ed3e8954d782bada15f719013892035d7 (patch)
tree788a00a70b2c66fa1dad6703dc1eda8a6ddaae8a
parentMerge PR #100 - avoid decoding of invalid Unicode output (diff)
feat: Allow custom arguments to be passed to eval
-rw-r--r--snekbox/api/resources/eval.py10
-rw-r--r--snekbox/nsjail.py7
2 files changed, 14 insertions, 3 deletions
diff --git a/snekbox/api/resources/eval.py b/snekbox/api/resources/eval.py
index d96b811..5b4d5a2 100644
--- a/snekbox/api/resources/eval.py
+++ b/snekbox/api/resources/eval.py
@@ -23,6 +23,12 @@ class EvalResource:
"properties": {
"input": {
"type": "string"
+ },
+ "args": {
+ "type": "array",
+ "contains": {
+ "type": "string"
+ }
}
},
"required": [
@@ -51,6 +57,7 @@ class EvalResource:
>>> {
... "input": "print(1 + 1)"
+ ... "args": ["-m", "timeit"] # This is optional
... }
Response format:
@@ -70,9 +77,10 @@ class EvalResource:
Unsupported content type; only application/JSON is supported
"""
code = req.media["input"]
+ args = req.media.get("args", ("-c",))
try:
- result = self.nsjail.python3(code)
+ result = self.nsjail.python3(code, extra_args=args)
except Exception:
log.exception("An exception occurred while trying to process the request")
raise falcon.HTTPInternalServerError
diff --git a/snekbox/nsjail.py b/snekbox/nsjail.py
index 9367cb2..835f384 100644
--- a/snekbox/nsjail.py
+++ b/snekbox/nsjail.py
@@ -171,12 +171,15 @@ class NsJail:
return "".join(output)
- def python3(self, code: str, *args) -> CompletedProcess:
+ def python3(self, code: str, *args, extra_args: Iterable[str] = ("-c",)) -> CompletedProcess:
"""
Execute Python 3 code in an isolated environment and return the completed process.
Additional arguments passed will be used to override the values in the NsJail config.
These arguments are only options for NsJail; they do not affect Python's arguments.
+
+ The `extra_args` keyword argument can be given, and this would replace the "-c" argument
+ given by default.
"""
cgroup = self._create_dynamic_cgroups()
@@ -190,7 +193,7 @@ class NsJail:
"--cgroup_pids_parent", cgroup,
*args,
"--",
- self.config.exec_bin.path, *self.config.exec_bin.arg, "-c", code
+ self.config.exec_bin.path, *self.config.exec_bin.arg, *extra_args, code
)
msg = "Executing code..."