by simonw · 3y ago · 0 comments
The thing I most want to solve right now is this: I want to write a regular Python application that can safely execute untrusted Python code in a WASM sandbox as part of its execution.
I want to do this so I can let end users customize my web applications in weird and interesting ways by pasting their own Python code into a textarea - think features like "run this Python code to transform my stored data" - without them being able to break my system.
This feels like it should be pretty easy with WebAssembly! It's the classic code sandboxing problem - long a big challenge in Python world - finally solved in a robust way.
I've been finding it surprisingly hard to get a proof-of-concept of this working though.
Essentially I want to be able to do this, in my regular Python code:
import some_webassembly_engine
python = some_webassembly_engine.load(
"python.wasm",
max_cpu_time_in_seconds=3.0,
max_allowed_memory_in_bytes=32000000
)
result = python.execute("3 + 5")
I've not yet figured out the incantations I need to actually do this - in particular the limits on CPU and memory time.I posed this question on Mastodon recently and Jim Kring put together this demo, which gets most of the way there (albeit using an old Python 3.6 build): https://github.com/jimkring/python-sandbox-wasm
It doesn't feel like this should be as hard to figure out as it is!
The remaining piece of the puzzle would be to create a wit-bindgen guest generator https://github.com/bytecodealliance/wit-bindgen#guests for this build of the python interpreter. You could then seamlessly call back and forth between the host and guest pythons, without even knowing that wasmtime is under the hood.
In general the Wasm Component ecosystem is still a few months away from being generally useful. There are a lot of people across the bytecode alliance working on the fundamentals right now, and we are making great progress, but its not ready to ship quite yet.
https://gist.github.com/pims/711549577759ad1341f1a90860f1f3a...
Just tried this and it works great!
I changed app.py to this:
import sqlite3
print(sqlite3.connect(":memory:").execute(
"select sqlite_version()"
).fetchone()[0])
And it output "3.39.2" - but the same code in my regular Python interpreter output "3.40.1", which demonstrates that the WASM Python there has its own WASM-compiled SQLite.Python SDK: https://extism.org/docs/integrate-into-your-codebase/python-...
Python is 1 of 15+ languages we support, and as far as I know it's the easiest way to setup a wasm engine in your app, load wasm code, and call a function using complex data I/O.
I'm one of the authors - happy to share more about it, or if you'd like to join our Discord we have lots of active users and contributors there: https://discord.gg/cx3usBCWnc
I see that the library itself is available in Python, but this page https://extism.org/docs/category/write-a-plug-in currently only lists Rust, JavaScript, Go, Haskell, AssemblyScript, Zig and C. Any chance Python might get added to that list as well?
Just found https://github.com/extism/extism-sqlite3 which is very relevant to my interests too!
[1] https://github.com/extism/js-pdk [2] https://discord.gg/mAADpt9r
cpu is really controlled by # milliseconds until the wasm code is trapped.
1. Sandboxing for CPU time and max allowed memory requires the enterprise edition, so you'd have to pay for it.
2. The Python engine isn't 100% compatible with regular Python, although that may not matter for your use case as the compatibility is pretty good and issues mostly show up around extension modules.
1. Capability control only works for JavaScript (https://www.graalvm.org/latest/reference-manual/embed-langua...)
2. The documentation says in no uncertain terms that running untrusted code is unsupported (https://www.graalvm.org/latest/security-guide/#security-mode...)
(I'm so close to building my own search engine just against my own content there.)
If anyone else is awake and wants to pick up the baton...
- CPU limits are just using an arbitrary amount of wasmtime "fuel". Would be worth looking into epoch interrupts instead.
- Memory limits aren't implemented. Seems like wasmtime-py doesn't expose bindings to anything with a wasmtime::ResourceLimiter trait.
- All i/o is going through tempfiles instead of dealing with proper interfaces.
Either way, I couldn't figure out how to do the above sequence of steps with any of the available Python WASM runtimes - they're all very under-documented at the moment, sadly. I tried all three of these:
- https://github.com/wasmerio/wasmer-python