Skip to content

Commit 1df5812

Browse files
committed
Allow setting a binary_path when calling /eval
1 parent a487120 commit 1df5812

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

config/snekbox.cfg

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,3 @@ cgroup_pids_max: 6
103103
cgroup_pids_mount: "/sys/fs/cgroup/pids"
104104

105105
iface_no_lo: true
106-
107-
exec_bin {
108-
path: "/lang/python/default/bin/python"
109-
arg: ""
110-
}

snekbox/api/resources/eval.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import logging
4+
from pathlib import Path
45

56
import falcon
67
from falcon.media.validators.jsonschema import validate
@@ -44,6 +45,7 @@ class EvalResource:
4445
"required": ["path"],
4546
},
4647
},
48+
"binary_path": {"type": "string"},
4749
},
4850
"anyOf": [
4951
{"required": ["input"]},
@@ -123,10 +125,18 @@ def on_post(self, req: falcon.Request, resp: falcon.Response) -> None:
123125
if "input" in body:
124126
body.setdefault("args", ["-c"])
125127
body["args"].append(body["input"])
128+
129+
binary_path = body.get("binary_path")
130+
if binary_path:
131+
binary_path = Path(binary_path)
132+
if not binary_path.as_posix().startswith("/lang/") or not binary_path.is_file():
133+
raise falcon.HTTPBadRequest(title="binary_path file is invalid")
134+
126135
try:
127136
result = self.nsjail.python3(
128137
py_args=body["args"],
129138
files=[FileAttachment.from_dict(file) for file in body.get("files", [])],
139+
binary_path=binary_path,
130140
)
131141
except ParsingError as e:
132142
raise falcon.HTTPBadRequest(title="Request file is invalid", description=str(e))

snekbox/nsjail.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,11 @@ def python3(
190190
files: FileAttachments to write to the sandbox prior to running Python.
191191
nsjail_args: Overrides for the NsJail configuration.
192192
binary_path: The path to the binary to execute under.
193-
If None, defaults to exec_bin.path from nsjail config
193+
If None, defaults to "/lang/python/default/bin/python"
194194
"""
195+
if not binary_path:
196+
binary_path = "/lang/python/default/bin/python"
197+
195198
if self.cgroup_version == 2:
196199
nsjail_args = ("--use_cgroupv2", *nsjail_args)
197200

@@ -215,7 +218,6 @@ def python3(
215218
f"{fs.home}:home",
216219
*nsjail_args,
217220
)
218-
219221
args = [
220222
self.nsjail_path,
221223
"--config",
@@ -224,7 +226,7 @@ def python3(
224226
nsj_log.name,
225227
*nsjail_args,
226228
"--",
227-
binary_path or self.config.exec_bin.path,
229+
binary_path,
228230
# Filter out empty strings at start of Python args
229231
# (causes issues with python cli)
230232
*iter_lstrip(self.config.exec_bin.arg),

0 commit comments

Comments
 (0)