Keep malformed Home sessions out of the local identity path
2 files changed,
+32
−10
internal/equipo/oficinaid/http.go+5 −10internal/equipo/oficinaid/http_test.go+27 −0
CI cancelled — superseded by a newer push — run details
diff --git a/internal/equipo/oficinaid/http.go b/internal/equipo/oficinaid/http.go| index 9b7a4f94..f48f6a44 100644 |
| --- a/internal/equipo/oficinaid/http.go |
| +++ b/internal/equipo/oficinaid/http.go |
| @@ -292,21 +292,16 @@ func (h *HTTP) Session(r *http.Request) (auth.Identity, bool) { |
| } |
| func (h *HTTP) Authenticate(next http.Handler) http.Handler { |
| return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| - id, ok := h.Session(r) |
| - if !ok { |
| - var method string |
| - if e := h.Store.DB.QueryRowContext(r.Context(), "SELECT method FROM sessions WHERE token_hash=?", sessions.HashToken(h.token(r))).Scan(&method); e == nil && method == Method { |
| - h.fail(w, r, ErrRefused) |
| - return |
| - } |
| + raw, ok := h.Auth.SessionFrom(r) |
| + if !ok || string(raw.Method) != Method { |
| h.Auth.RequireSession(next).ServeHTTP(w, r) |
| return |
| } |
| - if string(id.Method) != Method { |
| - h.Auth.RequireSession(next).ServeHTTP(w, r) |
| + id, ok := h.Session(r) |
| + if !ok { |
| + h.fail(w, r, ErrRefused) |
| return |
| } |
| - // Downstream handlers receive the same admitted member identity for either door. |
| next.ServeHTTP(w, identity.With(sessions.WithSession(r, sessions.Session{Subject: id.Address, Method: string(id.Method), AuthTime: id.AuthTime, At: id.At}), id)) |
| }) |
| } |
diff --git a/internal/equipo/oficinaid/http_test.go b/internal/equipo/oficinaid/http_test.go| new file mode 100644 |
| index 00000000..6274868b |
| --- /dev/null |
| +++ b/internal/equipo/oficinaid/http_test.go |
| @@ -0,0 +1,27 @@ |
| +package oficinaid |
| + |
| +import ( |
| + "amadan.net/rastrillo/rastrillo/auth" |
| + "net/http" |
| + "net/http/httptest" |
| + "testing" |
| + "time" |
| +) |
| + |
| +func TestIDDuplicateSessionCannotBypassResolution(t *testing.T) { |
| + s, k := fixture(t) |
| + _, token := establish(t, s, k, time.Now().Truncate(time.Second)) |
| + authentication, e := auth.New(auth.Config{DB: s.DB, Origin: s.Origin, InstanceKey: "test-identity-cookie-key", ProofOff: true}) |
| + if e != nil { |
| + t.Fatal(e) |
| + } |
| + h := &HTTP{Store: s, Auth: authentication, Render: func(w http.ResponseWriter, _ *http.Request, _ View, status int) { w.WriteHeader(status) }} |
| + r := httptest.NewRequest("GET", s.Origin+"/work", nil) |
| + r.AddCookie(&http.Cookie{Name: authentication.SessionCookie(), Value: token}) |
| + r.AddCookie(&http.Cookie{Name: authentication.SessionCookie(), Value: token}) |
| + out := httptest.NewRecorder() |
| + h.Authenticate(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(204) })).ServeHTTP(out, r) |
| + if out.Code < 400 { |
| + t.Fatal("Home subject escaped identity resolution", out.Code) |
| + } |
| +} |