mirror of
https://github.com/potats0/lua-resty-coraza.git
synced 2025-11-16 09:21:56 +03:00
chore: updated the code
This commit is contained in:
231
t/request.t
Normal file
231
t/request.t
Normal file
@@ -0,0 +1,231 @@
|
||||
use Test::Nginx::Socket 'no_plan';
|
||||
|
||||
our $HttpConfig = <<'_EOC_';
|
||||
lua_package_path "lib/?.lua;/usr/local/share/lua/5.1/?.lua;;";
|
||||
lua_socket_log_errors off;
|
||||
lua_need_request_body on;
|
||||
_EOC_
|
||||
|
||||
our $LocationConfig = <<'_EOC_';
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local coraza = require "resty.coraza"
|
||||
local waf = coraza.create_waf()
|
||||
if waf then
|
||||
ngx.say("done")
|
||||
end
|
||||
|
||||
}
|
||||
}
|
||||
_EOC_
|
||||
|
||||
add_block_preprocessor(sub {
|
||||
my ($block) = @_;
|
||||
|
||||
if (!defined $block->request) {
|
||||
$block->set_value("request", "GET /t");
|
||||
}
|
||||
});
|
||||
|
||||
no_shuffle();
|
||||
no_long_string();
|
||||
no_root_location();
|
||||
run_tests();
|
||||
|
||||
__DATA__
|
||||
|
||||
=== TEST 1: sanity
|
||||
--- http_config eval: $::HttpConfig
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local request = require "resty.coraza.request"
|
||||
if request then
|
||||
ngx.say("done")
|
||||
end
|
||||
}
|
||||
}
|
||||
--- error_code: 200
|
||||
--- response_body_like eval
|
||||
"done"
|
||||
|
||||
=== TEST 2: build_and_process_header
|
||||
--- http_config eval: $::HttpConfig
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local request = require "resty.coraza.request"
|
||||
local coraza = require "resty.coraza.coraza"
|
||||
local waf = coraza.new_waf()
|
||||
local tran = coraza.new_transaction(waf)
|
||||
request.build_and_process_header(tran)
|
||||
ngx.say("done")
|
||||
}
|
||||
}
|
||||
|
||||
--- more_headers
|
||||
aaa: bbbb
|
||||
cc:dd
|
||||
cc:ee
|
||||
--- error_code: 200
|
||||
--- response_body_like eval
|
||||
"done"
|
||||
--- error_log eval
|
||||
[
|
||||
"success to invoke coraza_add_request_header with aaa:bbbb",
|
||||
"potentially has HPP",
|
||||
"coraza_add_request_header with cc:dd",
|
||||
"coraza_add_request_header with cc:ee"
|
||||
]
|
||||
|
||||
=== TEST 3: build_and_process_body with simple body
|
||||
--- http_config eval: $::HttpConfig
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local request = require "resty.coraza.request"
|
||||
local coraza = require "resty.coraza.coraza"
|
||||
local waf = coraza.new_waf()
|
||||
local tran = coraza.new_transaction(waf)
|
||||
request.build_and_process_body(tran)
|
||||
ngx.say("done")
|
||||
}
|
||||
}
|
||||
--- request
|
||||
POST /t
|
||||
aaa=bbb
|
||||
--- error_code: 200
|
||||
--- response_body_like eval
|
||||
"done"
|
||||
--- error_log eval
|
||||
[
|
||||
"success to invoke coraza_append_request_body with aaa=bbb",
|
||||
"success to invoke coraza_process_request_body"
|
||||
]
|
||||
|
||||
=== TEST 4: build_and_process_body with Json body
|
||||
--- http_config eval: $::HttpConfig
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local request = require "resty.coraza.request"
|
||||
local coraza = require "resty.coraza.coraza"
|
||||
local waf = coraza.new_waf()
|
||||
local tran = coraza.new_transaction(waf)
|
||||
request.build_and_process_body(tran)
|
||||
ngx.say("done")
|
||||
}
|
||||
}
|
||||
|
||||
--- more_headers
|
||||
content-type: application/json
|
||||
--- request
|
||||
POST /t
|
||||
[{
|
||||
"msg": "Blocked User-Agent",
|
||||
"data": "User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.44",
|
||||
}]
|
||||
--- error_code: 200
|
||||
--- response_body_like eval
|
||||
"done"
|
||||
--- error_log eval
|
||||
[
|
||||
"Blocked User-Agent",
|
||||
"User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7",
|
||||
"success to invoke coraza_process_request_body"
|
||||
]
|
||||
|
||||
=== TEST 5: build_and_process_body with multipart-formdata
|
||||
--- http_config eval: $::HttpConfig
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local request = require "resty.coraza.request"
|
||||
local coraza = require "resty.coraza.coraza"
|
||||
local waf = coraza.new_waf()
|
||||
local tran = coraza.new_transaction(waf)
|
||||
request.build_and_process_body(tran)
|
||||
ngx.say("done")
|
||||
}
|
||||
}
|
||||
|
||||
--- more_headers
|
||||
Content-Type: multipart/form-data; boundary=boundary1234567890
|
||||
--- request
|
||||
POST /t/upload
|
||||
--boundary1234567890
|
||||
Content-Disposition: form-data; name="username"
|
||||
|
||||
John Doe
|
||||
--boundary1234567890
|
||||
Content-Disposition: form-data; name="profile_pic"; filename="pic.jpg"
|
||||
Content-Type: image/jpeg
|
||||
|
||||
[Binary data goes here]
|
||||
--boundary1234567890
|
||||
Content-Disposition: form-data; name="email"
|
||||
|
||||
john.doe@example.com
|
||||
--boundary1234567890--
|
||||
--- error_code: 200
|
||||
--- response_body_like eval
|
||||
"done"
|
||||
--- error_log eval
|
||||
[
|
||||
"success to invoke coraza_append_request_body with --boundary1234567890",
|
||||
"--boundary1234567890",
|
||||
"success to invoke coraza_process_request_body"
|
||||
]
|
||||
|
||||
=== TEST 6: build_and_process_body with none body
|
||||
--- http_config eval: $::HttpConfig
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local request = require "resty.coraza.request"
|
||||
local coraza = require "resty.coraza.coraza"
|
||||
local waf = coraza.new_waf()
|
||||
local tran = coraza.new_transaction(waf)
|
||||
request.build_and_process_body(tran)
|
||||
ngx.say("done")
|
||||
}
|
||||
}
|
||||
|
||||
--- request
|
||||
POST /t/upload
|
||||
--- error_code: 200
|
||||
--- response_body_like eval
|
||||
"done"
|
||||
--- error_log eval
|
||||
[
|
||||
"success to invoke coraza_process_request_body"
|
||||
]
|
||||
|
||||
=== TEST 7: build_and_process_get_args
|
||||
--- http_config eval: $::HttpConfig
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local request = require "resty.coraza.request"
|
||||
local coraza = require "resty.coraza.coraza"
|
||||
local waf = coraza.new_waf()
|
||||
local tran = coraza.new_transaction(waf)
|
||||
request.build_and_process_get_args(tran)
|
||||
ngx.say("done")
|
||||
}
|
||||
}
|
||||
|
||||
--- request
|
||||
POST /t/upload?aaa=bbb&ccc=ddd&eee=fff&eee=ggg
|
||||
--- error_code: 200
|
||||
--- response_body_like eval
|
||||
"done"
|
||||
--- error_log eval
|
||||
[
|
||||
"success to invoke coraza_add_get_args with aaa:bbb",
|
||||
"http get args potentially has HPP!",
|
||||
"with eee:fff",
|
||||
"with eee:ggg",
|
||||
"with ccc:ddd"
|
||||
]
|
||||
|
||||
142
t/response.t
Normal file
142
t/response.t
Normal file
@@ -0,0 +1,142 @@
|
||||
use Test::Nginx::Socket 'no_plan';
|
||||
|
||||
our $HttpConfig = <<'_EOC_';
|
||||
lua_package_path "lib/?.lua;/usr/local/share/lua/5.1/?.lua;;";
|
||||
lua_socket_log_errors off;
|
||||
lua_need_request_body on;
|
||||
_EOC_
|
||||
|
||||
our $LocationConfig = <<'_EOC_';
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local coraza = require "resty.coraza"
|
||||
local waf = coraza.create_waf()
|
||||
if waf then
|
||||
ngx.say("done")
|
||||
end
|
||||
|
||||
}
|
||||
}
|
||||
_EOC_
|
||||
|
||||
add_block_preprocessor(sub {
|
||||
my ($block) = @_;
|
||||
|
||||
if (!defined $block->request) {
|
||||
$block->set_value("request", "GET /t");
|
||||
}
|
||||
});
|
||||
|
||||
no_shuffle();
|
||||
no_long_string();
|
||||
no_root_location();
|
||||
run_tests();
|
||||
|
||||
__DATA__
|
||||
|
||||
=== TEST 1: sanity
|
||||
--- http_config eval: $::HttpConfig
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local coraza = require "resty.coraza.response"
|
||||
if coraza then
|
||||
ngx.say("done")
|
||||
end
|
||||
}
|
||||
}
|
||||
--- error_code: 200
|
||||
--- response_body_like eval
|
||||
"done"
|
||||
|
||||
=== TEST 2: clear_header_as_body_modified
|
||||
--- http_config eval: $::HttpConfig
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local coraza = require "resty.coraza.response"
|
||||
coraza.clear_header_as_body_modified()
|
||||
ngx.say("done")
|
||||
}
|
||||
}
|
||||
--- error_code: 200
|
||||
--- response_body_like eval
|
||||
"done"
|
||||
|
||||
=== TEST 3: build_and_process_body
|
||||
--- http_config eval: $::HttpConfig
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local coraza = require "resty.coraza.coraza"
|
||||
local waf = coraza.new_waf()
|
||||
local tran = coraza.new_transaction(waf)
|
||||
ngx.ctx.tran = tran
|
||||
ngx.say("aaaaaaaaaaaaaaaaaaaa")
|
||||
ngx.say("bbbbbbbbbbbbbbbbbbbb")
|
||||
}
|
||||
|
||||
body_filter_by_lua_block {
|
||||
local response = require "resty.coraza.response"
|
||||
response.build_and_process_body(ngx.ctx.tran)
|
||||
}
|
||||
}
|
||||
--- error_code: 200
|
||||
--- error_log eval
|
||||
["success to invoke coraza_append_response_body with aaaaaaaaaaaaaaaaaaaa",
|
||||
"success to invoke coraza_append_response_body with bbbbbbbbbbbbbbbbbbbb",
|
||||
"success to invoke coraza_process_response_body"]
|
||||
|
||||
=== TEST 4: build_and_process_header
|
||||
--- http_config eval: $::HttpConfig
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local coraza = require "resty.coraza.coraza"
|
||||
local waf = coraza.new_waf()
|
||||
local tran = coraza.new_transaction(waf)
|
||||
ngx.ctx.tran = tran
|
||||
ngx.header.aa="bb"
|
||||
ngx.header.cc={"dd", "ee"}
|
||||
}
|
||||
|
||||
body_filter_by_lua_block {
|
||||
local response = require "resty.coraza.response"
|
||||
response.build_and_process_header(ngx.ctx.tran)
|
||||
}
|
||||
}
|
||||
--- error_code: 200
|
||||
--- response_headers
|
||||
aa:bb
|
||||
cc:dd, ee
|
||||
--- error_log eval
|
||||
["success to invoke coraza_add_response_header with aa:bb",
|
||||
"with cc:dd", "with cc:ee"]
|
||||
|
||||
=== TEST 5: build_and_process_header with http 0.9
|
||||
--- http_config eval: $::HttpConfig
|
||||
--- config
|
||||
location /t {
|
||||
content_by_lua_block {
|
||||
local coraza = require "resty.coraza.coraza"
|
||||
local waf = coraza.new_waf()
|
||||
local tran = coraza.new_transaction(waf)
|
||||
ngx.ctx.tran = tran
|
||||
ngx.header.aa="bb"
|
||||
ngx.header.cc={"dd", "ee"}
|
||||
}
|
||||
|
||||
body_filter_by_lua_block {
|
||||
local response = require "resty.coraza.response"
|
||||
response.build_and_process_header(ngx.ctx.tran)
|
||||
}
|
||||
}
|
||||
|
||||
--- request
|
||||
GET /t HTTP/1.0
|
||||
--- error_code: 200
|
||||
--- response_headers
|
||||
aa:bb
|
||||
cc:dd, ee
|
||||
--- error_log eval
|
||||
["coraza_process_response_headers with 200 HTTP/1.0"]
|
||||
@@ -123,7 +123,7 @@ location /t {
|
||||
}
|
||||
--- error_code: 200
|
||||
--- error_log eval
|
||||
["Success to creat new transaction id"]
|
||||
["Success to creat new transaction"]
|
||||
|
||||
=== TEST 7: test new_transaction with nil waf pointer
|
||||
--- http_config eval: $::HttpConfig
|
||||
@@ -137,7 +137,7 @@ location /t {
|
||||
}
|
||||
--- error_code: 200
|
||||
--- error_log eval
|
||||
["Failed to creat new transaction id"]
|
||||
["Failed to creat new transaction"]
|
||||
|
||||
=== TEST 8: test process_connection
|
||||
--- http_config eval: $::HttpConfig
|
||||
|
||||
Reference in New Issue
Block a user