1
- import org .luaj .vm2 .*;
1
+ import org .luaj .vm2 .Globals ;
2
+ import org .luaj .vm2 .LoadState ;
3
+ import org .luaj .vm2 .LuaBoolean ;
4
+ import org .luaj .vm2 .LuaString ;
5
+ import org .luaj .vm2 .LuaTable ;
6
+ import org .luaj .vm2 .LuaThread ;
7
+ import org .luaj .vm2 .LuaValue ;
8
+ import org .luaj .vm2 .Varargs ;
2
9
import org .luaj .vm2 .compiler .LuaC ;
3
- import org .luaj .vm2 .lib .*;
4
- import org .luaj .vm2 .lib .jse .*;
10
+ import org .luaj .vm2 .lib .Bit32Lib ;
11
+ import org .luaj .vm2 .lib .DebugLib ;
12
+ import org .luaj .vm2 .lib .PackageLib ;
13
+ import org .luaj .vm2 .lib .TableLib ;
14
+ import org .luaj .vm2 .lib .TwoArgFunction ;
15
+ import org .luaj .vm2 .lib .ZeroArgFunction ;
16
+ import org .luaj .vm2 .lib .jse .JseBaseLib ;
17
+ import org .luaj .vm2 .lib .jse .JseMathLib ;
18
+ import org .luaj .vm2 .lib .jse .JseStringLib ;
5
19
6
20
/** Simple program that illustrates basic sand-boxing of client scripts
7
21
* in a server environment.
8
22
*
9
- * <p>Although this sandboxing is done primarily in Java here, these
23
+ * <p>Although this sandboxing is done primarily in Java here, these
10
24
* same techniques should all be possible directly from lua using metatables,
11
25
* and examples are shown in examples/lua/samplesandboxed.lua.
12
26
*
@@ -29,7 +43,7 @@ public static void main(String[] args) {
29
43
server_globals = new Globals ();
30
44
server_globals .load (new JseBaseLib ());
31
45
server_globals .load (new PackageLib ());
32
- server_globals .load (new StringLib ());
46
+ server_globals .load (new JseStringLib ());
33
47
34
48
// To load scripts, we occasionally need a math library in addition to compiler support.
35
49
// To limit scripts using the debug library, they must be closures, so we only install LuaC.
@@ -47,7 +61,7 @@ public static void main(String[] args) {
47
61
runScriptInSandbox ( "return getmetatable('abc').len" );
48
62
runScriptInSandbox ( "return getmetatable('abc').__index" );
49
63
50
- // Example user scripts that attempt rogue operations, and will fail.
64
+ // Example user scripts that attempt rogue operations, and will fail.
51
65
runScriptInSandbox ( "return setmetatable('abc', {})" );
52
66
runScriptInSandbox ( "getmetatable('abc').len = function() end" );
53
67
runScriptInSandbox ( "getmetatable('abc').__index = {}" );
@@ -61,9 +75,9 @@ public static void main(String[] args) {
61
75
LuaValue .ADD , new TwoArgFunction () {
62
76
public LuaValue call (LuaValue x , LuaValue y ) {
63
77
return LuaValue .valueOf (
64
- (x == TRUE ? 1.0 : x .todouble ()) +
78
+ (x == TRUE ? 1.0 : x .todouble ()) +
65
79
(y == TRUE ? 1.0 : y .todouble ()) );
66
- }
80
+ }
67
81
},
68
82
}));
69
83
runScriptInSandbox ( "return 5 + 6, 5 + true, false + 6" );
@@ -75,21 +89,21 @@ public LuaValue call(LuaValue x, LuaValue y) {
75
89
// that contain functions that can be abused.
76
90
static void runScriptInSandbox (String script ) {
77
91
78
- // Each script will have it's own set of globals, which should
92
+ // Each script will have it's own set of globals, which should
79
93
// prevent leakage between scripts running on the same server.
80
94
Globals user_globals = new Globals ();
81
95
user_globals .load (new JseBaseLib ());
82
96
user_globals .load (new PackageLib ());
83
97
user_globals .load (new Bit32Lib ());
84
98
user_globals .load (new TableLib ());
85
- user_globals .load (new StringLib ());
99
+ user_globals .load (new JseStringLib ());
86
100
user_globals .load (new JseMathLib ());
87
101
88
102
// This library is dangerous as it gives unfettered access to the
89
- // entire Java VM, so it's not suitable within this lightweight sandbox.
103
+ // entire Java VM, so it's not suitable within this lightweight sandbox.
90
104
// user_globals.load(new LuajavaLib());
91
105
92
- // Starting coroutines in scripts will result in threads that are
106
+ // Starting coroutines in scripts will result in threads that are
93
107
// not under the server control, so this libary should probably remain out.
94
108
// user_globals.load(new CoroutineLib());
95
109
@@ -98,31 +112,31 @@ static void runScriptInSandbox(String script) {
98
112
// user_globals.load(new JseIoLib());
99
113
// user_globals.load(new JseOsLib());
100
114
101
- // Loading and compiling scripts from within scripts may also be
115
+ // Loading and compiling scripts from within scripts may also be
102
116
// prohibited, though in theory it should be fairly safe.
103
117
// LoadState.install(user_globals);
104
118
// LuaC.install(user_globals);
105
119
106
- // The debug library must be loaded for hook functions to work, which
120
+ // The debug library must be loaded for hook functions to work, which
107
121
// allow us to limit scripts to run a certain number of instructions at a time.
108
122
// However we don't wish to expose the library in the user globals,
109
123
// so it is immediately removed from the user globals once created.
110
124
user_globals .load (new DebugLib ());
111
125
LuaValue sethook = user_globals .get ("debug" ).get ("sethook" );
112
126
user_globals .set ("debug" , LuaValue .NIL );
113
127
114
- // Set up the script to run in its own lua thread, which allows us
128
+ // Set up the script to run in its own lua thread, which allows us
115
129
// to set a hook function that limits the script to a specific number of cycles.
116
- // Note that the environment is set to the user globals, even though the
130
+ // Note that the environment is set to the user globals, even though the
117
131
// compiling is done with the server globals.
118
132
LuaValue chunk = server_globals .load (script , "main" , user_globals );
119
133
LuaThread thread = new LuaThread (user_globals , chunk );
120
134
121
- // Set the hook function to immediately throw an Error, which will not be
135
+ // Set the hook function to immediately throw an Error, which will not be
122
136
// handled by any Lua code other than the coroutine.
123
137
LuaValue hookfunc = new ZeroArgFunction () {
124
138
public LuaValue call () {
125
- // A simple lua error may be caught by the script, but a
139
+ // A simple lua error may be caught by the script, but a
126
140
// Java Error will pass through to top and stop the script.
127
141
throw new Error ("Script overran resource limits." );
128
142
}
0 commit comments