Skip to content

WebAssembly in Datum ​

Datum judges a submission, and prints a PDF on a server, with programs that run inside WebAssembly. This section explains what that means, how to run it, and how to lock it down on a server.

PageWhat it answers
This pageWhat runs in WebAssembly, why, and who is trusted with what
Running the sandboxHow to run it in the page, on a Datum host and on a runtime of your own
Hardening on a serverWhat a server must do around every run, and how to prove it does

The pages around this section cover the rest:

  • Writing a check. Validation and the sandbox has the routes a check can take, the context a rule reads and the error it returns.
  • The contract. Your own server has the request, the outcome, the run protocol and the interface versions.
  • The numbers. Limits has every cap in one place.

The parts ​

Five parts sit one inside the other. Each has one job.

text
your page or your server       builds the request
└─ a WebAssembly runtime       sets time, memory and mounts
   └─ the engine module        runs JavaScript
      └─ a program             judges answers, prints a PDF
         └─ your rule modules  your own checks
PartWhat it isWhere it comes from
The hostYour page, or your server's endpoint. It gathers what a run needs, starts the run and decides what the answer meansA Datum host (Spring Boot, FastAPI) or your own code
The runtimeWhat executes WebAssembly and enforces the limits on itThe browser in the page. On a server, wasmtime under FastAPI, Endive under Spring Boot, or one you choose
The engine moduleA JavaScript engine compiled to WebAssembly. It is the only part that is WebAssemblyIn the page a build of it is inside datum-wasm.js, with no file of its own. On a server it is qjs-wasi.wasm, a WASI command from its project's release page
The programsPlain JavaScript text the engine module evaluates: datum-sandbox-program.js judges a submission, datum-pdf-program.js prints or fills a PDFThe build of Datum, beside the bundles: see Files you host
Rule modulesSmall JavaScript modules of your own that the sandbox program runs after its own checksYou write them: see Rules of your own

Two things follow from that shape:

  • The programs are not WebAssembly. They are JavaScript, so one text runs in the page's engine, in the engine module and in a test. Nothing is compiled per form.
  • A form is data. A definition is never compiled or executed. The program reads it, and its rules are formulas the program evaluates itself.

Why WebAssembly ​

  • One program, one verdict. A server in Java, Python, C# or Node.js runs the same program text as the page. No host ports a rule, so no host can disagree about one. The same definition and the same answers give the same outcome, byte for byte.
  • Nothing is ambient. A WebAssembly module reaches only what its runtime hands it. A run is handed a request and a place to write an answer: no network, no clock, no environment, and no file but the programs.
  • The limits come from outside. The time budget and the memory limit are the runtime's. The code being limited cannot raise them, turn them off or catch them.
  • A run leaves nothing behind. On a server every run is a fresh instance, thrown away after it answers. Nothing one submission did is there for the next.

What it is not ​

  • Not a place for a stranger's code. Rule modules are code your team deploys, like any code on your server. The sandbox bounds what a mistake in one can cost; it is not a promise about code written to attack it. See Who is trusted with what.
  • Not where keys live. A signing key never enters a run. The PDF program prepares the bytes to sign and your server signs them: see Signing.
  • Not a way to fetch. A run never reaches the network. What a rule needs from outside, the host resolves first and hands in.
  • Not the only gate. Who may call an endpoint, how often and with how large a body are the host's to decide before any run starts.

Where it runs ​

PlaceWhat runs thereUse it for
The page, with wasm="auto"The sandbox program, in the engine module inside datum-wasm.jsDevelopment and testing: the page shows the verdict a server would give before a server exists
The builder's Try itThe same module, beside the builder's bundleAn author sees the server's verdict while building: see Embedding the builder
A Datum hostBoth programs, in the engine module, under the host's runtimeProduction: every submission is judged, and PDFs are printed, filled and signed
A server of your ownBoth programs, in the engine module, under a runtime you chooseA framework with no Datum host yet
The test command and the conformance suiteThe sandbox program you builtProving the program you ship before it serves a form: see Testing a form and the conformance suite

The page never runs the PDF program in WebAssembly. With pdf="auto" the PDF kernel is datum-pdf.js, plain JavaScript in the page, and it produces the same bytes as the PDF program does on a server.

The check in the page is a preview. A browser belongs to the person using it, so only the run on your server decides.

The life of a run ​

A validation run on a server, from the request to the reply:

  1. The host receives a delivery. It refuses a wrong content type, an oversized body and malformed JSON before anything else.
  2. It builds the run context. The definition comes from the host's own store, never from the request. The answers come from the envelope. The host adds what only it knows: the day, the host values, the options its data sources hold, and its policies.
  3. It takes a slot. A host runs a bounded number of runs at once. With no slot free in time it answers 503 server.busy.
  4. It starts a fresh instance. The runtime instantiates the engine module with a memory limit and a deadline, mounts the program read-only, and wires the request to standard input and a bounded buffer to standard output.
  5. The engine module loads the program. The host's script registers the rule modules, then calls the entry point with the request text.
  6. The program judges. It bounds the request, digests the delivered document, works out what is shown and required, holds every answer to its field, then runs your validators.
  7. The program answers. It writes the outcome as canonical JSON: the errors as codes, and the cleaned answers when nothing blocked.
  8. The host reads the answer. It checks that the output is exactly one answer, parses it, and discards the instance.
  9. The host replies. An outcome with cleaned answers is accepted. An outcome with a blocking error is a 422 with the codes. A run that gave no outcome is a 503, and nothing is stored.

In the page the steps are the same without the slot and the mount: the element builds the request, the engine module inside datum-wasm.js runs the program, and the verdict arrives on datum-wasm-validated. One thing differs. A form keeps its sandbox from one submit to the next, with the program and your rule modules loaded once. The program keeps its state where no rule module can name it, and the language's built-ins are frozen, so a run cannot change the one after it.

What crosses the boundary ​

Text goes in and text comes out. Nothing else is shared: no object, no function, no handle.

DirectionWhatBounded by
InThe run context as one JSON text: the form's root, the answers, the locale and the run's optionsLength and nesting depth, checked before the run
InThe program and the rule modules, as source textWhat your deployment ships
OutThe outcome as one canonical JSON textLength, checked before it is parsed
OutThe run log, inside the outcome200 entries, each cut at 8 KiB

What code in the sandbox can do ​

A rule module is an ES2020 program. It is handed one read-only context and returns codes.

It canIt cannot
Read the answers, the definition and what the host handed inRead the clock: Date is disabled. A rule that needs the day reads context.today
Compute with the whole language: strings, numbers, regular expressions, JSONDraw a random number: Math.random is disabled
Return errors, warnings and information, as codesFetch, open a file or start a timer
Write to the run log with context.logPrint: there is no console
Add formula functions for the form's rulesReturn a promise: a validator answers at once
Define helpers and globals of its ownChange a built-in: the language's own objects are frozen before a module loads

A rule that tries one of the right-hand column either gets no such name or throws, and a validator that throws blocks the submission with validator.failed.

Who is trusted with what ​

Every input to a run has a different owner. The design holds each one a different way.

InputWho supplies itTrusted toWhat holds it
The answersAnyone who can reach the endpointNothingThe program judges every key against the definition. The host bounds the body before the run
The delivered documentThe sameNothingDigested inside the program before anything else reads it. Rules never see it
The definitionYour authorsDescribe a form, not to spend your serverThe check operation at save and at registration, the size caps, the refusal of runaway patterns, and the run's budgets
Rule modulesYour deploymentRun as your server's own codeReview, a version and a test before they ship. The frozen language and the answer frame are guard rails, not a boundary against an attacker
The programsThe Datum release you builtJudge and print correctlyA digest in the host packages, and the interface version probe at start
The engine moduleIts project's releaseRun JavaScript correctlyA pinned SHA-256, checked before it is compiled
The runtimeIts projectConfine the moduleIts own releases: keep it patched

The line that matters most is between the first three rows and the rest. Answers and documents come from the Internet, and a definition comes from an author who may make a mistake. Rule modules, programs, the engine module and the runtime are things you deploy.

Hardening on a server turns each row into what a server does about it.

The same bytes everywhere ​

A verdict is only portable if nothing in a run depends on where it ran.

  • No clock and no randomness. The day is a value the host passes in as today. Without it a rule that reads the day is left unjudged and named in the outcome.
  • Canonical output. The program writes its own outcome with keys sorted by UTF-16 code unit and no whitespace, so the text is the same from every engine.
  • The digest is the program's. When the host sends the delivered document, the outcome carries its canonical text and SHA-256. Store or sign those, never your own re-serialisation.
  • The run log is plain data. The same run gives the same log on every host.
  • Parity is tested. The page and a server run different builds of a JavaScript engine, and the form makes its own checks in the browser's engine as well. datum-security-check --parity holds the form's own checks and the program to the same answer, vector by vector: see the conformance suite.

Versions ​

Each program states the interface version it speaks, and a host refuses a program or a definition it was not built for.

  • Build the programs from the release you deploy.
  • Deploy them to servers first, or together with the browser bundle, so a server is never more lenient than the page in front of it.
  • The current versions and the probe a host makes at start are under Interface versions.

Next ​

MIT licence. Your forms, your data, your keys.