Files
mpc/pkg/auth/middleware.go
zeekay 5c423debf4 fix(mpc): close Red P0/P1 findings — health gating + ZAP port + timing-safe + cloud-mode self-check
P0-mpc-1: /v1/mpc/health no longer leaks node identity. Probes return
{"status":"ok"} only; node info moved behind identity-gated /v1/mpc/info.
Drop the duplicate /v1/mpc/health handler in pkg/mpc/embed.go.

P0-mpc-2: drop port 9999 from Service in non-consensus deploys (default).
Only expose when consensus is explicitly enabled. Stops fingerprintable
closed-port + future surprise-bind on operator upgrade.

P0-mpc-3: cmd/mpcd/consensus.go internal API key compare now uses
crypto/subtle.ConstantTimeCompare. Closes Bearer-token timing leak.

P0-mpc-4: cmd/mpcd/consensus.go Fatal in production when
MPC_INTERNAL_API_KEY is unset and MPC_HSM_PROVIDER!=env. No more
deriving keys from disk-readable Ed25519 seed.

CC: pkg/auth/middleware.go now rejects empty X-Org-Id when require=true
(defense-in-depth). cmd/mpcd/main.go forces MPCD_REQUIRE_IDENTITY=true
when HANZO_DEPLOYMENT_MODE=cloud (Fatal on conflict).

Tests: pkg/auth + pkg/mpc both PASS.
Closes Red 2026-04-27 mpc findings.
2026-04-28 03:02:14 -07:00

88 lines
2.8 KiB
Go

// Copyright © 2026 Hanzo AI. MIT License.
// Package auth wires gateway-supplied identity headers (X-Org-Id,
// X-User-Id, X-User-Email) into request context. mpcd does not validate
// JWTs itself — hanzoai/gateway already did that. The middleware is
// the trust boundary: in production, only the gateway can reach mpcd,
// and MPCD_REQUIRE_IDENTITY=true rejects any request without identity
// headers.
package auth
import (
"context"
"net/http"
"strings"
)
const (
HeaderOrgID = "X-Org-Id"
HeaderUserID = "X-User-Id"
HeaderUserEmail = "X-User-Email"
)
type ctxKey int
const (
ctxKeyOrgID ctxKey = iota
ctxKeyUserID
ctxKeyUserEmail
)
// RequireIdentity reads identity headers and attaches them to ctx.
// When require=true:
// - Both X-Org-Id and X-User-Id absent (or whitespace-only) → 401.
// - X-Org-Id present-but-empty (whitespace-only) with X-User-Id set
// → 401. An empty OrgID would unscope every store query and
// collapse tenant isolation; defense-in-depth, since hanzoai/gateway
// should never forward an empty owner claim, but mpcd does not
// trust that assumption.
//
// When require=false, missing headers yield empty ctx values — the
// embedded/dev path.
func RequireIdentity(require bool) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rawOrg := r.Header.Get(HeaderOrgID)
rawUser := r.Header.Get(HeaderUserID)
org := strings.TrimSpace(rawOrg)
user := strings.TrimSpace(rawUser)
email := strings.TrimSpace(r.Header.Get(HeaderUserEmail))
if require {
if org == "" && user == "" {
http.Error(w, `{"error":"identity required","code":401}`, http.StatusUnauthorized)
return
}
// X-Org-Id present-but-empty (whitespace) with a user
// set would unscope queries — reject.
if rawOrg != "" && org == "" {
http.Error(w, `{"error":"empty org id","code":401}`, http.StatusUnauthorized)
return
}
}
ctx := r.Context()
ctx = context.WithValue(ctx, ctxKeyOrgID, org)
ctx = context.WithValue(ctx, ctxKeyUserID, user)
ctx = context.WithValue(ctx, ctxKeyUserEmail, email)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
}
// OrgID returns the org id attached by RequireIdentity, or "".
func OrgID(ctx context.Context) string { return strFromCtx(ctx, ctxKeyOrgID) }
// UserID returns the user id attached by RequireIdentity, or "".
func UserID(ctx context.Context) string { return strFromCtx(ctx, ctxKeyUserID) }
// UserEmail returns the user email attached by RequireIdentity, or "".
func UserEmail(ctx context.Context) string { return strFromCtx(ctx, ctxKeyUserEmail) }
func strFromCtx(ctx context.Context, k ctxKey) string {
if v, ok := ctx.Value(k).(string); ok {
return v
}
return ""
}