DEV Community

Cover image for Parse untrusted files with WebAssembly modules that can't touch your network or disk
Adan Potyondy
Adan Potyondy

Posted on Edited on Originally published at neblex.com AI-assisted

Parse untrusted files with WebAssembly modules that can't touch your network or disk

A supplier emails an invoice. It's a reply, so the text you need sits above a signature and a quoted thread. The PDF is attached as base64. The IBAN inside has two digits swapped.

You could send all of it to an AI model. But most of this work follows rules, and rules are cheaper, faster and give the same answer every time. We wrote those rules as small WebAssembly modules in Rust, and the whole library is MIT-licensed on GitHub: neblex-wasm-modules.

Each module takes JSON in and returns JSON out. Here is why we chose WebAssembly, and how five modules handled three invoice emails.

Why WebAssembly for parsing untrusted input?

A WebAssembly module runs in a sandbox separated from the host. It can only reach the outside world through its imports: a list of host functions, such as reading a file or opening a connection, that the host passes in when the module starts.

Our modules have no imports at all, and we check that before every release. So a module can't reach:

  • the network
  • the file system
  • the clock
  • the environment

It also holds no secrets, because it never needs a connection. That makes it a good fit for parsing files that arrive from outside your company.

The formats that need a real parser

Input What makes it awkward Module
Mainframe or bank fixed-width file Header, detail and trailer lines with different layouts, implied decimals, COBOL signed numbers fixed-width-parser
CSV export Title rows above the header, totals rows below the data csv-cleaner
Email thread Only the new reply matters, not the quoted history or signature email-parser
Invoice text Labeled values such as "Invoice No:" plus a line-item table key-value-extractor
Model output Code fences, trailing commas, cut-off JSON json-repair

The repository has the full list, with a sample input for each module.

Worked example: three supplier invoice emails

In our test, a supplier sent three invoices to an accounts payable inbox, each a reply with a PDF attached. Five modules ran on each email:

  1. email-parser splits the message. The new reply comes out apart from the signature and the quoted request. With include_attachments on, the PDF comes out as base64.
  2. pdf-to-json reads the PDF's text layer.
  3. key-value-extractor finds the fields you list, under the labels they appear with, and reads the line-item table.
  4. identifier-validator checks the IBAN's check digits.
  5. pii-redactor replaces names you list, email addresses and phone numbers in the reply text before an AI step or a log sees it.

The fields for step 3 are configuration, not code:

{
  "fields": {
    "invoice_number": ["invoice no", "invoice number"],
    "invoice_date": { "labels": ["invoice date"], "type": "date" },
    "due_date": { "labels": ["due date"], "type": "date" },
    "po_number": ["po number", "purchase order"],
    "total": { "labels": ["total due", "amount due"], "type": "amount" },
    "iban": ["iban"]
  },
  "date_order": "dmy",
  "tables": true
}
Enter fullscreen mode Exit fullscreen mode

The results:

Email What the modules returned Route
INV-24-0417 All 6 fields (total 511.70 EUR), 3 line items, IBAN valid Straight to the ERP
INV-24-0418 The same 6 fields and 3 line items; IBAN "check digits do not match" Review
INV-24-0419 A scanned PDF with no text layer: has_text false, no fields OCR, an AI step or a person

Five modules in a row, then three routes: one invoice straight to the ERP, one to review because its IBAN check digits do not match, and one scanned PDF to OCR, an AI step or a person

The second invoice was the one with the swapped digits. IBAN check digits catch every single mistyped character and nearly every swap of two nearby characters, so it stopped before a payment run could use it.

Put the module before the AI step

Two of the three invoices never reached a model. Compared with an AI step on the same document, a module:

  • costs nothing per record
  • returns in well under a second, with the same answer every time
  • says what it couldn't read, in fields such as missing and has_text, so you know exactly which documents need AI

The same idea works with AI agents. Give an agent a module as a tool when a question has one exact answer: is this IBAN well formed, how many cases are in three pallets, what is the batch number in this GS1 scan? Run pii-redactor before the agent sees the text, and json-repair after it replies.

Verify a module before you run it

Each release gives you three ways to check a file:

  • a SHA256SUMS file to confirm the download
  • a signed build attestation, checked with gh attestation verify
  • reproducible builds: building the tagged source with ./build.sh gives a byte-identical file

Write your own

Copy the template folder and replace its transform function. The contract is small: no imports, and exports named memory, alloc and invoke that handle JSON in and out. The README explains it in detail.

We write ours in Rust, but C, C++ or Zig code can usually be compiled into a module the same way. The repository includes scripts to build a module and test it on sample data.

What can go wrong

  • Documents vary. Test a module on a sample of your own files before relying on it.
  • Labels won't always match. key-value-extractor lists the fields it couldn't read under missing, so you can route that document elsewhere.
  • A valid format isn't a true fact. A valid IBAN might still not be the supplier's account. Compare bank details with your vendor master.
  • Redaction is one layer. pii-redactor finds patterns and the names you provide. It won't catch every name in free text.

How we run them

We built these modules for Neblex, our integration platform. An uploaded module shows up next to the built-in steps, any flow can use it, and any AI agent can call it as a tool. The same file runs in the cloud or on a Neblex Worker inside your own network.

The original post covers the Neblex setup: WebAssembly for AI agents and workflows.

Originally published on the Neblex Blog. Written by Maria Soria, Manager of Customer Success at Neblex.

Top comments (0)