mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Scribunto
synced 2024-09-23 18:30:32 +00:00
Allow nil in mw.text.jsonEncode
If it somehow gets in there (e.g. via a crafty __pairs), let it through. Change-Id: I9f79dbb1a09cd62b2a8f4b6beb84a3e2f1c85560
This commit is contained in:
parent
6c87ccd128
commit
58d722bcdf
|
@ -308,7 +308,7 @@ local function checkForJsonEncode( t, seen, lvl )
|
|||
if t ~= t or t >= math.huge or t <= -math.huge then
|
||||
error( "mw.text.jsonEncode: Cannot encode non-finite numbers", lvl )
|
||||
end
|
||||
elseif tp ~= 'boolean' and tp ~= 'string' then
|
||||
elseif tp ~= 'boolean' and tp ~= 'string' and tp ~= 'nil' then
|
||||
error( string.format( "mw.text.jsonEncode: Cannot encode type '%s'", tp ), lvl )
|
||||
end
|
||||
end
|
||||
|
|
|
@ -430,6 +430,33 @@ local tests = {
|
|||
args = { '"foo"' },
|
||||
expect = { 'foo' }
|
||||
},
|
||||
{ name = 'json encode, sneaky nil injection (object)',
|
||||
func = mw.text.jsonEncode,
|
||||
args = { setmetatable( {}, {
|
||||
__pairs = function ( t )
|
||||
return function ( t, k )
|
||||
if k ~= "foo" then
|
||||
return "foo", nil
|
||||
end
|
||||
end, t, nil
|
||||
end,
|
||||
} ) },
|
||||
expect = { '{"foo":null}' }
|
||||
},
|
||||
{ name = 'json encode, sneaky nil injection (array)',
|
||||
func = mw.text.jsonEncode,
|
||||
args = { setmetatable( { "one", "two", nil, "four" }, {
|
||||
__pairs = function ( t )
|
||||
return function ( t, k )
|
||||
k = k and k + 1 or 1
|
||||
if k <= 4 then
|
||||
return k, t[k]
|
||||
end
|
||||
end, t, nil
|
||||
end,
|
||||
} ) },
|
||||
expect = { '["one","two",null,"four"]' }
|
||||
},
|
||||
|
||||
{ name = 'json encode, invalid values (inf)',
|
||||
func = mw.text.jsonEncode,
|
||||
|
|
Loading…
Reference in a new issue