test(sandbox): use the active Python interpreter for argv fixtures (#2409)

This commit is contained in:
evan188199-tech
2026-11-03 00:44:32 -08:00
committed by Bingxi Zhao (Frank)
parent 294b04da09
commit a5ab4c6ea6

View File

@@ -15,6 +15,7 @@ from __future__ import annotations
import asyncio
import shlex
import sys
import pytest
@@ -42,6 +43,13 @@ HOSTILE_ARGS = [
"|tee /tmp/pwned",
]
#: Portable argv prefix that echoes one argument back as exact UTF-8 bytes.
ECHO_ARGV = [
sys.executable,
"-c",
"import sys; sys.stdout.buffer.write(sys.argv[1].encode('utf-8'))",
]
def test_of_argv_derives_the_shell_form() -> None:
request = ExecRequest.of_argv(["/bin/echo", "a b", "c;d"])
@@ -128,9 +136,7 @@ def test_the_sidecar_sends_both_spellings() -> None:
def test_the_local_backend_passes_a_hostile_argument_through_untouched(argument: str) -> None:
"""The argument arrives as data. Nothing expands, splits, or executes."""
backend = RestrictedSubprocessBackend()
request = ExecRequest.of_argv(
["/usr/bin/env", "python3", "-c", "import sys; print(sys.argv[1], end='')", argument]
)
request = ExecRequest.of_argv([*ECHO_ARGV, argument])
result = asyncio.run(backend.exec(request))
assert result.error == ""
@@ -159,8 +165,8 @@ def test_the_runner_prefers_argv_over_the_shell_string() -> None:
"""Sent both, it must run the vector — that is what makes the arguments data."""
result = runner_server.execute(
{
"command": "/bin/echo shell-form",
"argv": ["/bin/echo", "argv-form"],
"command": shlex.join([*ECHO_ARGV, "shell-form"]),
"argv": [*ECHO_ARGV, "argv-form"],
"limits": {"timeout_s": 10},
}
)
@@ -177,7 +183,7 @@ def test_the_runner_does_not_expand_a_hostile_argv_element() -> None:
result = runner_server.execute(
{
"command": "unused",
"argv": ["/bin/echo", "$HOME; rm -rf /"],
"argv": [*ECHO_ARGV, "$HOME; rm -rf /"],
"limits": {"timeout_s": 10},
}
)