Skip to content
18 changes: 16 additions & 2 deletions bin/hive-review.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,14 @@

set -euo pipefail

REQ_DIR="/var/run/hive-metrics/review-requests"
# Inside a sandbox (Podman or a Kubernetes Job) the pod-local relay dir is not
# reachable; hive sets HIVE_REVIEW_REQUEST_DIR to the kick's workspace spool and
# relays from there, attributing the request to the kick's own agent whatever
# this script writes in "agent" (hivecommons/hive#14986). The spool is
# group-writable, so hive also sets HIVE_REVIEW_SPOOL_KEY, a per-run key, and
# accepts only a request MACed with it: the request is then written as
# {"request": "<request JSON>", "mac": "<hex HMAC-SHA256(key, request JSON)>"}.
REQ_DIR="${HIVE_REVIEW_REQUEST_DIR:-/var/run/hive-metrics/review-requests}"

REPO=""; NUMBER=""; EVENT=""; BODY=""; BODY_FILE=""; THREAD=""; VERDICT_FILE=""; VERDICT=""; REVISE=0
while [ $# -gt 0 ]; do
Expand Down Expand Up @@ -198,8 +205,15 @@ if verdict.strip():
% (i, obj.get("repo"), obj.get("number"), repo, number))
raise SystemExit(2)
req["report"] = verdict
data = json.dumps(req)
# Read from the environment, never argv: argv is visible to every user.
key = os.environ.get("HIVE_REVIEW_SPOOL_KEY", "")
if key:
import hashlib, hmac
mac = hmac.new(key.encode(), data.encode(), hashlib.sha256).hexdigest()
data = json.dumps({"request": data, "mac": mac})
with open(temporary, "w") as fh:
json.dump(req, fh)
fh.write(data)
os.replace(temporary, path)
PY

Expand Down
38 changes: 38 additions & 0 deletions bin/test_hive_review.sh
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,44 @@ else
echo " PASS: no .tmp file remains (atomic replace)"; PASS=$((PASS + 1))
fi

# --- sandbox spool (#14986): HIVE_REVIEW_REQUEST_DIR redirects the request ---
# Runs the UNMODIFIED script: the override is the script's own behavior.
echo ""
echo "--- sandbox spool via HIVE_REVIEW_REQUEST_DIR ---"
SPOOL_DIR="$TMPDIR_ROOT/workspace/.hive/review-requests"
env HIVE_AGENT="spoolbot" HIVE_REVIEW_REQUEST_DIR="$SPOOL_DIR" bash "$SCRIPT" --repo "org/repo" 12 --comment --body "ran on the card" >/dev/null 2>&1 || true
SPOOL_FILES=("$SPOOL_DIR"/spoolbot-*.json)
if [ -f "${SPOOL_FILES[0]}" ]; then
check "spooled request is a comment" "comment" "$(field "${SPOOL_FILES[0]}" event)"
check "spooled request names the PR" "12" "$(field "${SPOOL_FILES[0]}" number)"
else
echo " FAIL: request not written to HIVE_REVIEW_REQUEST_DIR"; FAIL=$((FAIL + 2))
fi

# --- sandbox spool key: the request is MACed with HIVE_REVIEW_SPOOL_KEY ---
echo ""
echo "--- sandbox spool signed with HIVE_REVIEW_SPOOL_KEY ---"
SIGNED_DIR="$TMPDIR_ROOT/signed/.hive/review-requests"
SPOOL_KEY="0123456789abcdef0123456789abcdef"
env HIVE_AGENT="spoolbot" HIVE_REVIEW_REQUEST_DIR="$SIGNED_DIR" HIVE_REVIEW_SPOOL_KEY="$SPOOL_KEY" bash "$SCRIPT" --repo "org/repo" 12 --comment --body "signed" >/dev/null 2>&1 || true
SIGNED_FILES=("$SIGNED_DIR"/spoolbot-*.json)
if [ -f "${SIGNED_FILES[0]}" ]; then
# The relay recomputes HMAC-SHA256(key, request) over the exact request text.
check "envelope MAC verifies under the run key" "ok" "$(python3 -c '
import hashlib, hmac, json, sys
env = json.load(open(sys.argv[1]))
want = hmac.new(sys.argv[2].encode(), env["request"].encode(), hashlib.sha256).hexdigest()
print("ok" if hmac.compare_digest(env["mac"], want) else "bad")' "${SIGNED_FILES[0]}" "$SPOOL_KEY")"
check "signed request is a comment" "comment" "$(python3 -c 'import json,sys; print(json.loads(json.load(open(sys.argv[1]))["request"])["event"])' "${SIGNED_FILES[0]}")"
if grep -q "$SPOOL_KEY" "${SIGNED_FILES[0]}"; then
echo " FAIL: the spool key itself was written into the spool"; FAIL=$((FAIL + 1))
else
echo " PASS: the spool key is not in the spool file"; PASS=$((PASS + 1))
fi
else
echo " FAIL: signed request not written to HIVE_REVIEW_REQUEST_DIR"; FAIL=$((FAIL + 3))
fi

echo ""
echo "=== $PASS passed, $FAIL failed ==="
[ "$FAIL" -eq 0 ]
1 change: 1 addition & 0 deletions changelog.d/added-14986-sandbox-review-spool.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- A sandboxed reviewer can now post reviews ([#14986](https://github.com/hivecommons/hive/issues/14986)). Every sandbox kick (Podman or `sandbox.runtime: job`) gets a review spool at `.hive/review-requests` in its workspace, and `hive-review` inside the sandbox writes there via `HIVE_REVIEW_REQUEST_DIR`. The review relay picks spooled requests up through the same policy path as the pod-local drop box, attributing each one to the kick's agent from a hive-owned manifest under `<work_dir>/sandbox-review-spools`, never from the file's contents. Because the spool is group-writable, each kick also gets its own random spool key (`HIVE_REVIEW_SPOOL_KEY`, passed only through the sandbox environment), and the relay accepts only requests that carry a valid HMAC under that key and haven't been accepted before, so another agent can't post a review as the sandboxed reviewer by dropping a file into its spool. A reviewer on an accelerator spoke can now run the change on the card node and post its verdict from there.
4 changes: 4 additions & 0 deletions src/crux/agent/manager_sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"time"

"github.com/hivecommons/hive/crux"
"github.com/hivecommons/hive/crux/config"
"github.com/hivecommons/hive/crux/effects"
"github.com/hivecommons/hive/crux/kubejob"
Expand Down Expand Up @@ -150,6 +151,9 @@ func (m *Manager) startSandboxKickLocked(agent *AgentProcess, message string) er
Timeout: time.Duration(agent.Config.SandboxTimeoutS(m.sandboxConfig)) * time.Second,
PublishVerify: agent.Config.SandboxPublishVerify(m.sandboxConfig),
VerifyTimeout: time.Duration(agent.Config.SandboxPublishVerifyTimeoutS(m.sandboxConfig)) * time.Second,
// Every sandbox kick gets a review spool (hivecommons/hive#14986);
// whether the agent may post through it is the relay's decision.
ReviewSpoolDir: crux.SandboxReviewSpoolsDir(m.workDir),
}
// A kick template's own verify block wins over config so the policy
// that asks for a proven result also says how hive checks it.
Expand Down
19 changes: 19 additions & 0 deletions src/crux/agent/sandbox_executor.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package agent

import (
Expand Down Expand Up @@ -76,6 +76,10 @@
PublishVerify []string
// VerifyTimeout bounds each PublishVerify command; zero uses the default.
VerifyTimeout time.Duration
// ReviewSpoolDir is where the kick registers its review spool with the
// relay (crux.SandboxReviewSpoolsDir). Empty disables spooling: the
// sandbox then has no way to post a review.
ReviewSpoolDir string
}

type configSnapshot struct {
Expand Down Expand Up @@ -178,6 +182,17 @@
res.Error = err.Error()
return res, err
}
// A spool the relay cannot see is a lost review, not a failed kick: the
// rest of the run is still worth doing, so log and go on without one.
spool, spoolErr := e.openReviewSpool(spec.ReviewSpoolDir, spec.Agent, workspace)
if spoolErr != nil && e.Logger != nil {
e.Logger.Warn("sandbox review spool unavailable; the sandbox cannot post reviews this kick", "agent", spec.Agent, "error", spoolErr)
}
defer func() {
if err := spool.end(e.now()); err != nil && e.Logger != nil {
e.Logger.Warn("sandbox review spool: could not mark the kick ended", "agent", spec.Agent, "error", err)
}
}()
runCtx, cancel := context.WithTimeout(ctx, spec.Timeout)
defer cancel()
sres, runErr := e.launcher().Run(runCtx, sandbox.LaunchSpec{
Expand All @@ -187,9 +202,13 @@
WorkspaceMount: sandbox.DefaultWorkspaceMount,
WorkDir: sandbox.DefaultWorkspaceMount,
Command: command,
Env: spool.sandboxEnv(),
EnvAllowlist: spec.EnvAllowlist,
NetworkMode: spec.NetworkMode,
})
if ctx.Err() == nil && !spool.awaitDrain(ctx) && e.Logger != nil {
e.Logger.Warn("sandbox review spool: requests still pending after the drain wait", "agent", spec.Agent, "spool", spool.entry.Spool)
}
// SECURITY (#3940, CWE-532): the agent CLI (notably one supplied via a
// direct launch_cmd override, which never passes through agent-launch.sh's
// stderr scrubber) can emit token-shaped values on stdout/stderr — e.g.
Expand Down
55 changes: 15 additions & 40 deletions src/crux/agent/sandbox_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,17 +316,11 @@ func TestManagerSandboxPauseCancelsRunningKick(t *testing.T) {
if err := m.Pause("scanner", "test", "pause while running"); err != nil {
t.Fatalf("Pause: %v", err)
}
deadline := time.Now().Add(time.Second)
for time.Now().Before(deadline) {
waitFor(t, "sandbox agent to stay paused after cancellation", func() bool {
m.mu.RLock()
done := m.agents["scanner"].cancel == nil && m.agents["scanner"].State == StatePaused
m.mu.RUnlock()
if done {
return
}
time.Sleep(10 * time.Millisecond)
}
t.Fatalf("sandbox agent did not stay paused after cancellation: %+v", m.AllStatuses()["scanner"])
defer m.mu.RUnlock()
return m.agents["scanner"].cancel == nil && m.agents["scanner"].State == StatePaused
})
}

func TestManagerSandboxResumeWhileCancelDrainsReturnsIdle(t *testing.T) {
Expand All @@ -350,17 +344,11 @@ func TestManagerSandboxResumeWhileCancelDrainsReturnsIdle(t *testing.T) {
if err := m.Resume(context.Background(), "scanner", "test", "resume immediately"); err != nil {
t.Fatalf("Resume: %v", err)
}
deadline := time.Now().Add(time.Second)
for time.Now().Before(deadline) {
waitFor(t, "sandbox agent to return idle after resumed cancellation", func() bool {
m.mu.RLock()
done := m.agents["scanner"].cancel == nil && m.agents["scanner"].State == StateIdle
m.mu.RUnlock()
if done {
return
}
time.Sleep(10 * time.Millisecond)
}
t.Fatalf("sandbox agent did not return idle after resumed cancellation: %+v", m.AllStatuses()["scanner"])
defer m.mu.RUnlock()
return m.agents["scanner"].cancel == nil && m.agents["scanner"].State == StateIdle
})
}

func TestManagerSandboxRunningSkippedByTmuxCrashDetector(t *testing.T) {
Expand All @@ -387,17 +375,11 @@ func TestManagerSandboxRunningSkippedByTmuxCrashDetector(t *testing.T) {
if err := m.Pause("scanner", "test", "cleanup"); err != nil {
t.Fatalf("Pause cleanup: %v", err)
}
deadline := time.Now().Add(time.Second)
for time.Now().Before(deadline) {
waitFor(t, "sandbox cleanup to finish", func() bool {
m.mu.RLock()
done := m.agents["scanner"].cancel == nil
m.mu.RUnlock()
if done {
return
}
time.Sleep(10 * time.Millisecond)
}
t.Fatal("sandbox cleanup did not finish")
defer m.mu.RUnlock()
return m.agents["scanner"].cancel == nil
})
}

func TestManagerSandboxDoesNotPushWhenModeCannotPush(t *testing.T) {
Expand All @@ -419,17 +401,10 @@ func TestManagerSandboxDoesNotPushWhenModeCannotPush(t *testing.T) {
if err := m.SendKick("scanner", "fix"); err != nil {
t.Fatalf("SendKick: %v", err)
}
deadline := time.Now().Add(time.Second)
for time.Now().Before(deadline) {
if got := m.AllStatuses()["scanner"].State; got == StateIdle {
if runner.pushed || pr.called {
t.Fatalf("non-push mode pushed=%v pr=%v", runner.pushed, pr.called)
}
return
}
time.Sleep(10 * time.Millisecond)
waitFor(t, "sandbox agent to complete", func() bool { return m.AllStatuses()["scanner"].State == StateIdle })
if runner.pushed || pr.called {
t.Fatalf("non-push mode pushed=%v pr=%v", runner.pushed, pr.called)
}
t.Fatalf("sandbox agent did not complete: %+v", m.AllStatuses()["scanner"])
}

func TestAgentSandboxConfigGating(t *testing.T) {
Expand Down
162 changes: 162 additions & 0 deletions src/crux/agent/sandbox_review_spool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package agent

import (
"context"
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"os"
"path"
"path/filepath"
"strings"
"time"

"github.com/hivecommons/hive/crux"
"github.com/hivecommons/hive/crux/sandbox"
)

// reviewSpoolDrainTimeout bounds how long a finished kick waits for the
// review relay to deliver requests the sandbox left in its spool. The wait
// matters most for a run-stage kick, whose worktree — spool included — is
// removed as soon as Run returns. Vars so tests can shorten them.
var (
reviewSpoolDrainTimeout = 60 * time.Second
reviewSpoolDrainPoll = time.Second
)

// reviewSpoolHandle is one kick's registration with the review relay
// (hivecommons/hive#14986): the spool inside the workspace and the manifest,
// outside it, that names the agent the relay attributes the spool to.
type reviewSpoolHandle struct {
manifest string
entry crux.SandboxReviewSpool
}

// openReviewSpool creates the workspace spool and registers it. A nil handle
// with a nil error means spooling is not configured for this kick.
func (e *SandboxExecutor) openReviewSpool(manifestDir, agentName, workspace string) (*reviewSpoolHandle, error) {
if strings.TrimSpace(manifestDir) == "" {
return nil, nil
}
spool := filepath.Join(workspace, crux.SandboxReviewSpoolRelPath)
if err := os.MkdirAll(spool, 0o770); err != nil {
return nil, fmt.Errorf("create review spool: %w", err)
}
// Owner-only: the relay trusts a manifest's agent name, so nothing but
// hive may write here. The relay refuses a group- or world-writable dir.
if err := os.MkdirAll(manifestDir, 0o700); err != nil {
return nil, fmt.Errorf("create review spool manifest dir: %w", err)
}
if err := os.Chmod(manifestDir, 0o700); err != nil {
return nil, fmt.Errorf("secure review spool manifest dir: %w", err)
}
// The run's spool key: the only thing that lets the relay tell this
// sandbox's requests from ones another agent drops into the
// group-writable spool. It reaches the sandbox only through its
// environment, never through the workspace.
key := make([]byte, 32)
if _, err := rand.Read(key); err != nil {
return nil, fmt.Errorf("mint review spool key: %w", err)
}
h := &reviewSpoolHandle{
manifest: filepath.Join(manifestDir, fmt.Sprintf("%s-%d.json", sanitizeBranchPart(agentName), time.Now().UnixNano())),
entry: crux.SandboxReviewSpool{Agent: agentName, Spool: spool, Key: hex.EncodeToString(key), StartedAt: e.now().UTC()},
}
if err := h.write(); err != nil {
return nil, err
}
return h, nil
}

// sandboxEnv is what the sandbox needs to use the spool: hive-review writes
// to HIVE_REVIEW_REQUEST_DIR when it is set, and MACs each request with
// HIVE_REVIEW_SPOOL_KEY.
func (h *reviewSpoolHandle) sandboxEnv() map[string]string {
if h == nil {
return nil
}
return map[string]string{
crux.SandboxReviewSpoolEnv: path.Join(sandbox.DefaultWorkspaceMount, crux.SandboxReviewSpoolRelPath),
crux.SandboxReviewSpoolKeyEnv: h.entry.Key,
}
}

func (h *reviewSpoolHandle) write() error {
b, err := json.Marshal(h.entry)
if err != nil {
return err
}
f, err := os.CreateTemp(filepath.Dir(h.manifest), filepath.Base(h.manifest)+".tmp")
if err != nil {
return err
}
tmp := f.Name()
if _, err := f.Write(b); err != nil {
_ = f.Close()
_ = os.Remove(tmp)
return err
}
if err := f.Close(); err != nil {
_ = os.Remove(tmp)
return err
}
if err := os.Rename(tmp, h.manifest); err != nil {
_ = os.Remove(tmp)
return err
}
return nil
}

// end marks the kick finished. The relay keeps delivering what is still in
// the spool and drops the manifest once nothing is left.
func (h *reviewSpoolHandle) end(now time.Time) error {
if h == nil {
return nil
}
at := now.UTC()
h.entry.EndedAt = &at
return h.write()
}

// awaitDrain waits, bounded by reviewSpoolDrainTimeout, until the relay has
// consumed every request in the spool. A request the sandbox wrote in its
// last seconds would otherwise race the run-stage worktree removal.
func (h *reviewSpoolHandle) awaitDrain(ctx context.Context) bool {
if h == nil || !reviewSpoolPending(h.entry.Spool) {
return true
}
deadline := time.NewTimer(reviewSpoolDrainTimeout)
defer deadline.Stop()
tick := time.NewTicker(reviewSpoolDrainPoll)
defer tick.Stop()
for {
select {
case <-ctx.Done():
return false
case <-deadline.C:
return !reviewSpoolPending(h.entry.Spool)
case <-tick.C:
if !reviewSpoolPending(h.entry.Spool) {
return true
}
}
}
}

// reviewSpoolPending reports whether the spool still holds a request the
// relay has not consumed. Result files and quarantined requests do not
// count; neither does an in-progress "<name>.json.tmp".
func reviewSpoolPending(spool string) bool {
entries, err := os.ReadDir(spool)
if err != nil {
return false
}
for _, e := range entries {
name := e.Name()
if !e.IsDir() && strings.HasSuffix(name, ".json") && !strings.HasSuffix(name, ".result.json") {
return true
}
}
return false
}
Loading
Loading