Files
llmgw/internal/store/claims.go
T
Ilker ManapandClaude Opus 5 ccc9aa8b76
CI / test (push) Successful in 2m30s
Route every query through a dialect, before there is a second one
Groundwork for PostgreSQL, with no PostgreSQL in it yet: the store still opens
SQLite and behaves exactly as it did.

One set of queries rather than one per database. The SQL here is almost
entirely portable — ON CONFLICT upserts work in both, and the only construct
that genuinely differs is the placeholder — so two copies of every query would
buy nothing and drift, one query at a time, discovered in production.

The rewrite is a wrapper around the handle rather than a change at each call
site. There are sixty of those and sixty chances to forget one; this way the
only route to the driver goes through the rewrite. Making it a wrapper found
six places that reached past it, including all three manual transactions and
two prepared statements — every one of which would have worked on SQLite and
failed on PostgreSQL, on a path nobody exercises before deploying.

The rewriter skips quoted sections. No query here contains a question mark in
a literal today; this is so that adding one is a test failure rather than a
statement that silently binds the wrong argument.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-25 16:10:35 +02:00

152 lines
4.3 KiB
Go

package store
import (
"context"
"crypto/rand"
"database/sql"
"errors"
"time"
)
// ClaimTTL is how long an unclaimed link stays usable.
//
// Short enough that a link forwarded to the wrong place is unlikely to still
// work when it gets there; long enough to survive a weekend.
const ClaimTTL = 72 * time.Hour
// ErrClaimed says a claim link is gone: used, expired, or never real. The three
// are not distinguished, because saying which would confirm that a token was
// once valid.
var ErrClaimed = errors.New("this link is no longer valid")
// Claim is a pending one-time delivery of a key.
type Claim struct {
KeyID string
ExpiresAt time.Time
}
// NewClaimToken mints the token that goes in the link.
//
// The same 256 bits as an API key, for the same reason: it is a bearer
// credential for as long as it lives.
func NewClaimToken() (string, error) {
raw := make([]byte, keyBytes)
if _, err := rand.Read(raw); err != nil {
return "", err
}
return encodeBase62(raw), nil
}
// CreateClaim records a sealed key for later collection.
//
// The secret is sealed with the token, which is not stored, so what lands in
// the database cannot be opened by anything that can read the database.
func (s *SQLite) CreateClaim(ctx context.Context, keyID, token, secret string, ttl time.Duration) error {
sealed, err := seal(token, []byte(secret))
if err != nil {
return err
}
if ttl <= 0 {
ttl = ClaimTTL
}
now := time.Now()
_, err = s.db.ExecContext(ctx,
`INSERT INTO key_claims (token_hash, key_id, sealed, created_at, expires_at)
VALUES (?, ?, ?, ?, ?)`,
HashKey(token), keyID, sealed, millis(now), millis(now.Add(ttl)))
return err
}
// PeekClaim reports whether a link is still good, without consuming it.
//
// The page behind the link is shown before it is used, because link previewers
// follow URLs: a GET that consumed the claim would mean the first chat client
// to unfurl it burns the key.
func (s *SQLite) PeekClaim(ctx context.Context, token string) (Claim, error) {
row := s.db.QueryRowContext(ctx,
`SELECT key_id, expires_at, claimed_at FROM key_claims WHERE token_hash = ?`,
HashKey(token))
var c Claim
var expires int64
var claimed sql.NullInt64
if err := row.Scan(&c.KeyID, &expires, &claimed); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return Claim{}, ErrClaimed
}
return Claim{}, err
}
c.ExpiresAt = fromMillis(expires)
if claimed.Valid || time.Now().After(c.ExpiresAt) {
return Claim{}, ErrClaimed
}
return c, nil
}
// ConsumeClaim opens the sealed key and marks the link used.
//
// The update is the lock: exactly one caller can move claimed_at from NULL, so
// two people opening the link at the same moment produce one winner rather than
// two copies of the key.
func (s *SQLite) ConsumeClaim(ctx context.Context, token string) (string, error) {
hash := HashKey(token)
now := time.Now()
q, tx, err := s.begin(ctx)
if err != nil {
return "", err
}
defer func() { _ = tx.Rollback() }()
var sealed []byte
var expires int64
err = q.QueryRowContext(ctx,
`SELECT sealed, expires_at FROM key_claims WHERE token_hash = ? AND claimed_at IS NULL`,
hash).Scan(&sealed, &expires)
if errors.Is(err, sql.ErrNoRows) {
return "", ErrClaimed
}
if err != nil {
return "", err
}
if now.After(fromMillis(expires)) {
return "", ErrClaimed
}
secret, err := open(token, sealed)
if err != nil {
return "", ErrClaimed
}
// Blank the sealed value as well as marking it claimed, so a used link
// leaves nothing behind even before the row is purged.
res, err := q.ExecContext(ctx,
`UPDATE key_claims SET claimed_at = ?, sealed = x'' WHERE token_hash = ? AND claimed_at IS NULL`,
millis(now), hash)
if err != nil {
return "", err
}
if n, _ := res.RowsAffected(); n == 0 {
return "", ErrClaimed
}
if err := tx.Commit(); err != nil {
return "", err
}
return string(secret), nil
}
// PurgeClaims deletes rows that can no longer be used.
//
// Housekeeping rather than security — an expired row is already refused and its
// contents are already unopenable — but a table that only grows is its own
// problem.
func (s *SQLite) PurgeClaims(ctx context.Context, before time.Time) (int64, error) {
res, err := s.db.ExecContext(ctx,
`DELETE FROM key_claims WHERE expires_at < ? OR claimed_at IS NOT NULL`, millis(before))
if err != nil {
return 0, err
}
n, _ := res.RowsAffected()
return n, nil
}