feat(identity): add server sessions and audited mail recovery
This commit is contained in:
1 parent
20c03288d6
commit
4ee53d9a74
58 files changed
+5359
-567
No files matched your search
@@ -0,0 +1,96 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"lincodex/backend/internal/domain"
|
||||
)
|
||||
|
||||
func TestSessionRequestMetadataAcceptsOneOriginAndSessionCookie(t *testing.T) {
|
||||
token, err := domain.NewSecretToken()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var origin string
|
||||
var originOK, cookieMatches bool
|
||||
var incoming domain.SecretToken
|
||||
handler := SessionRequestMetadata()(http.HandlerFunc(func(_ http.ResponseWriter, request *http.Request) {
|
||||
origin, originOK = RequestOrigin(request.Context())
|
||||
incoming = IncomingSessionToken(request.Context())
|
||||
cookieMatches = SessionCookieMatches(request.Context(), token.Reveal())
|
||||
}))
|
||||
request := httptest.NewRequest(http.MethodPost, "/api/v1/identity/sessions", nil)
|
||||
request.Header.Set("Origin", "https://app.example.com")
|
||||
request.AddCookie(&http.Cookie{Name: SessionCookieName, Value: token.Reveal()})
|
||||
handler.ServeHTTP(httptest.NewRecorder(), request)
|
||||
if !originOK || origin != "https://app.example.com" || incoming.Hash() != token.Hash() || !cookieMatches {
|
||||
t.Fatal("metadata did not preserve the sole Origin and parsed session cookie")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionRequestMetadataRejectsAmbiguousOrMalformedBrowserHeaders(t *testing.T) {
|
||||
token, err := domain.NewSecretToken()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tests := map[string]func(*http.Request){
|
||||
"duplicate cookie": func(request *http.Request) {
|
||||
request.AddCookie(&http.Cookie{Name: SessionCookieName, Value: token.Reveal()})
|
||||
request.AddCookie(&http.Cookie{Name: SessionCookieName, Value: token.Reveal()})
|
||||
},
|
||||
"malformed cookie": func(request *http.Request) {
|
||||
request.AddCookie(&http.Cookie{Name: SessionCookieName, Value: "short"})
|
||||
},
|
||||
"duplicate origin": func(request *http.Request) {
|
||||
request.Header.Add("Origin", "https://app.example.com")
|
||||
request.Header.Add("Origin", "https://attacker.example")
|
||||
},
|
||||
"forwarded origin only": func(request *http.Request) {
|
||||
request.Header.Set("X-Forwarded-Host", "app.example.com")
|
||||
},
|
||||
}
|
||||
for name, prepare := range tests {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
var origin string
|
||||
var originOK bool
|
||||
var incoming domain.SecretToken
|
||||
handler := SessionRequestMetadata()(http.HandlerFunc(func(_ http.ResponseWriter, request *http.Request) {
|
||||
origin, originOK = RequestOrigin(request.Context())
|
||||
incoming = IncomingSessionToken(request.Context())
|
||||
}))
|
||||
request := httptest.NewRequest(http.MethodPost, "/api/v1/identity/sessions", nil)
|
||||
prepare(request)
|
||||
handler.ServeHTTP(httptest.NewRecorder(), request)
|
||||
if originOK || origin != "" || !incoming.IsZero() {
|
||||
t.Fatalf("ambiguous input was accepted: origin present=%v token present=%v", originOK, !incoming.IsZero())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionCookieMatchesOnlyTheBoundaryToken(t *testing.T) {
|
||||
first, err := domain.NewSecretToken()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
second, err := domain.NewSecretToken()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/v1/identity/me", nil)
|
||||
request.AddCookie(&http.Cookie{Name: SessionCookieName, Value: first.Reveal()})
|
||||
var matches []bool
|
||||
handler := SessionRequestMetadata()(http.HandlerFunc(func(_ http.ResponseWriter, request *http.Request) {
|
||||
matches = []bool{
|
||||
SessionCookieMatches(request.Context(), first.Reveal()),
|
||||
SessionCookieMatches(request.Context(), second.Reveal()),
|
||||
SessionCookieMatches(request.Context(), "short"),
|
||||
}
|
||||
}))
|
||||
handler.ServeHTTP(httptest.NewRecorder(), request)
|
||||
if len(matches) != 3 || !matches[0] || matches[1] || matches[2] {
|
||||
t.Fatalf("SessionCookieMatches() = %v", matches)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user