Repository navigation
fix(formats): validate App Store metadata write paths #23215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,19 +8,81 @@ | |
|
|
||
| import os.path | ||
| from pathlib import Path | ||
| from tempfile import TemporaryDirectory | ||
| from typing import IO | ||
| from unittest.mock import Mock, patch | ||
|
|
||
| from django.core.exceptions import ValidationError | ||
| from django.test import SimpleTestCase | ||
|
|
||
| from weblate.formats.tests.test_formats import BaseFormatTest | ||
| from weblate.formats.txt import AppStoreFormat, MultiparserError | ||
| from weblate.formats.txt import AppStoreFormat, MultiparserError, TextItem | ||
| from weblate.lang.models import Language | ||
| from weblate.trans.tests.utils import get_test_file | ||
|
|
||
| APPSTORE_FILE = get_test_file("short_description.txt") | ||
|
|
||
|
|
||
| class AppStoreWritePathTest(SimpleTestCase): | ||
| def test_save_rejects_symlinked_destination_directory(self) -> None: | ||
| with TemporaryDirectory() as directory: | ||
| root = Path(directory) | ||
| base = root / "metadata" | ||
| outside = root / "outside" | ||
| base.mkdir() | ||
| outside.mkdir() | ||
| (base / "changelogs").symlink_to(outside, target_is_directory=True) | ||
|
|
||
| def validate(filename: str) -> str: | ||
| if not Path(filename).resolve().is_relative_to(base.resolve()): | ||
| msg = "Invalid symbolic link in a repository." | ||
| raise ValidationError(msg) | ||
| return filename | ||
|
|
||
| store = AppStoreFormat(str(base), file_validator=validate) | ||
| unit = TextItem("changelogs/title.txt", 1, "attacker") | ||
| unit.needs_save = True | ||
| store.store.units.append(unit) | ||
|
|
||
| with self.assertRaises(ValidationError): | ||
| store.save() | ||
| target = outside / "title.txt" | ||
| self.assertFalse(target.exists()) | ||
|
|
||
| target.write_text("keep", encoding="utf-8") | ||
| with self.assertRaises(ValidationError): | ||
| store.save() | ||
| self.assertEqual(target.read_text(encoding="utf-8"), "keep") | ||
|
|
||
| def test_delete_rejects_symlinked_destination_directory(self) -> None: | ||
| with TemporaryDirectory() as directory: | ||
| root = Path(directory) | ||
| base = root / "metadata" | ||
| outside = root / "outside" | ||
| base.mkdir() | ||
| outside.mkdir() | ||
|
|
||
| def validate(filename: str) -> str: | ||
| if not Path(filename).resolve().is_relative_to(base.resolve()): | ||
| msg = "Invalid symbolic link in a repository." | ||
| raise ValidationError(msg) | ||
| return filename | ||
|
|
||
| store = AppStoreFormat(str(base), file_validator=validate) | ||
| (base / "changelogs").symlink_to(outside, target_is_directory=True) | ||
| target = outside / "title.txt" | ||
| unit = TextItem("changelogs/title.txt", 1, "") | ||
| for exists in (True, False): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: This change drops the save() check that an existing target file keeps its contents. Only delete_unit now runs with an existing file, and save() is only tested when the file is absent. Keep the existing-file save case, for example by running save() under the same Prompt for AI agents |
||
| with self.subTest(exists=exists): | ||
| if exists: | ||
| target.write_text("keep", encoding="utf-8") | ||
| with self.assertRaises(ValidationError): | ||
| store.delete_unit(unit) | ||
| if exists: | ||
| self.assertEqual(target.read_text(encoding="utf-8"), "keep") | ||
| target.unlink() | ||
|
|
||
|
|
||
| class AppStoreFormatTest(BaseFormatTest): | ||
| format_class = AppStoreFormat | ||
| FILE = APPSTORE_FILE | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.