This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Pizza Fox's profile picture

I Need Some Help With a Sandbox

Started by Pizza Fox, 25 September 2015 - 09:32 PM
Pizza Fox #1
Posted 25 September 2015 - 11:32 PM
Hey everyone, I need some help with making a sandbox for filesystem functions. What I would like is something that for the most part, isn't easily broken out of. It would make a folder, where programs would be run out of, and can only see what's inside of the folder. Additional things I would like are 1) code that puts all the files into that sandboxed folder, and 2) a way to get the data from any user inputted text(ex. program asks user for their name, user inputs name, program gets name, sets variable to whatever text was inputted.)

Any help is appreciated.

PS: Also, would I have to put this code in front of any program if I want to sandbox it, or is there a way to have some master program set up a sandbox that un-edited programs would be affected by.
Edited on 25 September 2015 - 09:42 PM
Lupus590 #2
Posted 26 September 2015 - 12:41 AM
Sandboxes mainly override the FS API so you need to modify it so that root is the folder which is for the program owns.

As for input, same thing, override the function calls that you want to change.

The difficult part is making sure to keep the original functions and having the originals useable by non-sandboxed programs even while the sandbox is running, for this the sandboxed program will need to be ran by os.run and not shell.run
Edited on 25 September 2015 - 10:43 PM
Bomb Bloke #3
Posted 26 September 2015 - 01:33 AM
To use os.run(), you'll need an understanding of Lua 5.1's use of environments. All the scripts that run on your computer share an environment table, into which goes all their global variables. The idea is that you create a unique environment table for the script you wish to sandbox, one which has an altered version of _G which contains altered versions of certain functions.

It'd be worthwhile reviewing some old threads for examples.