Networking
Every listening port in a sandbox has a public URL, https://<port>-<sandbox id>.<domain>. get_host(port) builds it for you.
A secure sandbox (the default) also requires its traffic access token in the X-Traffic-Access-Token header. The token changes
when the sandbox resumes.
import timeimport urllib.request
from svm import Sandbox
# No internet except the hosts on the allow list.network = {"allow_internet_access": False, "allow_out": ["pypi.org", "files.pythonhosted.org"]}
with Sandbox.create(template="base", network=network) as sandbox: sandbox.commands.run("python3 -m http.server 8000", background=True, cwd="/tmp") time.sleep(1) # give the server a moment to listen
host = sandbox.get_host(8000) # 8000-<sandbox id>.<domain> request = urllib.request.Request( f"https://{host}/", headers={"X-Traffic-Access-Token": sandbox.access.traffic_access_token}, ) with urllib.request.urlopen(request) as response: print(response.status, response.read(80))
# Change the egress policy while the sandbox runs. sandbox.update_network(allow_internet_access=False, allow_out=["pypi.org", "github.com"])import { Sandbox } from "@svm/sandbox";
const sandbox = await Sandbox.create({ template: "base", network: { allow_internet_access: false, allow_out: ["pypi.org", "*.pythonhosted.org"] },});try { await sandbox.commands.run("cd /tmp && python3 -m http.server 8000", { background: true });
const host = sandbox.getHost(8000); // 8000-<sandbox id>.<domain> const token = sandbox.access?.trafficAccessToken ?? ""; await new Promise((resolve) => setTimeout(resolve, 1000)); // let the server start const response = await fetch(`https://${host}/`, { headers: { "X-Traffic-Access-Token": token } }); console.log(response.status, (await response.text()).slice(0, 80));
// Replace the egress policy while the sandbox runs. await sandbox.updateNetwork({ allow_internet_access: false, allow_out: ["github.com"] });} finally { await sandbox.kill();}sandbox, err := client.CreateSandbox(ctx, svm.CreateSandboxOptions{ Template: "base", Network: map[string]any{ "allow_internet_access": false, "allow_out": []string{"pypi.org", "*.pythonhosted.org"}, },})if err != nil { return err}defer sandbox.Kill(ctx)commands, err := sandbox.Commands()if err != nil { return err}if _, err := commands.Run(ctx, "python3 -m http.server 8000", svm.RunOptions{Background: true}); err != nil { return err}time.Sleep(time.Second) // give the server a moment to listen
host, err := sandbox.GetHost(8000)if err != nil { return err}access, _ := sandbox.Access()request, err := http.NewRequestWithContext(ctx, "GET", "https://"+host+"/", nil)if err != nil { return err}request.Header.Set("X-Traffic-Access-Token", access.TrafficAccessToken)response, err := http.DefaultClient.Do(request)if err != nil { return err}response.Body.Close()fmt.Println(host, response.Status)
// Change the egress policy while it runs.if _, err := sandbox.UpdateNetwork(ctx, map[string]any{"allow_internet_access": true}); err != nil { return err}require "net/http"require "svm"
box = SVM::Sandbox.create(template: "base", network: { allow_internet_access: false, allow_out: ["pypi.org", "*.pythonhosted.org"] })begin box.commands.run("python3 -m http.server 8000", background: true, cwd: "/tmp") sleep 1
host = box.get_host(8000) # "8000-<sandbox id>.<domain>" uri = URI("https://#{host}/") request = Net::HTTP::Get.new(uri) request["X-Traffic-Access-Token"] = box.access["traffic_access_token"] # a secure sandbox wants it response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) { |http| http.request(request) } puts "#{response.code} from #{host}"
# Change the egress policy while the sandbox runs. box.update_network(allow_internet_access: false, allow_out: ["github.com"])ensure box.killendimport dev.svm.sandbox.Client;import dev.svm.sandbox.RunOptions;import dev.svm.sandbox.Sandbox;import java.net.URI;import java.net.http.HttpClient;import java.net.http.HttpRequest;import java.net.http.HttpResponse;import java.util.List;import java.util.Map;
public class NetworkingExample { public static void main(String[] args) throws Exception { Client client = Client.fromEnvironment(); Sandbox.CreateOptions options = new Sandbox.CreateOptions(); options.network = Map.of("allow_internet_access", false, "allow_out", List.of("pypi.org", "files.pythonhosted.org")); options.wait = true; try (Sandbox sandbox = Sandbox.create(client, options)) { RunOptions background = new RunOptions(); background.background = true; sandbox.commands().run("python3 -m http.server 8000", background); Thread.sleep(1000);
String host = sandbox.getHost(8000); // 8000-<sandbox id>.<domain> String token = (String) sandbox.access().get("traffic_access_token"); HttpRequest request = HttpRequest.newBuilder(URI.create("https://" + host + "/")) .header("X-Traffic-Access-Token", token).build(); HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString()); System.out.println(response.statusCode());
sandbox.updateNetwork(Map.of("allow_internet_access", true)); // applies at runtime } }}Egress rules
Section titled “Egress rules”A rule is a host, a *.domain or an IPv4 CIDR, with an optional :port. A rule can also limit methods, paths and headers. The first
rule that matches decides. When no rule matches, the default action applies: deny once you have allowed anything, allow
otherwise. Rules can change while the sandbox runs.
svm network egress set <id> --allow pypi.org --allow '*.github.com:443' --deny 10.0.0.0/8svm network egress get <id>Inbound allow list
Section titled “Inbound allow list”update_inbound admits only the listed client networks to the sandbox’s public URLs and custom domains. Everyone else gets 403.
A change takes effect within about 2 seconds.
Custom domains and local forwarding
Section titled “Custom domains and local forwarding”svm domain add app.example.com --sandbox <id> --port 8080 # prints the TXT record to createsvm domain verify app.example.comsvm sandbox forward <id> 8080 --local-port 8080 # a sandbox port on your machinesvm ssh <id> # needs sshd and your key in the template