So I have a game, with a modular framework (an api for modders) that can be written in python in the directory of
Game
¬ Info
¬ about.txt
¬ Packages
¬ mod1
¬ main.txt
¬ data
¬ credits.txt
¬ run.py
With main.py consisting of
import requests #is not allowed
userpassword = "peussweurdo"
open("Game/Info/About.txt", "r") #is allowed
open("Game/Info/About.txt", "w") #is not allowed
open("data/credits.txt") #is allowed
open("C:/Users/<user>/passwords") #is not allowed
The main things im trying to do are:
- Make sure import, exec and eval cannot be used
- Allow game to be accessed
I have tried the following
import os
mod_path = "Packages/mod1/main.txt"
mod_data = open(mod_path, "r").read()
def safe_open(path, mode="r"):
if path.startswith("Game") and mode != "r":
raise OSError("Accessing game files is read-only")
else:
return open("%s/%s" % (mod_path, path), mode)
methods = {
"open": safe_open
}
newly_defined = {}
exec(mod_data, methods, newly_defined)
But this ends up adding all of the builtins to methods and still allows for imports, execs and evals to be called. How'd I go about preventing this?