Skip to content

Commit

Permalink
🩹 Fix: Unintended overwritten bind variables (#2240)
Browse files Browse the repository at this point in the history
* bindMap key value should only be set when not already present

* add comment

Co-authored-by: Clark Winters <clwinters8@gmail.com>
  • Loading branch information
cwinters8 and Clark Winters committed Dec 1, 2022
1 parent 17dfcc7 commit f367916
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
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

0 comments on commit f367916

Please sign in to comment.