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): mask credentials in report edit history (#126450)

posthog[bot]10dfd4071bd5
diff --git a/products/signals/backend/artefact_schemas.py b/products/signals/backend/artefact_schemas.py
index e2ae21c4e0d6..b1b7605249f5 100644
--- a/products/signals/backend/artefact_schemas.py
+++ b/products/signals/backend/artefact_schemas.py
@@ -37,6 +37,7 @@ from pydantic import (
model_validator,
)
+from products.signals.backend.credential_redaction import redact_credentials
from products.signals.backend.enums import ReportLinkKind, ReportPriority, SuggestedSourceProduct
from products.signals.backend.report_checks import CheckComparison, CheckInconclusiveReason, CheckOutcome
from products.signals.backend.report_metrics import MAX_METRIC_UNIT_LENGTH, ReportMetricValueFormat
@@ -854,6 +855,12 @@ class TitleChange(BaseModel):
raise ValueError("must not be empty or whitespace-only")
return v
+ # The edit history outlives the edit, so a token pasted and then edited out must not stay here.
+ @field_validator("old_title", "new_title")
+ @classmethod
+ def redact_title_credentials(cls, v: str | None) -> str | None:
+ return None if v is None else redact_credentials(v)
+
class SummaryChange(BaseModel):
"""Content schema for a `summary_change` artefact: a record of an edit to the report's summary
@@ -876,6 +883,12 @@ class SummaryChange(BaseModel):
raise ValueError("must not be empty or whitespace-only")
return v
+ # The edit history outlives the edit, so a token pasted and then edited out must not stay here.
+ @field_validator("old_summary", "new_summary")
+ @classmethod
+ def redact_summary_credentials(cls, v: str | None) -> str | None:
+ return None if v is None else redact_credentials(v)
+
class RelatedTo(BaseModel):
"""Content schema for a `related_to` artefact: an untyped link from this report to another
diff --git a/products/signals/backend/credential_redaction.py b/products/signals/backend/credential_redaction.py
new file mode 100644
index 000000000000..ebc1ed057f3b
--- /dev/null
+++ b/products/signals/backend/credential_redaction.py
@@ -0,0 +1,40 @@
+"""Mask credential-shaped substrings in report text.
+
+A person can paste a token into a report and then edit it out. The edit history keeps the text
+before and after each edit, so it must not keep the token. Matching on token shapes needs no
+access to the live values.
+"""
+
+import re
+
+from posthog.models.utils import (
+ OAUTH_ACCESS_TOKEN_PREFIX,
+ OAUTH_REFRESH_TOKEN_PREFIX,
+ PERSONAL_API_KEY_PREFIX,
+ SECRET_API_TOKEN_PREFIX,
+)
+
+REDACTED = "[redacted]"
+
+# Project API tokens (`phc_`) are public by design and stay out of this list.
+_POSTHOG_SECRET_PREFIXES = (
+ PERSONAL_API_KEY_PREFIX,
+ SECRET_API_TOKEN_PREFIX,
+ OAUTH_ACCESS_TOKEN_PREFIX,
+ OAUTH_REFRESH_TOKEN_PREFIX,
+)
+
+_SECRET_PATTERNS: tuple[re.Pattern[str], ...] = (
+ re.compile(r"\b(?:" + "|".join(re.escape(p) for p in _POSTHOG_SECRET_PREFIXES) + r")[A-Za-z0-9]{16,}\b"),
+ # GitHub tokens: installation (ghs), classic PAT (ghp), OAuth (gho), user-to-server (ghu), refresh (ghr).
+ re.compile(r"\bgh[psour]_[A-Za-z0-9]{20,}\b"),
+ re.compile(r"\bgithub_pat_[A-Za-z0-9_]{20,}\b"),
+ # Credentials in a URL, such as `https://x-access-token:<token>@github.com/`.
+ re.compile(r"(?<=://)[^/\s:@]+:[^/\s@]+(?=@)"),
+)
+
+
+def redact_credentials(text: str) -> str:
+ for pattern in _SECRET_PATTERNS:
+ text = pattern.sub(REDACTED, text)
+ return text
diff --git a/products/signals/backend/serializers.py b/products/signals/backend/serializers.py
index 9065380fb8bc..f0a288b45583 100644
--- a/products/signals/backend/serializers.py
+++ b/products/signals/backend/serializers.py
@@ -39,6 +39,7 @@ from products.signals.backend.contracts import (
AlertScope,
scope_ids_problem,
)
+from products.signals.backend.credential_redaction import redact_credentials
from products.signals.backend.enums import (
ReportTaskActionKind,
ReviewerEditAction,
@@ -3348,6 +3349,15 @@ class SignalReportArtefactSerializer(serializers.ModelSerializer):
user_authored=self._report_is_user_authored(obj),
)
+ if obj.type in {
+ SignalReportArtefact.ArtefactType.TITLE_CHANGE,
+ SignalReportArtefact.ArtefactType.SUMMARY_CHANGE,
+ } and isinstance(parsed, dict):
+ # Rows written before the write path redacted credentials can still hold one.
+ return {
+ key: redact_credentials(value) if isinstance(value, str) else value for key, value in parsed.items()
+ }
+
if obj.type in {
SignalReportArtefact.ArtefactType.CHECK_RESULT,
SignalReportArtefact.ArtefactType.CHECK_OBSERVATION,
diff --git a/products/signals/backend/test/test_signal_report_api.py b/products/signals/backend/test/test_signal_report_api.py
index e0c40b081ea0..1bca7a32016f 100644
--- a/products/signals/backend/test/test_signal_report_api.py
+++ b/products/signals/backend/test/test_signal_report_api.py
@@ -5550,6 +5550,31 @@ class TestSignalReportContentUpdateAPI(APIBaseTest):
content = json.loads(artefacts[0].content)
assert content == {"old_summary": "Original summary", "new_summary": "New summary"}
+ def test_redacting_edit_does_not_expose_credential_through_artefacts_api(self):
+ token = "ghp_" + "Fake0Token" * 4
+ report = self._create_report()
+ report.summary = f"Run with token {token} to reproduce."
+ report.save(update_fields=["summary"])
+ SignalReportArtefact.objects.create(
+ team=self.team,
+ report=report,
+ type=SignalReportArtefact.ArtefactType.SUMMARY_CHANGE,
+ content=json.dumps({"old_summary": f"Legacy {token}", "new_summary": "Legacy edit"}),
+ )
+
+ response = self.client.patch(
+ self._url(str(report.id)), data={"summary": "Run with token [redacted token]."}, format="json"
+ )
+ assert response.status_code == status.HTTP_200_OK
+
+ stored = [a.content for a in self._artefacts(report, SignalReportArtefact.ArtefactType.SUMMARY_CHANGE)]
+ assert len(stored) == 2
+ assert token not in stored[1]
+ listed = self.client.get(self._artefacts_url(str(report.id)))
+ assert listed.status_code == status.HTTP_200_OK
+ assert token not in listed.content.decode()
+ assert "Run with token [redacted] to reproduce." in listed.content.decode()
+
def test_update_both_records_one_artefact_per_field(self):
report = self._create_report()
self.client.patch(