Skip to content

Commit 8052fb2

Browse files
authored
fix(sandboxing) allow string.rep in sandboxed environments
Reasoning: the idea behind limiting access to string.rep was disallowing a single operation from allocating too much memory. Given that in LuaJIT there are no debug hooks, it is trivial to implement a loop which allocates a lot of memory on every single iteration. Plus, given that the `string` table was global and obtainable via any sandboxed string, its sandboxing provoked issues on the global state. This change accepts `string.rep` in sandboxed environments as a practical compromise. Fixes Kong#7062 Fixes Kong#7133
1 parent 4fe108f commit 8052fb2

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

kong/tools/kong-lua-sandbox.lua

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
local sandbox = {
2-
_VERSION = "kong-lua-sandbox 1.0",
2+
_VERSION = "kong-lua-sandbox 1.1",
33
_DESCRIPTION = "A pure-lua solution for running untrusted Lua code.",
44
_URL = "https://github.com/kong/kong-lua-sandbox",
55
_LICENSE = [[
@@ -43,7 +43,6 @@ local BASE_ENV = {}
4343

4444
-- List of unsafe packages/functions:
4545
--
46-
-- * string.rep: can be used to allocate millions of bytes in 1 operation
4746
-- * {set|get}metatable: can be used to modify the metatable of global objects (strings, integers)
4847
-- * collectgarbage: can affect performance of other systems
4948
-- * dofile: can access the server filesystem
@@ -74,8 +73,8 @@ math.sin math.sinh math.sqrt math.tan math.tanh
7473
os.clock os.difftime os.time
7574
7675
string.byte string.char string.find string.format string.gmatch
77-
string.gsub string.len string.lower string.match string.reverse
78-
string.sub string.upper
76+
string.gsub string.len string.lower string.match string.rep
77+
string.reverse string.sub string.upper
7978
8079
table.insert table.maxn table.remove table.sort
8180
@@ -104,16 +103,13 @@ end)
104103

105104
-- auxiliary functions/variables
106105

107-
local string_rep = string.rep
108-
109106
local function sethook(f, key, quota)
110107
if type(debug) ~= 'table' or type(debug.sethook) ~= 'function' then return end
111108
debug.sethook(f, key, quota)
112109
end
113110

114111
local function cleanup()
115112
sethook()
116-
string.rep = string_rep -- luacheck: no global
117113
end
118114

119115
-- Public interface: sandbox.protect
@@ -161,8 +157,6 @@ function sandbox.protect(code, options)
161157
sethook(timeout, "", quota)
162158
end
163159

164-
string.rep = nil -- luacheck: no global
165-
166160
local t = table.pack(pcall(f, ...))
167161

168162
cleanup()

spec/01-unit/020-sandbox_spec.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,11 @@ describe("sandbox functions wrapper", function()
240240
_G.kong.configuration = deep_copy(base_conf)
241241
end)
242242

243+
it("has access to string.rep", function()
244+
assert.same("aaa", sandbox.sandbox("return string.rep('a', 3)")())
245+
assert.is_true(sandbox.sandbox("return ('a'):rep(3) == 'aaa'")())
246+
end)
247+
243248
it("has access to config.untrusted_lua_sandbox_environment", function()
244249
for _, m in ipairs(modules) do
245250
assert.same(find(m, _G), sandbox.sandbox(fmt("return %s", m))())

0 commit comments

Comments
 (0)