Files
ai/controllers/naked_handler_guard_test.go
zeekay 6952e90db8 one way for a controller test to be somebody
Four harnesses each built a request, a recorder and a fake session store, then
seeded whatever principal the test wanted to be true. Nothing reads that store
any more, and an identity a test can assert is not an authorisation test — it is
a test of the fake. They are replaced by visit plus one verb, presenting, which
takes the credential a caller actually arrives with: authtest.Bearer for one that
should be accepted, or the literal string a test wants refused.

authtest gains Signing, so the verifier's certificate has ONE installer. Two of
them raced over the same global, and the loser signed against a certificate that
was no longer installed.

principalUser and credentialUser were the same read: one asked for a session
first, the other deliberately bypassed the session because a stale guest in the
process-local store could shadow the real identity. That store is gone, so both
were reading one token and the order between them meant nothing. One function.

principalIsOwnBrand no longer answers yes for any verified token. It short-cut on
"there is a session", which was sound while a session could only exist on its own
brand's host, and became wrong when a session became the token itself — a sibling
brand's bearer took that branch and reached this brand's all-customer view. The
issuer is the only thing that ever named the minting brand, so it is the only
thing asked.
2026-08-17 09:56:36 -07:00

74 lines
3.0 KiB
Go

// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package controllers
import (
"net/http"
"testing"
"github.com/hanzoai/ai/internal/authtest"
iam "github.com/hanzoai/ai/internal/iam"
)
// TestNakedHandlersRequireSuperAdmin asserts that every platform-sensitive handler
// that previously relied SOLELY on the routers.permissionFilter gate now ALSO
// self-guards with c.RequireSuperAdmin() as its first statement (defense in depth,
// matching the provider_admin handlers). Even if the filter is ever bypassed (the
// path-normalization class this PR also closes), provider / model-route mutation and
// cluster (node/k8s) introspection can no longer be reached unauthenticated
// (=> 401) or by a mere org admin (=> 403).
//
// Only the DENY paths are exercised: RequireSuperAdmin() is the FIRST statement in
// each handler and returns before any request-body parse or datastore/object call, so
// no live DB is needed. The super-admin PASS path is covered by
// TestRequireSuperAdmin_SuperAdminPasses plus each handler's own downstream tests.
func TestNakedHandlersRequireSuperAdmin(t *testing.T) {
// Every handler this PR added the c.RequireSuperAdmin() guard to.
handlers := []struct {
name string
call func(*ApiController)
}{
{"UpdateProvider", (*ApiController).UpdateProvider},
{"DeleteProvider", (*ApiController).DeleteProvider},
{"RefreshMcpTools", (*ApiController).RefreshMcpTools},
{"GetModelRoutes", (*ApiController).GetModelRoutes},
{"GetModelRoute", (*ApiController).GetModelRoute},
{"AddModelRoute", (*ApiController).AddModelRoute},
{"UpdateModelRoute", (*ApiController).UpdateModelRoute},
{"DeleteModelRoute", (*ApiController).DeleteModelRoute},
{"ReloadModelConfig", (*ApiController).ReloadModelConfig},
{"GetNodes", (*ApiController).GetNodes},
{"GetK8sStatus", (*ApiController).GetK8sStatus},
}
orgAdmin := &iam.User{Owner: "maxpower", Name: "dave", IsAdmin: true}
for _, h := range handlers {
// Anonymous → 401 (fail closed, before any body/DB read).
c := toggling("")
h.call(c)
if answered(c) != http.StatusUnauthorized {
t.Errorf("%s anonymous = %d, want 401 (naked handler must self-auth)", h.name, answered(c))
}
// Org admin (not global) → 403 (no org-admin → platform-admin escalation).
c2 := toggling(authtest.Bearer(t, *orgAdmin))
h.call(c2)
if answered(c2) != http.StatusForbidden {
t.Errorf("%s org-admin = %d, want 403 (not a platform admin)", h.name, answered(c2))
}
}
}