Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions authkit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,38 @@ func TestSocialCallbackConsumesFlow(t *testing.T) {
}
}

func TestHTTPSocialFlowCookieCannotBeSetBySiblingSubdomain(t *testing.T) {
m := newMemory()
a, err := New(context.Background(), Config{BaseURL: "https://terminal.azdelphi.com", Storage: Storage{Users: m, Sessions: sessionMemory{m}, Flows: m, SocialAccounts: m}, EmailAndPassword: EmailAndPassword{Enabled: true}})
if err != nil {
t.Fatal(err)
}
a.Social = identity.NewSocialLoginService(m, a.Sessions)
a.providers[social.Google] = fakeSocial{}
handler := a.Handler()
start := request(handler, http.MethodGet, "/api/auth/sign-in/social/google", "")
if start.Code != http.StatusFound {
t.Fatalf("start: %d %s", start.Code, start.Body.String())
}
var flow *http.Cookie
for _, c := range start.Result().Cookies() {
if c.Name == "__Host-"+flowCookieName(social.Google) {
flow = c
}
}
if flow == nil || flow.Value == "" || !flow.Secure || !flow.HttpOnly || flow.Path != "/" || flow.Domain != "" {
t.Fatalf("HTTPS flow must be host-bound: %+v", flow)
}
legacy := *flow
legacy.Name = flowCookieName(social.Google)
if got := request(handler, http.MethodGet, "/api/auth/callback/google?state=state&code=code", "", &legacy); got.Code != http.StatusBadRequest {
t.Fatalf("parent-domain cookie accepted: %d", got.Code)
}
if got := request(handler, http.MethodGet, "/api/auth/callback/google?state=state&code=code", "", flow); got.Code != http.StatusSeeOther {
t.Fatalf("host-bound cookie rejected: %d %s", got.Code, got.Body.String())
}
}

func TestSocialSignInRequiresConfiguredMFA(t *testing.T) {
m := newMemory()
signer, _, err := eddsa.Generate()
Expand Down
19 changes: 16 additions & 3 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func (a *Auth) finishSocial(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r)
return
}
cookie, err := r.Cookie(flowCookieName(name))
cookie, err := r.Cookie(a.flowCookieName(name))
a.setFlowCookie(w, name, "", -1, name == social.Apple)
if err != nil {
http.Error(w, "invalid callback", http.StatusBadRequest)
Expand Down Expand Up @@ -442,13 +442,26 @@ func (a *Auth) clearMFAChallenge(w http.ResponseWriter) {

func flowCookieName(provider string) string { return "authkit.oauth." + provider }

func (a *Auth) flowCookieName(provider string) string {
if a.secure {
return "__Host-" + flowCookieName(provider)
}
return flowCookieName(provider)
}

func (a *Auth) setFlowCookie(w http.ResponseWriter, provider, value string, age int, crossSite bool) {
sameSite := http.SameSiteLaxMode
if crossSite {
sameSite = http.SameSiteNoneMode
}
http.SetCookie(w, &http.Cookie{Name: flowCookieName(provider), Value: value,
Path: a.basePath + "/callback/" + provider, HttpOnly: true, Secure: a.secure,
path := a.basePath + "/callback/" + provider
if a.secure {
// __Host- cookies must be Secure, host-only, and scoped to /. A
// sibling public site must not be able to replace an OAuth flow.
path = "/"
}
http.SetCookie(w, &http.Cookie{Name: a.flowCookieName(provider), Value: value,
Path: path, HttpOnly: true, Secure: a.secure,
SameSite: sameSite, MaxAge: age})
}

Expand Down
Loading