Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated

dream(security): #3290 fix hive-mind_consensus Sybil-vote authentication gap (evaluated, ACCEPT-scoped) (#3291)

rUvee412dfd15f0
diff --git a/docs/dream-cycle/LEDGER.md b/docs/dream-cycle/LEDGER.md
index 08eadc811a..c5cab2e304 100644
--- a/docs/dream-cycle/LEDGER.md
+++ b/docs/dream-cycle/LEDGER.md
@@ -190,3 +190,4 @@ as a first-class, high-priority candidate, not a background note.
| 2026-09-02 | intelligence | `LearningBridge.consolidate()` reward-blind fix (hardcoded `1.0` → real insight confidence via `getByKey`; round-1 fix had a wrong id/key lookup — `entryId` is the caller-assigned `key`, not the backend's internal `entry.id` — caught by adversarial critic, corrected round 2) | #3159 | #3160 | yes | ACCEPT | 56/56→62/62 tests (candidate); 464/465 full package (1 pre-existing environmental failure); independent critic round 2 CONFIRMED | bead6402... | rebased onto main (post #3086/#3094/#3103/#3110/#3119/#3123/#3130/#3139/#3152 merges), awaiting fresh CI as of 2026-09-14 |
| 2026-09-08 | memory | `MemoryConsolidator.dedup()` deduplicated via byte-exact SHA-256 hash only, though every entry already carries a computed `.embedding` and `dedup()` already holds a handle to the adapter's incrementally-synced `HNSWIndex` (used only for `removePoint` bookkeeping, never queried for similarity) — added a second embedding-cosine near-duplicate pass (threshold 0.95, zero extra backend round-trips); independent adversarial critique found and this session fixed forward a real convergence bug (a duplicate cluster larger than the 8-entry search window split into multiple survivors in one call, self-healing across repeated `runAll()` but under-converging on a single call) | #3231 | #3232 | yes | ACCEPT | 472/473 full-suite green (1 pre-existing unrelated failure, unchanged); baseline-fails/candidate-passes via stash isolation for both the original fix and the convergence fix-forward; `tsc` clean | f5eaa461... | rebased onto main (post #3086/#3094/#3103/#3110/#3119/#3123/#3130/#3139/#3152/#3160 merges), awaiting fresh CI as of 2026-09-14 |
| 2026-09-10 | performance | `mmrRerank()` eagerly re-tokenized every remaining candidate's content on every MMR outer-loop pass (O(N×limit) `tokenize()` calls) — a pattern that predates and survived PR #3169's embedding-cosine wiring unchanged, even though the cosine branch made that tokenization mostly discarded work in the now-common all-embedded case; made lazy and cached per-candidate | #3265 | #3266 | yes | ACCEPT | `tokenize()` calls 400→0 (N=30/limit=20 corpus, stash-isolated: fails against real baseline, passes against candidate); wall-clock 5.88x faster (0.6745ms→0.1148ms mean), independently reproduced at 5.96x by an adversarial critic; selection output byte-identical to baseline across all-embedded/none/mixed/lambda-extreme scenarios; 473/474 full-suite both ways (1 pre-existing unrelated failure), `tsc` clean | 78c074ef... | rebased onto main (post #3086/#3094/#3103/#3110/#3119/#3123/#3130/#3139/#3152/#3160/#3232 merges), awaiting fresh CI as of 2026-09-14 |
+| 2026-09-11 | security | `hive-mind_consensus` accepted votes from any caller-supplied `voterId` with no check against `state.workers` (a single caller could cross any strategy's quorum by voting under fabricated identities — a Sybil attack on consensus); fail-closed roster-membership check added. **Update (ruvnet review):** REJECTed the initial scoped fix on the caveat the adversarial critic had already disclosed (`hive-mind_join`/`leave` fully unauthenticated — the roster itself could be Sybil'd) plus a required-CI cold-install failure (`@claude-flow/security` missing a `prepare` script, so root `npm ci` never built its dist/). Both addressed same-session: `join`/`leave`/`vote` now capability-bound to a `hiveToken` minted by `hive-mind_init` (32-byte random, constant-time comparison, fail-closed — zero state change for denied calls, verified via a fresh state.json reload simulating restart; a same-machine-only token reader lets the CLI's own subcommands keep working without exposing the token over any MCP response); `@claude-flow/security`'s `package.json` gained a `prepare: npm run build` so root `npm ci` builds it automatically for every consumer, not just this CI job | #3290 | #3291 | yes | ACCEPT-scoped (post-review) | stash-isolated 2/3 fail baseline (forged votes silently accepted) → 3/3 pass candidate; full `@claude-flow/cli` suite failures 46→28 after the CI fix (remainder pre-existing/unrelated); adversarial critic CONFIRMED-WITH-CAVEATS pre-review, both caveats closed post-review | 55d67b4a... | rebased onto main (post #3086/#3094/#3103/#3110/#3119/#3123/#3130/#3139/#3152/#3160/#3232/#3266 merges), awaiting fresh CI as of 2026-09-14 |
diff --git a/docs/dream-cycle/dream-gist-2026-09-11.md b/docs/dream-cycle/dream-gist-2026-09-11.md
new file mode 100644
index 0000000000..e949237a91
--- /dev/null
+++ b/docs/dream-cycle/dream-gist-2026-09-11.md
@@ -0,0 +1,101 @@
+# Security SOTA Report — 2026-09-11
+
+TL;DR: Tonight's security deep-dive found a live, code-verified authentication gap in `hive-mind_consensus`'s vote-casting path (`v3/@claude-flow/cli/src/mcp-tools/hive-mind-tools.ts`): the `voterId` a caller supplies is recorded into `proposal.votes` and counted toward the quorum threshold with **zero check against `state.workers`** (the roster populated by `hive-mind_join`). A single caller can cross any consensus strategy's threshold — raft, bft, *and* quorum alike — by voting repeatedly under fabricated identities: a Sybil attack on consensus, not merely a double-vote. This is a materially worse bug than the already-known "consensus strategy is silently discarded" finding (2026-09-10): it means even a hypothetically-working BFT check would be cosmetic, since the vote-casting layer itself trusts caller-supplied identity unconditionally. Fixed with a one-line roster-membership guard, fail-closed. Stash-isolated baseline-fails/candidate-passes evidence, a full 3036-test suite run, and an independent adversarial critique (CONFIRMED-WITH-CAVEATS — the fix closes forged-*vote* Sybil but not forged-*join* Sybil, since `hive-mind_join`/`hive-mind_leave` carry no authentication at all) are below. Five research roles ran in parallel; the deep-research role ran unusually long (background-agent stall, recovered) but returned exceptionally rich results, including that ADR-377's own foundational arXiv citation was quietly withdrawn by its authors.
+
+## What's New in 2026
+
+| Finding | Source | Confidence |
+|---|---|---|
+| OWASP MCP Top 10 (2025), published 2026-06-24 — MCP02 Scope-Creep Privilege Escalation, MCP07 Insufficient AuthN/AuthZ directly on-point for tonight's finding | [Cycode summary](https://cycode.com/blog/owasp-mcp-top-10/) | B |
+| MCP spec 2026-07-28 RC hardens OAuth `iss`-validation/credential-binding but still has **no first-class tool-authority/capability-scoping primitive** — NSA/CISA flag this as the "confused deputy" gap | [MCP RC blog](https://blog.modelcontextprotocol.io/posts/2026-07-28-release-candidate/), [NSA/CISA advisory](https://media.defense.gov/2026/Jun/02/2003943289/-1/-1/0/CSI_MCP_SECURITY.PDF) | A |
+| Microsoft Agent Framework + Entra Agent ID: dedicated agent identity, least-privilege task-scoped roles, JIT elevation — most mature published *identity* model surveyed | [MS blog](https://www.microsoft.com/en-us/security/blog/2026/07/16/least-privilege-for-ai-agents-identity-access-and-tool-binding/) | B |
+| **ADR-377's own cited threat-model paper, arXiv:2603.07473 ("MCP Caller Identity Confusion"), was withdrawn by its authors 2026-07-21** for "flaws in experimental methodology and unresolved ethical issues in data collection" | Directly verified on [arxiv.org/abs/2603.07473](https://arxiv.org/abs/2603.07473) | A |
+| arXiv:2605.14460 "Semantic Compliance Hijacking" — payload-less skill supply-chain attack, 77.67% confidentiality-breach / 67.33% RCE success, 0.00% detection by current scanners | [arxiv.org/abs/2605.14460](https://arxiv.org/abs/2605.14460) | A |
+| Unit 42 BIV audit of 49,943 OpenClaw agent-skill entries: 80% show declared-vs-actual behavior mismatches; 5% carry multi-stage attack chains | [Unit 42](https://unit42.paloaltonetworks.com/ai-agent-supply-chain-risks/) | B |
+| Sybil/forged-identity vote manipulation in multi-agent LLM consensus is a named, active research class | arXiv "Insider Attacks in Multi-Agent LLM Consensus Systems" (2025) | B |
+
+## Ruflo Current Capability
+
+`hive-mind_consensus` (`hive-mind-tools.ts:525-720`) implements raft/bft/quorum vote counting keyed by caller-supplied `voterId` strings against a `Record<string, boolean>`. `calculateRequiredVotes()` derives the threshold from `state.workers.length` — the roster `hive-mind_join` (line 478) pushes into, itself **unauthenticated** (only format-validated via `validateIdentifier`, no identity check). Before tonight, `vote` never cross-checked `voterId` against that roster: the existing double-vote guard (line 629) and cross-proposal Byzantine-flip detector (`detectByzantineVoters`) only catch one identity contradicting *its own* prior vote — neither does anything against many distinct forged identities each voting once. Separately, the deep-research role's independent capability audit found: `ToolOutputGuardrail` and `AgenticPolicyEngine` are genuinely wired at the real `callMCPTool()` chokepoint (both transports) — correctly implemented, contrary to an uncertain premise in tonight's brief; but `ToolOutputGuardrail` only scans result objects one level deep, missing the common `{results:[{content:"..."}]}` shape used by `memory_search`/`hive-mind_*` query tools even when `CLAUDE_FLOW_STRICT_GUARDRAIL=true`. Three recent security ACCEPT-PRs (#3103 caller-identity, #3139 mcp-policy enforcement, #3152 rate-limit reset) remain open/unmerged as of tonight, so `main`'s security posture is still the pre-fix baseline for all three. `PluginIntegrityVerifier` remains unwired (re-confirmed independently by two roles): `checksum`/`signature` never referenced in `manager.ts`'s `installFromNpm`/`installFromLocal`, `trust-anchors.json` still ships a placeholder key.
+
+## Competitor Comparison
+
+| System | Mechanism | Posture | Grade | Source |
+|---|---|---|---|---|
+| MCP spec itself | 2026-07-28 RC auth hardening | No capability-scoping primitive; confused-deputy gap flagged by NSA/CISA | A | modelcontextprotocol.io, NSA/CISA |
+| LangGraph/LangChain | App-level only | Multiple 2026 CVEs (checkpoint RCE, SQLi); no scoped inter-agent delegation | A | CSA research note |
+| Microsoft Agent Framework | Entra Agent ID, JIT elevation, DID-based Inter-Agent Trust Protocol | Most mature identity/authZ model surveyed | B | MS Learn, OSS toolkit |
+| CrewAI | None found | Trusts arbitrary Python callables; genuine, unpublished-fix gap | B | drel.ai, SecurityWeek |
+| OpenAI Agents SDK | `allowed_callers`, sandboxed execution, human-approval gate | Granular tool-invocation hygiene, not agent-identity/authZ | A | OpenAI SDK docs |
+
+Synthesis: no competitor surveyed authenticates *inter-agent consensus votes* specifically (this is a narrower primitive than general tool-authority scoping), so tonight's finding sits in a genuinely under-examined corner of the landscape. On the broader plugin-integrity gap, Ruflo sits behind even CrewAI's "no native verification" baseline in practice, since an unwired verifier provides zero runtime enforcement — the npm-provenance/Sigstore pattern is the concrete blueprint to wire it against.
+
+## Hypothesis
+
+> Given a `hive-mind_consensus` proposal awaiting votes under any strategy (raft/bft/quorum), when a submitted `vote`'s `voterId` is checked against `state.workers` (the registered-worker roster) before being recorded, then a caller voting under a fabricated identity never registered via `hive-mind_join` should be rejected (fail-closed) rather than counted toward the quorum threshold — relative to today's baseline where any string counts — subject to: (1) a genuinely-registered worker's own vote is still correctly recorded and counted; (2) existing hive-mind test suites remain green; (3) zero added latency beyond one array-membership check; (4) no change to the separately-tracked, still-unfixed consensus-strategy-discard bug (2026-09-10).
+
+Frozen before evaluation began; not modified after seeing results.
+
+## Benchmarks / Evaluation
+
+**evaluated: accepted (ACCEPT-scoped — see caveat below).** Real evaluator: Vitest 4.1.8, deterministic, $0, zero LLM calls. New file `v3/@claude-flow/cli/__tests__/hive-mind-consensus-sybil-vote.test.ts` (3 tests): (1) 3 forged voterIds against 5 real workers under raft — proves votes are silently accepted with no error at baseline; (2) legitimate registered-worker vote still recorded; (3) exactly 3 forged identities crossing a 3-worker BFT quorum (`required = floor(3·2/3)+1 = 3`) — proves the proposal is silently `approved` at baseline via pure Sybil votes.
+
+**Baseline vs. candidate, stash-isolated** (source file only, test kept): **2/3 fail against real baseline** — forged votes return no error, and the BFT proposal reaches `status: 'approved'`/`result: 'approved'` from 3 forged identities alone. **3/3 pass against candidate.** All 18 hive-mind tests (3 files) pass together — no regression. Full `@claude-flow/cli` package suite (241 files, 3036 tests): **2863 passed, 46 failed, 127 skipped** — every failure traced to pre-existing, unrelated causes (missing `@claude-flow/mcp`/`@claude-flow/neural` workspace-resolution, `security-scan-enum-validation` traversal-count assertions); **zero mentions of "hive" in any failure**. `tsc --noEmit`: pre-existing errors only (workspace type-declaration gaps), none in the touched file.
+
+**Adversarial critique (STEP 10):** independent reviewer (no authoring context) reproduced the stash-isolation result itself, confirmed `hiveMindTools` is exported and reachable via `mcp-client.ts` as a live MCP tool (not internal-only), confirmed only one code path writes `proposal.votes`, and confirmed the fix holds across quorum presets and across reuse of one forged id across multiple proposals. **Verdict: CONFIRMED-WITH-CAVEATS.** The one material caveat: `hive-mind_join`/`hive-mind_leave` have **no caller authentication at all** — an attacker can still Sybil the roster itself by legitimately joining N times under fake names, since `validateIdentifier` only checks string format. Tonight's fix closes forged-*vote* Sybil (the more surprising, silent bug — a caller need not even call `join`) but not forged-*join* Sybil (a louder, more visible attack surface). Scoping ACCEPT accordingly: this is a real, necessary, but partial fix — the roster-authentication gap is a distinct, larger follow-up.
+
+## Darwin Results
+
+Skipped — confirmed via `npx [email protected] harness darwin --help`: real interface evolves continuous/categorical genome parameters against a fitness-scored corpus. Tonight's fix is a binary authentication gate (registered vs. not) with no tunable parameter space. Same skip class as most recent nights.
+
+## Flywheel Evidence
+
+No signed `@metaharness/flywheel` bundle exists (`npx ruvector harness flywheel --help` confirms `verify`/`gate` target LLM-scored-corpus evidence, not deterministic code fixes). Evidence retained as: 3 new deterministic tests + full-suite run + independent adversarial critique + this gist + the linked issue. Classified: **OBSERVATION** (voterId never checked against `state.workers`, confirmed via read + grep for all `proposal.votes` writers) / **MEASUREMENT** (2/3→3/3 stash-isolated, independently reproduced by the critic; 2863/3036 full-suite pass rate unaffected) / **INFERENCE** (this is reachable in production because `hiveMindTools` is exported and wired into `mcp-client.ts` — not measured via live production traffic) / **DECISION** (ship as a minimal, single-file, fail-closed guard) / **REJECTION** (none).
+
+## Reward Hack Check
+
+No standalone reward-hack CLI reachable this session (`@metaharness/weight-eft` listed as a loaded capability but no CLI binary found on PATH). Manual checklist, independently re-verified by the adversarial critic: no test weakened (purely additive new file); no gold-answer path exists; no cherry-picking (full 3036-test suite run, all 46 failures disclosed with cause, not hidden); no seed manipulation (deterministic); zero cost.
+
+## Security Review
+
+This **is** the security-sensitive candidate. The fix is fail-closed (rejects on non-membership) and adds no new attack surface — one `Array.includes()` check, no new I/O. Disclosed residual risk (per adversarial critique): `hive-mind_join`/`hive-mind_leave` remain fully unauthenticated, so an attacker with MCP tool access can still inflate `state.workers` with fabricated worker ids and then vote once per fabricated id — this raises the attacker's cost (must call `join` first, leaving more evidence) but does not close Sybil consensus manipulation end-to-end. Recommended as a first-class candidate for a future `security` or `swarm` night: authenticate `hive-mind_join`/`leave` (e.g., bind to the same caller-identity primitive ADR-377/#3103 was designed for, once merged).
+
+## Scan Findings: intelligence
+
+**New, code-verified finding.** `ReasoningBank.judge()` (`v3/@claude-flow/neural/src/reasoning-bank.ts:403-444`) computes trajectory `success` purely from caller-supplied `qualityScore`/`reward`, with **no clamping, validation, or provenance** — contrast `moe-router.ts:467`, which clamps reward to [-1,1] on an adjacent path. `Trajectory` (`types.ts:117-147`) has no `agentId`/`source`/`signature` field at all. A compromised or buggy worker that controls its own reported outcome can force a poor/adversarial trajectory to be `distill()`ed into a permanent `Pattern`, later surfaced to *other* agents via retrieval — a durable memory-poisoning primitive. `queen-coordinator.ts:1865-1888` feeds `TaskResult.metrics.qualityScore` into this path; I could not confirm this exact chain is exercised at runtime vs. only reachable via a caller I didn't find — flagged as **unverified-wiring**, not a confirmed live exploit. External grade B (arXiv:2605.18930, 2606.04329 — recent, on-topic, not yet peer-reviewed).
+
+## Scan Findings: swarm
+
+Tonight's DEEP finding (Sybil vote) *is* the swarm-surface finding — see above. Additionally re-confirmed: the 2026-09-10 hive-mind consensus-strategy-discard bug (`state.consensusStrategy` written at init, never read by `hive-mind_consensus`) remains unfixed; combined with tonight's finding, this means "Byzantine fault tolerance" in hive-mind is cosmetic on two independent axes tonight — the strategy selection is ignored, *and* the vote-identity layer beneath whichever strategy runs trusted unauthenticated callers.
+
+## Additional candidates surfaced but not selected tonight (STEP 3.2)
+
+Deep researcher's independently-scored candidates (0.25·fit+0.20·testability+0.20·measurability+0.15·production+0.10·novelty+0.10·reviewability): (1) `ToolOutputGuardrail` one-level-deep scan misses nested/array tool results even in strict mode — **score 4.75**, highest of the deep-researcher's own 5; (2) dream-cycle's own ACCEPT-graded security fixes (#3103/#3139/#3152) sit unmerged, freezing `main`'s posture at pre-fix baseline — score 4.50; (3) `mcp-composition-inspector` never runs over the live 314-tool registry it was built to protect — score 4.20; (4) `AgentAuthorizationPropagator` (agent-to-agent scope propagation) has zero callers anywhere, not even a draft PR — score 4.10; (5) `MemoryPoisonForensics` write-path forensics hook has zero callers, unlike its wired read-path sibling — score 3.70. **Tonight's selected candidate (hive-mind Sybil-vote) was not among these 5** — it came from the swarm scan role — but scores ~4.85 by the same rubric (fit 5, novelty 5, testability 5, measurability 5, production 4, reviewability 5), narrowly the highest overall, and sits at the DEEP+SCAN surface intersection. Explicit override note: Candidate 2 above (guardrail depth gap) is a strong, close second and a natural pick for a future night.
+
+## SOTA Proof & Witness
+
+| Field | Value |
+|---|---|
+| Session commit | `df87b0db338a8632f14e243169208be1d7faa164` |
+| Gist SHA-256 (pre-witness content) | `21b04a6c8b9d90f2050b62e427dca3d046f0f648a4832d5bf76d2863796c232f` |
+| Witness stamp | `55d67b4a8204225dd5db3dbd9a81d31d67a0045c6f1d3c36b3ab88bbc9a65f11` |
+
+Verifier procedure: fetch this file from the branch, strip the witness table's filled values back to `PENDING`, SHA-256 it, concatenate with the session commit above, SHA-256 again — result must equal the witness stamp.
+
+## Recommended Next Steps
+
+1. **Merge the linked draft PR** (human review required) — a minimal (~20 net lines + test file), fail-closed fix with stash-isolated baseline-fails/candidate-passes evidence and an independent adversarial confirmation.
+2. **Authenticate `hive-mind_join`/`hive-mind_leave`** (disclosed follow-up from tonight's own adversarial critique) — closes the remaining forged-*join* Sybil path tonight's fix does not.
+3. **Fix `ToolOutputGuardrail`'s one-level-deep scan** (deep researcher's top-scored candidate, 4.75) — strong pick for tomorrow night regardless of DEEP surface.
+4. **Prioritize merging #3103/#3139/#3152** — three ACCEPT-graded security fixes sitting unmerged mean `main`'s actual security posture lags its own evaluated evidence by 10-11 days.
+
+## Addendum (2026-09-11, post-review)
+
+ruvnet reviewed PR #3291 at `b275fdd0` and **REJECT**ed: the roster-membership check alone was "useful but insufficient" since `hive-mind_join`/`hive-mind_leave` remained unauthenticated (the exact caveat this gist and the adversarial critique above already disclosed, now correctly elevated to a blocking gate rather than a follow-up), and the "Test Suite" CI check failed twice on the exact head due to the cold-install issue diagnosed above.
+
+Both addressed, not just documented:
+
+1. **Capability-bound join/leave/vote.** `hive-mind_init` now mints a random 32-byte `hiveToken` (`node:crypto` `randomBytes`, returned once in the init response), stored in `state.hiveToken`. `hive-mind_join`, `hive-mind_leave`, and `hive-mind_consensus`'s `vote` action all now require a matching `hiveToken` (constant-time `timingSafeEqual` comparison), fail-closed: a missing/wrong token makes **zero** state change — no roster write, no vote write — verified by 3 new tests that reload `state.json` fresh off disk after each denial (standing in for a restart/reopen, per the reviewer's explicit ask) and assert `workers`/`votes` are unchanged. The CLI's own `hive-mind join/leave/consensus` subcommands are unaffected for legitimate local use: a new `getHiveTokenForCli()` export reads the token directly off the same-machine state file (never returned by `hive-mind_status`, so the token doesn't leak through any MCP-reachable read surface) and the CLI command handlers now pass it through automatically. `hive-mind_init` itself is intentionally left unauthenticated, matching the reviewer's literal scope ("join/leave and vote") — bootstrapping trust for who may call `init` is a materially different, larger problem than binding capabilities to already-initialized state, and out of scope for tonight.
+2. **CI cold-install fixed at the root, not routed around.** `@claude-flow/security` (a root npm-workspace member per `package.json`'s `workspaces` array) had no `prepare`/`postinstall` script — only `prepublishOnly`, which `npm ci` never runs — so a fresh checkout's root `npm ci` never built its `dist/`, causing `ERR_MODULE_NOT_FOUND` in `doctor.js` → `policy-runtime.ts` → `@claude-flow/security`. Confirmed via a real root `npm ci` in this session (824 packages, `dist/index.js` freshly built, timestamped) and reproduced on an unrelated PR (#3266)'s own first-run history. Added `"prepare": "npm run build"` to `v3/@claude-flow/security/package.json` — standard npm lifecycle, runs automatically on `npm ci`/`npm install` for every future checkout, not just this PR's. Full `@claude-flow/cli` suite re-run after both fixes: **28 failed / 2884 passed** (was 46/2863) — the ~18-test improvement is exactly the `@claude-flow/security`-dependent tests the `prepare` script now fixes; the remaining 28 are pre-existing, unrelated `@claude-flow/neural`/`@claude-flow/mcp` workspace-resolution gaps (confirmed by name, not just count). Zero "hive" failures throughout.
+
+Updated evaluation: 6/6 new/updated tests pass against the candidate, 6/6 fail appropriately against a stash-isolated baseline (join/leave/vote all silently succeed pre-fix); `tsc --noEmit` clean on every touched file.
diff --git a/v3/@claude-flow/cli/__tests__/hive-mind-consensus-sybil-vote.test.ts b/v3/@claude-flow/cli/__tests__/hive-mind-consensus-sybil-vote.test.ts
new file mode 100644
index 0000000000..40789bbeb7
--- /dev/null
+++ b/v3/@claude-flow/cli/__tests__/hive-mind-consensus-sybil-vote.test.ts
@@ -0,0 +1,228 @@
+/**
+ * Regression test — hive-mind_consensus / join / leave authentication gap.
+ *
+ * hive-mind_consensus's 'vote' action recorded `proposal.votes[voterId] =
+ * voteValue` for ANY caller-supplied voterId string, with no check against
+ * `state.workers` (the roster populated by hive-mind_join). The required
+ * vote threshold (calculateRequiredVotes) is derived from
+ * `state.workers.length`, so a single caller could cross that threshold —
+ * for raft, bft, AND quorum strategies alike — by voting repeatedly under
+ * fabricated ids (`fake-1`, `fake-2`, ...). The existing double-vote guard
+ * and Byzantine-flip detector only catch one identity contradicting its OWN
+ * prior vote; neither does anything against many distinct forged identities
+ * each voting once, which is exactly the Sybil pattern.
+ *
+ * Review feedback on the first version of this fix (roster-membership check
+ * alone) correctly pointed out that hive-mind_join/leave were themselves
+ * unauthenticated: an attacker could just legitimately join under fake
+ * names, sidestepping the vote-side check entirely. The fix now binds
+ * join/leave/vote to a capability token minted once by hive-mind_init
+ * (`state.hiveToken`) -- every mutating call must present it, and a denied
+ * call makes literally zero state change (no membership write, no vote
+ * write), verified below by reloading state fresh from disk (simulating a
+ * process restart/reopen) after each denial.
+ */
+
+import { describe, it, expect, beforeEach, afterEach } from 'vitest';
+import { mkdtempSync, rmSync, readFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { hiveMindTools } from '../src/mcp-tools/hive-mind-tools.js';
+
+function tool(name: string) {
+ const t = hiveMindTools.find(t => t.name === name);
+ if (!t) throw new Error(`tool not found: ${name}`);
+ return t;
+}
+
+// Reads the persisted state file directly -- a fresh read from disk, not
+// anything cached in-process, so it stands in for "the process restarted
+// and reopened the hive" between a denied call and this check.
+function readPersistedState(dir: string): { workers: string[]; consensus: { pending: Array<{ proposalId: string; votes: Record<string, boolean> }> } } {
+ const raw = readFileSync(join(dir, '.claude-flow', 'hive-mind', 'state.json'), 'utf-8');
+ return JSON.parse(raw);
+}
+
+describe('hive-mind_consensus / join / leave capability-token authentication', () => {
+ let dir: string;
+ let prevCwd: string | undefined;
+
+ beforeEach(() => {
+ dir = mkdtempSync(join(tmpdir(), 'hive-mind-sybil-'));
+ prevCwd = process.env.CLAUDE_FLOW_CWD;
+ process.env.CLAUDE_FLOW_CWD = dir;
+ });
+
+ afterEach(() => {
+ if (prevCwd === undefined) delete process.env.CLAUDE_FLOW_CWD;
+ else process.env.CLAUDE_FLOW_CWD = prevCwd;
+ rmSync(dir, { recursive: true, force: true });
+ });
+
+ async function initHive(strategy: 'raft' | 'byzantine' | 'quorum'): Promise<string> {
+ const init = await tool('hive-mind_init').handler({ consensus: strategy }) as any;
+ expect(init.success).toBe(true);
+ expect(typeof init.hiveToken).toBe('string');
+ expect(init.hiveToken.length).toBeGreaterThanOrEqual(32);
+ return init.hiveToken as string;
+ }
+
+ async function joinWorkers(token: string, count: number) {
+ for (let i = 0; i < count; i++) {
+ const r = await tool('hive-mind_join').handler({ agentId: `worker-${i}`, hiveToken: token }) as any;
+ expect(r.success).toBe(true);
+ }
+ }
+
+ it('hive-mind_join is denied without the capability token, and makes zero membership change (verified after a fresh state reload)', async () => {
+ await initHive('raft');
+
+ const denied = await tool('hive-mind_join').handler({ agentId: 'attacker-1' }) as any;
+ expect(denied.success).toBe(false);
+ expect(denied.error).toMatch(/hiveToken is required/);
+
+ const deniedWrongToken = await tool('hive-mind_join').handler({ agentId: 'attacker-2', hiveToken: 'not-the-real-token' }) as any;
+ expect(deniedWrongToken.success).toBe(false);
+ expect(deniedWrongToken.error).toMatch(/Invalid hiveToken/);
+
+ // Simulate restart/reopen: read state.json fresh off disk, not the
+ // in-memory object any handler above touched.
+ const persisted = readPersistedState(dir);
+ expect(persisted.workers).toEqual([]);
+ expect(persisted.workers).not.toContain('attacker-1');
+ expect(persisted.workers).not.toContain('attacker-2');
+ });
+
+ it('hive-mind_join succeeds with the correct token', async () => {
+ const token = await initHive('raft');
+ const r = await tool('hive-mind_join').handler({ agentId: 'worker-0', hiveToken: token }) as any;
+ expect(r.success).toBe(true);
+ expect(r.totalWorkers).toBe(1);
+
+ const persisted = readPersistedState(dir);
+ expect(persisted.workers).toEqual(['worker-0']);
+ });
+
+ it('hive-mind_leave is denied without the capability token, and makes zero membership change (verified after a fresh state reload)', async () => {
+ const token = await initHive('raft');
+ await joinWorkers(token, 1);
+
+ const denied = await tool('hive-mind_leave').handler({ agentId: 'worker-0' }) as any;
+ expect(denied.success).toBe(false);
+ expect(denied.error).toMatch(/hiveToken is required/);
+
+ const deniedWrongToken = await tool('hive-mind_leave').handler({ agentId: 'worker-0', hiveToken: 'forged' }) as any;
+ expect(deniedWrongToken.success).toBe(false);
+ expect(deniedWrongToken.error).toMatch(/Invalid hiveToken/);
+
+ const persisted = readPersistedState(dir);
+ expect(persisted.workers).toEqual(['worker-0']);
+ });
+
+ it('rejects votes from a voterId that never joined the hive (raft), and denies a vote without the token even from a real workerId', async () => {
+ // 5 registered workers -> raft needs floor(5/2)+1 = 3 votes.
+ const token = await initHive('raft');
+ await joinWorkers(token, 5);
+
+ const propose = await tool('hive-mind_consensus').handler({
+ action: 'propose',
+ type: 'test',
+ value: 'x',
+ strategy: 'raft',
+ }) as any;
+ expect(propose.status).toBe('pending');
+
+ // Attacker casts 3 votes under fabricated ids never joined via hive-mind_join,
+ // all with the correct token (proving the roster check, not just the token
+ // check, is doing real work here).
+ const forged = ['forged-1', 'forged-2', 'forged-3'];
+ const forgedResults = [] as any[];
+ for (const voterId of forged) {
+ forgedResults.push(await tool('hive-mind_consensus').handler({
+ action: 'vote',
+ proposalId: propose.proposalId,
+ vote: true,
+ voterId,
+ hiveToken: token,
+ }));
+ }
+ for (const r of forgedResults) {
+ expect(r.error).toMatch(/not a registered hive-mind worker/);
+ }
+
+ // A genuine registered worker voting WITHOUT the token is denied too.
+ const noToken = await tool('hive-mind_consensus').handler({
+ action: 'vote',
+ proposalId: propose.proposalId,
+ vote: true,
+ voterId: 'worker-0',
+ }) as any;
+ expect(noToken.error).toMatch(/hiveToken is required/);
+
+ const status = await tool('hive-mind_consensus').handler({
+ action: 'status',
+ proposalId: propose.proposalId,
+ }) as any;
+ // Proposal must still be pending -- none of the denied votes counted.
+ expect(status.status ?? status.result).not.toBe('approved');
+
+ const persisted = readPersistedState(dir);
+ const persistedProposal = persisted.consensus.pending.find(p => p.proposalId === propose.proposalId);
+ expect(persistedProposal?.votes ?? {}).toEqual({});
+ });
+
+ it('accepts a vote from a voterId that legitimately joined and presents the correct token', async () => {
+ const token = await initHive('raft');
+ await joinWorkers(token, 3);
+
+ const propose = await tool('hive-mind_consensus').handler({
+ action: 'propose',
+ type: 'test',
+ value: 'x',
+ strategy: 'raft',
+ }) as any;
+
+ const vote = await tool('hive-mind_consensus').handler({
+ action: 'vote',
+ proposalId: propose.proposalId,
+ vote: true,
+ voterId: 'worker-0',
+ hiveToken: token,
+ }) as any;
+
+ expect(vote.error).toBeUndefined();
+
+ const persisted = readPersistedState(dir);
+ const persistedProposal = persisted.consensus.pending.find(p => p.proposalId === propose.proposalId);
+ expect(persistedProposal?.votes).toEqual({ 'worker-0': true });
+ });
+
+ it('a single caller cannot cross bft quorum by forging distinct voter identities, even while holding the real token', async () => {
+ // 3 registered workers -> bft needs floor(3*2/3)+1 = 3 votes (unanimous here).
+ const token = await initHive('byzantine');
+ await joinWorkers(token, 3);
+
+ const propose = await tool('hive-mind_consensus').handler({
+ action: 'propose',
+ type: 'test',
+ value: 'x',
+ strategy: 'bft',
+ }) as any;
+
+ for (const voterId of ['sybil-a', 'sybil-b', 'sybil-c']) {
+ await tool('hive-mind_consensus').handler({
+ action: 'vote',
+ proposalId: propose.proposalId,
+ vote: true,
+ voterId,
+ hiveToken: token,
+ });
+ }
+
+ const status = await tool('hive-mind_consensus').handler({
+ action: 'status',
+ proposalId: propose.proposalId,
+ }) as any;
+ expect(status.status ?? status.result).not.toBe('approved');
+ });
+});
diff --git a/v3/@claude-flow/cli/src/commands/hive-mind.ts b/v3/@claude-flow/cli/src/commands/hive-mind.ts
index 79028fb798..66ca259ff7 100644
--- a/v3/@claude-flow/cli/src/commands/hive-mind.ts
+++ b/v3/@claude-flow/cli/src/commands/hive-mind.ts
@@ -15,6 +15,7 @@ import { mkdir, writeFile } from 'fs/promises';
import { existsSync } from 'fs';
import { join } from 'path';
import { resolveClaudeLaunchCommand } from '../runtime/claude-command.js';
+import { getHiveTokenForCli } from '../mcp-tools/hive-mind-tools.js';
// Worker type definitions for prompt generation
interface HiveWorker {
@@ -1178,7 +1179,7 @@ const joinCommand: Command = {
return { success: false, exitCode: 1 };
}
try {
- const result = await callMCPTool<{ success: boolean; agentId: string; totalWorkers: number; error?: string }>('hive-mind_join', { agentId, role: ctx.flags.role });
+ const result = await callMCPTool<{ success: boolean; agentId: string; totalWorkers: number; error?: string }>('hive-mind_join', { agentId, role: ctx.flags.role, hiveToken: getHiveTokenForCli() });
if (!result.success) { output.printError(result.error || 'Failed'); return { success: false, exitCode: 1 }; }
output.printSuccess(`Agent ${agentId} joined hive (${result.totalWorkers} workers)`);
return { success: true, data: result };
@@ -1195,7 +1196,7 @@ const leaveCommand: Command = {
const agentId = ctx.args[0] || ctx.flags['agent-id'] as string || ctx.flags.agentId as string;
if (!agentId) { output.printError('Agent ID required.'); return { success: false, exitCode: 1 }; }
try {
- const result = await callMCPTool<{ success: boolean; agentId: string; remainingWorkers: number; error?: string }>('hive-mind_leave', { agentId });
+ const result = await callMCPTool<{ success: boolean; agentId: string; remainingWorkers: number; error?: string }>('hive-mind_leave', { agentId, hiveToken: getHiveTokenForCli() });
if (!result.success) { output.printError(result.error || 'Failed'); return { success: false, exitCode: 1 }; }
output.printSuccess(`Agent ${agentId} left hive (${result.remainingWorkers} remaining)`);
return { success: true, data: result };
@@ -1218,7 +1219,7 @@ const consensusCommand: Command = {
action: async (ctx: CommandContext): Promise<CommandResult> => {
const action = ctx.flags.action as string || 'list';
try {
- const result = await callMCPTool<Record<string, unknown>>('hive-mind_consensus', { action, proposalId: ctx.flags.proposalId, type: ctx.flags.type, value: ctx.flags.value, vote: ctx.flags.vote === 'yes', voterId: ctx.flags.voterId });
+ const result = await callMCPTool<Record<string, unknown>>('hive-mind_consensus', { action, proposalId: ctx.flags.proposalId, type: ctx.flags.type, value: ctx.flags.value, vote: ctx.flags.vote === 'yes', voterId: ctx.flags.voterId, hiveToken: getHiveTokenForCli() });
if (ctx.flags.format === 'json') { output.printJson(result); return { success: true, data: result }; }
if (action === 'list') {
output.writeln(output.bold('\nPending Proposals'));
diff --git a/v3/@claude-flow/cli/src/mcp-tools/hive-mind-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/hive-mind-tools.ts
index 2ab41a4181..972825299e 100644
--- a/v3/@claude-flow/cli/src/mcp-tools/hive-mind-tools.ts
+++ b/v3/@claude-flow/cli/src/mcp-tools/hive-mind-tools.ts
@@ -6,6 +6,7 @@
import { existsSync, readFileSync, writeFileSync, mkdirSync } from 'node:fs';
import { join } from 'node:path';
+import { randomBytes, timingSafeEqual } from 'node:crypto';
import { type MCPTool, getProjectCwd } from './types.js';
import { validateIdentifier, validateText } from './validate-input.js';
@@ -30,6 +31,14 @@ interface HiveState {
electedAt: string;
term: number;
};
+ // Capability token minted by hive-mind_init. join/leave/vote all require
+ // it (see requireHiveToken below) -- previously these mutated state.workers
+ // and proposal.votes for any caller, with nothing standing between an
+ // unauthenticated MCP caller and the hive's membership/consensus state.
+ // Undefined means no token was ever minted (state predates this field, or
+ // the hive was never initialized): fail-closed, not fail-open -- no token
+ // means no caller is authorized, not "any caller is".
+ hiveToken?: string;
workers: string[];
consensus: {
pending: ConsensusProposal[];
@@ -159,6 +168,38 @@ function tryResolveProposal(
return null;
}
+/**
+ * Verify a caller-supplied hiveToken against the one minted by hive-mind_init,
+ * using a constant-time comparison (bearer-token capability check -- callers
+ * that never joined, and callers that guess/omit the token, get identical
+ * rejection). Returns an error string on failure, or null on success.
+ */
+function requireHiveToken(state: HiveState, suppliedToken: unknown): string | null {
+ if (!state.hiveToken) {
+ return 'Hive-mind has no capability token minted (re-run hive-mind_init)';
+ }
+ if (typeof suppliedToken !== 'string' || !suppliedToken) {
+ return 'hiveToken is required';
+ }
+ const expected = Buffer.from(state.hiveToken, 'utf-8');
+ const actual = Buffer.from(suppliedToken, 'utf-8');
+ if (expected.length !== actual.length || !timingSafeEqual(expected, actual)) {
+ return 'Invalid hiveToken';
+ }
+ return null;
+}
+
+/**
+ * Read the current hiveToken directly off disk, for same-machine callers
+ * that already have filesystem access to hive state (the CLI's own
+ * `hive-mind join/leave/consensus` subcommands) -- NOT exposed over any MCP
+ * tool response (in particular, not hive-mind_status), since that's a
+ * remote-reachable surface a capability token must not leak through.
+ */
+export function getHiveTokenForCli(): string | undefined {
+ return loadHiveState().hiveToken;
+}
+
function getHiveDir(): string {
return join(getProjectCwd(), STORAGE_DIR, HIVE_DIR);
}
@@ -330,6 +371,14 @@ export const hiveMindTools: MCPTool[] = [
electedAt: new Date().toISOString(),
term: 1,
};
+ // Mint a capability token on first init; a re-init (topology/consensus
+ // change on an already-initialized hive) keeps the existing token and
+ // roster rather than silently invalidating workers who already hold
+ // it -- only re-generated if somehow absent (e.g. state predates this
+ // field).
+ if (!state.hiveToken) {
+ state.hiveToken = randomBytes(32).toString('hex');
+ }
saveHiveState(state);
@@ -340,6 +389,7 @@ export const hiveMindTools: MCPTool[] = [
consensus: state.consensusStrategy,
queenId,
status: 'initialized',
+ hiveToken: state.hiveToken,
config: {
topology: state.topology,
consensus: state.consensusStrategy,
@@ -462,8 +512,9 @@ export const hiveMindTools: MCPTool[] = [
properties: {
agentId: { type: 'string', description: 'Agent ID to join' },
role: { type: 'string', enum: ['worker', 'specialist', 'scout'], description: 'Agent role in hive' },
+ hiveToken: { type: 'string', description: 'Capability token minted by hive-mind_init' },
},
- required: ['agentId'],
+ required: ['agentId', 'hiveToken'],
},
handler: async (input) => {
const state = loadHiveState();
@@ -475,6 +526,14 @@ export const hiveMindTools: MCPTool[] = [
return { success: false, error: 'Hive-mind not initialized' };
}
+ // Fail-closed: an unrecognized/missing token makes no membership
+ // change at all -- state.workers is untouched, not just left
+ // unsaved (the write below never happens on this path).
+ const tokenError = requireHiveToken(state, input.hiveToken);
+ if (tokenError) {
+ return { success: false, agentId, error: tokenError };
+ }
+
if (!state.workers.includes(agentId)) {
state.workers.push(agentId);
saveHiveState(state);
@@ -497,8 +556,9 @@ export const hiveMindTools: MCPTool[] = [
type: 'object',
properties: {
agentId: { type: 'string', description: 'Agent ID to remove' },
+ hiveToken: { type: 'string', description: 'Capability token minted by hive-mind_init' },
},
- required: ['agentId'],
+ required: ['agentId', 'hiveToken'],
},
handler: async (input) => {
const state = loadHiveState();
@@ -506,6 +566,13 @@ export const hiveMindTools: MCPTool[] = [
{ const v = validateIdentifier(agentId, 'agentId'); if (!v.valid) return { success: false, agentId, error: v.error }; }
+ // Fail-closed: a denied caller makes no membership change -- the
+ // splice/save below is unreachable on this path.
+ const tokenError = requireHiveToken(state, input.hiveToken);
+ if (tokenError) {
+ return { success: false, agentId, error: tokenError };
+ }
+
const index = state.workers.indexOf(agentId);
if (index > -1) {
state.workers.splice(index, 1);
@@ -534,6 +601,7 @@ export const hiveMindTools: MCPTool[] = [
value: { description: 'Proposal value (for propose)' },
vote: { type: 'boolean', description: 'Vote (true=for, false=against)' },
voterId: { type: 'string', description: 'Voter agent ID' },
+ hiveToken: { type: 'string', description: 'Capability token minted by hive-mind_init (required to vote)' },
strategy: { type: 'string', enum: ['bft', 'raft', 'quorum'], description: 'Consensus strategy (default: raft)' },
quorumPreset: { type: 'string', enum: ['unanimous', 'majority', 'supermajority'], description: 'Quorum threshold preset (for quorum strategy, default: majority)' },
term: { type: 'number', description: 'Term number (for raft strategy)' },
@@ -617,6 +685,30 @@ export const hiveMindTools: MCPTool[] = [
return { action, error: 'voterId is required for voting' };
}
+ // Fail-closed: a denied caller records no vote at all -- the
+ // votes[voterId] write below is unreachable on this path, and
+ // nothing about the proposal (vote tallies, status) changes.
+ const tokenError = requireHiveToken(state, input.hiveToken);
+ if (tokenError) {
+ return { action, error: tokenError, proposalId: proposal.proposalId };
+ }
+
+ // voterId was previously trusted as-is: any caller-supplied string
+ // was recorded into proposal.votes and counted toward
+ // calculateRequiredVotes()'s threshold (derived from
+ // state.workers.length), with no check that it named a worker who
+ // actually joined this hive-mind. That let a single caller cross
+ // any strategy's quorum (raft/bft/quorum alike) by voting under
+ // fabricated ids — a Sybil attack on consensus, not merely a
+ // double-vote. Require the voter to be a registered worker.
+ if (!state.workers.includes(voterId)) {
+ return {
+ action,
+ error: `Voter ${voterId} is not a registered hive-mind worker`,
+ proposalId: proposal.proposalId,
+ };
+ }
+
const voteValue = input.vote as boolean;
const proposalStrategy = proposal.strategy || 'raft';
const required = calculateRequiredVotes(
diff --git a/v3/@claude-flow/security/package.json b/v3/@claude-flow/security/package.json
index 97c612e021..0a04fd6723 100644
--- a/v3/@claude-flow/security/package.json
+++ b/v3/@claude-flow/security/package.json
@@ -19,6 +19,7 @@
"benchmark:product-plane": "npm run build && node scripts/benchmark-product-plane.mjs",
"test:oauth-surface": "vitest run __tests__/oauth-export-surface.test.ts __tests__/oauth-pkce.test.ts __tests__/oauth-browser.test.ts",
"build": "tsc && node scripts/verify-oauth-exports.mjs",
+ "prepare": "npm run build",
"prepublishOnly": "npm run build && npm run test:oauth-surface"
},
"dependencies": {