Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃┕ Fix: Unintended overwritten bind variables #2240

Merged
merged 2 commits into from Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion ctx.go
Expand Up @@ -1437,7 +1437,10 @@ func (c *Ctx) renderExtensions(bind interface{}) {
// Bind view map
if c.viewBindMap != nil {
for _, v := range c.viewBindMap.D {
bindMap[v.Key] = v.Value
// make sure key does not exist already
if _, ok := bindMap[v.Key]; !ok {
bindMap[v.Key] = v.Value
}
}
}

Expand Down
23 changes: 23 additions & 0 deletions ctx_test.go
Expand Up @@ -2851,6 +2851,29 @@ func Test_Ctx_RenderWithBind(t *testing.T) {

}

func Test_Ctx_RenderWithOverwrittenBind(t *testing.T) {
t.Parallel()

app := New()
c := app.AcquireCtx(&fasthttp.RequestCtx{})

err := c.Bind(Map{
"Title": "Hello, World!",
})
utils.AssertEqual(t, nil, err)
defer app.ReleaseCtx(c)
err = c.Render("./.github/testdata/index.tmpl", Map{
"Title": "Hello from Fiber!",
})

buf := bytebufferpool.Get()
_, _ = buf.WriteString("overwrite")
defer bytebufferpool.Put(buf)

utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, "<h1>Hello from Fiber!</h1>", string(c.Response().Body()))
}

func Test_Ctx_RenderWithBindLocals(t *testing.T) {
t.Parallel()

Expand Down