It feels to me like this kind of sandboxing should be the Hello World" of server-side WebAssembly - using wasmer of wasmtime or similar.
To my surprise, actually achieving this with any of those seems to be very poorly documented.
Last time I complained about this on Hacker News Tim Bart figured out a Python recipe for me! https://til.simonwillison.net/webassembly/python-in-a-wasm-s...
I don't have a great recipe for JavaScript yet though. Something based around QuickJS feels like it could work.
The characteristics I'm looking for are:
1. Runs a string of code I give it - pretty much like an eval() function - ideally without needing to write that code to disk
2. Code has a limited amount of memory available to it
3. Code has limited CPU available as well - if it tries to use too many cycles it quits with an error
4. Same with amount of time to execute - cut it off after a specified number of ms and return an error
Bonus: it would be nice to be able to selectively expose additional Al functions to it (like fetch() via a controlled HTTP proxy) but even without that I could do some very useful things.
I remain frustrated that this is so hard to figure out!