Files
Mark Gregory a60a33012b
ABSDEV Framework CI / framework-ci (push) Successful in 25s
Run approved commands with a fixed minimal environment
2026-11-28 13:11:26 +00:00

321 lines
7.1 KiB
Python

import io
import json
import os
import sys
import tempfile
import unittest
from pathlib import Path
from unittest.mock import patch
from core.execution import (
ExecutionError,
CommandRunner,
)
class ExecutionTests(unittest.TestCase):
def command(self, source):
return [
sys.executable,
"-I",
"-c",
source,
]
def runner(self, command, timeout=10):
return CommandRunner(
allowed_commands=[command],
timeout=timeout,
)
def test_successful_execution(self):
command = self.command(
"print('SUCCESS')"
)
output = io.BytesIO()
self.runner(command)(command, output)
self.assertEqual(
output.getvalue(),
b"SUCCESS\n",
)
def test_subprocess_environment_excludes_parent_credentials(self):
command = self.command(
"import json, os; print(json.dumps(dict(os.environ), sort_keys=True))"
)
output = io.BytesIO()
with patch.dict(os.environ, {
"PATH": "/untrusted",
"LC_ALL": "untrusted",
"ABSDEV_TEST_SECRET": "synthetic",
"DOCKER_HOST": "synthetic",
"PGPASSWORD": "synthetic",
}):
self.runner(command)(command, output)
self.assertEqual(json.loads(output.getvalue()), {"PATH": os.defpath, "LC_ALL": "C"})
def test_binary_output_preserved(self):
command = self.command(
"import sys; "
"sys.stdout.buffer.write(bytes(range(256)))"
)
output = io.BytesIO()
self.runner(command)(command, output)
self.assertEqual(
output.getvalue(),
bytes(range(256)),
)
def test_large_output_preserved(self):
command = self.command(
"import sys; "
"sys.stdout.buffer.write(b'x' * 1048576)"
)
output = io.BytesIO()
self.runner(command)(command, output)
self.assertEqual(
len(output.getvalue()),
1048576,
)
def test_output_to_file(self):
command = self.command(
"print('DATABASE BACKUP')"
)
with tempfile.TemporaryDirectory() as directory:
path = Path(directory) / "backup.sql"
with path.open("wb") as handle:
self.runner(command)(
command,
handle,
)
self.assertEqual(
path.read_bytes(),
b"DATABASE BACKUP\n",
)
def test_nonzero_exit_rejected(self):
command = self.command(
"raise SystemExit(7)"
)
with self.assertRaises(ExecutionError):
self.runner(command)(
command,
io.BytesIO(),
)
def test_timeout_rejected(self):
command = self.command(
"import time; time.sleep(10)"
)
with self.assertRaises(ExecutionError):
self.runner(command, timeout=1)(
command,
io.BytesIO(),
)
def test_unauthorised_command_rejected(self):
allowed = self.command("print('allowed')")
denied = self.command("print('denied')")
with self.assertRaises(ExecutionError):
self.runner(allowed)(
denied,
io.BytesIO(),
)
def test_modified_arguments_rejected(self):
command = self.command("print('allowed')")
runner = self.runner(command)
modified = command + ["unexpected"]
with self.assertRaises(ExecutionError):
runner(modified, io.BytesIO())
def test_command_string_rejected(self):
command = self.command("print('allowed')")
with self.assertRaises(ExecutionError):
self.runner(command)(
"echo unsafe",
io.BytesIO(),
)
def test_empty_command_rejected(self):
with self.assertRaises(ExecutionError):
CommandRunner(
allowed_commands=[[]],
)
def test_empty_allowlist_rejected(self):
with self.assertRaises(ExecutionError):
CommandRunner(allowed_commands=[])
def test_invalid_argument_rejected(self):
with self.assertRaises(ExecutionError):
CommandRunner(
allowed_commands=[["echo", 123]]
)
def test_null_argument_rejected(self):
with self.assertRaises(ExecutionError):
CommandRunner(
allowed_commands=[
["echo", "bad\x00argument"]
]
)
def test_invalid_timeout_rejected(self):
command = self.command("print('test')")
for timeout in (0, -1, True, 100000):
with self.subTest(timeout=timeout):
with self.assertRaises(ExecutionError):
self.runner(command, timeout)
def test_stdin_is_disabled(self):
command = self.command(
"import sys; "
"print(len(sys.stdin.read()))"
)
output = io.BytesIO()
self.runner(command)(command, output)
self.assertEqual(
output.getvalue(),
b"0\n",
)
def test_shell_metacharacters_not_interpreted(self):
command = self.command(
"import sys; print(sys.argv[1])"
) + ["$(echo unsafe); echo unsafe"]
output = io.BytesIO()
self.runner(command)(command, output)
self.assertEqual(
output.getvalue(),
b"$(echo unsafe); echo unsafe\n",
)
def test_sensitive_stderr_not_disclosed(self):
command = self.command(
"import sys; "
"sys.stderr.write('PRIVATE_TEST_VALUE'); "
"raise SystemExit(1)"
)
with self.assertRaises(ExecutionError) as error:
self.runner(command)(
command,
io.BytesIO(),
)
self.assertNotIn(
"PRIVATE_TEST_VALUE",
str(error.exception),
)
def test_allowlist_copied(self):
command = self.command("print('original')")
runner = self.runner(command)
command[-1] = "print('modified')"
with self.assertRaises(ExecutionError):
runner(command, io.BytesIO())
def test_missing_executable_rejected(self):
command = [
"/nonexistent/absdev-test-executable"
]
with self.assertRaises(ExecutionError):
self.runner(command)(
command,
io.BytesIO(),
)
def test_failed_output_not_published(self):
command = self.command(
"print('partial backup'); "
"raise SystemExit(1)"
)
output = io.BytesIO()
with self.assertRaises(ExecutionError):
self.runner(command)(command, output)
self.assertEqual(
output.getvalue(),
b"",
)
if __name__ == "__main__":
unittest.main()