44import org .luaj .vm2 .LoadState ;
55import org .luaj .vm2 .LuaValue ;
66import org .luaj .vm2 .compiler .LuaC ;
7- import org .luaj .vm2 .lib .*;
8- import org .luaj .vm2 .lib .jse .JseBaseLib ;
7+ import org .luaj .vm2 .lib .BaseLib ;
8+ import org .luaj .vm2 .lib .Bit32Lib ;
9+ import org .luaj .vm2 .lib .StringLib ;
10+ import org .luaj .vm2 .lib .TableLib ;
911import org .luaj .vm2 .lib .jse .JseMathLib ;
1012
1113/**
1719 * <li>os library - Prevents system command execution and file operations</li>
1820 * <li>io library - Prevents file system access</li>
1921 * <li>debug library - Prevents environment manipulation and introspection attacks</li>
20- * <li>load/loadfile/loadstring with bytecode - Prevents bytecode injection</li>
22+ * <li>package library - Prevents loading modules from disk</li>
23+ * <li>load/loadfile/dofile - Prevents loading code from files</li>
2124 * </ul>
2225 */
2326public final class SandboxedGlobals {
@@ -35,15 +38,22 @@ private SandboxedGlobals() {
3538 public static Globals create () {
3639 Globals globals = new Globals ();
3740
38- // Install safe base libraries
39- globals . load ( new JseBaseLib ()); // Basic functions (print, type, etc.) - but we'll remove dangerous ones
40- globals .load (new PackageLib ()); // Package/module system
41+ // Install safe base libraries only
42+ // Using BaseLib instead of JseBaseLib to avoid any file system access
43+ globals .load (new BaseLib ()); // Basic functions (print, type, tostring, etc.)
4144 globals .load (new Bit32Lib ()); // Bit operations
4245 globals .load (new TableLib ()); // Table manipulation
4346 globals .load (new StringLib ()); // String manipulation
4447 globals .load (new JseMathLib ()); // Math functions
4548
46- // Install the compiler so scripts can be loaded
49+ // NOTE: We intentionally do NOT load:
50+ // - PackageLib (can search/load files from disk)
51+ // - IoLib / JseIoLib (file system access)
52+ // - OsLib / JseOsLib (system commands, file operations)
53+ // - DebugLib (can manipulate environments)
54+ // - LuajavaLib (arbitrary Java class access)
55+
56+ // Install the compiler so scripts can be loaded from strings
4757 LoadState .install (globals );
4858 LuaC .install (globals );
4959
@@ -57,21 +67,13 @@ public static Globals create() {
5767 * Removes dangerous functions that could be used to escape the sandbox.
5868 */
5969 private static void removeDangerousFunctions (Globals globals ) {
60- // Remove functions that can load arbitrary code
61- globals .set ("dofile" , LuaValue .NIL ); // Can load files from disk
62- globals .set ("loadfile" , LuaValue .NIL ); // Can load files from disk
63-
64- // Note: We keep 'load' and 'loadstring' since they can only load Lua source code
65- // (not bytecode) when LuaC is the only compiler installed, making them relatively safe.
66- // However, if you want maximum security, uncomment these:
67- // globals.set("load", LuaValue.NIL);
68- // globals.set("loadstring", LuaValue.NIL);
70+ // Remove functions that can load code from files
71+ globals .set ("dofile" , LuaValue .NIL ); // Loads and executes files from disk
72+ globals .set ("loadfile" , LuaValue .NIL ); // Loads files from disk
6973
70- // Remove the package.loadlib function which can load native libraries
71- LuaValue pkg = globals .get ("package" );
72- if (pkg .istable ()) {
73- pkg .set ("loadlib" , LuaValue .NIL );
74- }
74+ // Remove load/loadstring to prevent any dynamic code execution
75+ // This is the safest option as it prevents all forms of dynamic code loading
76+ globals .set ("load" , LuaValue .NIL );
77+ globals .set ("loadstring" , LuaValue .NIL );
7578 }
7679}
77-
0 commit comments