Skip to content

The sandbox

The sandbox reads a DDI file with the Rust engine, run as a WebAssembly program. The program can read nothing but the bytes of the file it is given.

Why use it

A DDI file from someone else is input you do not trust. Both engines already refuse the known attacks. They read no external entity, fetch nothing over the network, and stop at limits on size, depth and entities. But a bug in a parser could still let a crafted file do harm.

The sandbox adds a second wall. It runs the engine under wasmtime, and the engine gets:

  • no file and no folder: it gets the file's bytes on its input, and nothing else;
  • no network;
  • no environment variables and no arguments;
  • a cap on its memory, and on what it writes;
  • a fuel limit, which caps the work it can do;
  • a time limit.

So even a bug in the engine cannot read your files, reach a server or run forever. Use the sandbox for files from the web, from users or from partners.

Build the guest

The program the sandbox runs is called the guest. It is not shipped with ddigraph yet. Build it from a clone of ddigraph. You need Rust, which rustup installs:

rustup target add wasm32-wasip1
cd rust
cargo build --release --target wasm32-wasip1 -p ddigraph-sandbox

This writes rust/target/wasm32-wasip1/release/ddigraph-sandbox.wasm. Tell ddigraph where it is:

export DDIGRAPH_SANDBOX_WASM="$PWD/target/wasm32-wasip1/release/ddigraph-sandbox.wasm"

Then install wasmtime, which comes with the sandbox extra:

pip install 'ddigraph[sandbox]'

The extra installs wasmtime 46.0.1 or later. Older versions charge no more fuel to copy or fill a large block of memory than a small one. So with them, fuel would not stop a guest that loops on such copies.

The sandbox does not need the compiled engine of ddigraph-core: the guest holds its own copy. The ddigraph-core wheels could carry the guest too, so one install would give you both.

Use it

Call ddigraph.sandbox.parse. It takes the options of the Rust engine, and gives each chunk and the report as MessagePack. Decode them with msgspec:

import msgspec

from ddigraph import sandbox
from ddigraph.ir.model import GraphChunk, ParseReport

encoded, encoded_report = sandbox.parse(
    "untrusted.xml",
    flavor="cdi",
    dataset_id="untrusted",
    dataset_name=None,
    chunk_size=200,
    resolve="exact",
    huge_tree=False,
)
chunks = [msgspec.msgpack.decode(chunk, type=GraphChunk) for chunk in encoded]
report = msgspec.msgpack.decode(encoded_report, type=ParseReport)

You get the same chunks and the same report as ir.parse(path, engine="rust") gives. Some details:

  • Pass wasm= to name the guest, instead of setting the variable.
  • The sandbox uses dataset_id as you give it. ir.parse cleans it up first, so pass the ID as you want it stored.
  • The guest reads every flavor the Rust engine reads: Codebook, DDI-Lifecycle and DDI-CDI. For a flavor name it does not know, it raises a ValueError.
  • The guest sees the file's path only as a name, for messages and provenance. On Windows, that name has forward slashes.

The limits

The sandbox keeps every limit of the engine: no DTD, no external entity, and the limits on text size, depth and names that huge_tree lifts. It adds three of its own:

  • Memory. memory_cap is 512 MiB unless you pass another. The engine needs about 6 times the size of a typical DDI-CDI file, and about 10 times the size of a DDI-Lifecycle file. A file of many small objects needs more. So the default cap lets it read a DDI-CDI file of up to about 80 MB, or a DDI-Lifecycle file of up to about 50 MB. A Codebook file needs about 40 times its size, so the default cap reads one of up to about 12 MB. A file larger than the cap is not read at all. The guest may not write more than the cap either.
  • Fuel. One unit of fuel is about one WebAssembly instruction. To copy or fill memory costs one unit for each byte. Unless you pass fuel, a parse gets 4,000 units for each byte of the file and 150,000 for each < in it, and at least 2 billion. The engine's work grows with the number of elements more than with the bytes. A DDI-CDI file uses about 450 units a byte, a DDI-Lifecycle file about 750, and a Codebook file about 2,400. A Codebook of empty variables, <var/>, uses about 90,000 units for each variable. One whose variables each hold dozens of empty elements uses up to about 131,000 for each <. The default fuel reads both. Elements nested more than about 250 deep need more, so pass a larger fuel for such a file. A guest that loops without end uses up 2 billion units in a second or two.
  • Time. Fuel does not count the time the guest spends in a call to the system interface, such as a call for random numbers. So a clock stops the guest too. Unless you pass timeout, in seconds, a parse gets 10 seconds for each MB of the file, and at least 60 seconds. Fuel stops the parse of a DDI file long before that. A file made mostly of < gets more fuel, and for a large one, the clock may stop it first.

The errors it raises:

  • XMLLimitError: the file passed one of the engine's limits, or the guest used all its fuel, reached its memory cap or ran out of time. The message names the limit. If you trust the file, pass a larger fuel, memory_cap or timeout.
  • ValueError: the file is not well-formed XML, holds what the mapping spec refuses, or is of a flavor the engine does not read.
  • RuntimeError: the guest stopped in some other way. That is a bug, so please report it.
  • ImportError without wasmtime, and FileNotFoundError without the guest. Each message says how to fix it.

How it works

ddigraph reads the file in Python. It writes a short request and the file's bytes to the guest's input, runs the guest, and reads the answer from its output. The guest calls the engine's parse_bytes on the bytes.

The guest asks WebAssembly's system interface (WASI) for very little. It reads its input, writes its output and its error stream, reads its empty environment, gets random numbers and exits. It has no call that opens a file or a socket. tests/test_sandbox.py checks this. It also runs a guest that looks for a folder, a variable or an argument, and checks it finds none.

nsjail

nsjail is a second way to jail the engine. It runs the native engine, so it is faster than WebAssembly. But it works on Linux only, it needs the compiled engine of ddigraph-core (not the stand-in pip takes where no wheel fits), and it needs user namespaces, which some containers do not allow.

scripts/nsjail_config.py writes a config that runs Python with ddigraph_core on one file:

python scripts/nsjail_config.py untrusted.xml --flavor cdi --dataset-id untrusted -o jail.cfg
nsjail --config jail.cfg > untrusted.msgpack

In the jail, Python sees the file, its own install, the folders of ddigraph_core and msgspec (not the rest of their site-packages), /dev/urandom and the system libraries. It sees them read-only, at the same paths, and sees nothing else. Some builds of Python need /dev/urandom to start. Python has no network, not even a loopback interface, and no environment. These options set its limits:

Option Default What it caps
--memory-mb 1024 Address space, in MiB
--cpu-seconds 60 CPU time
--output-mb 1024 The size of a file it writes, its output too
--time-limit 120 Seconds before nsjail stops it

The output is the chunks and the report, as one MessagePack array. Read it with msgspec.msgpack.decode(data, type=tuple[list[bytes], bytes]).

For contributors

The guest's code is in rust/crates/ddigraph-sandbox. The rust/README.md explains how to check the crates. The guest also builds for the host, so cargo test runs its tests there.

tests/test_sandbox.py skips without wasmtime or without the built guest. It looks for the guest in rust/target, or where DDIGRAPH_SANDBOX_WASM says. tests/test_nsjail_config.py checks the nsjail config. Its run under nsjail skips unless nsjail works on your machine.