Sandboxer
The main Sandboxer class. This class is intended to be used as follows,
where the module is placed in ServerScriptService named Init:
require(game:GetService("ServerScriptService").Init):Init()
This would be expected to be the first line of all scripts that are to be sandboxed
(not including --!strict, --!optimize, and other directives).
Having this be the first line guarantees that the script is sandboxed before any other code runs. You would have to check if it is the first line of code yourself, using external sources with modules such as rbx-reader.
Properties
InstanceSandboxer
This item is read only and cannot be modified. Read OnlySandboxer.InstanceSandboxer: typeof(InstanceSandboxer)A reference to the InstanceSandboxer class.
InstanceList
This item is read only and cannot be modified. Read OnlySandboxer.InstanceList: typeof(InstanceList)A reference to the InstanceList class.
Config
This item is read only and cannot be modified. Read OnlySandboxer.Config: typeof(Config)A reference to the Config.
Functions
EditDefaultSandbox
Sandboxer.EditDefaultSandbox(config: {[string]: any}--
A table containing the configuration for the sandbox.
) → boolean--
Whether the operation was completely successful.
Edits the default sandbox configuration. This is used to add or remove certain globals in the sandbox environment.
The config is saved and will be used for all future sandboxes made AFTER this function is called. Calls to this will overwrite previous values, if any.
Due to dictionary limitations, values cannot be set to nil. To remove a global,
set the value to false.
The following globals are always removed from the sandbox:
getfenvsetfenvloadstringnewproxydebug(library),debug.info, &debug.traceback
Attempting to add these functions/libraries will result in them being ignored.
However, you can set them to other values, such as a custom function.
This will also allow you to overwrite already sandboxed globals, such as game.
The sandboxer will make no attempt to secure custom functions, so use them at your own risk.
The function will return true if the operation was completely successful
(i.e., all values were set on the configuration table) and false if any
of the keys or values were forbidden globals.
The table is NOT recursively checked for forbidden globals. If you manage to bypass the checks, you are responsible for any consequences.
Init
Sandboxer:Init() → ()Initializes the sandbox environment for the calling script.
The following globals are removed:
SharedTabledebuglibrarygetfenvsetfenvloadstringnewproxyxpcall(note: usepcallinstead)
All other globals are set to sandboxed versions of the originals.
A separate table is created for _G / shared
Sandbox
Sandboxer:Sandbox(fnOrLevel: InstanceSandboxer.AnyFn | number--
The function to sandbox or the level to sandbox at.
) → ()Sandboxes the given function or the calling script at the specified level.
| Level | Definition |
|---|---|
| 1 | function calling Sandboxer:Sandbox |
| 2 | caller of the above function |
| 3... | etc. |
Errors
| Type | Description |
|---|---|
| "level must be at least 1" | If fnOrLevel is a number, it must be at least 1. |
SandboxString
Sandboxer:SandboxString(src: string,--
The specified string to be loaded as Luau code.
chunkname: string--
An optional chunk name for error messages and debug information.
) → InstanceSandboxer.AnyFn
Loads the Luau chunk src as a function using loadstring and
sandboxes it. ServerScriptService.LoadStringEnabled must be true
for this to work as per the loadstring rules.
Errors
| Type | Description |
|---|---|
| "loadstring() is not available" | ServerScriptService.LoadStringEnabled was false. |