gofiber / fiber

⚡️ Express inspired web framework written in Go
https://gofiber.io
MIT License
33.9k stars 1.67k forks source link

🐛 [Bug]: Missing ContextKey in requestid middleware configuration #2732

Closed gopkg-dev closed 11 months ago

gopkg-dev commented 11 months ago

Bug Description

This is my fix:

https://github.com/gofiber/fiber/pull/2731

-   if cfg.ContextKey == "" {
+   if cfg.ContextKey == nil || cfg.ContextKey == "" {
        cfg.ContextKey = ConfigDefault.ContextKey
    }

The fix checks if cfg.ContextKey is either nil or an empty string before assigning the default value ConfigDefault.ContextKey. This ensures that the ContextKey is not missing or empty.

How to Reproduce

package main

import (
    "github.com/gofiber/fiber/v2"
    "github.com/gofiber/fiber/v2/middleware/requestid"
    "github.com/gofiber/fiber/v2/utils"
)
import "log"

func main() {
    app := fiber.New()

    // Steps to reproduce
    app.Use(requestid.New(requestid.Config{
        // ContextKey is currently nil
        Generator: utils.UUID,
    }))

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

Expected Behavior

...

Fiber Version

v2.51.0

Code Snippet (optional)

No response

Checklist:

Z3NTL3 commented 11 months ago

ContextKey is a string i assume and a string can never be nil in Go unless its an interface{}.

So strings in Go can never be nil, but only be a zero value to their concrete type which is an empty string.

Conclusion: Invalid bug mark? plus invalid condition check; unnecessary or operator (slightly more usage on memory (also not fitting innovation of fiber and fasthttp)

@ReneWerner87 correct if its wrong, thnx ^^

ReneWerner87 commented 11 months ago

@Z3NTL3 pls check https://github.com/gofiber/fiber/pull/2731#issuecomment-1823236098