: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(auth): add blank=True to PersonalAPIKey scoped fields (#72066)
Nick Best67381aab9360
diff --git a/posthog/admin/test_admin.py b/posthog/admin/test_admin.pyindex 07780eb17ed4..5b537f3531af 100644--- a/posthog/admin/test_admin.py+++ b/posthog/admin/test_admin.py@@ -9,11 +9,13 @@ from django.test import RequestFactoryfrom posthog.admin import _OAUTH_ADMIN_MODEL_NAMES, install_admin_app_list_overrides, register_all_adminfrom posthog.admin.admins.event_ingestion_restriction_config import EventIngestionRestrictionConfigAdminfrom posthog.admin.admins.global_rate_limit_threshold_config import GlobalRateLimitThresholdConfigAdmin+from posthog.admin.admins.personal_api_key_admin import PersonalAPIKeyAdminfrom posthog.admin.admins.user_admin import UserAdmin, UserChangeFormfrom posthog.admin.inlines.organization_member_inline import OrganizationMemberForUserInline, OrganizationMemberInline-from posthog.models import User+from posthog.models import PersonalAPIKey, Userfrom posthog.models.event_ingestion_restriction_config import EventIngestionRestrictionConfigfrom posthog.models.global_rate_limit_threshold_config import GlobalRateLimitThresholdConfig+from posthog.models.utils import generate_random_token_personal, hash_key_valuefrom products.alerts.backend.models.alert import AlertConfigurationfrom products.cdp.backend.admin.plugin_attachment_inline import PluginAttachmentInline@@ -251,3 +253,38 @@ class TestOrganizationMemberInlineConfig(BaseTest):assert "invited_by" in OrganizationMemberInline.readonly_fieldsassert "invited_by" in OrganizationMemberForUserInline.fieldsassert "invited_by" in OrganizationMemberForUserInline.readonly_fields+++class TestPersonalAPIKeyAdminForm(BaseTest):+ def test_change_form_saves_unscoped_key_with_blank_scoped_fields(self):+ # scoped_teams/scoped_organizations must stay optional in Django forms+ # (blank=True on the model): null/[] means "unscoped key", the normal state+ # of every UI-minted key. Without it the admin change form demands values+ # for both, making it impossible to edit an unscoped key at all (the flow+ # for granting OAuth-hidden scopes like batch_import_support:read to a+ # staff key).+ key = PersonalAPIKey.objects.create(+ user=self.user, label="support", secure_value=hash_key_value(generate_random_token_personal())+ )+ key_admin = PersonalAPIKeyAdmin(PersonalAPIKey, admin.site)+ request = RequestFactory().get("/admin/")++ form_class = key_admin.get_form(request, key, change=True)+ form = form_class(+ data={+ "label": "support",+ # Admin renders DateTimeField with a split date/time widget.+ "created_at_0": "2026-01-01",+ "created_at_1": "00:00:00",+ "scopes": "user:read,batch_import_support:read",+ "scoped_teams": "",+ "scoped_organizations": "",+ },+ instance=key,+ )++ assert form.is_valid(), form.errors+ saved = form.save()+ assert saved.scopes == ["user:read", "batch_import_support:read"]+ assert not saved.scoped_teams+ assert not saved.scoped_organizationsdiff --git a/posthog/migrations/1261_alter_personalapikey_scoped_organizations_and_more.py b/posthog/migrations/1261_alter_personalapikey_scoped_organizations_and_more.pynew file mode 100644index 000000000000..2cfff0300225--- /dev/null+++ b/posthog/migrations/1261_alter_personalapikey_scoped_organizations_and_more.py@@ -0,0 +1,28 @@+# Generated by Django 5.2.14 on 2026-07-17 18:53++import django.contrib.postgres.fields+from django.db import migrations, models+++class Migration(migrations.Migration):+ dependencies = [("posthog", "1260_activitylog_idx_alog_org_created_at_and_more")]++ operations = [+ migrations.AlterField(+ model_name="personalapikey",+ name="scoped_organizations",+ field=django.contrib.postgres.fields.ArrayField(+ base_field=models.CharField(max_length=100),+ blank=True,+ null=True,+ size=None,+ ),+ ),+ migrations.AlterField(+ model_name="personalapikey",+ name="scoped_teams",+ field=django.contrib.postgres.fields.ArrayField(+ base_field=models.IntegerField(), blank=True, null=True, size=None+ ),+ ),+ ]diff --git a/posthog/migrations/max_migration.txt b/posthog/migrations/max_migration.txtindex a471fe6d3ed5..af37778359e5 100644--- a/posthog/migrations/max_migration.txt+++ b/posthog/migrations/max_migration.txt@@ -1 +1 @@-1260_activitylog_idx_alog_org_created_at_and_more+1261_alter_personalapikey_scoped_organizations_and_morediff --git a/posthog/models/personal_api_key.py b/posthog/models/personal_api_key.pyindex 5e88b996a008..14353d0f7601 100644--- a/posthog/models/personal_api_key.py+++ b/posthog/models/personal_api_key.py@@ -47,8 +47,8 @@ class PersonalAPIKey(ModelActivityMixin, models.Model):last_used_at = models.DateTimeField(null=True, blank=True)last_rolled_at = models.DateTimeField(null=True, blank=True)scopes: ArrayField = ArrayField(models.CharField(max_length=100), default=list)- scoped_teams: ArrayField = ArrayField(models.IntegerField(), null=True)- scoped_organizations: ArrayField = ArrayField(models.CharField(max_length=100), null=True)+ scoped_teams: ArrayField = ArrayField(models.IntegerField(), null=True, blank=True)+ scoped_organizations: ArrayField = ArrayField(models.CharField(max_length=100), null=True, blank=True)# DEPRECATED: value is no longer persisted; use secure_value for hash of valuevalue = deprecate_field(models.CharField(unique=True, max_length=50, editable=False, null=True, blank=True))