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(devex): block new custom django admin views and templates (#128991)

Julian Bez61aa7f16620f
diff --git a/.semgrep/rules/devex/no-new-custom-admin-views.html b/.semgrep/rules/devex/no-new-custom-admin-views.html
new file mode 100644
index 000000000000..0caeae0c0163
--- /dev/null
+++ b/.semgrep/rules/devex/no-new-custom-admin-views.html
@@ -0,0 +1,3 @@
+<!-- ruleid: no-new-custom-admin-templates -->
+{% extends "admin/change_form.html" %}
+{% block content %}{{ block.super }}{% endblock %}
diff --git a/.semgrep/rules/devex/no-new-custom-admin-views.py b/.semgrep/rules/devex/no-new-custom-admin-views.py
new file mode 100644
index 000000000000..f6d468fb036b
--- /dev/null
+++ b/.semgrep/rules/devex/no-new-custom-admin-views.py
@@ -0,0 +1,25 @@
+# Test cases for no-new-custom-admin-views.
+# ruff: noqa
+
+
+class TeamAdmin(admin.ModelAdmin):
+ # ok: no-new-custom-admin-views
+ list_display = ("id", "name")
+
+ # ruleid: no-new-custom-admin-views
+ change_form_template = "admin/posthog/team/change_form.html"
+
+ # ruleid: no-new-custom-admin-views
+ def get_urls(self):
+ # ruleid: no-new-custom-admin-views
+ return [path("<path:object_id>/rebuild/", self.admin_site.admin_view(self.rebuild))] + super().get_urls()
+
+ # ok: no-new-custom-admin-views
+ def has_delete_permission(self, request, obj=None):
+ return False
+
+
+urlpatterns = [
+ # ruleid: no-new-custom-admin-views
+ path("admin/tophog/", admin.site.admin_view(tophog_dashboard_view)),
+]
diff --git a/.semgrep/rules/devex/no-new-custom-admin-views.yaml b/.semgrep/rules/devex/no-new-custom-admin-views.yaml
new file mode 100644
index 000000000000..cddf1a59debd
--- /dev/null
+++ b/.semgrep/rules/devex/no-new-custom-admin-views.yaml
@@ -0,0 +1,52 @@
+rules:
+ - id: no-new-custom-admin-views
+ message: |
+ New staff workflows go in the PostHog Admin console, not in custom Django admin views,
+ URLs or template overrides. The console sits under /admin/console/, so the same staff
+ check and OAuth step-up apply, and its pages do not break when Django changes admin
+ internals. Add a Panel in products/<product>/backend/console.py; see
+ posthog/admin/console/AGENTS.md. Django admin stays for plain CRUD: list_display,
+ search_fields, readonly_fields, inlines and simple actions are fine.
+ Pre-existing findings are grandfathered; only newly introduced findings block CI.
+ languages: [python]
+ severity: WARNING
+ pattern-either:
+ - pattern: |
+ def get_urls(self, ...):
+ ...
+ - patterns:
+ - pattern: $TEMPLATE = $VALUE
+ - metavariable-regex:
+ metavariable: $TEMPLATE
+ regex: ^(change_form_template|change_list_template|add_form_template|delete_confirmation_template|delete_selected_confirmation_template|object_history_template|popup_response_template)$
+ - pattern: $SITE.admin_view(...)
+ paths:
+ include:
+ - '**/admin.py'
+ - '**/admin/**/*.py'
+ - ee/urls.py
+ exclude:
+ - posthog/admin/console/**
+ - '**/test/**'
+ - '**/tests/**'
+ - '**/test_*.py'
+ metadata:
+ category: maintainability
+ subcategory: audit
+ confidence: HIGH
+ - id: no-new-custom-admin-templates
+ message: |
+ Django admin picks up a template under templates/admin/ by its path alone, so this file
+ overrides or extends an admin page without any Python change. New staff pages go in the
+ PostHog Admin console; see posthog/admin/console/AGENTS.md.
+ Pre-existing findings are grandfathered; only newly introduced findings block CI.
+ languages: [generic]
+ severity: WARNING
+ pattern-regex: \{%\s*extends\s+["']admin/
+ paths:
+ include:
+ - '**/templates/admin/**/*.html'
+ metadata:
+ category: maintainability
+ subcategory: audit
+ confidence: HIGH
diff --git a/posthog/admin/console/AGENTS.md b/posthog/admin/console/AGENTS.md
index fcaf70e742d4..c6a7194c65b3 100644
--- a/posthog/admin/console/AGENTS.md
+++ b/posthog/admin/console/AGENTS.md
@@ -2,6 +2,7 @@
The staff back office at `/admin/console/`.
It replaces custom Django admin views and templates for staff workflows.
+The `no-new-custom-admin-views` semgrep rule blocks new ones, and the program ends when its findings reach zero.
The Django admin stays for plain CRUD on models.
## Invariants