Skip to content

Commit eba83d4

Browse files
authored
Merge pull request #142 from gofiber/codex/2025-07-19-19-10-29
2 parents 9537b2f + 0c8cf9b commit eba83d4

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

cmd/internal/migrations/v3/common.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,40 @@ func MigrateCORSConfig(cmd *cobra.Command, cwd string, _, _ *semver.Version) err
194194
// MigrateCSRFConfig updates csrf middleware configuration fields
195195
func MigrateCSRFConfig(cmd *cobra.Command, cwd string, _, _ *semver.Version) error {
196196
replacer := strings.NewReplacer("Expiration:", "IdleTimeout:")
197-
re := regexp.MustCompile(`\s*SessionKey:\s*[^,]+,?\n`)
197+
reSession := regexp.MustCompile(`\s*SessionKey:\s*[^,]+,?\n`)
198+
reKeyLookup := regexp.MustCompile(`(\s*)KeyLookup:\s*([^,\n]+)(,?)(\n?)`)
198199
err := internal.ChangeFileContent(cwd, func(content string) string {
199200
content = replacer.Replace(content)
200-
return re.ReplaceAllString(content, "")
201+
content = reSession.ReplaceAllString(content, "")
202+
203+
content = reKeyLookup.ReplaceAllStringFunc(content, func(s string) string {
204+
sub := reKeyLookup.FindStringSubmatch(s)
205+
indent := sub[1]
206+
val := strings.TrimSpace(sub[2])
207+
comma := sub[3]
208+
newline := sub[4]
209+
210+
if uq, err := strconv.Unquote(val); err == nil {
211+
val = uq
212+
}
213+
214+
var extractor string
215+
switch {
216+
case strings.HasPrefix(val, "header:"):
217+
extractor = fmt.Sprintf("Extractor: csrf.FromHeader(%q)", strings.TrimPrefix(val, "header:"))
218+
case strings.HasPrefix(val, "form:"):
219+
extractor = fmt.Sprintf("Extractor: csrf.FromForm(%q)", strings.TrimPrefix(val, "form:"))
220+
case strings.HasPrefix(val, "query:"):
221+
extractor = fmt.Sprintf("Extractor: csrf.FromQuery(%q)", strings.TrimPrefix(val, "query:"))
222+
default:
223+
// Unsupported or insecure value (e.g. cookie) - remove
224+
return ""
225+
}
226+
227+
return fmt.Sprintf("%s%s%s%s", indent, extractor, comma, newline)
228+
})
229+
230+
return content
201231
})
202232
if err != nil {
203233
return fmt.Errorf("failed to migrate CSRF configs: %w", err)

cmd/internal/migrations/v3/common_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,31 @@ var _ = csrf.New(csrf.Config{
401401
assert.Contains(t, buf.String(), "Migrating CSRF middleware configs")
402402
}
403403

404+
func Test_MigrateCSRFConfig_KeyLookup(t *testing.T) {
405+
t.Parallel()
406+
407+
dir, err := os.MkdirTemp("", "mcsrfkl")
408+
require.NoError(t, err)
409+
defer func() { require.NoError(t, os.RemoveAll(dir)) }()
410+
411+
file := writeTempFile(t, dir, `package main
412+
import (
413+
"github.com/gofiber/fiber/v2/middleware/csrf"
414+
)
415+
var _ = csrf.New(csrf.Config{
416+
KeyLookup: "header:X-CSRF-Token",
417+
})`)
418+
419+
var buf bytes.Buffer
420+
cmd := newCmd(&buf)
421+
require.NoError(t, v3.MigrateCSRFConfig(cmd, dir, nil, nil))
422+
423+
content := readFile(t, file)
424+
assert.NotContains(t, content, "KeyLookup")
425+
assert.Contains(t, content, `Extractor: csrf.FromHeader("X-CSRF-Token")`)
426+
assert.Contains(t, buf.String(), "Migrating CSRF middleware configs")
427+
}
428+
404429
func Test_MigrateMonitorImport(t *testing.T) {
405430
t.Parallel()
406431

0 commit comments

Comments
 (0)