Running untrusted pull request code from agents and contributors
Originally published on sandscale.dev . A pull request is a request to run someone else's code on your machine. CI has always worked this way: check out the branch, install dependencies, run the tests. The code that runs includes the test files, the build scripts, the install hooks of every new dependency, and anything the PR changed in your CI config. That was already a risk with human…
Pull requests allow developers to run someone else's code on their machine during the testing phase. This introduces risks such as code escaping or damaging the system, exfiltrating sensitive information, establishing persistence, or consuming excessive resources. To mitigate these risks, using a sandbox environment can isolate the untrusted code.
Sandscale provides a sandboxing service that creates a Firecracker microVM for each PR, ensuring a separate kernel and network environment for each test run. By starting a fresh sandbox for each PR, you can limit the potential damage that malicious code can cause. The sandbox is stopped automatically after a certain time limit to prevent runaway processes, and the use of a template with a short TTL_seconds value further ensures efficient resource management.
To run the untrusted code, you start by creating a sandbox with a short life using the API and specifying a unique name for the PR. The sandbox is then populated with the PR's files using a POST request, without exposing it to the internet. The tests are run within the sandbox using the POST /exec endpoint, which returns useful information such as exit codes, stdout, and stderr.
After the tests complete, the sandbox should be stopped and discarded, leaving no trace of the potentially malicious code. While the sandbox provides a strong boundary against unauthorized access and data exfiltration, it's essential to remember that no protection is foolproof. The exit codes and logs should still be treated as untrusted input, and a thorough review of the PR's output is necessary before considering it safe to merge.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.