Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新增request解析数组功能 #19

Closed
yingshang opened this issue May 6, 2020 · 0 comments
Closed

新增request解析数组功能 #19

yingshang opened this issue May 6, 2020 · 0 comments

Comments

@yingshang
Copy link

json

local function _process_json_args(json_args,t)
        local t = t or {}
        local i =0
        for k,v in pairs(json_args) do
                if type(v) == 'table' then
                        for _k,_v in pairs(v) do

                                if type(_v) == "table" then
                                    t = _process_json_args(_v,t)

                                else
                                        if type(t[k]) == "table" then
                                                table.insert(t[k],_v)

                                        elseif type(t[k]) == "string" then
                                                local tmp = {}
                                                table.insert(tmp,t[k.."_".._k])
                                                table.insert(tmp,_v)
                                                t[k.."_".._k] = tmp

                                        else

                                        t[k] = _v
                                        end
                                end

                        end
                else
                                         if type(t[k]) == "table" then
                                                table.insert(t[k],v)
                                        elseif type(t[k]) == "string" then
                                                local tmp = {}
                                                table.insert(tmp,t[k.."_"..i])
                                                table.insert(tmp,v)
                                                t[k.."_"..i] = tmp
                                                i = i+1
                                        else

                                        t[k] = v
                                        end
                end
        end
        return t
end

GET数组

local function _parse_request_uri()
    local t = ngx.req.get_uri_args()
    local _t = {}

    for k,v in pairs(t) do
        if type(v) == "table" then
            for _k,_v in pairs(t) do
                if type(_v)=="table" then
                    for _key,_value in pairs(_v) do
                        _t[_k.."-".._key] = _value
                    end
                end
            end
        else
            _t[k] = v
        end
    end
    ngx.req.set_uri_args(t)
    ngx.ctx.parse_request_uri = _t
    return _t
end

post body plain

local function _parse_request_body()

	local content_type = ngx.req.get_headers()["Content-type"]
	if (type(content_type) == "table") then
    local error_info = {}
    error_info['headers'] = ngx.ctx.request_get_headers or _get_headers()
    error_info['log_type'] = "error_log"
    error_info['error_type'] = "parse_request_body"
    error_info['error_info'] = "Request contained multiple content-type headers"
    error_info['remote_addr'] = ngx.var.remote_addr
    ngx.ctx.error_log = error_info
		ngx.log(ngx.ERR,"Request contained multiple content-type headers")
		exit_code.return_exit()
	end

	if ngx.ctx.upload_request then
      ngx.ctx.parse_request_body = {}
      return {}
  end

  if  ngx.req.get_body_file() then
    local error_info = {}
    error_info['headers'] = ngx.ctx.request_get_headers or _get_headers()
    error_info['log_type'] = "error_log"
    error_info['error_type'] = "parse_request_body"
    error_info['error_info'] = "request body size larger than client_body_buffer_size, refuse request "
    error_info['remote_addr'] = ngx.var.remote_addr
    ngx.ctx.error_log = error_info
		ngx.log(ngx.ERR,"request body size larger than client_body_buffer_size, refuse request ")
		exit_code.return_error()
	end
	
	if content_type and  ngx.re.find(content_type, [=[^application/json;]=],"oij") and ngx.req.get_headers()["Content-Length"] and tonumber(ngx.req.get_headers()["Content-Length"]) ~= 0 then
	
		local json_args_raw = ngx.req.get_body_data()
		if not json_args_raw then
			ngx.ctx.parse_request_body = {}
			return {}
		end 

		local json_args,err = cjson.decode(json_args_raw)

		if json_args == nil then
      local error_info = {}
      error_info['headers'] = ngx.ctx.request_get_headers or _get_headers()
      error_info['log_type'] = "error_log"
      error_info['error_type'] = "parse_request_body"
      error_info['error_info'] = "failed to decode json args :"..err
      error_info['remote_addr'] = ngx.var.remote_addr
      ngx.ctx.error_log = error_info
      ngx.log(ngx.ERR,"failed to decode json args :",err)
      exit_code.return_error()
		end
		local t = {}
		t = _process_json_args(json_args)
		ngx.ctx.parse_request_body = t 
		return t 
	end

	local post_args,err = ngx.req.get_post_args(210)
	if not post_args then
    local error_info = {}
    error_info['headers'] = ngx.ctx.request_get_headers or _get_headers()
    error_info['log_type'] = "error_log"
    error_info['error_type'] = "parse_request_body"
    error_info['error_info'] = "failed to get post args: "..err
    error_info['remote_addr'] = ngx.var.remote_addr
    ngx.ctx.error_log = error_info
		ngx.log(ngx.ERR,"failed to get post args: ", err)
		exit_code.return_error()
	end
	if #_table_keys(post_args) > 200 then
    local error_info = {}
    error_info['headers'] = ngx.ctx.request_get_headers or _get_headers()
    error_info['log_type'] = "error_log"
    error_info['error_type'] = "parse_request_body"
    error_info['error_info'] = "post args count error,is attack!"
    error_info['remote_addr'] = ngx.var.remote_addr
    ngx.ctx.error_log = error_info
		ngx.log(ngx.ERR,"post args count error,is attack!")
		exit_code.return_error()
	end
	local json_check = cjson.decode(ngx.req.get_body_data())
	if json_check then
		local _tmp = {}
		_tmp = _process_json_args(json_check)
		ngx.ctx.parse_request_body = _tmp
		return _tmp
	end




local _t = {}
for _k, _v in pairs(post_args) do
    if type(_v) == "table" then
        for _key, _value in pairs(_v) do
            _t[_k .. "_" .. _key] = _value
        end

    else
        _t[_k] = _v

    end
end


	ngx.ctx.parse_request_body = _t
	return _t
end
@jx-sec jx-sec closed this as completed Sep 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants