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.

chore(semgrep): add rule to prefer uuid7 for Django UUIDField (#58254)

Julian Bez4d34fb4df180
diff --git a/.semgrep/rules/prefer-uuid7-django-pk.yaml b/.semgrep/rules/prefer-uuid7-django-pk.yaml
new file mode 100644
index 000000000000..da40a6221984
--- /dev/null
+++ b/.semgrep/rules/prefer-uuid7-django-pk.yaml
@@ -0,0 +1,36 @@
+rules:
+ - id: prefer-uuid7-django-pk
+ message: >-
+ Do not use `uuid.uuid4` as the default for a Django `UUIDField`
+ primary key. Inherit from `UUIDModel` (which sets a `uuid7` PK for
+ you), or set `default=uuid7` from `posthog.models.utils` explicitly.
+ UUIDv7 embeds a 48-bit unix-ms timestamp in its high bits, so
+ cross-millisecond collisions are structurally impossible regardless
+ of entropy quality, and the time-prefix gives much better B-tree
+ index locality on inserts. `UUIDModel` is the established convention
+ — over 100 models already inherit from it. UUIDs are not the right
+ primitive for secret tokens regardless of version; use
+ `secrets.token_urlsafe(...)` for those. See `posthog/models/utils.py`
+ for `UUIDModel` and `uuid7`.
+ severity: WARNING
+ languages: [python]
+ patterns:
+ - pattern-either:
+ - pattern: models.UUIDField(..., default=uuid.uuid4, ...)
+ - pattern: models.UUIDField(..., default=uuid4, ...)
+ - pattern: models.UUIDField(..., primary_key=True, ...)
+ paths:
+ include:
+ - '**/models.py'
+ - '**/models/**'
+ exclude:
+ # Generated migration files — historical state, can't be changed retroactively.
+ - '**/migrations/**'
+ # The rule lives next to its own fixtures.
+ - '**/.semgrep/**'
+ # Tests for models live alongside but shouldn't be linted.
+ - '**/test/**'
+ - '**/tests/**'
+ - '**/*_test.py'
+ - '**/test_*.py'
+ - '**/conftest*.py'
diff --git a/products/conversations/backend/models/restore_token.py b/products/conversations/backend/models/restore_token.py
index 947fa7db5364..8e38d71951f9 100644
--- a/products/conversations/backend/models/restore_token.py
+++ b/products/conversations/backend/models/restore_token.py
@@ -22,6 +22,7 @@ DEFAULT_TOKEN_TTL_MINUTES = 60
class ConversationRestoreToken(models.Model):
+ # nosemgrep: prefer-uuid7-django-pk -- TODO: migrate to uuid7 or clarify intent
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
token_hash = models.CharField(max_length=64, unique=True, db_index=True)
team = models.ForeignKey("posthog.Team", on_delete=models.CASCADE, related_name="conversation_restore_tokens")
diff --git a/products/endpoints/backend/models.py b/products/endpoints/backend/models.py
index c5630d7a8d97..e60355491919 100644
--- a/products/endpoints/backend/models.py
+++ b/products/endpoints/backend/models.py
@@ -95,6 +95,7 @@ class EndpointVersion(UpdatedMetaFields, models.Model):
This allows users to execute specific versions or track query evolution over time.
"""
+ # nosemgrep: prefer-uuid7-django-pk -- TODO: migrate to uuid7 or clarify intent
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
endpoint = models.ForeignKey("Endpoint", on_delete=models.CASCADE, related_name="versions")
team = models.ForeignKey(
diff --git a/products/product_tours/backend/models.py b/products/product_tours/backend/models.py
index 40b711b6f26c..511ae3af6ef2 100644
--- a/products/product_tours/backend/models.py
+++ b/products/product_tours/backend/models.py
@@ -14,6 +14,7 @@ class ProductTourManager(models.Manager):
class ProductTour(models.Model):
"""A product tour guides users through application features."""
+ # nosemgrep: prefer-uuid7-django-pk -- TODO: migrate to uuid7 or clarify intent
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
team = models.ForeignKey(
diff --git a/products/streamlit_apps/backend/models.py b/products/streamlit_apps/backend/models.py
index 2386ea36ce58..1641990da797 100644
--- a/products/streamlit_apps/backend/models.py
+++ b/products/streamlit_apps/backend/models.py
@@ -6,6 +6,7 @@ from posthog.utils import generate_short_id
class StreamlitApp(models.Model):
+ # nosemgrep: prefer-uuid7-django-pk -- TODO: migrate to uuid7 or clarify intent
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
short_id = models.CharField(max_length=12, blank=True, default=generate_short_id)
team = models.ForeignKey("posthog.Team", on_delete=models.CASCADE)
@@ -47,6 +48,7 @@ class StreamlitApp(models.Model):
class StreamlitAppVersion(models.Model):
+ # nosemgrep: prefer-uuid7-django-pk -- TODO: migrate to uuid7 or clarify intent
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
app = models.ForeignKey(StreamlitApp, on_delete=models.CASCADE, related_name="versions")
version_number = models.PositiveIntegerField()
@@ -76,6 +78,7 @@ class StreamlitAppSandbox(models.Model):
STOPPED = "stopped", "Stopped"
ERROR = "error", "Error"
+ # nosemgrep: prefer-uuid7-django-pk -- TODO: migrate to uuid7 or clarify intent
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
app = models.OneToOneField(StreamlitApp, on_delete=models.CASCADE, related_name="sandbox")
version = models.ForeignKey(StreamlitAppVersion, on_delete=models.SET_NULL, null=True, blank=True)
diff --git a/products/tasks/backend/models.py b/products/tasks/backend/models.py
index 9df5763fed16..bd3167507b63 100644
--- a/products/tasks/backend/models.py
+++ b/products/tasks/backend/models.py
@@ -62,6 +62,7 @@ class Task(DeletedMetaFields, models.Model):
# signal report tasks originate indirectly via signals from other products.
SIGNAL_REPORT = "signal_report", "Signal Report"
+ # nosemgrep: prefer-uuid7-django-pk -- TODO: migrate to uuid7 or clarify intent
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
team = models.ForeignKey("posthog.Team", on_delete=models.CASCADE)
created_by = models.ForeignKey("posthog.User", on_delete=models.SET_NULL, null=True, blank=True, db_index=False)
@@ -436,6 +437,7 @@ class TaskAutomation(models.Model):
FAILED = "failed", "Failed"
RUNNING = "running", "Running"
+ # nosemgrep: prefer-uuid7-django-pk -- TODO: migrate to uuid7 or clarify intent
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
cron_expression = models.CharField(max_length=100)
timezone = models.CharField(max_length=128, default="UTC")
@@ -524,6 +526,7 @@ class TaskRun(models.Model):
LOCAL = "local", "Local"
CLOUD = "cloud", "Cloud"
+ # nosemgrep: prefer-uuid7-django-pk -- TODO: migrate to uuid7 or clarify intent
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
task = models.ForeignKey(Task, on_delete=models.CASCADE, related_name="runs")
team = models.ForeignKey("posthog.Team", on_delete=models.CASCADE)
diff --git a/products/visual_review/backend/models.py b/products/visual_review/backend/models.py
index ad17e0f820bc..56a7a89c93ae 100644
--- a/products/visual_review/backend/models.py
+++ b/products/visual_review/backend/models.py
@@ -29,6 +29,7 @@ class Repo(ProductTeamModel):
repo_full_name is kept for API calls and display, auto-updated on rename detection.
"""
+ # nosemgrep: prefer-uuid7-django-pk -- TODO: migrate to uuid7 (UUIDModel)
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
# GitHub identity: numeric ID is stable, full_name is for API calls + display
@@ -85,6 +86,7 @@ class Artifact(ProductTeamModel):
Same hash = same artifact. Deduplicated across all runs in a repo.
"""
+ # nosemgrep: prefer-uuid7-django-pk -- TODO: migrate to uuid7 (UUIDModel)
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
repo = models.ForeignKey(Repo, on_delete=models.CASCADE, related_name="artifacts")
@@ -115,6 +117,7 @@ class Run(ProductTeamModel):
Created when CI posts a manifest. Tracks status through diff processing.
"""
+ # nosemgrep: prefer-uuid7-django-pk -- TODO: migrate to uuid7 (UUIDModel)
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
repo = models.ForeignKey(Repo, on_delete=models.CASCADE, related_name="runs")
@@ -187,6 +190,7 @@ class RunSnapshot(ProductTeamModel):
Links current captured image to baseline. Stores diff results.
"""
+ # nosemgrep: prefer-uuid7-django-pk -- TODO: migrate to uuid7 (UUIDModel)
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
run = models.ForeignKey(Run, on_delete=models.CASCADE, related_name="snapshots")
@@ -304,6 +308,7 @@ class ToleratedHash(ProductTeamModel):
baseline_hash no longer matches.
"""
+ # nosemgrep: prefer-uuid7-django-pk -- TODO: migrate to uuid7 (UUIDModel)
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
repo = models.ForeignKey(Repo, on_delete=models.CASCADE, related_name="tolerated_hashes")
@@ -355,6 +360,7 @@ class QuarantinedIdentifier(ProductTeamModel):
runs remain stable even if quarantine policy changes later.
"""
+ # nosemgrep: prefer-uuid7-django-pk -- TODO: migrate to uuid7 (UUIDModel)
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
repo = models.ForeignKey(Repo, on_delete=models.CASCADE, related_name="quarantined_identifiers")