: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(legal-documents): opt out of AI training when a BAA is signed (#83926)
Matt Brooker1e98dd740c7a
diff --git a/products/legal_documents/backend/logic/__init__.py b/products/legal_documents/backend/logic/__init__.pyindex 86c9d596d1fd..4221cd35a9ef 100644--- a/products/legal_documents/backend/logic/__init__.py+++ b/products/legal_documents/backend/logic/__init__.py@@ -307,9 +307,9 @@ BAA_SIGNED_AI_DISABLED_TEMPLATE = "baa_signed_ai_disabled"def apply_baa_signed_side_effects(document: LegalDocument) -> None:"""When a BAA is signed, opt the organization out of AI data processing and- notify its owners. The BAA does not cover third-party AI subprocessors, so- we fail safe to opt-out — owners can re-enable from settings if they want- AI features for non-PHI workflows.+ out of AI training, then notify its owners. The BAA does not cover+ third-party AI subprocessors, so we fail safe to opt-out — owners can+ re-enable from settings if they want AI features for non-PHI workflows.Best-effort: any failure here is logged but does not roll back the BAAsignature itself (PandaDoc has already collected it).@@ -319,9 +319,15 @@ def apply_baa_signed_side_effects(document: LegalDocument) -> None:organization = document.organizationtry:+ updated_fields = []if organization.is_ai_data_processing_approved is not False:organization.is_ai_data_processing_approved = False- organization.save(update_fields=["is_ai_data_processing_approved"])+ updated_fields.append("is_ai_data_processing_approved")+ if organization.is_ai_training_opted_in is not False:+ organization.is_ai_training_opted_in = False+ updated_fields.append("is_ai_training_opted_in")+ if updated_fields:+ organization.save(update_fields=updated_fields)except Exception as exc:logger.exception("legal_document_baa_opt_out_failed", document_id=str(document.id), error=str(exc))capture_exception(exc, additional_properties={"legal_document_id": str(document.id)})diff --git a/products/legal_documents/backend/tests/test_api.py b/products/legal_documents/backend/tests/test_api.pyindex 8675a0fe35ad..41151a8ce0e1 100644--- a/products/legal_documents/backend/tests/test_api.py+++ b/products/legal_documents/backend/tests/test_api.py@@ -750,11 +750,16 @@ class TestLegalDocumentPandaDocWebhook(APIBaseTest):):yield- def test_signed_baa_opts_organization_out_of_ai_data_processing(self) -> None:+ # Both flags send customer data to a third party the BAA does not cover: one to the LLM+ # subprocessors, one to the replay ML training mirror. Signing a BAA has to clear both.+ AI_OPT_OUT_FLAGS = [("is_ai_data_processing_approved",), ("is_ai_training_opted_in",)]++ @parameterized.expand(AI_OPT_OUT_FLAGS)+ def test_signed_baa_opts_organization_out(self, flag: str) -> None:self._swap_to_baa_document()- # New orgs default to True now — explicitly set so this isn't accidentally testing the default.- self.organization.is_ai_data_processing_approved = True- self.organization.save(update_fields=["is_ai_data_processing_approved"])+ # Both default to True now — explicitly set so this isn't accidentally testing the default.+ setattr(self.organization, flag, True)+ self.organization.save(update_fields=[flag])body = json.dumps(self._completed_payload(template_id=self.BAA_TEMPLATE_ID)).encode("utf-8")with self._override(), self._fake_pdf_pipeline():@@ -762,12 +767,13 @@ class TestLegalDocumentPandaDocWebhook(APIBaseTest):self.assertEqual(response.status_code, status.HTTP_200_OK)self.organization.refresh_from_db()- self.assertFalse(self.organization.is_ai_data_processing_approved)+ self.assertFalse(getattr(self.organization, flag))- def test_signed_dpa_does_not_change_ai_flag(self) -> None:+ @parameterized.expand(AI_OPT_OUT_FLAGS)+ def test_signed_dpa_leaves_ai_flags_alone(self, flag: str) -> None:# Default fixture is a DPA, so don't swap.- self.organization.is_ai_data_processing_approved = True- self.organization.save(update_fields=["is_ai_data_processing_approved"])+ setattr(self.organization, flag, True)+ self.organization.save(update_fields=[flag])body = json.dumps(self._completed_payload()).encode("utf-8")with self._override(), self._fake_pdf_pipeline():@@ -775,7 +781,7 @@ class TestLegalDocumentPandaDocWebhook(APIBaseTest):self.assertEqual(response.status_code, status.HTTP_200_OK)self.organization.refresh_from_db()- self.assertTrue(self.organization.is_ai_data_processing_approved)+ self.assertTrue(getattr(self.organization, flag))def test_signed_baa_emails_org_owners(self) -> None:self._swap_to_baa_document()