diff --git a/lib/crowdsec.lua b/lib/crowdsec.lua index 0bb960f..1106a38 100644 --- a/lib/crowdsec.lua +++ b/lib/crowdsec.lua @@ -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" diff --git a/t/26_appsec_failure_action_deny.t b/t/26_appsec_failure_action_deny.t new file mode 100644 index 0000000..79768b5 --- /dev/null +++ b/t/26_appsec_failure_action_deny.t @@ -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 diff --git a/t/conf_t/26_appsec_failure_action_deny_crowdsec_nginx_bouncer.conf b/t/conf_t/26_appsec_failure_action_deny_crowdsec_nginx_bouncer.conf new file mode 100644 index 0000000..e816a5a --- /dev/null +++ b/t/conf_t/26_appsec_failure_action_deny_crowdsec_nginx_bouncer.conf @@ -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