-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(requestid): fix condition for setting default value of ContextKey… #2731
Conversation
… in configDefault function The condition for setting the default value of ContextKey in the configDefault function has been fixed. Previously, it only checked if cfg.ContextKey was an empty string, but now it also checks if it is nil. This ensures that the default value is set correctly even if cfg.ContextKey is not explicitly provided.
Thanks for opening this pull request! 🎉 Please check out our contributing guidelines. If you need help or want to chat with us, join us on Discord https://gofiber.io/discord |
@@ -57,7 +57,7 @@ func configDefault(config ...Config) Config { | |||
if cfg.Generator == nil { | |||
cfg.Generator = ConfigDefault.Generator | |||
} | |||
if cfg.ContextKey == "" { | |||
if cfg.ContextKey == nil || cfg.ContextKey == "" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ContextKey will only ever be "" if the user explicitly sets it to that value. In that case, overriding it to be "requestid" might be surprising.
Though I suppose changing it to accept "" now might break someone's code out there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be fine, check this #2731 (comment)
@gaby @efectn @gopkg-dev @Z3NTL3 @nickajacks1 @sixcolors @benjajaja sorry for that, but i think it would be better to go back to the previous state |
I feel like I've seen context keys as types/global vars before, something like type contextKey int
const (
contextKeyRequestId contextKey = iota
contextKeySession
// etc...
) For this PR, if it was me, I'd probably keep it as an interface, do a nil check and leave it at that. |
Will review tonight or tomorrow night. |
I have looked on GoDoc and I've seen that But there is one more thing, I've looked into the middleware code. And I've seen there is no appropiate type checking. Because an
|
@Z3NTL3 whats your opinion on this #2731 (comment) |
revert |
Revert. String zero value used for default is behavior elsewhere too. And follow up discussions can be held to adopt a universal approach when agreed upon. |
If the package main
import (
"log"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/requestid"
)
func main() {
app := fiber.New()
// Current implementation
app.Use(requestid.New(requestid.Config{
ContextKey: requestid.ConfigDefault.ContextKey,
Generator: util.NewXID,
}))
// Expected behavior
app.Use(requestid.New(requestid.Config{
Generator: util.NewXID,
}))
log.Fatal(app.Listen(":3000"))
} |
When viewing the GoDoc we can see ContextKey is an I've looked into the middleware source. And I've seen there is no appropiate type checking. Because an ``interface{} type in Go can be anything, when the users passes an object thats very off, like a struct or map it will put this object as a key to c.Locals which will result in unexpected behaviour and leak something bad off. There should be a check with reflect package to check if the given ContextKey is a string or int, anything else should set configuration to default. The reason for this is - Explained
So its better to revert it back to be a string instead of an interface, to avoid overloads in different validating parts and memory leaks. In that matter, your PR becomes sadly said invalid. Idk what the maintainers will do. |
When we examine this method from We can see that this method converts anything to a byte representation. So when we allow an interface the user may pass anything. When a struct or map is passed for example then it will be represented like:
So this is the ContextKey, a string including the byte representation of the struct Thats so illegal, the performance will tear down so hard. Some very illegal behaviour. For those reasons, reverting the |
I do agree. We should keep parameters/fields as type-safe as possible. |
closed because of #2742 |
This is not correct, at all: The std package
So context keys should never be restricted to strings by libraries, and both fiber and fasthttp correctly follow this and allow The requestid middleware cannot not be an exception to the The revert should be reverted, but the condition should be changed to a simple |
Just because the methodic which is used inside supports all types, this does not have to be used outside for the key |
… in configDefault function
The condition for setting the default value of ContextKey in the configDefault function has been fixed. Previously, it only checked if cfg.ContextKey was an empty string, but now it also checks if it is nil. This ensures that the default value is set correctly even if cfg.ContextKey is not explicitly provided.
Description
Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.
Explain the details for making this change. What existing problem does the pull request solve?
Fixes # (issue)
Type of change
Please delete options that are not relevant.
Checklist:
Commit formatting:
Use emojis on commit messages so it provides an easy way of identifying the purpose or intention of a commit. Check out the emoji cheatsheet here: https://gitmoji.carloscuesta.me/