audit-1 (critical 1): mandatory operator signatures bound to content+deployment

- signing module: canonical content hashes + approval_message
  sha256(cayce-ops:<type>:<deployment_id>:<id>:<content_hash>)
- approve/approve-draft/publisher-approve REQUIRE signature; without
  CAYCE_OPS_OPERATOR_PUBKEY+DEPLOYMENT_ID the daemons refuse to
  execute/send/publish (no degraded mode)
- run-time re-verification at execute/send/publish: unsigned rows (raw SQL
  approval) or edited content revert to pre-approval state, audited as error
- FULL signatures stored on rows (operator_signature, migration 0003) and
  in audit detail — never truncated
- signature failures audited from CLI wrappers too (1f)
- AUTONOMOUS_MODE approval path removed (D13): no non-human approvals

tests: SQL-approved row refused at execute/send/publish; to_addr change
blocks send; foreign deployment/content signatures refused; full-signature
persistence. 65 tests green.
This commit is contained in:
2026-10-20 02:20:09 +00:00
parent 61d5cfd67d
commit 40453aadb4
13 changed files with 1209 additions and 273 deletions
+106 -67
View File
@@ -1,16 +1,30 @@
"""publisher tests — calendar, human gate, nostr publish, disabled adapters."""
"""publisher tests — calendar, human gate (signed), nostr publish, gates."""
from __future__ import annotations
import os
from datetime import UTC, datetime, timedelta
import pytest
from cayceops.components.publisher import adapters, nostr, publisher
from cayceops.models import AuditEntry, ContentItem, ContentStatus
# invented test key (never a real nostr key — allowlisted in .gitleaks.toml)
TEST_KEY = "11" * 32
# invented test keys (never real — allowlisted in .gitleaks.toml)
TEST_KEY = "11" * 32 # nostr signing key
OPERATOR_SECRET = bytes.fromhex("22" * 32) # approval signing key
def _sign(msg: bytes, secret: bytes = OPERATOR_SECRET) -> str:
from coincurve import PrivateKey
return PrivateKey(secret).sign_schnorr(msg, aux_randomness=b"\x00" * 32).hex()
def _sign_content(item: ContentItem, deployment: str = "itest") -> str:
from cayceops.signing import approval_message, content_content_hash
chash = content_content_hash(item.channel, item.caption, item.link)
return _sign(approval_message("content", deployment, item.id, chash))
def _item(channel="nostr", theme="atm-fees", scheduled=None, status=ContentStatus.APPROVED):
@@ -24,6 +38,16 @@ def _item(channel="nostr", theme="atm-fees", scheduled=None, status=ContentStatu
)
def _approved_item(cfg, sess, **kw) -> ContentItem:
"""A content item properly approved with a signature (env set first)."""
cfg() # sets the signing env before any approval
item = _item(status=ContentStatus.DRAFT, **kw)
sess.add(item)
sess.flush()
assert publisher._approve(item.id, "gustavo", sess, signature=_sign_content(item)) == 0
return item
# --- nostr client -------------------------------------------------------------
@@ -65,10 +89,16 @@ def test_approve_gate(cfg, sess):
item = _item(status=ContentStatus.DRAFT)
sess.add(item)
sess.flush()
cfg() # signing env first
assert publisher._approve(item.id, "gustavo", sess) == 0
# unsigned approval refused (mandatory signature, no degraded mode)
assert publisher._approve(item.id, "gustavo", sess, signature=None) == 2
# signed approval accepted, FULL signature stored
sig = _sign_content(item)
assert publisher._approve(item.id, "gustavo", sess, signature=sig) == 0
assert item.status is ContentStatus.APPROVED
assert publisher._approve(item.id, "gustavo", sess) == 1 # not draft anymore
assert item.operator_signature == sig and len(sig) == 128
assert publisher._approve(item.id, "gustavo", sess, signature=sig) == 1 # not draft anymore
actor = sess.query(AuditEntry).filter_by(action="approve_content").first().actor
assert actor == "gustavo"
@@ -78,9 +108,7 @@ def test_approve_gate(cfg, sess):
def test_publish_due_nostr_happy_path(cfg, sess):
item = _item() # approved, scheduled None -> due now
sess.add(item)
sess.flush()
item = _approved_item(cfg, sess)
relays_seen = []
@@ -99,25 +127,69 @@ def test_publish_due_nostr_happy_path(cfg, sess):
assert item.status is ContentStatus.PUBLISHED
assert item.post_ref and len(item.post_ref) == 64
assert item.published_at is not None
assert "wss://relay.example.org" in relays_seen[0] # noqa: E501
assert "wss://relay.example.org" in relays_seen[0]
def test_future_scheduled_item_not_published(cfg, sess):
from datetime import UTC, datetime, timedelta
item = _item(scheduled=datetime.now(UTC) + timedelta(hours=2))
sess.add(item)
sess.flush()
config = cfg(CAYCE_OPS_NOSTR_PRIVATE_KEY=TEST_KEY, CAYCE_OPS_NOSTR_RELAYS="wss://r.example.org")
stats = publisher._publish_due(config, sess, nostr_sender=lambda r, e, timeout=10: [])
item = _approved_item(
cfg, sess, scheduled=datetime.now(UTC) + timedelta(hours=2)
)
config = cfg(
CAYCE_OPS_NOSTR_PRIVATE_KEY=TEST_KEY, CAYCE_OPS_NOSTR_RELAYS="wss://r.example.org"
)
stats = publisher._publish_due(
config, sess, nostr_sender=lambda r, e, timeout=10: []
)
assert stats["published"] == 0
assert item.status is ContentStatus.APPROVED
def test_nostr_without_config_fails_clean(cfg, sess):
item = _item()
def test_unsigned_sql_approved_item_not_published(cfg, sess):
"""Round-2 required test: raw-SQL approved row (no signature) not published."""
config = cfg(
CAYCE_OPS_NOSTR_PRIVATE_KEY=TEST_KEY, CAYCE_OPS_NOSTR_RELAYS="wss://r.example.org"
)
item = _item() # status=APPROVED, no signature
sess.add(item)
sess.flush()
stats = publisher._publish_due(config, sess, nostr_sender=lambda r, e, timeout=10: [])
assert stats["published"] == 0 and stats["tampered"] == 1
assert item.status is ContentStatus.DRAFT # reverted
refused = sess.query(AuditEntry).filter_by(action="publish_refused").one()
assert refused.result == "error"
def test_tampered_caption_refused_by_signature(cfg, sess):
"""Round-2 required test: signature over other content refused at publish."""
item = _approved_item(cfg, sess)
item.caption = item.caption + " EDITED" # content no longer matches sig
nostr_config = cfg(
CAYCE_OPS_NOSTR_PRIVATE_KEY=TEST_KEY, CAYCE_OPS_NOSTR_RELAYS="wss://r.example.org"
)
stats = publisher._publish_due(
nostr_config, sess, nostr_sender=lambda r, e, timeout=10: []
)
assert stats["published"] == 0 and stats["tampered"] == 1
assert item.status is ContentStatus.DRAFT
def test_foreign_deployment_signature_refused(cfg, sess, signer):
"""Round-2 required test: a valid signature for another deployment_id is
refused at approval time."""
item = _item(status=ContentStatus.DRAFT)
sess.add(item)
sess.flush()
other_dep = signer.raw(
signer.msg("content", "other-deploy", item.id, signer.content_hash(item))
)
assert publisher._approve(item.id, "gustavo", sess, signature=other_dep) == 2
assert item.status is ContentStatus.DRAFT
def test_nostr_without_config_fails_clean(cfg, sess):
"""Signed item but no nostr credentials -> clean failure, item stays."""
item = _approved_item(cfg, sess)
config = cfg() # no key, no relays
stats = publisher._publish_due(config, sess)
assert stats["failed"] == 1
@@ -126,14 +198,15 @@ def test_nostr_without_config_fails_clean(cfg, sess):
def test_non_nostr_channels_skipped_terminal(cfg, sess):
item = _item(channel="facebook", theme="lightning-abroad")
sess.add(item)
sess.flush()
item = _approved_item(cfg, sess, channel="facebook", theme="lightning-abroad")
stats = publisher._publish_due(cfg(), sess)
assert stats["skipped"] == 1
assert item.status is ContentStatus.SKIPPED # terminal (audit fix)
skip = sess.query(AuditEntry).filter_by(action="publish_skipped_terminal").one()
assert skip.detail["channel"] == "facebook"
stats2 = publisher._publish_due(cfg(), sess)
assert stats2["skipped"] == 0 # never revisited
skips = sess.query(AuditEntry).filter_by(action="publish_skipped_terminal").count()
assert skips == 1
def test_meta_adapters_disabled_by_default(cfg):
@@ -164,61 +237,27 @@ def test_unknown_brand_rejected():
images.render_brand_image(brand="xx", kicker=None, lines=["A"], out_path="/tmp/x.jpg")
# --- audit corrections ----------------------------------------------------------
def test_publish_refuses_tampered_caption(cfg, sess):
"""Medium finding: approved caption was not bound to content."""
item = _item(status=ContentStatus.DRAFT)
sess.add(item)
sess.flush()
assert publisher._approve(item.id, "gustavo", sess) == 0
item.caption = item.caption + " TAMPERED"
config = cfg(CAYCE_OPS_NOSTR_PRIVATE_KEY=TEST_KEY, CAYCE_OPS_NOSTR_RELAYS="wss://r.example.org")
stats = publisher._publish_due(config, sess, nostr_sender=lambda r, e, timeout=10: [])
assert stats["published"] == 0 and stats["tampered"] == 1
assert item.status is ContentStatus.APPROVED # stays, needs re-approval
action = sess.query(AuditEntry).filter_by(action="publish_refused_hash_mismatch").one()
assert action.result == "error"
def test_disabled_channel_is_terminal(cfg, sess):
"""Medium finding: disabled channels wrote skip audits every 5 minutes."""
item = _item(channel="facebook", theme="lightning-abroad")
sess.add(item)
sess.flush()
config = cfg()
stats1 = publisher._publish_due(config, sess)
assert stats1["skipped"] == 1
assert item.status is ContentStatus.SKIPPED # terminal
stats2 = publisher._publish_due(config, sess)
assert stats2["skipped"] == 0 # never revisited
skips = sess.query(AuditEntry).filter_by(action="publish_skipped_terminal").count()
assert skips == 1
# --- round-2: attempts budget --------------------------------------------------
def test_publish_attempts_bounded(cfg, sess, monkeypatch):
"""Medium finding: nostr failures retried without limit."""
from cayceops.components.publisher import nostr as nostr_mod
item = _item()
sess.add(item)
sess.flush()
def always_fail(relays, event, timeout=10.0):
raise RuntimeError("relay down")
monkeypatch.setattr(nostr_mod, "publish", always_fail)
config = cfg(
CAYCE_OPS_NOSTR_PRIVATE_KEY=TEST_KEY,
CAYCE_OPS_NOSTR_RELAYS="wss://r.example.org",
CAYCE_OPS_MAX_ATTEMPTS="1",
)
item = _approved_item(cfg, sess)
def always_fail(relays, event, timeout=10.0):
raise RuntimeError("relay down")
monkeypatch.setattr(nostr_mod, "publish", always_fail)
publisher._publish_due(config, sess, nostr_sender=always_fail)
assert item.attempts == 1
assert item.status is ContentStatus.SKIPPED # budget exhausted -> terminal
import os # noqa: E402 (used by image test)