Skip to content

Commit

Permalink
Allow exec() in log phase
Browse files Browse the repository at this point in the history
Fixes issue #228.
  • Loading branch information
p0pr0ck5 committed Oct 22, 2016
1 parent 53fdfbb commit 22f1b1c
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 7 deletions.
13 changes: 10 additions & 3 deletions lib/resty/waf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ local function _finalize(self, ctx)

-- store the local copy of the ctx table
ngx.ctx = ctx

if (ctx.phase == 'log') then
self:write_log_events(true)
end
end

-- use the lookup table to figure out what to do
Expand Down Expand Up @@ -598,12 +602,15 @@ end

-- push log data regarding matching rule(s) to the configured target
-- in the case of socket or file logging, this data will be buffered
function _M.write_log_events(self)
function _M.write_log_events(self, has_ctx)
-- there is a small bit of code duplication here to get our context
-- because this lives outside exec()
local ctx = ngx.ctx
if (ctx.opts) then
_load(self, ctx.opts)

if (not has_ctx) then
if (ctx.opts) then
_load(self, ctx.opts)
end
end

if (not ctx.lrw_initted) then
Expand Down
3 changes: 2 additions & 1 deletion lib/resty/waf/collections.lua
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ _M.lookup = {
collections.RESPONSE_BODY = table_concat(ctx.buffers, '')
ngx.arg[1] = collections.RESPONSE_BODY
end
end
end,
log = function() end
}

return _M
2 changes: 1 addition & 1 deletion lib/resty/waf/phase.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ _M.version = "0.8.2"

local util = require "resty.waf.util"

_M.phases = { access = 1, header_filter = 2, body_filter = 3 }
_M.phases = { access = 1, header_filter = 2, body_filter = 3, log = 4 }

function _M.is_valid_phase(phase)
return util.table_has_key(phase, _M.phases)
Expand Down
2 changes: 1 addition & 1 deletion t/unit/phase/01_phase.t
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ true
--- no_error_log
[error]

=== TEST 11: Log is not a valid phase
=== TEST 11: Log is a valid phase
--- http_config eval: $::HttpConfig
--- config
location /t {
Expand Down
2 changes: 1 addition & 1 deletion tools/Modsec2LRW.pm
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ my $phase_lookup = {
2 => 'access',
3 => 'header_filter',
4 => 'body_filter',
5 => 'body_filter', # lua-resty-waf doesnt have a proper logging phase
5 => 'log',
};

my $op_sep_lookup = {
Expand Down

0 comments on commit 22f1b1c

Please sign in to comment.