beads/cmd/bd/memory_proxied_integration_test.go
Julian Knutsen a72a15220b
Refuse kv.* point reads on the settings role and stop printing them (#5493)
* Refuse kv.* point reads on the settings role and stop printing them

The `bd kv` keys and the `bd remember` memories nested under them are user
data stored as config rows. They ride in the settings table without being
settings, and only the ENUMERATION was filtered: a caller naming an exact
key still got the value back from GetSetting. That is not a smaller hole
than the enumeration was. `bd remember` derives its key from the content it
stores, so the keys are guessable, and the walk-around is one request to
GET /v0/beads/config/kv.memory.<slug> — a surface with no authentication
whose redaction decides on the key NAME while a memory's secret is in the
VALUE.

The point read now answers a `kv.` key exactly as it answers a key nothing
ever stored: the echoed key, an empty value, a nil error. Indistinguishable
from absence on purpose — this role has no ErrNotFound to give, and an
error of any kind would confirm the guessed key to the caller. The row is
not touched, and no read reaches the store at all.

The refusal lives in internal/workapi beside FilterSettingsEnumeration,
where both WorkspaceConfig bodies already reach for the enumeration rule,
so the three legs inherit one decision. Both filters now decide with
workapi.KeyIsOnTheKVPlane, which is the single statement of where the plane
boundary runs.

`bd config show` gets the same rule (bd-klko9). Its database section reads
GetAllConfig raw rather than through the role, so it inherited neither
filter, and it prints values in FULL because it is the operator's
provenance view — every stored memory went into every terminal and pasted
bug report that ran the command. Nothing replaces the view; `bd kv list`
and `bd memories` are the surfaces that own those rows.

The writes are deliberately untouched: SetSetting and UnsetSetting still
take a verbatim `kv.` key, which keeps the escape hatch for a wedged
memory, and a write discloses nothing.

RunWorkspaceConfigPointReadRefusesTheKVPlane pins it on all three legs. It
reads the config table out of band around the refusal, because "the row is
there and the read refuses it" has to be a different fact from "there is no
row", and it asserts a settings key still reads back so a role that
answered "" to everything cannot pass. Reverting either half of the
production refusal turns it red on dolt, embeddeddolt and uow.

bd-rfwtv, bd-klko9

* test(memory): move the proxied leg onto the new settings-plane contract

The proxied memory journey asserted that `bd config get kv.memory.<key>`
returns the memory content, with a comment calling it "visible via bd
config get". That is exactly the behaviour this branch removes, so the
case was pinning the old contract from a shard that does not run locally
-- which is why it stayed green until CI reached it.

It now asserts the new one: the point read answers as an unset key does,
and the content does not appear in the output. The rest of the journey is
untouched and still proves the memory is readable through the surfaces
that own it -- `bd memories` lists it, `bd recall` returns it
byte-for-byte -- so this is the proxied leg of the same firewall the role
contract pins, not a weakened assertion.

Also records in the CHANGELOG what the change does NOT buy. `bd serve`
has no authentication and serves every memory in full through
/v0/beads/memories by design, so this is plane hygiene -- keeping user
data out of the routes that get republished into agent transcripts -- and
not a confidentiality boundary against anyone who can reach the port.

---------

Co-authored-by: Review Sim <ci@beads.test>
2026-08-09 11:06:51 -07:00

193 lines
7.1 KiB
Go

//go:build cgo
package main
import (
"strings"
"testing"
)
func bdProxiedMem(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
stdout, stderr, err := bdProxiedRunBuffers(t, bd, dir, args...)
if err != nil {
t.Fatalf("bd %s failed: %v\nstdout:\n%s\nstderr:\n%s",
strings.Join(args, " "), err, stdout, stderr)
}
return stdout
}
func bdProxiedMemFail(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
stdout, stderr, err := bdProxiedRunBuffers(t, bd, dir, args...)
if err == nil {
t.Fatalf("expected bd %s to fail; got stdout:\n%s\nstderr:\n%s",
strings.Join(args, " "), stdout, stderr)
}
return stdout + stderr
}
func TestProxiedServerMemory(t *testing.T) {
requireSharedProxiedServer(t)
t.Parallel()
bd := buildEmbeddedBD(t)
t.Run("remember_memories_forget_recall_journey", func(t *testing.T) {
t.Parallel()
p := newSharedProxiedProject(t, bd, "pmem")
// Multi-line content must round-trip byte-for-byte through recall.
content := "Dolt phantom DBs hide in three places:\n 1. the data dir\n 2. the global config\n 3. the server cache"
out := bdProxiedMem(t, bd, p.dir, "remember", content, "--key", "dolt-phantoms")
if !strings.Contains(out, "Remembered [dolt-phantoms]") {
t.Errorf("expected 'Remembered [dolt-phantoms]', got: %s", out)
}
// Auto-generated key from prose content.
out = bdProxiedMem(t, bd, p.dir, "remember", "always run tests with -race flag")
if !strings.Contains(out, "Remembered [always-run-tests-with-race-flag]") {
t.Errorf("expected slug key in output, got: %s", out)
}
// Memories land under kv.memory.<key> in the config table — the
// namespace `bd export --all` sweeps — but the settings plane does not
// serve them. A point read answers exactly as an unset key does, so a
// caller cannot tell a refusal from an absence. This is the proxied
// leg of the same firewall the role contract pins; recall below is the
// route that still reads the content.
out = bdProxiedMem(t, bd, p.dir, "config", "get", "kv.memory.dolt-phantoms")
if !strings.Contains(out, "(not set)") {
t.Errorf("expected kv.memory.dolt-phantoms to read as unset through config get, got: %q", out)
}
if strings.Contains(out, content) {
t.Errorf("config get leaked memory content: %q", out)
}
// List all.
out = bdProxiedMem(t, bd, p.dir, "memories")
if !strings.Contains(out, "Memories (2):") {
t.Errorf("expected 'Memories (2):', got: %s", out)
}
for _, key := range []string{"dolt-phantoms", "always-run-tests-with-race-flag"} {
if !strings.Contains(out, key) {
t.Errorf("expected %q in memories listing: %s", key, out)
}
}
// Keyword search matches content, not just keys.
out = bdProxiedMem(t, bd, p.dir, "memories", "phantom")
if !strings.Contains(out, "dolt-phantoms") {
t.Errorf("expected search hit for 'phantom': %s", out)
}
if strings.Contains(out, "always-run-tests-with-race-flag") {
t.Errorf("search should not match unrelated memory: %s", out)
}
// Recall returns the full content byte-for-byte (listing truncates).
out = bdProxiedMem(t, bd, p.dir, "recall", "dolt-phantoms")
if out != content+"\n" {
t.Errorf("recall not byte-preserving:\nwant: %q\ngot: %q", content+"\n", out)
}
// Update in place via explicit --key.
out = bdProxiedMem(t, bd, p.dir, "remember", "updated insight", "--key", "dolt-phantoms")
if !strings.Contains(out, "Updated [dolt-phantoms]") {
t.Errorf("expected 'Updated [dolt-phantoms]', got: %s", out)
}
out = bdProxiedMem(t, bd, p.dir, "recall", "dolt-phantoms")
if out != "updated insight\n" {
t.Errorf("expected updated content, got: %q", out)
}
// Forget removes it.
out = bdProxiedMem(t, bd, p.dir, "forget", "dolt-phantoms")
if !strings.Contains(out, "Forgot [dolt-phantoms]") {
t.Errorf("expected 'Forgot [dolt-phantoms]', got: %s", out)
}
out = bdProxiedMem(t, bd, p.dir, "memories")
if strings.Contains(out, "dolt-phantoms") {
t.Errorf("expected dolt-phantoms gone after forget: %s", out)
}
// Forget of a missing key reports not-found and exits nonzero,
// same as classic.
out = bdProxiedMemFail(t, bd, p.dir, "forget", "dolt-phantoms")
if !strings.Contains(out, "No memory with key") {
t.Errorf("expected 'No memory with key' on double forget, got: %s", out)
}
// Recall of a missing key likewise.
out = bdProxiedMemFail(t, bd, p.dir, "recall", "dolt-phantoms")
if !strings.Contains(out, "No memory with key") {
t.Errorf("expected 'No memory with key' on missing recall, got: %s", out)
}
})
t.Run("remember_bare_key_desire_path", func(t *testing.T) {
t.Parallel()
p := newSharedProxiedProject(t, bd, "pmemb")
bdProxiedMem(t, bd, p.dir, "remember", "the auth module uses JWT not sessions", "--key", "auth-jwt")
// A bare existing key READS instead of writing (= bd recall).
stdout, stderr, err := bdProxiedRunBuffers(t, bd, p.dir, "remember", "auth-jwt")
if err != nil {
t.Fatalf("bare-existing-key remember should recall, got error: %v\n%s%s", err, stdout, stderr)
}
if stdout != "the auth module uses JWT not sessions\n" {
t.Errorf("expected recalled content on stdout, got: %q", stdout)
}
if !strings.Contains(stderr, "a bare existing key READS") {
t.Errorf("expected desire-path notice on stderr, got: %q", stderr)
}
// A bare key naming nothing is refused, not stored.
out := bdProxiedMemFail(t, bd, p.dir, "remember", "no-such-memory-key")
if !strings.Contains(out, "refusing to store a bare key-like token") {
t.Errorf("expected bare-key refusal, got: %s", out)
}
out = bdProxiedMem(t, bd, p.dir, "memories")
if strings.Contains(out, "no-such-memory-key") {
t.Errorf("refused bare key must not be stored: %s", out)
}
// A bare bd command name is caught by the command guard.
out = bdProxiedMemFail(t, bd, p.dir, "remember", "list")
if !strings.Contains(out, "looks like a command") {
t.Errorf("expected command-word guard, got: %s", out)
}
// The two refusals either side of the desire path fire the same way
// here as on the direct route — one branch, one derivation, both
// routes. DeriveKey("") is "", so an insight nothing derives from
// would satisfy the bare-slug test and be READ instead of refused if
// the branch did not exclude the empty key.
for _, tc := range []struct{ insight, want string }{
{"", "memory content cannot be empty"},
{" ", "memory content cannot be empty"},
{"!!!", "could not generate key from content"},
} {
out = bdProxiedMemFail(t, bd, p.dir, "remember", tc.insight)
if !strings.Contains(out, tc.want) {
t.Errorf("bd remember %q: expected %q, got: %s", tc.insight, tc.want, out)
}
}
})
t.Run("remember_creates_dolt_commit", func(t *testing.T) {
t.Parallel()
p := newSharedProxiedProject(t, bd, "pmemc")
db := openProxiedDB(t, p)
before := proxiedDoltHead(t, db)
bdProxiedMem(t, bd, p.dir, "remember", "one write is one commit", "--key", "tx-shape")
after := proxiedDoltHead(t, db)
if after == before {
t.Errorf("HEAD did not advance on remember: before=%s after=%s", before, after)
}
if n := proxiedDoltCommitCountSince(t, db, before); n != 1 {
t.Errorf("expected exactly 1 Dolt commit for remember, got %d", n)
}
})
}