Fiber: 馃悰 CSRF cookie is still SameSite=Lax when receiving the config

Created on 30 Sep 2020  路  6Comments  路  Source: gofiber/fiber

Fiber version
2.0.4
Issue description

Code snippet

csrf.New() // Strict
csrf.New(csrf.Config{}}) // Lax
csrf.New(csrf.Config{Cookie: &fiber.Cookie{Path: "/"}}) // Lax
鈽笍 Bug 馃彿 Wait for Release

Most helpful comment

Here is a repro.

// v2.0.4
    var app = fiber.New()
    app.Use("/signup", csrf.New(csrf.Config{Cookie: &fiber.Cookie{Path: "/signup"}}))
    app.Get("/signup", func(c *fiber.Ctx) error {
        c.Type("html")
        return c.Send([]byte("<h1>Signup</h1>"))
    })
    app.Listen(":" + env.Port)
Set-Cookie: 9572ced8-1be6-4a53-8c00-12a66cb5bc03; expires=Sat, 03 Oct 2020 01:49:05 GMT; path=/signup; SameSite=Lax

All 6 comments

Nice catch @falsandtru, fixed in https://github.com/gofiber/fiber/pull/860

Looks like the other defaults such as the name are lost too.

Set-Cookie: 26db76a7-1693-4070-a877-b681f87108ee; expires=Thu, 01 Oct 2020 13:46:51 GMT; path=/; SameSite=Lax

Seems to work fine on my end, by any chance you could share your example to reproduce?

Set-Cookie: _csrf=dad426b8-1054-4c14-89f5-1e6ce0159415; expires=Fri, 02 Oct 2020 21:21:37 GMT; path=/; SameSite=Strict

Here is a repro.

// v2.0.4
    var app = fiber.New()
    app.Use("/signup", csrf.New(csrf.Config{Cookie: &fiber.Cookie{Path: "/signup"}}))
    app.Get("/signup", func(c *fiber.Ctx) error {
        c.Type("html")
        return c.Send([]byte("<h1>Signup</h1>"))
    })
    app.Listen(":" + env.Port)
Set-Cookie: 9572ced8-1be6-4a53-8c00-12a66cb5bc03; expires=Sat, 03 Oct 2020 01:49:05 GMT; path=/signup; SameSite=Lax

Could anyone reproduce it?

Able to reproduce. This is due to this line in csrf.New:

        if cfg.Cookie == nil {
            cfg.Cookie = ConfigDefault.Cookie
            if cfg.Cookie.Name == "" {
                cfg.Cookie.Name = "_csrf"
            }
            if cfg.Cookie.SameSite == "" {
                cfg.Cookie.SameSite = "Strict"
            }
        }

If you did pass a cookie, it expects all fields to be there since cfg.Cookie == nil will evaluate to true. I'll make a PR.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Badrouu17 picture Badrouu17  路  4Comments

renatojf picture renatojf  路  3Comments

lucasmdomingues picture lucasmdomingues  路  3Comments

mhf-ir picture mhf-ir  路  3Comments

faultable picture faultable  路  3Comments