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(tasks): make repo-less github access opt-in (#126073)

Charles Vien7084fea3c68d
diff --git a/products/business_knowledge/backend/task_profiles.py b/products/business_knowledge/backend/task_profiles.py
index 3dc174c1f174..7e2fa00718db 100644
--- a/products/business_knowledge/backend/task_profiles.py
+++ b/products/business_knowledge/backend/task_profiles.py
@@ -35,7 +35,7 @@ BUSINESS_KNOWLEDGE = TaskProfile(
mcp_scopes=BK_MCP_SCOPES,
mcp_store_servers=False,
oauth_app=OAuthApp.ARRAY,
- github_resolution=GithubResolution.ALWAYS,
+ github_resolution=GithubResolution.WITH_REPOSITORY,
user_authorable_prs=False,
default_pr_authorship=PrAuthorship.BOT,
create_pr=False,
diff --git a/products/tasks/backend/facade/api.py b/products/tasks/backend/facade/api.py
index abba7acadcaa..518774d2f97a 100644
--- a/products/tasks/backend/facade/api.py
+++ b/products/tasks/backend/facade/api.py
@@ -6475,7 +6475,16 @@ def _resolve_cloud_pr_authorship_mode(
user_github_integration_is_usable,
)
- if task.origin_product == Task.OriginProduct.ACCOUNT_AUDIT:
+ if not Task._github_resolution_allowed(
+ task.origin_product, has_repository=bool(task.repositories or task.repository)
+ ):
+ if pr_authorship_mode == PrAuthorshipMode.USER:
+ return None, contracts.TaskRunValidationError(
+ kind="validation_error",
+ code="github_authorization_required",
+ detail="User-authored runs are not allowed for this task without a repository.",
+ attr="pr_authorship_mode",
+ )
return None, None
if pr_authorship_mode != PrAuthorshipMode.USER or github_user_token:
@@ -8466,6 +8475,12 @@ def update_task(
logger.info("perform_update called for task %s with validated_data: %s", task.id, validated_data)
for key, value in validated_data.items():
setattr(task, key, value)
+ github_fields_touched = repo_fields_touched or "github_user_integration" in validated_data
+ if github_fields_touched and not Task._github_resolution_allowed(
+ task.origin_product, has_repository=bool(task.repositories or task.repository)
+ ):
+ task.github_integration = None
+ task.github_user_integration = None
task.save()
logger.info("Task %s updated successfully", task.id)
diff --git a/products/tasks/backend/models.py b/products/tasks/backend/models.py
index d6dbaeb87715..f89a0af3b3ac 100644
--- a/products/tasks/backend/models.py
+++ b/products/tasks/backend/models.py
@@ -1297,19 +1297,15 @@ class Task(Taggable, DeletedMetaFields, models.Model):
@staticmethod
def _github_resolution_allowed(origin_product: str, *, has_repository: bool) -> bool:
- # Audits and repo-less signals, autoresearch or metrics import tasks must carry no GitHub
- # credential. Provisioning injects any attached integration; these runs read member-authored
- # text, so an attached token turns planted text into repository access.
- return origin_product != Task.OriginProduct.ACCOUNT_AUDIT and (
- has_repository
- or origin_product
- not in (
- Task.OriginProduct.SIGNALS_CHAT,
- Task.OriginProduct.SIGNAL_REPORT,
- Task.OriginProduct.SIGNALS_SCOUT_SUGGESTIONS,
- Task.OriginProduct.AUTORESEARCH,
- Task.OriginProduct.METRICS_IMPORT,
- )
+ # Repo-less tokens are unscoped. Scouts are listed so their read-only token keeps refreshing.
+ if origin_product == Task.OriginProduct.ACCOUNT_AUDIT:
+ return False
+ return has_repository or origin_product in (
+ Task.OriginProduct.USER_CREATED,
+ Task.OriginProduct.SLACK,
+ Task.OriginProduct.POSTHOG_AI,
+ Task.OriginProduct.IMAGE_BUILDER,
+ Task.OriginProduct.SIGNALS_SCOUT,
)
@staticmethod
diff --git a/products/tasks/backend/tests/test_api.py b/products/tasks/backend/tests/test_api.py
index 5f8bd70d2664..bbe5d2db56ea 100644
--- a/products/tasks/backend/tests/test_api.py
+++ b/products/tasks/backend/tests/test_api.py
@@ -3852,6 +3852,86 @@ class TestTaskAPI(BaseTaskAPITest):
task.refresh_from_db()
self.assertIsNone(task.signal_report_id)
+ @parameterized.expand(
+ [
+ ("refused_origin_without_repository", Task.OriginProduct.WORKFLOW, "github_integration", [], None, False),
+ (
+ "refused_origin_removing_its_repository",
+ Task.OriginProduct.WORKFLOW,
+ "github_integration",
+ ["posthog/posthog"],
+ [],
+ False,
+ ),
+ (
+ "refused_origin_adding_a_repository",
+ Task.OriginProduct.WORKFLOW,
+ "github_integration",
+ [],
+ ["posthog/posthog"],
+ True,
+ ),
+ (
+ "opted_in_origin_without_repository",
+ Task.OriginProduct.USER_CREATED,
+ "github_integration",
+ [],
+ None,
+ True,
+ ),
+ (
+ "refused_origin_personal_integration_only",
+ Task.OriginProduct.WORKFLOW,
+ "github_user_integration",
+ [],
+ None,
+ False,
+ ),
+ (
+ "opted_in_origin_personal_integration_only",
+ Task.OriginProduct.USER_CREATED,
+ "github_user_integration",
+ [],
+ None,
+ True,
+ ),
+ ]
+ )
+ @patch("products.tasks.backend.facade.api.inaccessible_repositories_via_integration", return_value=[])
+ def test_patch_attaches_github_only_where_create_would(
+ self,
+ _name: str,
+ origin_product: Task.OriginProduct,
+ patched_field: str,
+ repositories: list[str],
+ patched_repositories: list[str] | None,
+ attaches_github: bool,
+ _mock_repository_access: MagicMock,
+ ):
+ integration = Integration.objects.create(team=self.team, kind="github", config={})
+ task = Task.objects.create(
+ team=self.team,
+ created_by=self.user,
+ title="Task",
+ description="Task",
+ origin_product=origin_product,
+ repository=repositories[0] if repositories else None,
+ repositories=repositories,
+ github_integration=integration if repositories else None,
+ )
+ patched_id = (
+ _grant_user_github_access(self.user).id if patched_field == "github_user_integration" else integration.id
+ )
+ body: dict = {patched_field: str(patched_id)}
+ if patched_repositories is not None:
+ body["repositories"] = patched_repositories
+
+ response = self.client.patch(f"/api/projects/@current/tasks/{task.id}/", body, format="json")
+
+ self.assertEqual(response.status_code, status.HTTP_200_OK, response.json())
+ task.refresh_from_db()
+ self.assertEqual(getattr(task, f"{patched_field}_id"), patched_id if attaches_github else None)
+
def test_update_task(self):
task = self.create_task("Original Task")
@@ -5108,6 +5188,47 @@ class TestTaskAPI(BaseTaskAPITest):
self.assertEqual(task_run.state["pr_authorship_mode"], "bot")
mock_workflow.assert_not_called()
+ @parameterized.expand(
+ [
+ (endpoint, available, is_creator)
+ for endpoint in ("runs", "run")
+ for available in ("user_integration", "team_integration_only", "existing_team_integration")
+ for is_creator in (True, False)
+ ]
+ )
+ @patch("products.tasks.backend.temporal.client.execute_task_processing_workflow")
+ def test_run_endpoints_reject_user_authorship_for_refused_repo_less_task(
+ self, endpoint, available, is_creator, mock_workflow
+ ):
+ integration = Integration.objects.create(team=self.team, kind="github", config={"access_token": "token"})
+ creator = self.user if is_creator else self.create_organization_user("task-creator")
+ if available == "user_integration":
+ _grant_user_github_access(creator)
+ task = self.create_task(created_by=creator)
+ task.origin_product = Task.OriginProduct.WORKFLOW
+ task.github_integration = integration if available == "existing_team_integration" else None
+ task.save(update_fields=["origin_product", "github_integration"])
+
+ response = self.client.post(
+ f"/api/projects/@current/tasks/{task.id}/{endpoint}/",
+ {
+ "environment": "cloud",
+ "pr_authorship_mode": "user",
+ "run_source": "manual",
+ },
+ format="json",
+ )
+
+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST, response.json())
+ self.assertEqual(response.json()["code"], "github_authorization_required")
+ self.assertFalse(task.runs.exists())
+ task.refresh_from_db()
+ self.assertEqual(
+ task.github_integration_id, integration.id if available == "existing_team_integration" else None
+ )
+ self.assertIsNone(task.github_user_integration_id)
+ mock_workflow.assert_not_called()
+
@patch("products.tasks.backend.temporal.client.execute_task_processing_workflow")
def test_create_run_endpoint_cannot_start_a_product_private_account_audit(self, mock_workflow):
Integration.objects.create(team=self.team, kind="github", config={"access_token": "token"})
diff --git a/products/tasks/backend/tests/test_models.py b/products/tasks/backend/tests/test_models.py
index 1a33d36be394..91c8d142b278 100644
--- a/products/tasks/backend/tests/test_models.py
+++ b/products/tasks/backend/tests/test_models.py
@@ -668,19 +668,27 @@ class TestTask(TestCase):
@parameterized.expand(
[
- (Task.OriginProduct.SIGNALS_CHAT,),
- (Task.OriginProduct.SIGNAL_REPORT,),
- (Task.OriginProduct.SIGNALS_SCOUT_SUGGESTIONS,),
- (Task.OriginProduct.AUTORESEARCH,),
- (Task.OriginProduct.ACCOUNT_AUDIT,),
+ (Task.OriginProduct.SIGNALS_CHAT, False),
+ (Task.OriginProduct.SIGNAL_REPORT, False),
+ (Task.OriginProduct.SIGNALS_SCOUT_SUGGESTIONS, False),
+ (Task.OriginProduct.AUTORESEARCH, False),
+ (Task.OriginProduct.ACCOUNT_AUDIT, False),
+ (Task.OriginProduct.WORKFLOW, False),
+ (Task.OriginProduct.SUPPORT_REPLY, False),
+ (Task.OriginProduct.TASK_ANALYSIS, False),
+ (Task.OriginProduct.SPACE_SETUP, False),
+ (Task.OriginProduct.USER_CREATED, True),
+ (Task.OriginProduct.SLACK, True),
+ (Task.OriginProduct.IMAGE_BUILDER, True),
+ (Task.OriginProduct.SIGNALS_SCOUT, True),
]
)
@patch("products.tasks.backend.temporal.client.execute_task_processing_workflow")
- def test_create_and_run_attaches_no_github_integration_to_a_repo_less_restricted_origin(
- self, origin_product, mock_execute_workflow
+ def test_create_and_run_attaches_github_to_a_repo_less_task_only_for_opted_in_origins(
+ self, origin_product, attaches_github, mock_execute_workflow
):
user = User.objects.create(email="[email protected]")
- Integration.objects.create(team=self.team, kind="github", config={})
+ integration = Integration.objects.create(team=self.team, kind="github", config={})
with self.captureOnCommitCallbacks(execute=True):
task = Task.create_and_run(
@@ -691,7 +699,7 @@ class TestTask(TestCase):
user_id=user.id,
)
- self.assertIsNone(task.github_integration)
+ self.assertEqual(task.github_integration, integration if attaches_github else None)
self.assertIsNone(task.github_user_integration)
@parameterized.expand(