-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.lua
291 lines (254 loc) · 8.57 KB
/
env.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
local BYTECODE_CHAR = 27
local function wrap(f, obj)
return function(...)
return f(obj, ...)
end
end
function libox.safe.rep(str, n)
if #str * n > 64000 then
error("string length overflow", 2)
end
return string.rep(str, n)
end
function libox.safe.PcgRandom(seed, seq)
if seq and #seq > 1000 then error("Sequence too large, size limit is 1000", 2) end
local pcg = PcgRandom(seed, seq)
-- now make the interface
local interface = {
next = wrap(pcg.next, pcg),
rand_normal_dist = function(min, max, num_trials)
if num_trials and num_trials > 50 then
error("too many trials", 2)
end
return pcg:rand_normal_dist(min, max, num_trials)
end
}
return interface
end
function libox.safe.PerlinNoise(noiseparams)
if type(noiseparams) ~= "table" then return false, "noiseparams is a table, deprecated syntax is not supported." end
noiseparams.persistence = noiseparams.persistence or noiseparams.persist
noiseparams.persist = nil
local check, element = libox.type_check(noiseparams, {
offset = libox.type("number"),
scale = libox.type("number"),
spread = libox.type_vector,
seed = libox.type("number"),
octaves = libox.type("number"),
persistence = libox.type("number"),
lacunarity = libox.type("number"),
flags = function(x)
if x ~= nil then
return type(x) == "string"
else
return true
end
end,
})
if not check then
return false, element
end
local core = PerlinNoise(noiseparams)
local interface = {
get_2d = wrap(core.get_2d, core),
get_3d = wrap(core.get_3d, core)
}
return interface
end
function libox.safe.pcall(f, ...)
local ret_values = { pcall(f, ...) }
if not debug.gethook() then
error("Code timed out!", 2)
end
return unpack(ret_values)
end
function libox.safe.xpcall(f, handler, ...)
local ret_values = {
xpcall(f, function(...)
if not debug.gethook() then
error("Code timed out!", 2)
return
end
return handler(...)
end, ...)
}
if not debug.gethook() then
error("Code timed out!", 2)
end
return unpack(ret_values)
end
function libox.safe.get_loadstring(env) -- chunkname is ignored
return function(code, chunkname)
if chunkname ~= nil then
error("Adding a chunkname is forbidden", 2)
end
if type(code) == "string" and #code > 64000 then error("Code too long :/", 2) end
if string.byte(code, 1) == BYTECODE_CHAR then
error("dont sneak in bytecode (mod security will prevent you anyway)", 2)
end
local f, errmsg = loadstring(code)
if f == nil then
return nil, errmsg
end
setfenv(f, env)
if rawget(_G, "jit") then
jit.off(f, true)
end
return f, errmsg
end
end
--[[
safe_date is from the mooncontroller mod
licensed under LGPLv3, by OgelGames
]]
-- Wraps os.date to only replace valid formats,
-- ignoring invalid ones that would cause a hard crash.
local TIME_MAX = 32535244800 -- 01/01/3001
-- todo: change on 01/01/3001
local function safe_date(str, time)
if type(time) ~= "number" then
time = os.time()
elseif time < 0 or time >= TIME_MAX then
return nil
end
if type(str) ~= "string" then
return os.date("%c", time)
end
if str == "*t" then
return os.date("*t", time)
end
str = string.gsub(str, "%%[aAbBcdHImMpSwxXyY]", function(s)
return os.date(s, time)
end)
return str
end
local function datetable()
return os.date("*t")
end
function libox.create_basic_environment()
--[[
get the safest, least strict lua environment
*for the "normal" sandbox, when using the "coroutine" sandbox you will need to add coroutine.yield
and edit pcall/xpcall to run coroutine.yield instead of erroring
]]
-- INCLUDES: basic lib (minus coroutine, add that yourself if you need to), string, table, math, bit, os, a bit of minetest, some minetest classes
-- is meant to be added on top of
local env = {
assert = assert,
error = error,
collectgarbage = function(arg)
if arg ~= "count" then error("The only valid mode to collectgarbage is count") end
return collectgarbage("count")
end,
ipairs = ipairs,
pairs = pairs,
next = next,
pcall = libox.safe.pcall,
xpcall = libox.safe.xpcall,
select = select,
unpack = function(t, a, b) -- from mooncontroller
assert(not b or b < 2 ^ 30)
return unpack(t, a, b)
end,
tonumber = tonumber,
tostring = tostring,
type = type,
}
env.loadstring = libox.safe.get_loadstring(env)
env._G = env
env.string = {
byte = string.byte,
char = string.char,
dump = string.dump,
find = function(s, pattern, init, plain)
if not plain then
error(
"string.find: the fourth parameter (plain) must be true, if you need patterns, use pat.find instead",
2)
end
return string.find(s, pattern, init, true)
end,
format = string.format,
len = string.len,
lower = string.lower,
rep = libox.safe.rep,
reverse = string.reverse,
sub = string.sub,
upper = string.upper,
-- minetest helpers
trim = string.trim,
split = function(str, delim, include_empty, max_splits, sep_is_pattern)
if sep_is_pattern == true then
error("The fourth argument (sep_is_pattern) must be false", 2)
end
return string.split(str, delim, include_empty, max_splits, false)
end,
}
env.table = {
insert = table.insert,
maxn = table.maxn,
remove = table.remove,
sort = table.sort,
concat = table.concat,
-- minetest only
indexof = table.indexof,
copy = table.copy,
insert_all = table.insert_all,
key_value_swap = table.key_value_swap,
shuffle = table.shuffle,
-- luajit only
move = table.move,
}
env.math = {}
for _, v in ipairs({
"abs", "acos", "asin", "atan", "atan2", "ceil", "cos", "cosh", "deg", "exp", "floor",
"fmod", "frexp", "huge", "ldexp", "log", "log10", "max", "min", "modf", "pi", "pow",
"rad", "random", "sin", "sinh", "sqrt", "tan", "tanh",
-- minetest helpers
"hypot", "sign", "factorial", "round"
}) do
env.math[v] = math[v]
end
env.bit = table.copy(bit)
env.vector = table.copy(vector)
env.vector.metatable = nil -- it's useless to add it, and it's undocumented
env.os = {
clock = os.clock,
datetable = datetable,
difftime = os.difftime,
time = os.time,
date = safe_date,
}
env.minetest = {
hash_node_position = minetest.hash_node_position,
get_position_from_hash = minetest.get_position_from_hash,
formspec_escape = libox.sandbox_lib_f(minetest.formspec_escape),
explode_table_event = libox.sandbox_lib_f(minetest.explode_table_event),
explode_textlist_event = libox.sandbox_lib_f(minetest.explode_textlist_event),
explode_scrollbar_event = libox.sandbox_lib_f(minetest.explode_scrollbar_event),
inventorycube = libox.sandbox_lib_f(minetest.inventorycube),
urlencode = libox.sandbox_lib_f(minetest.urlencode),
rgba = libox.sandbox_lib_f(minetest.rgba),
encode_base64 = libox.sandbox_lib_f(minetest.encode_base64),
decode_base64 = libox.sandbox_lib_f(minetest.decode_base64),
get_us_time = libox.sandbox_lib_f(minetest.get_us_time),
} -- safe minetest functions
-- extra global environment stuff
for _, v in ipairs({
"dump", "dump2"
}) do
env[v] = libox.sandbox_lib_f(_G[v])
end
-- oh yeah who could forget...
-- some random minetest stuffs
env.PcgRandom = libox.sandbox_lib_f(libox.safe.PcgRandom)
env.PerlinNoise = libox.sandbox_lib_f(libox.safe.PerlinNoise)
env.traceback = libox.traceback
env.pat = {
find = wrap(libox.pat.find, libox.pat),
match = wrap(libox.pat.match, libox.pat),
gmatch = wrap(libox.pat.gmatch, libox.pat),
}
libox.supply_additional_environment(env) -- for mods to use
return env
end