Skip to content
quarry Artifact [428c8d084a]
Login

Artifact [428c8d084a]

Artifact 428c8d084a33f626b75fac224b7109bbecf82813b8b8be277645f979a7d89328:


package web

import (
	"errors"
	"html"
	"net/http"
	"net/http/httptest"
	"net/url"
	"path/filepath"
	"regexp"
	"strings"
	"testing"

	"craftdesign.group/quarry/internal/api"
	"craftdesign.group/quarry/internal/index"
	"craftdesign.group/quarry/internal/oltp"
	"craftdesign.group/quarry/internal/ops"
)

// presetCaps is the letters a role this form offers writes, read from
// the preset table the form itself offers, so a role that gains or loses
// a letter cannot leave the token ceiling silently stale.
func presetCaps(t *testing.T, role string) string {
	t.Helper()
	for _, p := range oltp.Presets {
		if strings.EqualFold(p.Name, role) {
			return p.Caps
		}
	}
	t.Fatalf("FAIL: no preset named %q", role)
	return ""
}

// repoAgentTokenOf reads the one token an agent holds, failing when it
// holds any other count.
func repoAgentTokenOf(t *testing.T, db *oltp.DB, agent string) oltp.Token {
	t.Helper()
	toks, err := db.Tokens(agent)
	if err != nil {
		t.Fatal(err)
	}
	if len(toks) != 1 {
		t.Fatalf("FAIL: %s holds %d tokens, want exactly 1", agent, len(toks))
	}
	return toks[0]
}

// shownToken reads the value the once panel displayed, from the only
// place a token value ever exists: the page body.
var shownTokenRe = regexp.MustCompile(`data-hook="repo-agent-token-value"[^>]*>([^<]+)<`)

func shownToken(t *testing.T, body string) string {
	t.Helper()
	m := shownTokenRe.FindStringSubmatch(body)
	if m == nil {
		t.Fatalf("FAIL: no agent token value in the page")
	}
	return strings.TrimSpace(m[1])
}

// tplBlock reads the text of the element tagged data-hook=hook, so a
// test can assert on the recipe a reader would copy without depending
// on the page's display wording elsewhere.
func tplBlock(t *testing.T, body, hook string) string {
	t.Helper()
	re := regexp.MustCompile(`data-hook="` + regexp.QuoteMeta(hook) + `"[^>]*>([^<]*)<`)
	m := re.FindStringSubmatch(body)
	if m == nil {
		return ""
	}
	return strings.TrimSpace(html.UnescapeString(m[1]))
}

// failingMintAccts is the real store with only the mint broken, which is
// the one step that can fail after the agent and its grant are written.
type failingMintAccts struct{ agentAccts }

func (failingMintAccts) MintToken(string, oltp.TokenSpec) (string, oltp.Token, error) {
	return "", oltp.Token{}, errors.New("the token seal is unavailable")
}

// repoAgentForgeWith is repoAgentForge whose account store is built by
// accounts from the fixture's own db, so a test can fail one step of the
// create/grant/mint sequence while reading the same rows the pages do.
func repoAgentForgeWith(t *testing.T, accounts func(*oltp.DB) api.AccountStore) (http.Handler, *oltp.DB) {
	t.Helper()
	dir := t.TempDir()
	ix, err := index.Open(filepath.Join(dir, "index.db"))
	if err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() { ix.Close() })
	db, err := oltp.Open(filepath.Join(dir, "oltp.db"))
	if err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() { db.Close() })
	for _, who := range []string{"root", "jva"} {
		if err := db.AddAccount(who, "pw-"+who+"-12345", who == "root"); err != nil {
			t.Fatal(err)
		}
	}
	if err := db.AddRepo("alpha", t.TempDir(), "", oltp.Public); err != nil {
		t.Fatal(err)
	}
	if _, err := db.GrantGuarded("root", "jva", "alpha", oltp.RepoAdmin); err != nil {
		t.Fatal(err)
	}
	st := api.AccountStore(agentAccts{db})
	if accounts != nil {
		st = accounts(db)
	}
	prefs := newMemPrefs()
	r := ops.NewRegistry()
	if err := api.RegisterAll(r, api.Deps{Version: "t",
		Repos:    api.NewRepos(map[string]*api.Repo{"alpha": {Index: ix, Code: "c1", Chat: "c1"}}),
		Prefs:    prefs,
		Accounts: st, AgentsPerPerson: 3,
		CapsFor: func(a, repo string) api.CapSet { return db.EffectiveCaps(a, repo) },
	}); err != nil {
		t.Fatal(err)
	}
	h := HandlerWith(r, nil, nil, []Option{
		WithSessions(fakeSessions{user: "jva", roles: map[string]ops.Auth{"alpha": ops.Admin}}), WithPrefs(prefs)},
		Verify(func(string, string, string, ops.Auth) api.Decision { return api.Denied }))
	return h, db
}

// The token is optional: the form offers the choice, starts it off, and
// a create that does not ask for one mints nothing.
func TestARepositoryAgentTokenIsOptionalAndOffByDefault(t *testing.T) {
	h, db := repoAgentForge(t)

	body := repoAgentDo(t, h, "GET", "/r/alpha/settings", nil).Body.String()
	if !hasHook(body, "repo-agent-with-token") {
		t.Error("FAIL: the form offers no optional push token")
	}
	if strings.Contains(body, `name="with_token" value="1" checked`) {
		t.Error("FAIL: the push token starts checked")
	}

	rec := repoAgentDo(t, h, "POST", "/r/alpha/settings/agents", url.Values{"name": {"quiet-bot"}, "role": {"developer"}})
	if rec.Code != http.StatusSeeOther || !strings.HasPrefix(rec.Header().Get("Location"), "/me/agents/quiet-bot") {
		t.Fatalf("FAIL: the unchecked create answered %d to %q", rec.Code, rec.Header().Get("Location"))
	}
	if toks, err := db.Tokens("quiet-bot"); err != nil || len(toks) != 0 {
		t.Errorf("FAIL: an unchecked create minted %d tokens: %v", len(toks), err)
	}
	t.Log("ok: the push token is a choice, and unchecked mints nothing")
}

// Asking for a token mints exactly one: sync only, this repository only,
// cut to the role picked, shown once beside a runnable recipe that never
// puts the value in the command, the argv or a saved file.
func TestARepositoryAgentTokenIsShownOnceBesideItsCommand(t *testing.T) {
	h, db := repoAgentForge(t)

	rec := repoAgentDo(t, h, "POST", "/r/alpha/settings/agents",
		url.Values{"name": {"push-bot"}, "role": {"developer"}, "with_token": {"1"}})
	loc := rec.Header().Get("Location")
	if rec.Code != http.StatusSeeOther || !strings.Contains(loc, "#agents") {
		t.Fatalf("FAIL: the create answered %d to %q", rec.Code, loc)
	}
	if strings.Contains(loc, "qat_") {
		t.Errorf("FAIL: the token value travelled in a URL: %q", loc)
	}
	tok := repoAgentTokenOf(t, db, "push-bot")
	if strings.Join(tok.Scopes, ",") != "sync" || strings.Join(tok.Repos, ",") != "alpha" {
		t.Errorf("FAIL: minted scopes %v repos %v, want sync on alpha only", tok.Scopes, tok.Repos)
	}
	if tok.Ceiling != presetCaps(t, "developer") {
		t.Errorf("FAIL: the ceiling is %q, want developer's %q", tok.Ceiling, presetCaps(t, "developer"))
	}

	// A browser does not send the fragment; httptest keeps it in the raw
	// query, so strip it before following the link.
	page := repoAgentDo(t, h, "GET", strings.SplitN(loc, "#", 2)[0], nil)
	if page.Code != 200 || !hasHook(page.Body.String(), "repo-agent-token-once") {
		t.Fatalf("FAIL: the redirect target answered %d without the once panel", page.Code)
	}
	body := page.Body.String()
	value := shownToken(t, body)
	if !strings.HasPrefix(value, "qat_") {
		t.Errorf("FAIL: the shown value is not a token: %q", value)
	}
	// A runnable recipe that reads the secret without echo into the
	// environment, and names the exact sync endpoint, the agent, --once.
	// A developer's recipe is the push one, which begins with the clone.
	recipe := tplBlock(t, body, "repo-agent-push-command")
	for _, want := range []string{"printf 'Paste the token", "read -rs ZIT_PASSWORD", "export ZIT_PASSWORD", "unset ZIT_PASSWORD", "zit clone", "http://", "zit push", "--user 'push-bot'", "--once"} {
		if !strings.Contains(recipe, want) {
			t.Errorf("FAIL: the recipe is missing %q: %q", want, recipe)
		}
	}
	// The exact URL is repeated on clone AND push, because clone --once
	// saves no remote for a later command to use.
	if strings.Count(recipe, "http://example.com/r/alpha") < 2 {
		t.Errorf("FAIL: the exact URL is not on both clone and push: %q", recipe)
	}
	// No heredoc: it would eat the read's stdin.
	if strings.Contains(recipe, "<<") || strings.Contains(recipe, "read -rs -p") {
		t.Errorf("FAIL: the recipe uses a stdin-stealing or zsh-broken form: %q", recipe)
	}
	// The value itself never appears in a command, and no command assigns
	// it literally, which would put it in shell history or argv.
	if strings.Contains(recipe, value) || strings.Contains(recipe, "ZIT_PASSWORD=qat_") {
		t.Errorf("FAIL: a command carries the value or a literal assignment: %q", recipe)
	}

	// The one render carrying the token is unstoreable and leaks no
	// Referer, matching the agent-token pages.
	if got := page.Header().Get("Cache-Control"); got != "no-store" {
		t.Errorf("FAIL: the token page Cache-Control is %q, want no-store", got)
	}
	if got := page.Header().Get("Referrer-Policy"); got != "no-referrer" {
		t.Errorf("FAIL: the token page Referrer-Policy is %q, want no-referrer", got)
	}

	// Shown once: a reload of the same collected link yields nothing.
	if again := repoAgentDo(t, h, "GET", strings.SplitN(loc, "#", 2)[0], nil).Body.String(); strings.Contains(again, value) {
		t.Error("FAIL: the token survived a second render")
	}
	t.Log("ok: one sync-only, repository-limited token, shown once beside a runnable recipe")
}

// A reader's token is a genuine sync credential but it cannot push, and
// the page says so plainly rather than handing a reader a push recipe it
// cannot run.
func TestAReaderRepositoryAgentTokenCannotPush(t *testing.T) {
	h, db := repoAgentForge(t)
	rec := repoAgentDo(t, h, "POST", "/r/alpha/settings/agents",
		url.Values{"name": {"read-bot"}, "role": {"reader"}, "with_token": {"1"}})
	loc := rec.Header().Get("Location")
	if rec.Code != http.StatusSeeOther || !strings.Contains(loc, "#agents") {
		t.Fatalf("FAIL: the create answered %d to %q", rec.Code, loc)
	}
	tok := repoAgentTokenOf(t, db, "read-bot")
	if tok.Ceiling != presetCaps(t, "reader") {
		t.Errorf("FAIL: the ceiling is %q, want reader's %q", tok.Ceiling, presetCaps(t, "reader"))
	}
	if c, ok := db.TokenCredential("alpha", tok); ok || c.CheckIn || c.Publish {
		t.Errorf("FAIL: a reader's token can write: %+v ok=%v", c, ok)
	}
	body := repoAgentDo(t, h, "GET", strings.SplitN(loc, "#", 2)[0], nil).Body.String()
	recipe := tplBlock(t, body, "repo-agent-clone-command")
	if recipe == "" {
		t.Fatal("FAIL: a reader's token is not shown with the pull recipe it can run")
	}
	if !strings.Contains(recipe, "zit pull") || !strings.Contains(recipe, "--once") {
		t.Errorf("FAIL: a reader's recipe does not pull the exact URL with --once: %q", recipe)
	}
	if strings.Contains(recipe, "zit push") {
		t.Errorf("FAIL: a reader's recipe pushes, which its token cannot: %q", recipe)
	}
	if hasHook(body, "repo-agent-push-command") {
		t.Error("FAIL: a reader is handed a push recipe its token cannot run")
	}
	if !hasHook(body, "repo-agent-no-push") {
		t.Error("FAIL: the page does not say a reader's token cannot push")
	}
	t.Log("ok: a reader's token is shown for what it is, not as pushing power")
}

// The agent never holds more than the caller, and the token is cut to the
// role and to the repository: it cannot reach another one, and each
// offered role is capped by its own preset.
func TestARepositoryAgentTokenNeverExceedsItsRoleOrRepository(t *testing.T) {
	h, db := repoAgentForge(t)
	if err := db.AddRepo("beta", t.TempDir(), "", oltp.Public); err != nil {
		t.Fatal(err)
	}
	callerCaps := db.EffectiveCaps("jva", "alpha")
	for i, role := range []string{"reader", "developer", "maintainer"} {
		agent := "cap-bot-" + string(rune('a'+i))
		rec := repoAgentDo(t, h, "POST", "/r/alpha/settings/agents",
			url.Values{"name": {agent}, "role": {role}, "with_token": {"1"}})
		if rec.Code != http.StatusSeeOther || strings.Contains(rec.Header().Get("Location"), "e=") {
			t.Fatalf("FAIL: %s was not made: %d %q", role, rec.Code, rec.Header().Get("Location"))
		}
		tok := repoAgentTokenOf(t, db, agent)
		if tok.Ceiling != presetCaps(t, role) {
			t.Errorf("FAIL: %s's token ceiling is %q, want %q", role, tok.Ceiling, presetCaps(t, role))
		}
		if _, ok := db.TokenCredential("beta", tok); ok {
			t.Errorf("FAIL: %s's token reaches beta", role)
		}
		for _, l := range db.TokenCaps(tok, "alpha") {
			if !callerCaps.HasAny(string(l)) {
				t.Errorf("FAIL: %s's token holds %c, which its maker does not", role, l)
			}
		}
	}
	t.Log("ok: each token is cut to its role, its repository, and its maker")
}

// Every way the sequence can stop is reported as it really is: an agent
// whose grant failed mints nothing, and an agent whose mint failed is
// left made and holding its role, with no token and no second attempt.
func TestARepositoryAgentTokenIsNotMintedOnAFailedPath(t *testing.T) {
	h, db := repoAgentForgeHeld(t, "this repository is under a hold")
	rec := repoAgentDo(t, h, "POST", "/r/alpha/settings/agents",
		url.Values{"name": {"grantless-bot"}, "role": {"developer"}, "with_token": {"1"}})
	loc := rec.Header().Get("Location")
	if rec.Code != http.StatusSeeOther || !strings.Contains(loc, "e=") || strings.Contains(loc, "s=") {
		t.Fatalf("FAIL: a failed grant answered %d to %q", rec.Code, loc)
	}
	if toks, _ := db.Tokens("grantless-bot"); len(toks) != 0 {
		t.Errorf("FAIL: a failed grant minted %d tokens", len(toks))
	}

	// The mint fails after the agent and grant stand: the agent is left
	// made, holding its role, with no token, and the page says so.
	h2, db2 := repoAgentForgeWith(t, func(db *oltp.DB) api.AccountStore {
		return failingMintAccts{agentAccts{db}}
	})
	rec = repoAgentDo(t, h2, "POST", "/r/alpha/settings/agents",
		url.Values{"name": {"mintless-bot"}, "role": {"developer"}, "with_token": {"1"}})
	loc = rec.Header().Get("Location")
	if rec.Code != http.StatusSeeOther || !strings.Contains(loc, "e=") || strings.Contains(loc, "s=") {
		t.Fatalf("FAIL: a failed mint answered %d to %q", rec.Code, loc)
	}
	// The fragment is the browser's, outside the encoded query: an error
	// that swallowed it would show "#agents" as part of its sentence. The
	// decoded e value must not carry the anchor, and the fragment must be
	// the real one, appended after the query and outside it.
	if !strings.HasSuffix(loc, "#agents") {
		t.Errorf("FAIL: the anchor is not the redirect fragment: %q", loc)
	}
	if q, err := url.Parse(strings.SplitN(loc, "#", 2)[0]); err == nil {
		if msg := q.Query().Get("e"); strings.Contains(msg, "#agents") || strings.Contains(msg, "agents") {
			t.Errorf("FAIL: the error message carries the anchor: %q", msg)
		}
	}
	if db2.Operator("mintless-bot") != "repo:alpha" {
		t.Errorf("FAIL: a failed mint did not leave the agent made: %q", db2.Operator("mintless-bot"))
	}
	if !db2.EffectiveCaps("mintless-bot", "alpha").HasAny("i") {
		t.Error("FAIL: a failed mint left the agent without its role")
	}
	if toks, _ := db2.Tokens("mintless-bot"); len(toks) != 0 {
		t.Errorf("FAIL: a failed mint left %d tokens", len(toks))
	}
	t.Log("ok: a stopped sequence mints nothing and says so")
}

// A mint that fails is not retried, and the page's advice is not a loop:
// with the seal unavailable, "make one from its agent page" would hit the
// same failure, so the message says what is actually true instead.
func TestAFailedMintDoesNotPrescribeTheSameBrokenOperation(t *testing.T) {
	h, _ := repoAgentForgeWith(t, func(db *oltp.DB) api.AccountStore {
		return failingMintAccts{agentAccts{db}}
	})
	rec := repoAgentDo(t, h, "POST", "/r/alpha/settings/agents",
		url.Values{"name": {"nopush-bot"}, "role": {"developer"}, "with_token": {"1"}})
	q, err := url.Parse(strings.SplitN(rec.Header().Get("Location"), "#", 2)[0])
	if err != nil {
		t.Fatal(err)
	}
	msg := q.Query().Get("e")
	if msg == "" {
		t.Fatalf("FAIL: a failed mint carried no error: %q", rec.Header().Get("Location"))
	}
	if !strings.Contains(msg, "seal") {
		t.Errorf("FAIL: the message does not carry the real reason: %q", msg)
	}
	if strings.Contains(msg, "Make one from its agent page") {
		t.Errorf("FAIL: the advice prescribes the same broken mint: %q", msg)
	}
	t.Log("ok: a failed mint says what is true, not a loop back to the same failure")
}

// A request on a token manages no credentials: the repository form does
// not create an agent or mint a token, even when the token holds admin.
func TestARepositoryAgentTokenFormRefusesATokenCaller(t *testing.T) {
	h, db := repoAgentForge(t)
	inner := h
	bound := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		tok := ops.TokenAuth{ID: "t1", Caps: func(string) ops.Caps { return oltp.CapSet("a") }}
		inner.ServeHTTP(w, r.WithContext(ops.WithToken(r.Context(), tok)))
	})
	req := httptest.NewRequest("POST", "/r/alpha/settings/agents",
		strings.NewReader(url.Values{"name": {"tok-bot"}, "role": {"developer"}, "with_token": {"1"}}.Encode()))
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	req.AddCookie(&http.Cookie{Name: sessionCookie, Value: "good"})
	rec := httptest.NewRecorder()
	bound.ServeHTTP(rec, req)

	if db.Operator("tok-bot") != "" {
		t.Error("FAIL: a token-bound request created an agent")
	}
	if toks, _ := db.Tokens("tok-bot"); len(toks) != 0 {
		t.Errorf("FAIL: a token-bound request minted %d tokens", len(toks))
	}
	if rec.Code != http.StatusSeeOther || !strings.Contains(rec.Header().Get("Location"), "e=") {
		t.Errorf("FAIL: the refusal answered %d to %q", rec.Code, rec.Header().Get("Location"))
	}
	t.Log("ok: only a person's session creates the agent and its token")
}

// shellQuoteTestForm is the exact shape shellQuote must produce: the text
// inside single quotes, with an embedded quote spelled '\”. Reads the
// source so a "helpful" tidy-up to double quotes or %q cannot pass.
func TestShellQuoteWrapsAndEscapes(t *testing.T) {
	cases := map[string]string{
		"plain":        `'plain'`,
		"a b":          `'a b'`,
		`x'; rm -rf /`: `'x'\''; rm -rf /'`,
		"$(id)":        `'$(id)'`,
		"`id`":         "'`id`'",
		"http://h/r":   `'http://h/r'`,
	}
	for in, want := range cases {
		if got := shellQuote(in); got != want {
			t.Errorf("FAIL: shellQuote(%q) = %q, want %q", in, got, want)
		}
	}
	t.Log("ok: every dynamic shell argument is a single quoted word")
}

// A hostile agent name cannot break out of the recipe's quoting. The
// account store already refuses such a name at the door (validLogin), so
// this exercises the second gate directly: repoAgentRecipes must emit the
// name as ONE shell-quoted word whatever it contains, so a future loosening
// of the store's alphabet cannot turn a name into shell syntax. The string
// is asserted on, never run.
func TestAMaliciousAgentNameCannotInjectShell(t *testing.T) {
	_, push := repoAgentRecipes("http://h/r/alpha", `x'; touch /tmp/pwned; echo '`, presetCaps(t, "developer"))
	if push == "" {
		t.Fatal("FAIL: no recipe built")
	}
	if !strings.Contains(push, shellQuote(`x'; touch /tmp/pwned; echo '`)) {
		t.Errorf("FAIL: the name is not shell-quoted as one word: %q", push)
	}
	// Outside the quoted word, the dangerous fragment never appears bare,
	// so it cannot start a new command.
	if strings.Contains(push, "; touch /tmp/pwned; echo ' --user") {
		t.Errorf("FAIL: the name's metacharacters escape quoting: %q", push)
	}
	t.Log("ok: a hostile agent name is inert data, never shell syntax")
}

// A Host header with shell metacharacters reaches origin(), and so the
// exact sync URL in the recipe. It must be shell-quoted there too.
func TestAHostileHostCannotInjectShell(t *testing.T) {
	h, db := repoAgentForge(t)
	rec := repoAgentDo(t, h, "POST", "/r/alpha/settings/agents",
		url.Values{"name": {"host-bot"}, "role": {"developer"}, "with_token": {"1"}})
	loc := rec.Header().Get("Location")
	tok := repoAgentTokenOf(t, db, "host-bot")
	_ = tok
	// A Host header cannot carry spaces in a real request, but quotes and
	// $ are legal in the header value and reach origin() unchanged.
	evilHost := `evil';$(id);'`
	req := httptest.NewRequest("GET", strings.SplitN(loc, "#", 2)[0], nil)
	req.Host = evilHost
	req.AddCookie(&http.Cookie{Name: sessionCookie, Value: "good"})
	rec2 := httptest.NewRecorder()
	h.ServeHTTP(rec2, req)
	recipe := tplBlock(t, rec2.Body.String(), "repo-agent-push-command")
	if recipe == "" {
		t.Fatal("FAIL: no recipe rendered")
	}
	if strings.Contains(recipe, "http://"+evilHost) {
		t.Errorf("FAIL: the hostile Host is unquoted in the recipe: %q", recipe)
	}
	if !strings.Contains(recipe, shellQuote("http://"+evilHost+"/r/alpha")) {
		t.Errorf("FAIL: the exact URL is not shell-quoted: %q", recipe)
	}
	t.Log("ok: a hostile Host is inert data in the recipe URL")
}

// A nonce filed by one person for one repository is not a bearer a
// different person can redeem through another repository, and the failed
// attempt must NOT consume it: the rightful holder still gets the token.
func TestTheAgentTokenNonceIsBoundToItsPersonAndRepository(t *testing.T) {
	v := newAgentTokenVault()
	nonce := v.stash("jva", "alpha", agentTokenPayload{value: "qat_secret", agent: "bot", ceiling: "u"})
	if nonce == "" {
		t.Fatal("FAIL: nothing stashed")
	}
	if _, ok := v.take(nonce, "root", "alpha"); ok {
		t.Error("FAIL: another person redeemed the nonce")
	}
	if _, ok := v.take(nonce, "jva", "beta"); ok {
		t.Error("FAIL: another repository redeemed the nonce")
	}
	p, ok := v.take(nonce, "jva", "alpha")
	if !ok || p.value != "qat_secret" {
		t.Errorf("FAIL: an unauthorized attempt consumed the rightful secret: ok=%v %+v", ok, p)
	}
	if _, ok := v.take(nonce, "jva", "alpha"); ok {
		t.Error("FAIL: the nonce was reusable after the rightful holder spent it")
	}
	t.Log("ok: the nonce is bound to person and repository, and a wrong asker cannot spend it")
}