forked from p0pr0ck5/lua-resty-waf
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This overrides the deny_status option on a per-rule basis.
- Loading branch information
Showing
6 changed files
with
211 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
use Test::Nginx::Socket::Lua; | ||
|
||
repeat_each(3); | ||
plan tests => repeat_each() * 4 * blocks(); | ||
|
||
no_shuffle(); | ||
run_tests(); | ||
|
||
__DATA__ | ||
|
||
=== TEST 1: No status nondisruptive action uses waf._deny_status | ||
--- http_config | ||
init_by_lua_block{ | ||
if (os.getenv("LRW_COVERAGE")) then | ||
runner = require "luacov.runner" | ||
runner.tick = true | ||
runner.init({savestepsize = 110}) | ||
jit.off() | ||
end | ||
} | ||
--- config | ||
location /t { | ||
access_by_lua ' | ||
local lua_resty_waf = require "resty.waf" | ||
local waf = lua_resty_waf:new() | ||
waf:set_option("debug", true) | ||
waf:set_option("mode", "ACTIVE") | ||
waf:set_option("add_ruleset_string", "10100", [=[{"access":[{"actions":{"disrupt":"DENY"},"id":"12345","operator":"REGEX","pattern":"foo","vars":[{"parse":{"values":1},"type":"REQUEST_ARGS"}]}],"body_filter":[],"header_filter":[]}]=]) | ||
waf:exec() | ||
ngx.log(ngx.INFO, "We should not see this") | ||
'; | ||
|
||
content_by_lua 'ngx.exit(ngx.HTTP_OK)'; | ||
} | ||
--- request | ||
GET /t?a=foo | ||
--- error_code: 403 | ||
--- error_log | ||
Rule action was DENY, so telling nginx to quit | ||
--- no_error_log | ||
[error] | ||
We should not see this | ||
|
||
=== TEST 2: status nondisruptive action overrides waf._deny_status | ||
--- http_config | ||
init_by_lua_block{ | ||
if (os.getenv("LRW_COVERAGE")) then | ||
runner = require "luacov.runner" | ||
runner.tick = true | ||
runner.init({savestepsize = 110}) | ||
jit.off() | ||
end | ||
} | ||
--- config | ||
location /t { | ||
access_by_lua ' | ||
local lua_resty_waf = require "resty.waf" | ||
local waf = lua_resty_waf:new() | ||
waf:set_option("debug", true) | ||
waf:set_option("mode", "ACTIVE") | ||
waf:set_option("add_ruleset_string", "10100", [=[{"access":[{"actions":{"disrupt":"DENY","nondisrupt":[{"action":"status","data":404}]},"id":"12345","operator":"REGEX","pattern":"foo","vars":[{"parse":{"values":1},"type":"REQUEST_ARGS"}]}],"body_filter":[],"header_filter":[]}]=]) | ||
waf:exec() | ||
'; | ||
|
||
content_by_lua 'ngx.exit(ngx.HTTP_OK)'; | ||
} | ||
--- request | ||
GET /t?a=foo | ||
--- error_code: 404 | ||
--- error_log | ||
Rule action was DENY, so telling nginx to quit | ||
Overriding status from 403 to 404 | ||
--- no_error_log | ||
[error] | ||
|
||
=== TEST 3: ctx.rule_status is reset every rule | ||
--- http_config | ||
init_by_lua_block{ | ||
if (os.getenv("LRW_COVERAGE")) then | ||
runner = require "luacov.runner" | ||
runner.tick = true | ||
runner.init({savestepsize = 110}) | ||
jit.off() | ||
end | ||
} | ||
--- config | ||
location /s { | ||
access_by_lua ' | ||
local lua_resty_waf = require "resty.waf" | ||
local waf = lua_resty_waf:new() | ||
waf:set_option("debug", true) | ||
waf:set_option("mode", "ACTIVE") | ||
waf:set_option("add_ruleset_string", "10200", [=[{"access":[{"actions":{"disrupt":"DENY"},"id":"12345","operator":"REGEX","pattern":"foo","vars":[{"parse":{"values":1},"type":"REQUEST_ARGS"}]}],"body_filter":[],"header_filter":[]}]=]) | ||
waf:exec() | ||
'; | ||
|
||
content_by_lua 'ngx.exit(ngx.HTTP_OK)'; | ||
} | ||
|
||
location /t { | ||
access_by_lua ' | ||
local lua_resty_waf = require "resty.waf" | ||
local waf = lua_resty_waf:new() | ||
waf:set_option("debug", true) | ||
waf:set_option("mode", "ACTIVE") | ||
waf:set_option("add_ruleset_string", "10100", [=[{"access":[{"actions":{"disrupt":"DENY","nondisrupt":[{"action":"status","data":404}]},"id":"12345","operator":"REGEX","pattern":"foo","vars":[{"parse":{"values":1},"type":"REQUEST_ARGS"}]}],"body_filter":[],"header_filter":[]}]=]) | ||
waf:exec() | ||
'; | ||
|
||
content_by_lua 'ngx.exit(ngx.HTTP_OK)'; | ||
} | ||
--- request eval | ||
["GET /t?a=foo", "GET /s?a=foo"] | ||
--- error_code eval | ||
[404, 403] | ||
--- no_error_log | ||
[error] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use Test::Nginx::Socket::Lua; | ||
|
||
repeat_each(3); | ||
plan tests => repeat_each() * 4 * blocks(); | ||
|
||
no_shuffle(); | ||
run_tests(); | ||
|
||
__DATA__ | ||
|
||
=== TEST 1: status sets ctx.rule_status | ||
--- http_config | ||
init_by_lua_block{ | ||
if (os.getenv("LRW_COVERAGE")) then | ||
runner = require "luacov.runner" | ||
runner.tick = true | ||
runner.init({savestepsize = 1}) | ||
jit.off() | ||
end | ||
} | ||
--- config | ||
location /t { | ||
content_by_lua ' | ||
local actions = require "resty.waf.actions" | ||
local ctx = {} | ||
local new_status = ngx.HTTP_NOT_FOUND | ||
actions.nondisruptive_lookup["status"]( | ||
{ _debug = true, _debug_log_level = ngx.INFO, _deny_status = ngx.HTTP_FORBIDDEN }, | ||
new_status, | ||
ctx | ||
) | ||
ngx.say(ctx.rule_status) | ||
'; | ||
} | ||
--- request | ||
GET /t | ||
--- error_code: 200 | ||
--- response_body | ||
404 | ||
--- error_log | ||
Overriding status from 403 to 404 | ||
--- no_error_log | ||
[error] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters