Skip to content

Prevent panic in NewSession function #140

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

Merged
merged 3 commits into from
Feb 9, 2018
Merged

Conversation

ahmdrz
Copy link
Contributor

@ahmdrz ahmdrz commented Jan 7, 2018

Hi gorilla team !

@kisielk
Copy link
Contributor

kisielk commented Jan 15, 2018

Where does this actually cause a panic?

@ahmdrz
Copy link
Contributor Author

ahmdrz commented Jan 15, 2018

Here is an example :

s := sessions.NewSession(store, "session-name")
s.Save(r, w)

On Line 112 in store.go

// Save adds a single session to the response.
func (s *CookieStore) Save(r *http.Request, w http.ResponseWriter,
	session *Session) error {
	encoded, err := securecookie.EncodeMulti(session.Name(), session.Values,
		s.Codecs...)
	if err != nil {
		return err
	}
	http.SetCookie(w, NewCookie(session.Name(), encoded, session.Options))
	return nil
}

Is session.Options point to null , Panic occurred :

invalid memory address or nil pointer dereference

@kisielk
Copy link
Contributor

kisielk commented Jan 15, 2018

Hm interesting that nobody noticed this before, I guess it only affects CookieStore.

Do you think you can add a test for this?

@ahmdrz
Copy link
Contributor Author

ahmdrz commented Jan 15, 2018 via email

@ahmdrz
Copy link
Contributor Author

ahmdrz commented Jan 16, 2018

Here is a simple test function.

@kisielk
Copy link
Contributor

kisielk commented Jan 16, 2018

LGTM. @elithrar for a sanity check?

Copy link
Contributor

@elithrar elithrar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Need to update the test so that it fails under the current ref first
  • Update the test name to scope it further - e.g. TestCookieStoreMapPanic


err = session.Save(req, w)
if err != nil {
t.Fatal("failed to save session", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test should "fail" in the current revision: the panic will cause the test not to run as it won't get to this stage. You should use a deferred recover() to "catch" any potential panic and then call t.Fatal.

Copy link
Contributor Author

@ahmdrz ahmdrz Jan 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test should "fail" in the current revision

Your right , But in my PR, there is no failure because I fixed bug.

@elithrar elithrar self-assigned this Jan 16, 2018
@elithrar elithrar added the bug label Jan 16, 2018
@ahmdrz
Copy link
Contributor Author

ahmdrz commented Jan 16, 2018

Okay , Here you are

@ahmdrz
Copy link
Contributor Author

ahmdrz commented Jan 26, 2018

Hello ?

@kisielk kisielk merged commit 6ba88b7 into gorilla:master Feb 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants