mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
The beego half of this was closed: every chat.go / message.go handler stamps Owner from the chatOwner constant, so a request can no longer name the ledger its answer debits. The ZAP twin plane mirrors all seven of those handlers, registers them as native cloud methods, and stamped nothing — it took Owner straight off the body, while its own header comment claimed "org scope is always the resolved user's Owner". This makes the code true. Owner was not the only value the caller chose, and the twin was not the only way in. A row's User is the other half of the subject AddTransactionForMessage debits, and two values escape the join it builds. Empty leaves the subject "admin/", the admin ORG's own pool wallet — Hanzo's platform account, not any customer's — and it used to pass the ownership guard outright: an unauthenticated request has no session username, so the test read "" != "" and every anonymous caller was the current user of the empty user. A User already carrying "/" is passed through unjoined, so "victim/bob" names that tenant's wallet directly, and the admin branch waved it through. Both guards now answer for NAMES on both sides, which is what they were always trying to ask. Organization is the org key the usage plane reads (recordCasibaseChatUsage and both TTS recorders take it, falling back to Owner), and no handler touched it. It is a fact about the caller, so it is stamped like the owner: from the principal on create, from the stored row on update, and from its CHAT on a message — a turn cannot be booked to a different tenant than the conversation it is in. And a delete asked the attacker whether the attack was allowed. The body names WHICH chat, owner and name; its `user` is a separate field the caller also chose, so writing its own name there and a victim's chat name in `name` satisfied the check and destroyed the chat and every message in it. Deletes and updates now authorize against the STORED row on both planes, including the edit branch of AddMessage, which drops every turn after the one it rewrites. The sweep of all 176 registerCloud methods found one more twin with the same drift: the beego AddStore was hardened to resolve its owner through GetScopedOwner and zapAddStoreHandler was not, so any signed-in tenant could file a store into the chat plane's own org — where it is reachable as a default store, and a default store names the model every chat answer runs and bills. It now resolves through the seam that file already had. Separately, a generation that produced no text had still not terminated as far as the row was concerned. The claim tests an empty text column, which a tool-call-only turn, a filtered response or a carrier parse that stripped everything satisfies forever, so the next request won the claim, ran the model again and took another debit — and the ledger mints its own entry per call, so every repeat was another invoice for one turn. Termination is now its own recorded fact, written before the charge so a persist that fails after the money moved cannot reopen it, and the lease keeps its real job: releasing a generator that died, not one that finished. Two more unauthenticated nil-derefs of the class already fixed: getMessage answers (nil, nil) for an id no row matches, and both DeleteWelcomeMessage and object.UpdateMessage read through it. Every fix is pinned by a test that fails without it — sixteen reverts, sixteen failures, none committed. Co-authored-by: Hanzo Dev <[email protected]>
138 lines
5.7 KiB
Go
138 lines
5.7 KiB
Go
// Copyright 2023-2026 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 object
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/hanzoai/ai/util"
|
|
)
|
|
|
|
// TestAnEmptyAnswerIsNotGeneratedTwice is the invariant the claim alone could not
|
|
// hold. The claim's condition is an empty text column, and a completion that produced
|
|
// no text satisfies it forever — a tool-call-only turn, a response the provider
|
|
// filtered, a carrier parse that stripped everything, or a persist that failed after
|
|
// the debit landed. So the row read as unanswered, the next request won the claim, ran
|
|
// the model again and took a second debit. The debit is not idempotent (the ledger
|
|
// mints its own entry per call), so every repeat is another invoice for one turn.
|
|
//
|
|
// Termination is the fact that settles it, and it is recorded separately from the text
|
|
// precisely because there is no text. Drop the answered_time clause from
|
|
// ClaimMessageAnswer and this sees the second generation.
|
|
func TestAnEmptyAnswerIsNotGeneratedTwice(t *testing.T) {
|
|
read := answerable(t, "file:claimsettled?mode=memory&cache=shared")
|
|
|
|
held := read()
|
|
if claimed, err := ClaimMessageAnswer(held); err != nil || !claimed {
|
|
t.Fatalf("first claim = %v, %v; want true, nil", claimed, err)
|
|
}
|
|
if err := SettleMessageAnswer(held); err != nil {
|
|
t.Fatalf("SettleMessageAnswer: %v", err)
|
|
}
|
|
|
|
// The premise: the row really is still textless. Without this the test could pass
|
|
// for the old reason (a non-empty answer) and prove nothing about termination.
|
|
if got := read().Text; got != "" {
|
|
t.Fatalf("the row under test must still be textless, got %q", got)
|
|
}
|
|
|
|
if claimed, _ := ClaimMessageAnswer(read()); claimed {
|
|
t.Error("a terminated generation was claimed again — a second completion AND a second charge for one turn")
|
|
}
|
|
}
|
|
|
|
// TestASettledAnswerOutlivesItsLease is the second window. The lease exists for a
|
|
// generator that DIED mid-answer: past answerLease the message is claimable again so a
|
|
// crash costs a delay rather than a message nobody can answer. A generation that
|
|
// FINISHED is not that, and must not be handed to the next request when its claim
|
|
// lapses — otherwise an empty answer is re-billable every fifteen minutes, forever.
|
|
func TestASettledAnswerOutlivesItsLease(t *testing.T) {
|
|
read := answerable(t, "file:claimsettledstale?mode=memory&cache=shared")
|
|
|
|
held := read()
|
|
if claimed, err := ClaimMessageAnswer(held); err != nil || !claimed {
|
|
t.Fatalf("first claim = %v, %v", claimed, err)
|
|
}
|
|
if err := SettleMessageAnswer(held); err != nil {
|
|
t.Fatalf("SettleMessageAnswer: %v", err)
|
|
}
|
|
|
|
stale := read()
|
|
stale.ClaimedTime = util.GetTimeAgo(answerLease + time.Minute)
|
|
if _, err := UpdateMessage(stale.GetId(), stale, false); err != nil {
|
|
t.Fatalf("UpdateMessage: %v", err)
|
|
}
|
|
|
|
if claimed, _ := ClaimMessageAnswer(read()); claimed {
|
|
t.Error("an expired lease handed a FINISHED generation to a second request — one turn, another invoice every lease")
|
|
}
|
|
}
|
|
|
|
// TestReleaseCannotUnsettleATerminatedAnswer covers the exit path the answer handler
|
|
// actually takes: it settles, then returns, and the deferred release runs. Release is
|
|
// conditional on the text still being empty, which a settled EMPTY answer is — so if
|
|
// settling lived in the claim column alone, the release would immediately undo it.
|
|
func TestReleaseCannotUnsettleATerminatedAnswer(t *testing.T) {
|
|
read := answerable(t, "file:claimsettledrelease?mode=memory&cache=shared")
|
|
|
|
held := read()
|
|
if claimed, err := ClaimMessageAnswer(held); err != nil || !claimed {
|
|
t.Fatalf("first claim = %v, %v", claimed, err)
|
|
}
|
|
if err := SettleMessageAnswer(held); err != nil {
|
|
t.Fatalf("SettleMessageAnswer: %v", err)
|
|
}
|
|
|
|
ReleaseMessageAnswer(held)
|
|
|
|
if claimed, _ := ClaimMessageAnswer(read()); claimed {
|
|
t.Error("the deferred release handed a terminated generation back for a second completion and a second charge")
|
|
}
|
|
}
|
|
|
|
// TestSettlingDoesNotStrandAFailedGeneration is the other side: a generation that
|
|
// failed BEFORE it terminated releases and is retryable at once, exactly as before.
|
|
// Settling must refuse the repeat, not the retry.
|
|
func TestSettlingDoesNotStrandAFailedGeneration(t *testing.T) {
|
|
read := answerable(t, "file:claimsettledretry?mode=memory&cache=shared")
|
|
|
|
held := read()
|
|
if claimed, err := ClaimMessageAnswer(held); err != nil || !claimed {
|
|
t.Fatalf("first claim = %v, %v", claimed, err)
|
|
}
|
|
ReleaseMessageAnswer(held)
|
|
|
|
if claimed, err := ClaimMessageAnswer(read()); err != nil || !claimed {
|
|
t.Errorf("a generation that failed before terminating must be retryable at once, got %v, %v", claimed, err)
|
|
}
|
|
}
|
|
|
|
// TestUpdateMessageSurvivesAMissingRow pins the nil-deref: getMessage answers
|
|
// (nil, nil) for an id no row matches, so the miss arrives as a nil origin and not as
|
|
// an error, and reading TextTokenCount off it panics the process. This route is
|
|
// reachable without a session, so the panic is an unauthenticated crash.
|
|
func TestUpdateMessageSurvivesAMissingRow(t *testing.T) {
|
|
answerable(t, "file:updatemissing?mode=memory&cache=shared")
|
|
|
|
ok, err := UpdateMessage("admin/message_nope", &Message{Owner: "admin", Name: "message_nope"}, false)
|
|
if err != nil {
|
|
t.Fatalf("updating a message that does not exist must not error, got %v", err)
|
|
}
|
|
if ok {
|
|
t.Error("updating a message that does not exist reported success")
|
|
}
|
|
}
|