Skip to content

Commit 6f00b36

Browse files
committed
fix(data-mask): compact JSON array when removing an element
1 parent 5c5e1b2 commit 6f00b36

2 files changed

Lines changed: 94 additions & 1 deletion

File tree

apisix/plugins/data-mask.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ local ngx = ngx
1818
local ipairs = ipairs
1919
local next = next
2020
local type = type
21+
local t_remove = table.remove
2122
local re_sub = ngx.re.sub
2223
local core = require("apisix.core")
2324
local jp = require("jsonpath")
@@ -181,7 +182,11 @@ local function mask_json(obj, conf)
181182
end
182183
local index = table_index(node.path[#node.path])
183184
if conf.action == "remove" then
184-
nested[index] = nil
185+
if type(index) == "number" then
186+
t_remove(nested, index)
187+
else
188+
nested[index] = nil
189+
end
185190
masked = true
186191
elseif conf.action == "replace" then
187192
nested[index] = conf.value

t/plugin/data-mask.t

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,3 +897,91 @@ nginx_config:
897897
GET /hello?password=secret&token=mytoken
898898
--- access_log eval
899899
qr/GET \/hello\?token=\*\*\*\*\* HTTP\/\d+\.\d+/
900+
901+
902+
903+
=== TEST 21: create route for removing a middle JSON array element
904+
--- config
905+
location /t {
906+
content_by_lua_block {
907+
local t = require("lib.test_admin").test
908+
local code, body = t('/apisix/admin/routes/1',
909+
ngx.HTTP_PUT,
910+
[[{
911+
"plugins": {
912+
"data-mask": {
913+
"request": [
914+
{
915+
"action": "remove",
916+
"body_format": "json",
917+
"name": "$.items[1]",
918+
"type": "body"
919+
}
920+
]
921+
},
922+
"file-logger": {
923+
"include_req_body": true,
924+
"path": "mask-json-array-hole.log"
925+
}
926+
},
927+
"upstream": {
928+
"nodes": {
929+
"127.0.0.1:1982": 1
930+
},
931+
"type": "roundrobin"
932+
},
933+
"uri": "/hello"
934+
}]]
935+
)
936+
937+
if code >= 300 then
938+
ngx.status = code
939+
end
940+
ngx.say(body)
941+
}
942+
}
943+
944+
945+
946+
=== TEST 22: verify removing a middle array element compacts the array
947+
--- config
948+
location /t {
949+
content_by_lua_block {
950+
local core = require("apisix.core")
951+
local t = require("lib.test_admin").test
952+
953+
os.remove("mask-json-array-hole.log")
954+
local code = t("/hello", ngx.HTTP_POST, [[{"items":["a","drop-me","c"]}]])
955+
956+
local fd, err = io.open("mask-json-array-hole.log", "r")
957+
if not fd then
958+
core.log.error("failed to open file: ", err)
959+
return
960+
end
961+
local line = fd:read()
962+
local log = core.json.decode(line)
963+
os.remove("mask-json-array-hole.log")
964+
965+
if not log or not log.request or not log.request.body then
966+
ngx.say("missing logged request body")
967+
return
968+
end
969+
970+
local body = core.json.decode(log.request.body)
971+
if not body or type(body.items) ~= "table" then
972+
ngx.say("items missing: " .. tostring(log.request.body))
973+
return
974+
end
975+
if #body.items ~= 2 then
976+
ngx.say("expected compacted array of 2, got: " .. core.json.encode(body.items))
977+
return
978+
end
979+
if body.items[1] ~= "a" or body.items[2] ~= "c" then
980+
ngx.say("array not compacted: " .. core.json.encode(body.items))
981+
return
982+
end
983+
ngx.say("success")
984+
}
985+
}
986+
--- response_body
987+
success

0 commit comments

Comments
 (0)