Skip to content

Prevent panic in NewSession function - #140

Merged
kisielk merged 3 commits into
gorilla:masterfrom
ahmdrz:master
Feb 9, 2018
Merged

kisielk merged 3 commits into
gorilla:masterfrom
ahmdrz:master

Conversation

@ahmdrz

@ahmdrz ahmdrz commented Jan 7, 2018

Copy link
Copy Markdown
Contributor

Hi gorilla team !

@kisielk

kisielk commented Jan 15, 2018

Copy link
Copy Markdown
Contributor

Where does this actually cause a panic?

@ahmdrz

ahmdrz commented Jan 15, 2018

Copy link
Copy Markdown
Contributor Author

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

kisielk commented Jan 15, 2018

Copy link
Copy Markdown
Contributor

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

Do you think you can add a test for this?

@ahmdrz

ahmdrz commented Jan 15, 2018 via email

Copy link
Copy Markdown
Contributor Author

@ahmdrz

ahmdrz commented Jan 16, 2018

Copy link
Copy Markdown
Contributor Author

Here is a simple test function.

@kisielk

kisielk commented Jan 16, 2018

Copy link
Copy Markdown
Contributor

LGTM. @elithrar for a sanity check?

@elithrar elithrar left a comment

Copy link
Copy Markdown
Contributor

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

Comment thread sessions_test.go

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

Copy link
Copy Markdown
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.

@ahmdrz ahmdrz Jan 16, 2018 •

Copy link
Copy Markdown
Contributor Author

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

ahmdrz commented Jan 16, 2018

Copy link
Copy Markdown
Contributor Author

Okay , Here you are

@ahmdrz

ahmdrz commented Jan 26, 2018

Copy link
Copy Markdown
Contributor Author

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