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
47 changes: 47 additions & 0 deletions authkit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,53 @@ func TestHTTPSessionCookieCannotBeSetBySiblingSubdomain(t *testing.T) {
}
}

func TestCookieMutationRejectsSiblingOrigin(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)
}
handler := a.Handler()
created := request(handler, http.MethodPost, "/api/auth/sign-up/email", `{"email":"user@example.com","password":"password123","name":"Test User"}`)
if created.Code != http.StatusCreated {
t.Fatalf("signup: %d %s", created.Code, created.Body.String())
}
var session *http.Cookie
for _, cookie := range created.Result().Cookies() {
if cookie.Name == hostSessionCookie {
session = cookie
}
}
if session == nil {
t.Fatal("session cookie missing")
}
mutate := func(origin, fetchSite string) int {
r := httptest.NewRequest(http.MethodPost, "/api/auth/sign-out", nil)
r.AddCookie(session)
if origin != "" {
r.Header.Set("Origin", origin)
}
if fetchSite != "" {
r.Header.Set("Sec-Fetch-Site", fetchSite)
}
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
return w.Code
}
if got := mutate("https://attacker.example.azdelphi.com", "same-site"); got != http.StatusForbidden {
t.Fatalf("sibling origin: %d", got)
}
if got := mutate("", "same-site"); got != http.StatusForbidden {
t.Fatalf("missing sibling origin: %d", got)
}
if got := request(handler, http.MethodGet, "/api/auth/session", "", session); got.Code != http.StatusOK {
t.Fatalf("session unexpectedly revoked: %d", got.Code)
}
if got := mutate("https://terminal.azdelphi.com", "same-origin"); got != http.StatusOK {
t.Fatalf("same origin rejected: %d", got)
}
}

type fakeSocial struct{}

func (fakeSocial) Begin() (string, social.Pending, error) {
Expand Down
19 changes: 18 additions & 1 deletion http.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,24 @@ func (a *Auth) Handler() http.Handler {
}
mux.HandleFunc("GET "+prefix+"/session", a.currentSession)
mux.HandleFunc("POST "+prefix+"/sign-out", a.signOut)
return mux
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// A public sibling subdomain is same-site, so SameSite=Lax does not
// protect cookie-authenticated mutations. OAuth callbacks are bound to
// their one-time flow cookie and must accept provider cross-origin POSTs.
if r.Method != http.MethodGet && r.Method != http.MethodHead && r.Method != http.MethodOptions &&
!strings.HasPrefix(r.URL.Path, prefix+"/callback/") {
if _, err := r.Cookie(a.sessionCookieName()); err == nil {
origin := r.Header.Get("Origin")
fetchSite := r.Header.Get("Sec-Fetch-Site")
if (origin != "" && origin != a.config.BaseURL) ||
(origin == "" && (fetchSite == "same-site" || fetchSite == "cross-site")) {
http.Error(w, "invalid origin", http.StatusForbidden)
return
}
}
}
mux.ServeHTTP(w, r)
})
}

func (a *Auth) signUpEmail(w http.ResponseWriter, r *http.Request) {
Expand Down
Loading