feat(identity): enforce shared session and browser request security
This commit is contained in:
1 parent
7df521bd89
commit
86f562f5b6
23 files changed
+430
-121
No files matched your search
@@ -5,6 +5,8 @@ import (
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -22,32 +24,41 @@ type scriptedRegistration struct {
|
||||
err error
|
||||
register identity.RegisterCommand
|
||||
resend identity.ResendCommand
|
||||
calls int
|
||||
}
|
||||
|
||||
func (registration *scriptedRegistration) Register(_ context.Context, command identity.RegisterCommand) error {
|
||||
registration.calls++
|
||||
registration.register = command
|
||||
return registration.err
|
||||
}
|
||||
|
||||
func (registration *scriptedRegistration) ResendVerification(_ context.Context, command identity.ResendCommand) error {
|
||||
registration.calls++
|
||||
registration.resend = command
|
||||
return registration.err
|
||||
}
|
||||
|
||||
const identityTestOrigin = "https://app.example"
|
||||
|
||||
func newIdentityRouter(t *testing.T, registration v1.AccountRegistration) http.Handler {
|
||||
t.Helper()
|
||||
logger := slog.New(slog.DiscardHandler)
|
||||
healthHandler := newTestHealthHandler(func(context.Context) error { return nil })
|
||||
handler := v1.NewHandler(healthHandler, problem.NewResponder(logger), v1.WithIdentity(v1.NewIdentityHandler(registration)))
|
||||
identityHandler := v1.NewIdentityHandler(registration, v1.WithPublicOrigin(identityTestOrigin))
|
||||
handler := v1.NewHandler(healthHandler, problem.NewResponder(logger), v1.WithIdentity(identityHandler))
|
||||
return newTestRouterWithAPI(t, healthHandler, handler, logger)
|
||||
}
|
||||
|
||||
func TestRegistrationOperationsMapUsecaseOutcomes(t *testing.T) {
|
||||
registerRequest := identityRequest{
|
||||
method: http.MethodPost, path: registrationPath, body: registrationBody(validPassword, validConsent),
|
||||
headers: map[string]string{"Idempotency-Key": testIdempotencyKey},
|
||||
headers: map[string]string{"Idempotency-Key": testIdempotencyKey, "Origin": identityTestOrigin},
|
||||
}
|
||||
resendRequest := identityRequest{
|
||||
method: http.MethodPost, path: resendPath, body: `{"email":"candidate@example.com"}`,
|
||||
headers: map[string]string{"Origin": identityTestOrigin},
|
||||
}
|
||||
resendRequest := identityRequest{method: http.MethodPost, path: resendPath, body: `{"email":"candidate@example.com"}`}
|
||||
staleConsent := &identity.InvalidFieldError{Pointer: "/consent_c1/text_version", Err: errors.New("stale")}
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
@@ -105,7 +116,7 @@ func TestRegistrationPassesTheRequestToTheUsecase(t *testing.T) {
|
||||
router := newIdentityRouter(t, registration)
|
||||
response := serveIdentityRequest(t, router, identityRequest{
|
||||
method: http.MethodPost, path: registrationPath, body: registrationBody(validPassword, validConsent),
|
||||
headers: map[string]string{"Idempotency-Key": testIdempotencyKey},
|
||||
headers: map[string]string{"Idempotency-Key": testIdempotencyKey, "Origin": identityTestOrigin},
|
||||
})
|
||||
if response.Code != http.StatusAccepted {
|
||||
t.Fatalf("status = %d: %s", response.Code, response.Body.String())
|
||||
@@ -118,3 +129,78 @@ func TestRegistrationPassesTheRequestToTheUsecase(t *testing.T) {
|
||||
t.Fatalf("command = %+v, want %+v", registration.register, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistrationAndResendRejectOriginBeforeUsecase(t *testing.T) {
|
||||
for _, operation := range []struct {
|
||||
name string
|
||||
request identityRequest
|
||||
}{
|
||||
{
|
||||
name: "register",
|
||||
request: identityRequest{
|
||||
method: http.MethodPost, path: registrationPath, body: registrationBody(validPassword, validConsent),
|
||||
headers: map[string]string{"Idempotency-Key": testIdempotencyKey},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "resend",
|
||||
request: identityRequest{method: http.MethodPost, path: resendPath, body: `{"email":"candidate@example.com"}`},
|
||||
},
|
||||
} {
|
||||
for _, originCase := range []struct {
|
||||
name string
|
||||
origins []string
|
||||
headers map[string]string
|
||||
}{
|
||||
{name: "missing"},
|
||||
{name: "null origin", origins: []string{"null"}},
|
||||
{name: "foreign origin", origins: []string{"https://attacker.example"}},
|
||||
{name: "duplicate origin", origins: []string{identityTestOrigin, "https://attacker.example"}},
|
||||
{name: "forwarded host only", headers: map[string]string{"X-Forwarded-Host": "app.example"}},
|
||||
} {
|
||||
t.Run(operation.name+"/"+originCase.name, func(t *testing.T) {
|
||||
registration := &scriptedRegistration{}
|
||||
request := operation.request
|
||||
request.headers = cloneHeaders(request.headers)
|
||||
for name, value := range originCase.headers {
|
||||
request.headers[name] = value
|
||||
}
|
||||
response := serveIdentityRequestWithOrigins(t, newIdentityRouter(t, registration), request, originCase.origins)
|
||||
requireProblem(t, response, http.StatusForbidden, api.ProblemCodeCsrfCheckFailed)
|
||||
if registration.calls != 0 {
|
||||
t.Fatalf("identity usecase calls = %d, want zero before origin check", registration.calls)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func serveIdentityRequestWithOrigins(
|
||||
t *testing.T,
|
||||
router http.Handler,
|
||||
identityRequest identityRequest,
|
||||
origins []string,
|
||||
) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
request := httptest.NewRequest(identityRequest.method, identityRequest.path, strings.NewReader(identityRequest.body))
|
||||
if identityRequest.body != "" {
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
for name, value := range identityRequest.headers {
|
||||
request.Header.Set(name, value)
|
||||
}
|
||||
for _, origin := range origins {
|
||||
request.Header.Add("Origin", origin)
|
||||
}
|
||||
response := httptest.NewRecorder()
|
||||
router.ServeHTTP(response, request)
|
||||
return response
|
||||
}
|
||||
|
||||
func cloneHeaders(headers map[string]string) map[string]string {
|
||||
cloned := make(map[string]string, len(headers))
|
||||
for name, value := range headers {
|
||||
cloned[name] = value
|
||||
}
|
||||
return cloned
|
||||
}
|
||||
Reference in new issue
Block a user