Skip to content

Commit

Permalink
refactor: improve CSRF infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Aug 25, 2020
1 parent cd84e51 commit 7e367e7
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 88 deletions.
1 change: 0 additions & 1 deletion cmd/daemon/serve.go
Expand Up @@ -57,7 +57,6 @@ func servePublic(d driver.Driver, wg *sync.WaitGroup, cmd *cobra.Command, args [
c.SelfPublicURL().Hostname(),
!flagx.MustGetBool(cmd, "dev"),
)
csrf.ExemptPath(session.RouteWhoami)
r.WithCSRFHandler(csrf)
n.UseHandler(r.CSRFHandler())
server := graceful.WithDefaults(&http.Server{
Expand Down
10 changes: 2 additions & 8 deletions driver/configuration/provider_viper_test.go
Expand Up @@ -8,8 +8,6 @@ import (
"testing"
"time"

"github.com/markbates/pkger"

"github.com/ory/x/logrusx"

_ "github.com/ory/jsonschema/v3/fileloader"
Expand All @@ -28,15 +26,11 @@ import (
var schema []byte

func init() {
file, err := pkger.Open("/.schema/config.schema.json")
var err error
schema, err = ioutil.ReadFile("../../.schema/config.schema.json")
if err != nil {
panic("Unable to open configuration JSON Schema.")
}
defer file.Close()
schema, err = ioutil.ReadAll(file)
if err != nil {
panic("Unable to read configuration JSON Schema.")
}
}

func TestViperProvider(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion persistence/sql/migratest/migration_test.go
Expand Up @@ -51,7 +51,7 @@ func TestMigrations(t *testing.T) {
l := logrusx.New("", "", logrusx.ForceLevel(logrus.TraceLevel))
plog.Logger = gobuffalologger.Logrus{FieldLogger: l.Entry}

if !testing.Short() && false {
if !testing.Short() {
dockertest.Parallel([]func(){
func() {
connections["postgres"] = dockertest.ConnectToTestPostgreSQLPop(t)
Expand Down
3 changes: 3 additions & 0 deletions selfservice/strategy/password/.schema/login.schema.json
Expand Up @@ -11,6 +11,9 @@
"type": "string",
"minLength": 1
},
"csrf_token": {
"type": "string"
},
"identifier": {
"type": "string",
"minLength": 1
Expand Down
Expand Up @@ -11,6 +11,9 @@
"type": "string",
"minLength": 1
},
"csrf_token": {
"type": "string"
},
"traits": {}
}
}
19 changes: 13 additions & 6 deletions selfservice/strategy/password/login.go
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"

"github.com/julienschmidt/httprouter"
"github.com/justinas/nosurf"
"github.com/pkg/errors"

"github.com/ory/x/decoderx"
Expand All @@ -26,6 +27,7 @@ const (
)

func (s *Strategy) RegisterLoginRoutes(r *x.RouterPublic) {
s.d.CSRFHandler().ExemptPath(RouteLogin)
r.POST(RouteLogin, s.handleLogin)
}

Expand Down Expand Up @@ -103,6 +105,17 @@ func (s *Strategy) handleLogin(w http.ResponseWriter, r *http.Request, _ httprou
return
}

var p LoginFormPayload
if err := s.hd.Decode(r, &p, decoderx.MustHTTPRawJSONSchemaCompiler(loginSchema)); err != nil {
s.handleLoginError(w, r, ar, &p, err)
return
}

if ar.Type == flow.TypeBrowser && !nosurf.VerifyToken(s.d.GenerateCSRFToken(r), p.CSRFToken) {
s.handleLoginError(w, r, ar, &p, x.ErrInvalidCSRFToken)
return
}

if _, err := s.d.SessionManager().FetchFromRequest(r.Context(), r); err == nil && !ar.Forced {
if ar.Type == flow.TypeBrowser {
http.Redirect(w, r, s.c.SelfServiceBrowserDefaultReturnTo().String(), http.StatusFound)
Expand All @@ -113,12 +126,6 @@ func (s *Strategy) handleLogin(w http.ResponseWriter, r *http.Request, _ httprou
return
}

var p LoginFormPayload
if err := s.hd.Decode(r, &p, decoderx.MustHTTPRawJSONSchemaCompiler(loginSchema)); err != nil {
s.handleLoginError(w, r, ar, &p, err)
return
}

if err := ar.Valid(); err != nil {
s.handleLoginError(w, r, ar, &p, err)
return
Expand Down

0 comments on commit 7e367e7

Please sign in to comment.