Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(signals): stop a forged scout row minting a billing exemption (#102705)

posthog[bot]82c9d71ee125
diff --git a/products/signals/backend/billing.py b/products/signals/backend/billing.py
index e78a9a90a7c2..67c07aec327c 100644
--- a/products/signals/backend/billing.py
+++ b/products/signals/backend/billing.py
@@ -51,7 +51,7 @@ from posthog.models.organization import BillingPeriod
from products.signals.backend.artefact_schemas import TASK_RUN_TYPE_IMPLEMENTATION
from products.signals.backend.enums import SignalSourceProduct
from products.signals.backend.models import SignalReport, SignalReportRefund, SignalReportTask, SignalScoutRun
-from products.signals.backend.scout_harness.lazy_seed import scout_skill_row_origin
+from products.signals.backend.scout_harness.lazy_seed import scout_skill_row_is_proven_canonical
from products.skills.backend.models.skills import LLMSkill
if TYPE_CHECKING:
@@ -126,11 +126,11 @@ def _scout_skill_is_canonical(team_id: int, skill_name: str) -> bool:
`skill_name` alone doesn't attest PostHog-system origin: a team can edit a seeded scout in
place and the diverged row keeps its canonical name, which would let a repurposed fork mint
- permanent exemptions. `scout_skill_row_origin` settles it by content hash against the
- fingerprint stamped at seed time. Fails closed — no row, no proof → billable.
+ permanent exemptions. `scout_skill_row_is_proven_canonical` settles it by content hash against
+ the fingerprint stamped at seed time. Fails closed — no row, no hash, no match → billable.
"""
skill = LLMSkill.objects.filter(team_id=team_id, name=skill_name, deleted=False, is_latest=True).first()
- return skill is not None and scout_skill_row_origin(skill) == "canonical"
+ return skill is not None and scout_skill_row_is_proven_canonical(skill)
def system_billing_exempt_reason(team_id: int, report_id: str | uuid.UUID) -> str | None:
diff --git a/products/signals/backend/scout_harness/lazy_seed.py b/products/signals/backend/scout_harness/lazy_seed.py
index 78b4d236deaf..266a658a928d 100644
--- a/products/signals/backend/scout_harness/lazy_seed.py
+++ b/products/signals/backend/scout_harness/lazy_seed.py
@@ -603,6 +603,18 @@ def scout_skill_row_origin(skill: LLMSkill) -> Literal["canonical", "custom"]:
return "custom" if _compute_row_hash(skill, list(skill.files.all())) != stored_hash else "canonical"
+def scout_skill_row_is_proven_canonical(skill: LLMSkill) -> bool:
+ """`scout_skill_row_origin`, plus the baseline hash that makes the verdict provable.
+
+ The two disagree on one row: a seeded row carrying no `canonical_hash`. `scout_skill_row_origin`
+ keeps it canonical because its consumer (the self-improvement gate) is conservative in the
+ direction of not inviting edits. A caller that *grants* something on canonical origin is
+ conservative the other way, so it reads that row as `sync_canonical_skills` does — no baseline
+ hash, no claim.
+ """
+ return scout_skill_row_origin(skill) == "canonical" and (skill.metadata or {}).get("canonical_hash") is not None
+
+
def _create_skill_from_canonical(team: Team, canonical: CanonicalSkill, canonical_hash: str) -> None:
"""Insert a brand-new row for a (team, canonical.name) that has no prior history.
diff --git a/products/signals/backend/test/test_billing.py b/products/signals/backend/test/test_billing.py
index 4d2dcc9ab68a..81cc90516045 100644
--- a/products/signals/backend/test/test_billing.py
+++ b/products/signals/backend/test/test_billing.py
@@ -465,3 +465,11 @@ class TestBillingExemptions(BaseTest):
forked_report = _make_report(self.team)
self._scout_run("signals-scout-health-checks", [str(forked_report.id)])
self.assertIsNone(system_billing_exempt_reason(self.team.id, forked_report.id))
+
+ # A republished row that kept the seed tag but dropped the stored hash proves nothing
+ # about its content, so it cannot mint an exemption either.
+ skill.metadata.pop("canonical_hash")
+ skill.save(update_fields=["metadata"])
+ unproven_report = _make_report(self.team)
+ self._scout_run("signals-scout-health-checks", [str(unproven_report.id)])
+ self.assertIsNone(system_billing_exempt_reason(self.team.id, unproven_report.id))
diff --git a/products/skills/backend/api/community_skill_services.py b/products/skills/backend/api/community_skill_services.py
index cc15caa7ff1c..ba7c023a2c0c 100644
--- a/products/skills/backend/api/community_skill_services.py
+++ b/products/skills/backend/api/community_skill_services.py
@@ -11,7 +11,7 @@ from ..marketplace.packaging import SPEC_DESCRIPTION_MAX_LENGTH
from ..models.community_skills import CommunitySkill, CommunitySkillVote
from ..models.skills import LLMSkill
from .skill_serializers import validate_allowed_tool, validate_new_skill_name_value, validate_skill_file_path
-from .skill_services import MAX_SKILL_FILE_BYTES, create_skill
+from .skill_services import HARNESS_OWNED_METADATA_KEYS, MAX_SKILL_FILE_BYTES, create_skill
from .skill_template_services import parse_template_variables, render_template_skill
# Namespaces where PostHog auto-registers and runs a skill under privileged scopes on an enrolled
@@ -28,11 +28,6 @@ RESERVED_INSTALL_NAME_PREFIXES: dict[str, str] = {
"review-hog-": "PostHog-managed review skills",
}
-# Provenance keys ReviewHog stamps on the rows it manages. Its prune keys on `seeded_by`, so if a
-# catalog entry carried these they could make a user's freshly installed skill disappear on the next
-# review sync — strip them before stamping community provenance, the way duplicate_skill drops seeded_by.
-_INTERNAL_METADATA_KEYS = frozenset({"seeded_by", "canonical_hash", "source"})
-
class CommunitySkillNotFoundError(Exception):
pass
@@ -143,9 +138,11 @@ def install_community_skill(
body = locked.body
# Stamp provenance so an installed skill can be traced back to its community source, but
- # first drop any internal ReviewHog ownership keys the catalog entry might carry.
+ # first drop any harness-owned keys the catalog entry might carry. ReviewHog's prune keys on
+ # `seeded_by`, so a catalog entry carrying it could make a freshly installed skill disappear
+ # on the next review sync.
metadata: dict[str, Any] = {
- **{k: v for k, v in (locked.metadata or {}).items() if k not in _INTERNAL_METADATA_KEYS},
+ **{k: v for k, v in (locked.metadata or {}).items() if k not in HARNESS_OWNED_METADATA_KEYS},
"community_skill_slug": locked.slug,
"community_skill_id": str(locked.id),
}
diff --git a/products/skills/backend/api/skill_services.py b/products/skills/backend/api/skill_services.py
index c80ff388c8e6..5e9cbaaad826 100644
--- a/products/skills/backend/api/skill_services.py
+++ b/products/skills/backend/api/skill_services.py
@@ -433,6 +433,24 @@ def _carry_forward(payload_value: Any, current_value: Any) -> Any:
return payload_value if payload_value is not None else current_value
+# Provenance keys a PostHog harness stamps on the rows it seeds, and reads back as proof that a row
+# is still the content it shipped. `metadata` is a free-form dict any project editor can send, so a
+# caller-supplied value for one of these is a forged attestation: dropping `canonical_hash` from a
+# seeded Signals scout, or writing a hash of the caller's own content, makes an edited scout read as
+# PostHog-shipped and mints a permanent billing exemption for its reports.
+HARNESS_OWNED_METADATA_KEYS = frozenset({"seeded_by", "canonical_hash", "source"})
+
+
+def _resolve_published_metadata(payload_value: dict[str, Any] | None, current_value: dict | None) -> dict[str, Any]:
+ """Caller metadata for a new version, with the harness-owned provenance pinned to the current row."""
+ current = dict(current_value or {})
+ if payload_value is None:
+ return current
+ resolved = {k: v for k, v in payload_value.items() if k not in HARNESS_OWNED_METADATA_KEYS}
+ resolved.update({k: v for k, v in current.items() if k in HARNESS_OWNED_METADATA_KEYS})
+ return resolved
+
+
def apply_skill_file_edits(file_content: str, edits: list[dict[str, str]], *, file_path: str) -> str:
"""Apply sequential find/replace edits to a single bundled skill file.
@@ -524,7 +542,7 @@ def publish_skill_version(
license=_carry_forward(license, current_latest.license),
compatibility=_carry_forward(compatibility, current_latest.compatibility),
allowed_tools=_carry_forward(allowed_tools, current_latest.allowed_tools),
- metadata=_carry_forward(metadata, current_latest.metadata),
+ metadata=_resolve_published_metadata(metadata, current_latest.metadata),
# Categorization is a property of the skill, not the version — carry it forward so editing
# a scout (or any categorized skill) doesn't drop it out of its tab.
category=current_latest.category,
diff --git a/products/skills/backend/api/test/test_skills.py b/products/skills/backend/api/test/test_skills.py
index 1d5cbfb8e64f..94beec99fd26 100644
--- a/products/skills/backend/api/test/test_skills.py
+++ b/products/skills/backend/api/test/test_skills.py
@@ -1064,6 +1064,26 @@ class TestLLMSkillAPI(APIBaseTest):
assert data["license"] == "MIT"
assert data["compatibility"] == "Python 3.12+"
+ @parameterized.expand(
+ [
+ ("drops_the_hash", {"seeded_by": "signals_scout_harness"}),
+ ("forges_the_hash", {"seeded_by": "signals_scout_harness", "canonical_hash": "forged"}),
+ ("forges_the_seed_tag", {"seeded_by": "someone_else", "canonical_hash": "forged", "source": "elsewhere"}),
+ ]
+ )
+ def test_publish_cannot_rewrite_harness_provenance_metadata(self, _label, supplied_metadata):
+ seeded = {"seeded_by": "signals_scout_harness", "canonical_hash": "abc123", "source": "products/signals/skills"}
+ self.create_skill(name="signals-scout-health-checks", metadata=seeded)
+
+ response = self.client.patch(
+ self._url("name/signals-scout-health-checks"),
+ data={"body": "# Repurposed", "metadata": {**supplied_metadata, "note": "mine"}, "base_version": 1},
+ format="json",
+ )
+
+ assert response.status_code == status.HTTP_200_OK
+ assert response.json()["metadata"] == {**seeded, "note": "mine"}
+
def test_publish_can_update_description(self):
self.create_skill(name="update-desc", description="Old desc.", body="# Body")