Skip to content

Commit 148a972

Browse files
Implement native HAProxy redirect for CAPTCHA validation
- Replace Lua-based redirect with native HAProxy 302 redirect for allow decisions - Add performance optimization by calling Lua only for ban and captcha remediations - Update both HAProxy configuration examples with the new approach - Add dedicated section explaining the performance benefits - Reduce overhead and improve scalability by minimizing Lua processing
1 parent fae3a28 commit 148a972

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

crowdsec-docs/unversioned/bouncers/haproxy_spoa.mdx

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,14 @@ frontend http-in
169169
bind *:80
170170
filter spoe engine crowdsec config /etc/haproxy/crowdsec.cfg
171171
http-request set-header X-CrowdSec-Remediation %[var(txn.crowdsec.remediation)]
172-
http-request lua.crowdsec_handle if { var(txn.crowdsec.remediation) -m found }
172+
173+
## Handle 302 redirect for successful captcha validation (native HAProxy redirect)
174+
http-request redirect code 302 location %[var(txn.crowdsec.redirect)] if { var(txn.crowdsec.remediation) -m str "allow" } { var(txn.crowdsec.redirect) -m found }
175+
176+
## Call lua script only for ban and captcha remediations (performance optimization)
177+
http-request lua.crowdsec_handle if { var(txn.crowdsec.remediation) -m str "captcha" }
178+
http-request lua.crowdsec_handle if { var(txn.crowdsec.remediation) -m str "ban" }
179+
173180
use_backend <whatever>
174181
175182
backend crowdsec-spoa
@@ -212,6 +219,25 @@ recaptcha
212219
turnstile
213220
```
214221

222+
#### Native HAProxy Redirect (Performance Optimization)
223+
224+
The HAProxy SPOA bouncer now supports native HAProxy redirects for successful CAPTCHA validation, providing better performance and reduced Lua overhead:
225+
226+
```haproxy
227+
## Handle 302 redirect for successful captcha validation (native HAProxy redirect)
228+
http-request redirect code 302 location %[var(txn.crowdsec.redirect)] if { var(txn.crowdsec.remediation) -m str "allow" } { var(txn.crowdsec.redirect) -m found }
229+
230+
## Call lua script only for ban and captcha remediations (performance optimization)
231+
http-request lua.crowdsec_handle if { var(txn.crowdsec.remediation) -m str "captcha" }
232+
http-request lua.crowdsec_handle if { var(txn.crowdsec.remediation) -m str "ban" }
233+
```
234+
235+
This approach provides:
236+
- **Native 302 redirects**: Uses HAProxy's built-in redirect functionality instead of Lua
237+
- **Performance optimization**: Lua script is only called for `ban` and `captcha` remediations
238+
- **Reduced overhead**: Eliminates unnecessary Lua processing for `allow` decisions
239+
- **Better scalability**: Native HAProxy operations are more efficient than Lua-based solutions
240+
215241
### Prometheus Metrics
216242

217243
Enable and expose metrics:
@@ -391,7 +417,13 @@ frontend test
391417
392418
http-request set-header X-CrowdSec-Remediation %[var(txn.crowdsec.remediation)] if { var(txn.crowdsec.remediation) -m found }
393419
http-request set-header X-CrowdSec-IsoCode %[var(txn.crowdsec.isocode)] if { var(txn.crowdsec.isocode) -m found }
394-
http-request lua.crowdsec_handle if { var(txn.crowdsec.remediation) -m found }
420+
421+
## Handle 302 redirect for successful captcha validation (native HAProxy redirect)
422+
http-request redirect code 302 location %[var(txn.crowdsec.redirect)] if { var(txn.crowdsec.remediation) -m str "allow" } { var(txn.crowdsec.redirect) -m found }
423+
424+
## Call lua script only for ban and captcha remediations (performance optimization)
425+
http-request lua.crowdsec_handle if { var(txn.crowdsec.remediation) -m str "captcha" }
426+
http-request lua.crowdsec_handle if { var(txn.crowdsec.remediation) -m str "ban" }
395427
396428
use_backend test_backend
397429
```

0 commit comments

Comments
 (0)