From d5a9333cd98e288e161b265b3a0ad7bacada4f92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C4=8Ciha=C5=99?= Date: Sun, 29 Nov 2026 09:29:16 +0100 Subject: [PATCH] fix(backups): warn when a restore makes a project public A public-only workspace plan can make a non-public project accessible even when the backup contains no restricted components. Warn in the import preview and report the actual access change on completion. Fixes #23180 --- docs/admin/backup.rst | 9 +- docs/changes.rst | 2 + .../templates/trans/project_import_info.html | 15 ++- weblate/trans/backups.py | 20 +++- weblate/trans/forms.py | 10 +- weblate/trans/tests/test_backups.py | 104 +++++++++++++++++- 6 files changed, 146 insertions(+), 14 deletions(-) diff --git a/docs/admin/backup.rst b/docs/admin/backup.rst index be76657956b9..bb600ae51505 100644 --- a/docs/admin/backup.rst +++ b/docs/admin/backup.rst @@ -29,10 +29,11 @@ The backup can be restored when creating a project (see :ref:`api` since version 2026.7; The destination workspace's billing plan determines project access on restore. -When the plan requires public content, restricted components in the backup -are restored as public, including imports initiated by superusers. The import -preview warns about this outcome and lists the affected component paths before -you submit it. The completed import reports each component made public. +When the plan requires public content, non-public projects and restricted +components are restored as public, including imports initiated by superusers. +The import preview warns about this outcome and lists any affected component +paths before you submit it. The completed import reports the change in project +access and each component made public. Choose a workspace whose plan permits private content to preserve restrictions. Restoring without destination billing retains the backup's access settings. See :ref:`workspace-project-creation` for the rules when moving existing projects. diff --git a/docs/changes.rst b/docs/changes.rst index 549182bb8a7a..438b2e82a3de 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -97,6 +97,8 @@ Weblate 2026.12 .. rubric:: Security fixes +* Warn before and after restoring a :ref:`project backup ` when a public-only billing plan makes the project public. + * Enforced GitHub App repository and push settings when restoring a :ref:`projectbackup`. * Enforced :setting:`AUTH_LOCK_ATTEMPTS` for password and :ref:`two-factor <2fa>` sign-ins even when requests arrive concurrently. * Protected translation file operations, :ref:`downloads `, and :ref:`JavaScript localization CDN ` extraction against races with concurrent repository updates. diff --git a/weblate/templates/trans/project_import_info.html b/weblate/templates/trans/project_import_info.html index ac81633d4aac..4f6e7c30281d 100644 --- a/weblate/templates/trans/project_import_info.html +++ b/weblate/templates/trans/project_import_info.html @@ -8,13 +8,18 @@

{{ projectbackup.timestamp|naturaltime }}

-{% if restricted_components %} +{% if non_public_project or restricted_components %} {% endif %} diff --git a/weblate/trans/backups.py b/weblate/trans/backups.py index caee6af1e748..0142af70327c 100644 --- a/weblate/trans/backups.py +++ b/weblate/trans/backups.py @@ -2605,8 +2605,26 @@ def get_restricted_components(self) -> list[str]: def get_restore_warnings(self) -> list[str]: """Return user-facing warnings for all changes made during import.""" + project_warnings = [] + if ( + self.public_only_restore + and self.project is not None + and self.project.access_control == Project.ACCESS_PUBLIC + and (original_access := self.data["project"]["access_control"]) + != Project.ACCESS_PUBLIC + ): + project_warnings.append( + gettext( + "Project access changed from %(original)s to %(current)s because the target workspace's billing plan requires public content." + ) + % { + "original": dict(Project.ACCESS_CHOICES)[original_access], + "current": self.project.get_access_control_display(), + } + ) return ( - [ + project_warnings + + [ gettext( "Component %(component)s was skipped because its linked repository is unavailable." ) diff --git a/weblate/trans/forms.py b/weblate/trans/forms.py index 08852cbe32fa..bd63a85e4014 100644 --- a/weblate/trans/forms.py +++ b/weblate/trans/forms.py @@ -4350,15 +4350,19 @@ def __init__( ) else: del self.fields["skip_duplicates"], self.fields["duplicate_scope"] + has_billing = "weblate.billing" in settings.INSTALLED_APPS self.helper.layout = Layout( ContextDiv( template="trans/project_import_info.html", context={ "projectbackup": projectbackup, + "non_public_project": ( + has_billing + and projectbackup.data["project"]["access_control"] + != Project.ACCESS_PUBLIC + ), "restricted_components": ( - projectbackup.get_restricted_components() - if "weblate.billing" in settings.INSTALLED_APPS - else [] + projectbackup.get_restricted_components() if has_billing else [] ), }, ), diff --git a/weblate/trans/tests/test_backups.py b/weblate/trans/tests/test_backups.py index 38b207260832..bdb37e4a8a2e 100644 --- a/weblate/trans/tests/test_backups.py +++ b/weblate/trans/tests/test_backups.py @@ -1218,21 +1218,31 @@ def test_restore_public_plan_normalizes_restricted_components(self) -> None: for warning in restore.get_restore_warnings() if "restored as public" in warning ] + project_warnings = [ + warning + for warning in restore.get_restore_warnings() + if "Project access changed" in warning + ] if made_public: self.assertEqual(len(public_warnings), 1) self.assertIn(self.component.slug, public_warnings[0]) + self.assertEqual(len(project_warnings), 1) + self.assertIn("from Private to Public", project_warnings[0]) self.assertEqual( Client().get(component.get_absolute_url()).status_code, 200 ) else: self.assertEqual(public_warnings, []) + self.assertEqual(project_warnings, []) # Reusing the restore must not mutate input flags or retain old warnings. self.assertIn(self.component.slug, restore.get_restricted_components()) @modify_settings(INSTALLED_APPS={"append": "weblate.billing"}) def test_import_preview_warns_before_exposing_restricted_components(self) -> None: Project.objects.filter(pk=self.project.pk).update( - use_shared_tm=False, contribute_shared_tm=False + access_control=Project.ACCESS_PUBLIC, + use_shared_tm=False, + contribute_shared_tm=False, ) Component.objects.filter(pk=self.component.pk).update(restricted=True) self.project.refresh_from_db() @@ -1255,6 +1265,25 @@ def test_import_preview_warns_before_exposing_restricted_components(self) -> Non self.assertEqual(Project.objects.filter(workspace=billing.workspace).count(), 0) # The notice remains accurate if a different workspace is selected below it. self.assertContains(response, "If the selected workspace") + self.assertNotContains(response, "this project will be publicly accessible") + + result = import_project_backup( + "Restored public project", + "restored-public-project", + self.user.id, + self.client.session["import_project"], + billing_id=billing.pk, + workspace_id=str(billing.workspace_id), + ) + self.assertEqual( + result["warnings"], + [ + ( + f"Restricted component {self.component.slug} was restored as public " + "because the target workspace's billing plan requires public content." + ) + ], + ) def test_download_uses_configured_static_storage(self) -> None: storage = MagicMock() @@ -5529,6 +5558,79 @@ def test_view_restore_schedules_background_import(self, delay: Mock) -> None: self.assertFalse(Project.objects.filter(slug="import-test").exists()) +class BackupVisibilityTest(ReusableViewTestCase): + def _backup_project(self, access_control: int) -> ProjectBackup: + Project.objects.filter(pk=self.project.pk).update( + access_control=access_control, + use_shared_tm=False, + contribute_shared_tm=False, + ) + self.project.component_set.update(restricted=False) + self.project.refresh_from_db() + backup = ProjectBackup() + backup.backup_project(self.project) + self.addCleanup(Path(backup.filename).unlink, missing_ok=True) + return backup + + @modify_settings(INSTALLED_APPS={"append": "weblate.billing"}) + def test_import_warns_before_and_after_making_private_project_public(self) -> None: + backup = self._backup_project(Project.ACCESS_PRIVATE) + billing = create_test_billing(self.user) + billing.plan.change_access_control = False + billing.plan.save(update_fields=["change_access_control"]) + self.user.is_superuser = True + self.user.save(update_fields=["is_superuser"]) + url = reverse("create-project-import") + with open(backup.filename, "rb") as handle: + response = self.client.post( + url, {"zipfile": handle, "workspace": billing.workspace_id}, follow=True + ) + self.assertContains(response, "Public access on import") + self.assertContains( + response, "this project will be publicly accessible after import." + ) + self.assertNotContains(response, "the following restricted components") + self.assertFalse(Project.objects.filter(workspace=billing.workspace).exists()) + + with modify_settings(INSTALLED_APPS={"remove": "weblate.billing"}): + self.assertNotContains(self.client.get(url), "Public access on import") + + result = import_project_backup( + "Restored private project", + "restored-private-project", + self.user.id, + self.client.session["import_project"], + billing_id=billing.pk, + workspace_id=str(billing.workspace_id), + ) + project = Project.objects.get(slug="restored-private-project") + self.assertEqual(project.access_control, Project.ACCESS_PUBLIC) + self.assertEqual( + result, + { + "message": "Project backup import completed with warnings.", + "warnings": [ + ( + "Project access changed from Private to Public because the target " + "workspace's billing plan requires public content." + ) + ], + }, + ) + + @modify_settings(INSTALLED_APPS={"append": "weblate.billing"}) + def test_import_preview_omits_warning_for_public_backup(self) -> None: + backup = self._backup_project(Project.ACCESS_PUBLIC) + self.user.is_superuser = True + self.user.save(update_fields=["is_superuser"]) + with open(backup.filename, "rb") as handle: + response = self.client.post( + reverse("create-project-import"), {"zipfile": handle}, follow=True + ) + with remove_file_after(self.client.session["import_project"]): + self.assertNotContains(response, "Public access on import") + + class GitHubAppBackupTest(ReusableViewTestCase): def github_app_backup(self) -> str: Component.objects.filter(pk=self.component.pk).update(