Files
f02a678dff 🐛 redirect a captcha submitted again after it was solved (#428)
* 🐛 redirect a captcha submitted again after it was solved

With the captcha open in two tabs, solving it in the first tab and then
submitting it in the second sent that second POST on to the backend,
because the IP had already solved it. A backend that only accepts GET
on that page answered 405.

A form POST that carries the provider's response field, from an IP that
has already solved the captcha, is now redirected to the same URL, like
a captcha that was just solved. This covers both the LAPI and the
AppSec captcha. Only bodies with a Content-Length of at most 64 KiB are
read, and the body is restored for the next handler. Any other request
from that IP goes through as before.

Based on the idea in #374 by svmrw.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* 🍱 add testing for e2e captcha

* 🍱 fix README

* 🍱 fix README

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-authored-by: maxlerebourg <maxlerebourg@gmail.com>
2026-11-14 17:21:33 +01:00

74 lines
2.6 KiB
Go

package crowdsec_bouncer_traefik_plugin //nolint:revive,stylecheck
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
cache "github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin/pkg/cache"
captcha "github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin/pkg/captcha"
configuration "github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin/pkg/configuration"
logger "github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin/pkg/logger"
)
func TestSolvedCaptchaSubmission(t *testing.T) {
log := logger.New("ERROR", "")
cacheClient := &cache.Client{}
cacheClient.New(log, false, "", nil, "", "")
cacheClient.Set("192.0.2.1_captcha", cache.CaptchaDoneValue, 60)
captchaClient := &captcha.Client{}
if err := captchaClient.New(log, cacheClient, &http.Client{}, configuration.HcaptchaProvider, "", "", "", "", "site", "secret", "", "captcha.html", 60); err != nil {
t.Fatal(err)
}
tests := []struct {
name string
body string
appsec bool
wantCode int
wantNext bool
}{
{name: "lapi captcha submitted again", body: "h-captcha-response=token", wantCode: http.StatusFound},
{name: "lapi other form", body: "name=value", wantCode: http.StatusOK, wantNext: true},
{name: "appsec captcha submitted again", body: "h-captcha-response=token", appsec: true, wantCode: http.StatusFound},
{name: "appsec other form", body: "name=value", appsec: true, wantCode: http.StatusOK, wantNext: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var nextBody string
bouncer := &Bouncer{
next: http.HandlerFunc(func(_ http.ResponseWriter, req *http.Request) {
body, _ := io.ReadAll(req.Body)
nextBody = string(body)
}),
cacheClient: cacheClient,
captchaClient: captchaClient,
log: log,
}
req := httptest.NewRequest(http.MethodPost, "http://example.com/page?a=1", strings.NewReader(tt.body))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
rw := httptest.NewRecorder()
if tt.appsec {
if !bouncer.handleAppsecDecisionServeHTTP(rw, req, "192.0.2.1", &AppSecResponse{Action: appsecCaptchaAction}) {
bouncer.next.ServeHTTP(rw, req)
}
} else {
bouncer.handleRemediationServeHTTP(rw, req, "192.0.2.1", cache.CaptchaValue)
}
if rw.Code != tt.wantCode {
t.Errorf("status = %d, want %d", rw.Code, tt.wantCode)
}
if tt.wantCode == http.StatusFound && rw.Header().Get("Location") != "http://example.com/page?a=1" {
t.Errorf("Location = %q", rw.Header().Get("Location"))
}
if tt.wantNext != (nextBody == tt.body) {
t.Errorf("next got body %q, want next called: %v", nextBody, tt.wantNext)
}
})
}
}