CI / test (push) Successful in 2m43s
Issuing a key worked; getting it to the person it was issued for did not. The operator had a secret on their terminal and no way to send it that did not leave a bearer credential in a chat log forever. `keys create --claim` prints a link instead. GET shows a button and consumes nothing; POST reveals the key and burns the link. That split is not politeness — chat clients, mail scanners and link previewers fetch URLs the moment they are posted, so a GET that consumed the claim would mean the first thing to unfurl the link destroys the key before anyone reads it. A pending claim has to hold the key somewhere, which would otherwise put a working credential in the database and undo what hashing bought. So the key is sealed with a key derived from the link's own token, and the token is never stored — only its hash. A database leak yields two useless hashes and a blob nothing in the database can open. HKDF rather than the raw token, so the value used for lookup can never become the value that decrypts. Consuming is one UPDATE moving claimed_at off NULL, so two people opening the same link produce one key rather than two copies. A used link, an expired one and a token that was never real all render identically, because saying which confirms that a token was once valid. The page is no-store, no-referrer and noindex: the URL is itself the credential. `GET /me` reports a key's own limits and spend, so nobody has to ask an operator to read a dashboard they cannot reach. Outside /v1, because it is not part of the OpenAI API. docs/onboarding.md is written for whoever operates this, including the parts that are deliberately absent — no self-service, no SSO, and why. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
212 lines
5.4 KiB
Go
212 lines
5.4 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"path/filepath"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func claimFixture(t *testing.T) (*SQLite, string, string) {
|
|
t.Helper()
|
|
ctx := context.Background()
|
|
s, err := OpenSQLite(ctx, filepath.Join(t.TempDir(), "c.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = s.Close() })
|
|
tree(t, s)
|
|
|
|
secret, k, err := NewKey("key1", "n", "proj1")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
k.CreatedAt = time.Now()
|
|
if err := s.CreateKey(ctx, k); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
token, err := NewClaimToken()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.CreateClaim(ctx, k.ID, token, secret, ClaimTTL); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return s, token, secret
|
|
}
|
|
|
|
func TestAClaimDeliversTheKeyExactlyOnce(t *testing.T) {
|
|
s, token, secret := claimFixture(t)
|
|
ctx := context.Background()
|
|
|
|
if _, err := s.PeekClaim(ctx, token); err != nil {
|
|
t.Fatalf("a fresh link is not valid: %v", err)
|
|
}
|
|
got, err := s.ConsumeClaim(ctx, token)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got != secret {
|
|
t.Fatal("the delivered key is not the one that was issued")
|
|
}
|
|
|
|
if _, err := s.ConsumeClaim(ctx, token); !errors.Is(err, ErrClaimed) {
|
|
t.Errorf("the link worked twice: %v", err)
|
|
}
|
|
if _, err := s.PeekClaim(ctx, token); !errors.Is(err, ErrClaimed) {
|
|
t.Errorf("a used link still looks valid: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestTheDatabaseNeverHoldsAUsableKey(t *testing.T) {
|
|
// The property that makes a pending claim safe to store at all: what is in
|
|
// the table cannot be opened by anything that can read the table.
|
|
s, token, secret := claimFixture(t)
|
|
|
|
var sealed []byte
|
|
if err := s.db.QueryRow(`SELECT sealed FROM key_claims`).Scan(&sealed); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(sealed) == 0 {
|
|
t.Fatal("nothing was stored")
|
|
}
|
|
if string(sealed) == secret {
|
|
t.Fatal("the key is sitting in the database in the clear")
|
|
}
|
|
// And the token itself is not there either, so nothing in the database can
|
|
// derive the key that opens it.
|
|
var count int
|
|
if err := s.db.QueryRow(`SELECT COUNT(*) FROM key_claims WHERE token_hash = ?`, []byte(token)).
|
|
Scan(&count); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if count != 0 {
|
|
t.Fatal("the claim token was stored, not just its hash")
|
|
}
|
|
|
|
if _, err := open("wrong token entirely", sealed); !errors.Is(err, ErrSealed) {
|
|
t.Errorf("a wrong token opened the blob: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestAClaimedLinkLeavesNothingBehind(t *testing.T) {
|
|
s, token, _ := claimFixture(t)
|
|
if _, err := s.ConsumeClaim(context.Background(), token); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var sealed []byte
|
|
if err := s.db.QueryRow(`SELECT sealed FROM key_claims`).Scan(&sealed); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(sealed) != 0 {
|
|
t.Errorf("a used claim still holds %d bytes", len(sealed))
|
|
}
|
|
}
|
|
|
|
func TestOnlyOneOfTwoSimultaneousClaimsWins(t *testing.T) {
|
|
// Two people opening the same link at the same moment must produce one key,
|
|
// not two copies of it.
|
|
s, token, _ := claimFixture(t)
|
|
|
|
var wg sync.WaitGroup
|
|
var mu sync.Mutex
|
|
won := 0
|
|
for range 16 {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
if _, err := s.ConsumeClaim(context.Background(), token); err == nil {
|
|
mu.Lock()
|
|
won++
|
|
mu.Unlock()
|
|
}
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
if won != 1 {
|
|
t.Errorf("%d callers each received the key", won)
|
|
}
|
|
}
|
|
|
|
func TestAnExpiredLinkIsRefused(t *testing.T) {
|
|
ctx := context.Background()
|
|
s, err := OpenSQLite(ctx, filepath.Join(t.TempDir(), "c.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer s.Close()
|
|
tree(t, s)
|
|
|
|
secret, k, _ := NewKey("key1", "n", "proj1")
|
|
k.CreatedAt = time.Now()
|
|
must(t, s.CreateKey(ctx, k))
|
|
|
|
token, _ := NewClaimToken()
|
|
must(t, s.CreateClaim(ctx, k.ID, token, secret, ClaimTTL))
|
|
// Age it directly. CreateClaim treats a non-positive TTL as "use the
|
|
// default", which is the right thing for a caller and no way to build the
|
|
// row this test needs.
|
|
if _, err := s.db.Exec(`UPDATE key_claims SET expires_at = ?`,
|
|
time.Now().Add(-time.Hour).UnixMilli()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if _, err := s.PeekClaim(ctx, token); !errors.Is(err, ErrClaimed) {
|
|
t.Errorf("an expired link looks valid: %v", err)
|
|
}
|
|
if _, err := s.ConsumeClaim(ctx, token); !errors.Is(err, ErrClaimed) {
|
|
t.Errorf("an expired link delivered a key: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestAnUnknownLinkLooksLikeAUsedOne(t *testing.T) {
|
|
// Distinguishing them would confirm that a token was once valid.
|
|
s, token, _ := claimFixture(t)
|
|
ctx := context.Background()
|
|
must(t, func() error { _, err := s.ConsumeClaim(ctx, token); return err }())
|
|
|
|
unknown, _ := NewClaimToken()
|
|
_, usedErr := s.PeekClaim(ctx, token)
|
|
_, unknownErr := s.PeekClaim(ctx, unknown)
|
|
if usedErr.Error() != unknownErr.Error() {
|
|
t.Errorf("used says %q, unknown says %q", usedErr, unknownErr)
|
|
}
|
|
}
|
|
|
|
func TestPurgeRemovesWhatCannotBeUsed(t *testing.T) {
|
|
s, token, _ := claimFixture(t)
|
|
ctx := context.Background()
|
|
must(t, func() error { _, err := s.ConsumeClaim(ctx, token); return err }())
|
|
|
|
n, err := s.PurgeClaims(ctx, time.Now())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if n != 1 {
|
|
t.Errorf("purged %d rows", n)
|
|
}
|
|
}
|
|
|
|
func TestSealRoundTrips(t *testing.T) {
|
|
token, _ := NewClaimToken()
|
|
sealed, err := seal(token, []byte("sk-llmgw-secret"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := open(token, sealed)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if string(got) != "sk-llmgw-secret" {
|
|
t.Errorf("round trip gave %q", got)
|
|
}
|
|
// Tampering must fail rather than produce something.
|
|
sealed[len(sealed)-1] ^= 0xff
|
|
if _, err := open(token, sealed); !errors.Is(err, ErrSealed) {
|
|
t.Errorf("a tampered blob opened: %v", err)
|
|
}
|
|
}
|