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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 The session middleware performs deserialization, which will cause a crash #1051

Closed
lovesky opened this issue Dec 3, 2020 · 2 comments
Closed

Comments

@lovesky
Copy link

lovesky commented Dec 3, 2020

Fiber version
v2.2.2

Issue description
Save the session to the database, restart the program, and perform session deserialization will cause a crash.

The following is a simple test, there are two functions, the test1 is gotiny.Marshal and then gotiny.Unmarshal, and the test2 is direct gotiny.Unmarshal. There is no problem executing the two together. But if you comment out the test1 and run the test2 directly, an error will be reported: panic: unknown typ:int

Code snippet

package main

import (
	"fmt"
	"gotiny" // github.com/gofiber/fiber/v2/internal/gotiny
)

type data struct {
	d map[string]interface{}
}

func main() {
	test1()
	test2()
}

func test1() {
	a := data{d: make(map[string]interface{})}
	a.d["user_role"] = 1
	b := gotiny.Marshal(&a)
        // [3 1 9 117 115 101 114 95 114 111 108 101 3 105 110 116 2]
	fmt.Println(b)
	c := data{}
	gotiny.Unmarshal(b, &c)
        // {map[user_role:1]}
	fmt.Println(c)
}

func test2() {
	d := data{}
	e := []byte{3, 1, 9, 117, 115, 101, 114, 95, 114, 111, 108, 101, 3, 105, 110, 116, 2}
        // panic: unknown typ:int
	gotiny.Unmarshal(e, &d)
        // {map[user_role:1]}
	fmt.Println(d)
}
@Fenny
Copy link
Member

Fenny commented Dec 3, 2020

I think we need to expose the Register logic in session mw to restore / support custom structs on restart https://github.com/niubaoshu/gotiny/blob/master/register.go#L116

We can add this option to the Config where you can define custom structs. i.e.:

store := session.New(session.Config{
    RegisterType: data,
})

We also need to catch that panic by default and notify the user to use the new RegisterType option.
Do you have any advice regarding the name? Maybe we should support multiple types RegisterTypes: []interface{}?

Fenny added a commit to Fenny/fiber that referenced this issue Dec 3, 2020
Fenny added a commit that referenced this issue Dec 9, 2020
@Fenny
Copy link
Member

Fenny commented Dec 9, 2020

You can now use the following to solve this issue:

store := session.New()
store.RegisterType(User)

Available in v2.2.4 https://github.com/gofiber/fiber/releases/tag/v2.2.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants