Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
Force a default session secret in development environment fixes #1067
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed May 30, 2018
1 parent 4297c7d commit d24cfe5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
8 changes: 6 additions & 2 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,12 @@ func optionsWithDefaults(opts Options) Options {
if opts.SessionStore == nil {
secret := envy.Get("SESSION_SECRET", "")
// In production a SESSION_SECRET must be set!
if opts.Env == "production" && secret == "" {
logrus.Warn("Unless you set SESSION_SECRET env variable, your session storage is not protected!")
if secret == "" {
if opts.Env == "development" || opts.Env == "test" {
secret = "buffalo-secret"
} else {
logrus.Warn("Unless you set SESSION_SECRET env variable, your session storage is not protected!")
}
}
opts.SessionStore = sessions.NewCookieStore([]byte(secret))
}
Expand Down
46 changes: 46 additions & 0 deletions options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package buffalo

import (
"net/http"
"strings"
"testing"

"github.com/gobuffalo/envy"
"github.com/stretchr/testify/require"
)

func TestOptions_NewOptions(t *testing.T) {
tests := []struct {
name string
env string
secret string
expectErr string
}{
{name: "Development doesn't fail with no secret", env: "development", secret: "", expectErr: "securecookie: the value is not valid"},
{name: "Development doesn't fail with secret set", env: "development", secret: "secrets", expectErr: "securecookie: the value is not valid"},
{name: "Test doesn't fail with secret set", env: "test", secret: "", expectErr: "securecookie: the value is not valid"},
{name: "Test doesn't fail with secret set", env: "test", secret: "secrets", expectErr: "securecookie: the value is not valid"},
{name: "Production fails with no secret", env: "production", secret: "", expectErr: "securecookie: hash key is not set"},
{name: "Production doesn't fail with secret set", env: "production", secret: "secrets", expectErr: "securecookie: the value is not valid"},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
r := require.New(t)
envy.Temp(func() {
envy.Set("GO_ENV", test.env)
envy.Set("SESSION_SECRET", test.secret)

opts := NewOptions()

req, _ := http.NewRequest("GET", "/", strings.NewReader(""))
req.AddCookie(&http.Cookie{Name: "_buffalo_session"})

_, err := opts.SessionStore.New(req, "_buffalo_session")

r.Error(err)
r.Equal(test.expectErr, err.Error())
})
})
}
}

0 comments on commit d24cfe5

Please sign in to comment.