Skip to content

Commit ea608ae

Browse files
authored
fix(middleware): reject CSRF TokenLookup that produces no extractors in v4 (#3149)
A TokenLookup whose sources are all unknown (for example "nope:nope") parsed without error but yielded zero extractors. With nothing to check, unsafe requests were let through without any token validation. Fail middleware creation instead, so the misconfiguration surfaces at startup. Backport of #3067.
1 parent 600b985 commit ea608ae

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

‎middleware/csrf.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package middleware
55

66
import (
77
"crypto/subtle"
8+
"errors"
89
"net/http"
910
"slices"
1011
"strings"
@@ -165,6 +166,9 @@ func (config CSRFConfig) ToMiddleware() (echo.MiddlewareFunc, error) {
165166
if cErr != nil {
166167
return nil, cErr
167168
}
169+
if len(extractors) == 0 {
170+
return nil, errors.New("echo csrf middleware could not create extractors from TokenLookup string")
171+
}
168172

169173
return func(next echo.HandlerFunc) echo.HandlerFunc {
170174
return func(c echo.Context) error {

‎middleware/csrf_test.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@ func TestCSRF_tokenExtractors(t *testing.T) {
156156
givenQueryTokens: map[string][]string{},
157157
expectToMiddlewareError: "extractor source for lookup could not be split into needed parts: q",
158158
},
159+
{
160+
name: "nok, TokenLookup with only unknown source yields no extractors",
161+
whenTokenLookup: "nope:nope",
162+
givenCSRFCookie: "token",
163+
givenMethod: http.MethodPut,
164+
givenQueryTokens: map[string][]string{},
165+
expectToMiddlewareError: "echo csrf middleware could not create extractors from TokenLookup string",
166+
},
159167
}
160168

161169
for _, tc := range testCases {

0 commit comments

Comments
 (0)