mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
* fix(security): close Great-Audit F1 (unauth admin RAG/scrape), F-sk (sk- zero-billing), F4 (auth 200→401)
F1 (CRITICAL, live): unauthenticated callers could reach admin-tenant
index/scrape/ingest via preview mode. Three-layer fix:
- conf.IsPreviewMode/DisablePreviewMode: ONE env-first accessor
(DISABLE_PREVIEW_MODE beats app.conf) — the reversible, no-rebuild
lever ops flips on the cloud CR. util.go + authz_filter.go route
through it (no more direct beego.AppConfig reads).
- requireIndexAuth: REMOVE the preview→{Owner:"admin"} fallback. A
no-credential caller is now 401, never admin — even under preview.
- permissionFilter: fail-closed credential gate for the write/ingest/
scrape/RAG endpoints (scrape, index, search, docs/ingest, rag/*,
embed, query, documents, …) that previously fell through the central
filter. Explicit set (not a blanket default) so health/metrics/
wecom/memory stay reachable. Controller still does authoritative auth.
F-sk (money leak): authResolveProvider's sk- branch left authUser nil, so
reserveBudget + recordUsage (gated on authUser != nil) were skipped —
sk- + shared do-ai upstream ran at ZERO cost (chat AND images). Now
attribute+bill to the provider-row OWNER ORG via providerKeyBillingUser
(org-level subject, resolved before the model-route swap; unattributable
provider is refused, fail-secure).
F4: search/RAG auth failures used plain ResponseError (HTTP 200). Switched
resolveSearchAuth + requireIndexAuth auth-fail paths to ResponseUnauthorized
(401); pk- write attempt → ResponseForbidden (403).
Tests: conf env-lever; requireIndexAuth/resolveSearchAuth no-cred→401 (proven
under preview mode); pk-→403; providerKeyBillingUser owner attribution +
org-level subject; permissionFilter anonymous write→401, credentialed
pass-through, benign paths ungated. go build/vet/test green.
* style: gofumpt (Go-Linter)
81 lines
2.7 KiB
Go
81 lines
2.7 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 conf
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
// TestDisablePreviewModeEnvLever proves the DISABLE_PREVIEW_MODE env override is
|
|
// honored env-first — the reversible, no-rebuild lever ops flips on the deployed
|
|
// cloud CR to close the preview-mode surface (F1). "true" disables preview
|
|
// (preview OFF); any other explicit value keeps it enabled.
|
|
func TestDisablePreviewModeEnvLever(t *testing.T) {
|
|
orig, had := os.LookupEnv("DISABLE_PREVIEW_MODE")
|
|
t.Cleanup(func() {
|
|
if had {
|
|
os.Setenv("DISABLE_PREVIEW_MODE", orig)
|
|
} else {
|
|
os.Unsetenv("DISABLE_PREVIEW_MODE")
|
|
}
|
|
})
|
|
|
|
cases := []struct {
|
|
env string
|
|
wantDisable bool
|
|
}{
|
|
{"true", true},
|
|
{"TRUE", true},
|
|
{" true ", true}, // trimmed
|
|
{"false", false},
|
|
{"", false}, // empty is not "true" → preview stays ON (fail toward dev default, not toward disabling)
|
|
{"1", false}, // only the literal "true" flips it — no ambiguity
|
|
{"yes", false},
|
|
}
|
|
for _, tc := range cases {
|
|
os.Setenv("DISABLE_PREVIEW_MODE", tc.env)
|
|
if got := DisablePreviewMode(); got != tc.wantDisable {
|
|
t.Errorf("DISABLE_PREVIEW_MODE=%q → DisablePreviewMode()=%v, want %v", tc.env, got, tc.wantDisable)
|
|
}
|
|
// IsPreviewMode is exactly the negation — the semantic callers use.
|
|
if got := IsPreviewMode(); got != !tc.wantDisable {
|
|
t.Errorf("DISABLE_PREVIEW_MODE=%q → IsPreviewMode()=%v, want %v", tc.env, got, !tc.wantDisable)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestDisablePreviewModeEnvBeatsFile locks that the env is consulted FIRST, so a
|
|
// deployed binary (whose app.conf, if any, sets disablePreviewMode=false) can be
|
|
// flipped to preview-OFF purely by env — no image rebuild.
|
|
func TestDisablePreviewModeEnvBeatsFile(t *testing.T) {
|
|
orig, had := os.LookupEnv("DISABLE_PREVIEW_MODE")
|
|
t.Cleanup(func() {
|
|
if had {
|
|
os.Setenv("DISABLE_PREVIEW_MODE", orig)
|
|
} else {
|
|
os.Unsetenv("DISABLE_PREVIEW_MODE")
|
|
}
|
|
})
|
|
|
|
os.Setenv("DISABLE_PREVIEW_MODE", "true")
|
|
if IsPreviewMode() {
|
|
t.Fatal("with DISABLE_PREVIEW_MODE=true, IsPreviewMode() must be false regardless of app.conf")
|
|
}
|
|
if !DisablePreviewMode() {
|
|
t.Fatal("with DISABLE_PREVIEW_MODE=true, DisablePreviewMode() must be true regardless of app.conf")
|
|
}
|
|
}
|