mirror of
https://github.com/hanzoai/mpc.git
synced 2026-10-30 19:45:28 +00:00
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.
143 lines
3.3 KiB
Go
143 lines
3.3 KiB
Go
// Copyright © 2026 Hanzo AI. MIT License.
|
|
|
|
package auth
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestRequireIdentity(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
require bool
|
|
headers map[string]string
|
|
wantCode int
|
|
wantOrg string
|
|
wantUser string
|
|
wantEmail string
|
|
wantNext bool
|
|
}{
|
|
{
|
|
name: "headers present, require true",
|
|
require: true,
|
|
headers: map[string]string{
|
|
HeaderOrgID: "hanzo",
|
|
HeaderUserID: "user-123",
|
|
HeaderUserEmail: "[email protected]",
|
|
},
|
|
wantCode: http.StatusOK,
|
|
wantOrg: "hanzo",
|
|
wantUser: "user-123",
|
|
wantEmail: "[email protected]",
|
|
wantNext: true,
|
|
},
|
|
{
|
|
name: "missing headers, require true → 401",
|
|
require: true,
|
|
headers: nil,
|
|
wantCode: http.StatusUnauthorized,
|
|
wantNext: false,
|
|
},
|
|
{
|
|
name: "missing headers, require false → next with empty ctx",
|
|
require: false,
|
|
headers: nil,
|
|
wantCode: http.StatusOK,
|
|
wantNext: true,
|
|
},
|
|
{
|
|
name: "partial headers (only org), require true → allowed",
|
|
require: true,
|
|
headers: map[string]string{HeaderOrgID: "lux"},
|
|
wantCode: http.StatusOK,
|
|
wantOrg: "lux",
|
|
wantNext: true,
|
|
},
|
|
{
|
|
name: "X-Org-Id whitespace-only with user set, require true → 401",
|
|
require: true,
|
|
headers: map[string]string{
|
|
HeaderOrgID: " ",
|
|
HeaderUserID: "user-7",
|
|
},
|
|
wantCode: http.StatusUnauthorized,
|
|
wantNext: false,
|
|
},
|
|
{
|
|
name: "X-Org-Id whitespace-only alone, require true → 401",
|
|
require: true,
|
|
headers: map[string]string{
|
|
HeaderOrgID: " \t\n ",
|
|
},
|
|
wantCode: http.StatusUnauthorized,
|
|
wantNext: false,
|
|
},
|
|
{
|
|
name: "X-Org-Id padded with whitespace gets trimmed",
|
|
require: true,
|
|
headers: map[string]string{
|
|
HeaderOrgID: " hanzo ",
|
|
HeaderUserID: "u-1",
|
|
HeaderUserEmail: " [email protected] ",
|
|
},
|
|
wantCode: http.StatusOK,
|
|
wantOrg: "hanzo",
|
|
wantUser: "u-1",
|
|
wantEmail: "[email protected]",
|
|
wantNext: true,
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
var (
|
|
called bool
|
|
gotOrg string
|
|
gotUser string
|
|
gotEmail string
|
|
)
|
|
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
called = true
|
|
gotOrg = OrgID(r.Context())
|
|
gotUser = UserID(r.Context())
|
|
gotEmail = UserEmail(r.Context())
|
|
w.WriteHeader(http.StatusOK)
|
|
})
|
|
|
|
handler := RequireIdentity(tc.require)(next)
|
|
req := httptest.NewRequest(http.MethodGet, "/v1/mpc/health", nil)
|
|
for k, v := range tc.headers {
|
|
req.Header.Set(k, v)
|
|
}
|
|
rec := httptest.NewRecorder()
|
|
handler.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != tc.wantCode {
|
|
t.Fatalf("status: want %d got %d (body=%q)", tc.wantCode, rec.Code, rec.Body.String())
|
|
}
|
|
if called != tc.wantNext {
|
|
t.Fatalf("next called: want %v got %v", tc.wantNext, called)
|
|
}
|
|
if gotOrg != tc.wantOrg {
|
|
t.Fatalf("org: want %q got %q", tc.wantOrg, gotOrg)
|
|
}
|
|
if gotUser != tc.wantUser {
|
|
t.Fatalf("user: want %q got %q", tc.wantUser, gotUser)
|
|
}
|
|
if gotEmail != tc.wantEmail {
|
|
t.Fatalf("email: want %q got %q", tc.wantEmail, gotEmail)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestAccessorsEmptyContext(t *testing.T) {
|
|
ctx := context.Background()
|
|
if OrgID(ctx) != "" || UserID(ctx) != "" || UserEmail(ctx) != "" {
|
|
t.Fatalf("expected all-empty accessors on bare context")
|
|
}
|
|
}
|