Skip to content

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 time
import 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"])

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.

Terminal window
svm network egress set <id> --allow pypi.org --allow '*.github.com:443' --deny 10.0.0.0/8
svm network egress get <id>

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.

Terminal window
svm domain add app.example.com --sandbox <id> --port 8080 # prints the TXT record to create
svm domain verify app.example.com
svm sandbox forward <id> 8080 --local-port 8080 # a sandbox port on your machine
svm ssh <id> # needs sshd and your key in the template