mediawiki-extensions-Scribunto/tests/engines/LuaStandalone/StandaloneTests.lua
Brad Jorsch f52136eada Handle invalid keys in Lua-to-PHP calls for LuaStandalone
PHP can't handle having arrays/objects or functions as keys in its
arrays, so make sure we don't try to pass them from Lua. Booleans aren't
really well-handled either, so let's disallow them too.

Also, add tests for proper stringification of floats and infinities when
those are used as keys.

Note this behavior change is needed to match the change in LuaSandbox
for fixing bug 54527, but isn't itself a security issue.

Change-Id: I1e2951bbe8cb78358650ad377bf7119fcac4485d
2014-01-14 10:03:42 -05:00

49 lines
1.4 KiB
Lua

local testframework = require( 'Module:TestFramework' )
local function setfenv1()
local ok, err = pcall( function()
setfenv( 2, {} )
end )
if not ok then
err = string.gsub( err, '^%S+:%d+: ', '' )
error( err )
end
end
local function getfenv1()
local env
pcall( function()
env = getfenv( 2 )
end )
return env
end
return testframework.getTestProvider( {
{ name = 'setfenv on a C function', func = setfenv1,
expect = "'setfenv' cannot set the requested environment, it is protected",
},
{ name = 'getfenv on a C function', func = getfenv1,
expect = { nil },
},
{ name = 'Invalid array key (table)', func = mw.var_export,
args = { { [{}] = 1 } },
expect = 'Cannot use table as an array key when passing data from Lua to PHP',
},
{ name = 'Invalid array key (boolean)', func = mw.var_export,
args = { { [true] = 1 } },
expect = 'Cannot use boolean as an array key when passing data from Lua to PHP',
},
{ name = 'Invalid array key (function)', func = mw.var_export,
args = { { [tostring] = 1 } },
expect = 'Cannot use function as an array key when passing data from Lua to PHP',
},
{ name = 'Unusual array key (float)', func = mw.var_export,
args = { { [1.5] = 1 } },
expect = { "array ( '1.5' => 1, )" }
},
{ name = 'Unusual array key (inf)', func = mw.var_export,
args = { { [math.huge] = 1 } },
expect = { "array ( 'inf' => 1, )" }
},
} )