Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/crowdsec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,8 @@ function csmod.AppSecCheck(ip)
if runtime.conf["APPSEC_FAILURE_ACTION"] == DENY then
ok = false
remediation = runtime.conf["FALLBACK_REMEDIATION"]
-- no status from the appsec: let the remediation use its own (RET_CODE for ban)
status_code = nil
end

local method = "GET"
Expand Down
138 changes: 138 additions & 0 deletions t/26_appsec_failure_action_deny.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
use Test::Nginx::Socket 'no_plan';

run_tests();

__DATA__

=== TEST 1: APPSEC_FAILURE_ACTION=deny returns 403 when the appsec is unreachable

--- main_config
load_module /usr/share/nginx/modules/ndk_http_module.so;
load_module /usr/share/nginx/modules/ngx_http_lua_module.so;

--- http_config

lua_package_path './lib/?.lua;;';
lua_shared_dict crowdsec_cache 50m;
lua_ssl_trusted_certificate /etc/ssl/certs/ca-certificates.crt;


init_by_lua_block
{
cs = require "crowdsec"
local ok, err = cs.init("./t/conf_t/26_appsec_failure_action_deny_crowdsec_nginx_bouncer.conf", "crowdsec-nginx-bouncer/v1.0.8")
if ok == nil then
ngx.log(ngx.ERR, "[Crowdsec] " .. err)
error()
end
ngx.log(ngx.ALERT, "[Crowdsec] Initialisation done")
}

access_by_lua_block {
local cs = require "crowdsec"
cs.Allow(ngx.var.remote_addr)
}

server {
listen 8081;

location = /v1/decisions {
content_by_lua_block {
ngx.print('null')
}
}
}


--- config


location = /t {
set_real_ip_from 127.0.0.1;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
content_by_lua_block {
ngx.say("Hello, world")
}
}

--- more_headers
X-Forwarded-For: 1.1.1.2

--- request
GET /t

--- error_code: 403



=== TEST 2: APPSEC_FAILURE_ACTION=deny returns 403 when the appsec answers with an error

--- main_config
load_module /usr/share/nginx/modules/ndk_http_module.so;
load_module /usr/share/nginx/modules/ngx_http_lua_module.so;

--- http_config

lua_package_path './lib/?.lua;;';
lua_shared_dict crowdsec_cache 50m;
lua_ssl_trusted_certificate /etc/ssl/certs/ca-certificates.crt;


init_by_lua_block
{
cs = require "crowdsec"
local ok, err = cs.init("./t/conf_t/26_appsec_failure_action_deny_crowdsec_nginx_bouncer.conf", "crowdsec-nginx-bouncer/v1.0.8")
if ok == nil then
ngx.log(ngx.ERR, "[Crowdsec] " .. err)
error()
end
ngx.log(ngx.ALERT, "[Crowdsec] Initialisation done")
}

access_by_lua_block {
local cs = require "crowdsec"
cs.Allow(ngx.var.remote_addr)
}

server {
listen 8081;

location = /v1/decisions {
content_by_lua_block {
ngx.print('null')
}
}
}

server {
listen 7422;

location / {
content_by_lua_block {
ngx.status = 500
ngx.print('internal error')
}
}
}


--- config


location = /t {
set_real_ip_from 127.0.0.1;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
content_by_lua_block {
ngx.say("Hello, world")
}
}

--- more_headers
X-Forwarded-For: 1.1.1.2

--- request
GET /t

--- error_code: 403
20 changes: 20 additions & 0 deletions t/conf_t/26_appsec_failure_action_deny_crowdsec_nginx_bouncer.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
APPSEC_URL=http://127.0.0.1:7422
ENABLED=true
API_URL=http://127.0.0.1:8081
API_KEY=test_key
CACHE_EXPIRATION=1
BOUNCING_ON_TYPE=all
FALLBACK_REMEDIATION=ban
REQUEST_TIMEOUT=3000
UPDATE_FREQUENCY=10
MODE=live
EXCLUDE_LOCATION=
BAN_TEMPLATE_PATH=./ban
REDIRECT_LOCATION=
RET_CODE=
CAPTCHA_PROVIDER=
SECRET_KEY=
SITE_KEY=
CAPTCHA_TEMPLATE_PATH=/var/lib/crowdsec/lua/templates/captcha.html
CAPTCHA_EXPIRATION=3600
APPSEC_FAILURE_ACTION=deny
Loading