beads/internal/httpclient/open_with_test.go
Julian Knutsen 6c1848a77e
feat(backend/http): per-open credential is the only credential (S6f) (#7469)
* feat(backend/http): per-open credential is the only credential (S6f)

One process may open several beads HTTP projects, on one server or many,
each with its own credential (gc G7: per-city/per-rig credentials). The
ambient ladder is keyed by host[:port] and read from process-global env,
so two projects behind one host would share a token.

- bdhttp.Options gains Credential (CredentialProvider). Open, Handshake
  and Connect with it set go through the new httpclient.DialWithCredential
  / HandshakeWithCredential: no bearer ladder (BEADS_HTTP_TOKEN,
  BEADS_HTTP_TOKEN_COMMAND, credentials file), CA pinned to Target.CAFile
  (never BEADS_HTTP_CA_FILE), and the plaintext grant only from
  Target/DialOptions (never BEADS_HTTP_ALLOW_INSECURE).
- httpclient.OpenWith takes the same explicit path for a
  ProvidedCredential in backends.OpenOptions.
- Register panics on a non-nil Options.Credential: its dialer is
  process-wide.
- All package env reads go through one getenv seam so a test can prove
  the explicit door reads none. No process-wide credential cache exists;
  the provider lives on the store's own wire client.
- Package doc gains a "Credentials: one per open, never one per host"
  section.

Tests: two stores on one host (direct door and registry OpenWith) each
send their own token; explicit credential overrides poisoned ambient
token/command/credentials file; explicit open and handshake survive a
broken ambient env; Handshake with a Credential; getenv spy sees no reads
on the explicit door; Register refuses a credential.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(httpclient): S6f review fixups for the explicit door's plaintext refusal

Apply the two minor findings and two nits from the review of #7469:

- The explicit-credential door (DialWithCredential: OpenWith with a
  ProvidedCredential, and backend/http's Open and Handshake with
  Options.Credential) never reads BEADS_HTTP_ALLOW_INSECURE, but its
  plaintext refusal still said to set it. That refusal now names
  Target.AllowInsecureCredential (what `bd connect --allow-plaintext`
  records) and says an explicit credential takes no plaintext grant from
  the environment. The ambient ladder's refusal is unchanged.
- Pin that property at each production door. Every explicit-door test
  dialed loopback, where the guard returns before it reads the variable,
  so a door reverted to the env-consulting DialWith left them all green.
  TestExplicitCredentialIgnoresTheEnvPlaintextGrant drives OpenWith, Open
  and Handshake against a plain-http, non-loopback stand-in with
  BEADS_HTTP_ALLOW_INSECURE=1: each refuses before the wire and names the
  grant it honors, each sends its token once with
  Target.AllowInsecureCredential, and an ambient control shows the same
  environment does grant the built-in ladder. Reverting any one door to
  DialWith now fails that door's subtest, and the guard unit test checks
  the refusal text.
- Narrow the "reads no environment" docs to what holds: getenv is the
  reader for the variables that choose or grant a credential, and the
  explicit door reads none of them. The proxy variables, and on !unix with
  a CA file BEADS_ALLOW_INSECURE_CA_FILE_PERMISSIONS, are still read on
  every door; neither chooses a credential or grants plaintext.
- TestExplicitCredentialReadsNoEnvironment checked GetMetadata's error,
  which is nil on a failed handshake; it now checks the project id.

Behaviour change against main: OpenWith with a ProvidedCredential to a
plain-http, non-loopback server is refused even with
BEADS_HTTP_ALLOW_INSECURE=1, which main honored there. Grant it per
target with Target.AllowInsecureCredential (`bd connect --allow-plaintext`
records it in the workspace sidecar).

Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 15:16:14 +00:00

242 lines
9.9 KiB
Go

// Written fresh for OSS beads S6 (no bd-enterprise source copied).
package httpclient
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"net/url"
"path/filepath"
"sort"
"strings"
"sync"
"testing"
"github.com/steveyegge/beads/internal/storage/backends"
)
// literalSecretProvider is a CredentialProvider whose unexported field holds
// the literal secret value, the same shape a real token-bearing provider
// takes. It exists only so a test can grep for that literal in an error
// string: fmt's %+v prints unexported struct fields by value, so if OpenWith
// (or anything it calls) ever formats opts.Credential directly into an error
// — rather than formatting only the target and the wrapped dial error, as
// open_with.go's "dialing %s: %w" does today — this field's value leaks into
// logs and terminals, exactly what wire.CredentialProvider's own doc comment
// on Authorize forbids ("The error must not carry the credential").
type literalSecretProvider struct {
secret string
}
func (literalSecretProvider) Authorize(context.Context, *http.Request) error { return nil }
func (literalSecretProvider) Refresh(context.Context) (bool, error) { return false, nil }
// TestOpenWithDialErrorNeverFormatsTheCredential kills the mutation that
// widens open_with.go's dial-failure wrap from
// `fmt.Errorf("dialing %s: %w", target, err)` to one that also interpolates
// opts.Credential (e.g. "dialing %s cred=%+v: %w"). A credential a caller
// supplies through OpenOptions.Credential must never surface in an error this
// package hands back — the error travels into the embedder's own logs and
// terminals, same as the doc comment on CredentialProvider.Authorize says.
//
// The dial target names a CA file that does not exist, which DialWith (via
// dialTransport) refuses before opening any socket: a config-time error, not
// a network one, so the test stays fast and never reaches any real network
// — the standing safety rule for this suite — while still giving OpenWith a
// real, non-nil error from the exact call it wraps.
func TestOpenWithDialErrorNeverFormatsTheCredential(t *testing.T) {
beadsDir := t.TempDir()
base, err := url.Parse("http://127.0.0.1:1/")
if err != nil {
t.Fatalf("parse base url: %v", err)
}
target := Target{BaseURL: base, CAFile: filepath.Join(beadsDir, "no-such-ca.pem")}
if err := SaveTarget(beadsDir, target); err != nil {
t.Fatalf("SaveTarget: %v", err)
}
const secret = "KILL-ME-SECRET-1234"
opts := backends.OpenOptions{
Credential: ProvidedCredential{Provider: literalSecretProvider{secret: secret}},
}
_, err = OpenWith(context.Background(), beadsDir, opts, DialOptions{}, false)
if err == nil {
t.Fatal("OpenWith against an unreachable loopback port returned no error; cannot assert what its error does, or does not, carry")
}
if strings.Contains(err.Error(), secret) {
t.Errorf("OpenWith's error carries the literal credential secret %q: %v", secret, err)
}
}
// fixedTokenProvider signs every request with one token. It caches nothing
// beyond its own field, so what a server sees is exactly what the store that
// holds it was handed.
type fixedTokenProvider struct{ token string }
func (p fixedTokenProvider) Authorize(_ context.Context, req *http.Request) error {
req.Header.Set("Authorization", "Bearer "+p.token)
return nil
}
func (fixedTokenProvider) Refresh(context.Context) (bool, error) { return false, nil }
// contextOnlyServer answers the v0 handshake for one project and records each
// request's Authorization header.
func contextOnlyServer(t *testing.T, project string) (*httptest.Server, func() []string) {
t.Helper()
var mu sync.Mutex
var auths []string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
auths = append(auths, r.Header.Get("Authorization"))
mu.Unlock()
if r.URL.Path != "/v0/beads/context" {
http.NotFound(w, r)
return
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"api_version":"v0","backend":"dolt","bd_version":"9.9.9",` +
`"beads_dir":"/srv/.beads","capabilities":["issues.get"],` +
`"database":"beads","dolt_mode":"server","project_id":"` + project + `","repo_root":"/srv",` +
`"schema_version":1,"wire_revision":1,"min_client_wire_revision":1}`))
}))
t.Cleanup(srv.Close)
return srv, func() []string {
mu.Lock()
defer mu.Unlock()
return append([]string(nil), auths...)
}
}
// spyGetenv swaps getenv, the package's reader for the variables that choose
// or grant a credential, for a recorder for the life of t. It must not be used from a parallel test: getenv is a package
// variable, and top-level tests that call t.Parallel only resume after every
// sequential test (this one included) has finished and restored it.
func spyGetenv(t *testing.T) func() []string {
t.Helper()
var mu sync.Mutex
seen := map[string]bool{}
prev := getenv
getenv = func(name string) string {
mu.Lock()
seen[name] = true
mu.Unlock()
// Answer "1" for every name: were any of them consulted, the
// plaintext opt-in would read as granted and a malformed token or
// CA pattern would fail the dial, so a leak shows as behaviour as
// well as in the record.
return "1"
}
t.Cleanup(func() { getenv = prev })
return func() []string {
mu.Lock()
defer mu.Unlock()
out := make([]string, 0, len(seen))
for name := range seen {
out = append(out, name)
}
sort.Strings(out)
return out
}
}
// TestExplicitCredentialReadsNoEnvironment pins the explicit-credential door's
// promise: a dial, a handshake and a registry OpenWith handed their own
// credential consult none of the variables that choose or grant one — no
// token, no token command, no CA pattern, no plaintext opt-in. The control
// dial through the ambient ladder proves the spy actually sees reads.
func TestExplicitCredentialReadsNoEnvironment(t *testing.T) {
srv, auths := contextOnlyServer(t, "proj-env")
base, err := url.Parse(srv.URL)
if err != nil {
t.Fatalf("parse server URL: %v", err)
}
target := Target{BaseURL: base, ExpectProjectID: "proj-env"}
creds := fixedTokenProvider{token: "explicit"}
ctx := context.Background()
seen := spyGetenv(t)
conn, err := DialWithCredential(target, creds, DialOptions{})
if err != nil {
t.Fatalf("DialWithCredential: %v", err)
}
if _, err := conn.ServerContext(ctx); err != nil {
t.Fatalf("ServerContext: %v", err)
}
if _, err := HandshakeWithCredential(ctx, target, creds, DialOptions{}); err != nil {
t.Fatalf("HandshakeWithCredential: %v", err)
}
beadsDir := t.TempDir()
if err := SaveTarget(beadsDir, target); err != nil {
t.Fatalf("SaveTarget: %v", err)
}
store, err := OpenWith(ctx, beadsDir, backends.OpenOptions{Credential: ProvidedCredential{Provider: creds}}, DialOptions{}, false)
if err != nil {
t.Fatalf("OpenWith: %v", err)
}
t.Cleanup(func() { _ = store.Close() })
// GetMetadata reports a failed handshake as ("", nil), so the project id
// is the evidence this open reached the server at all.
if got, err := store.GetMetadata(ctx, "_project_id"); err != nil || got != "proj-env" {
t.Fatalf("GetMetadata = (%q, %v), want proj-env from the server", got, err)
}
// A plain-http, non-loopback target is where the guard would consult
// BEADS_HTTP_ALLOW_INSECURE; on this door it must not. Dialing builds the
// guard without opening a socket.
remote := Target{BaseURL: mustParseURL(t, "http://"+nonLoopbackTestHost+"/")}
if _, err := DialWithCredential(remote, creds, DialOptions{}); err != nil {
t.Fatalf("DialWithCredential(non-loopback): %v", err)
}
if got := seen(); len(got) != 0 {
t.Errorf("the explicit-credential door read the environment: %v", got)
}
for i, got := range auths() {
if got != "Bearer explicit" {
t.Errorf("request %d carried Authorization %q, want the explicit credential", i, got)
}
}
// Control: the ambient door does read the environment, so an empty
// record above is evidence rather than a spy that never fires.
// Its outcome is irrelevant (the spy's "1" is not a valid CA pattern).
_, _ = Dial(remote, DialOptions{})
if got := seen(); len(got) == 0 {
t.Fatal("the spy saw no reads from the ambient Dial either; it is not wired to the package's env reads")
}
}
// TestDialWithCredentialRefusesNil keeps "explicit" from meaning "nothing":
// a nil provider on this door is ErrCredentialRequired, not an
// unauthenticated dial and not a fall-back to the ambient ladder.
func TestDialWithCredentialRefusesNil(t *testing.T) {
target := Target{BaseURL: mustParseURL(t, "http://127.0.0.1:1/")}
if _, err := DialWithCredential(target, nil, DialOptions{}); !errors.Is(err, ErrCredentialRequired) {
t.Fatalf("DialWithCredential(nil) = %v, want ErrCredentialRequired", err)
}
}
// TestGuardInsecureCredentialIgnoresEnvWhenNotConsulted is the guard half of
// the explicit door: with consultEnv false, BEADS_HTTP_ALLOW_INSECURE=1 grants
// nothing, the refusal wrapper stays on, and its refusal names the grant that
// door honors instead of the variable it never reads.
func TestGuardInsecureCredentialIgnoresEnvWhenNotConsulted(t *testing.T) {
t.Setenv(AllowInsecureCredentialEnv, "1")
target := Target{BaseURL: mustParseURL(t, "http://"+nonLoopbackTestHost+"/")}
guarded := guardInsecureCredential(target, stubProvider{tag: "explicit"}, false, false)
if _, ok := guarded.(*insecureCredentialGuard); !ok {
t.Fatalf("guardInsecureCredential(consultEnv=false) returned %T, want the refusal wrapper despite %s=1", guarded, AllowInsecureCredentialEnv)
}
err := guarded.Authorize(context.Background(), newTestRequest(t, "http://"+nonLoopbackTestHost+"/v0/beads/context"))
if err == nil || !strings.Contains(err.Error(), "refusing to send a credential") {
t.Fatalf("Authorize = %v, want the plaintext refusal", err)
}
if msg := err.Error(); strings.Contains(msg, AllowInsecureCredentialEnv) || !strings.Contains(msg, "Target.AllowInsecureCredential") {
t.Errorf("refusal %q must name Target.AllowInsecureCredential and not %s, which this door never reads", msg, AllowInsecureCredentialEnv)
}
}