Skip to content

🐛 [Bug]: CSRF middleware default ContextKey and PassLocalsToViews #2682

@ly020044

Description

@ly020044

Bug Description

I'm using csrf middleware with v2.49.2 is worked. But after upgrade to v2.50.0, Errors will occur when visit any page.

failed to render: [Error (where: checkForValidIdentifiers)] context-key 'fiber.csrf.handler' (value: '&{config:0x14001198a00 sessionManager:<nil> storageManager:0x14000842e80}') is not a valid identifier

If change csrf config HandlerContextKey="csrf", is worked. Because HandlerContextKey is default value fiber.csrf.handle, So the default value contains the . character. .xx is usually used in templates to access properties or methods.

How to Reproduce

Steps to reproduce the behavior:

  1. Set fiber.Config PassLocalsToViews: true
  2. Use django html engine.
  3. Use CSRF middleware

Expected Behavior

Can visit all pages.

Fiber Version

v2.50.0

Code Snippet (optional)

package main

import (
	"log"

	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/middleware/csrf"
	"github.com/gofiber/template/django/v3"
)

func main() {
	engine := django.New("./views", ".html")

	// Pass the engine to the Views
	app := fiber.New(fiber.Config{
		Views:             engine,
		PassLocalsToViews: true,
	})

	app.Use(csrf.New())

	app.Get("/", func(c *fiber.Ctx) error {
		// Render index
		return c.Render("index", fiber.Map{
			"Title": "Hello, World!",
		})
	})

	log.Fatal(app.Listen(":3000"))
}

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my problem prior to opening this one.
  • I understand that improperly formatted bug reports may be closed without explanation.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions