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:
Brad Jorsch 2015-02-05 13:44:49 -05:00 committed by Anomie
parent 6c87ccd128
commit 58d722bcdf
2 changed files with 28 additions and 1 deletions

View file

@ -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

View file

@ -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,