|
| 1 | +import pytest |
| 2 | +import os |
1 | 3 | import base64 |
2 | 4 | import json |
3 | 5 | import threading |
@@ -58,6 +60,10 @@ def do_GET(self): |
58 | 60 | return |
59 | 61 |
|
60 | 62 | if parsed.path == f"/v1/fleets/{FLEET_ID}/files": |
| 63 | + if self.server.file_failures > 0: |
| 64 | + self.server.file_failures -= 1 |
| 65 | + self._send_response(503, "temporary failure", "text/plain") |
| 66 | + return |
61 | 67 | self._send_response( |
62 | 68 | 200, |
63 | 69 | json.dumps(self.server.fleet_files), |
@@ -92,6 +98,7 @@ def __init__(self, port, fleet_config, fleet_files=None): |
92 | 98 | self.httpd.fleet_files = fleet_files or [] |
93 | 99 | self.httpd.last_modified = LAST_MODIFIED |
94 | 100 | self.httpd.requests = [] |
| 101 | + self.httpd.file_failures = 0 |
95 | 102 | self.thread = threading.Thread(target=self.httpd.serve_forever, daemon=True) |
96 | 103 |
|
97 | 104 | def __enter__(self): |
@@ -723,3 +730,146 @@ def test_fleet_startup_keeps_flat_ref_when_migration_target_is_blocked(tmp_path) |
723 | 730 | assert paths["timestamp_blocker"].is_file() |
724 | 731 | assert _read_ref_path(paths["ref_file"]) == paths["flat_config"] |
725 | 732 | assert marker in log_text |
| 733 | + |
| 734 | + |
| 735 | +@pytest.mark.parametrize("follow_symlinks", [None, True, False], ids=["default", "follow", "harden"]) |
| 736 | +def test_fleet_attachment_symlink_policy(tmp_path, follow_symlinks): |
| 737 | + import os |
| 738 | + from email.utils import parsedate_to_datetime |
| 739 | + import pytest |
| 740 | + |
| 741 | + if os.name == "nt": |
| 742 | + pytest.skip("POSIX containment policy") |
| 743 | + api_port = find_available_port() |
| 744 | + cache_dir = tmp_path / "fleet-cache" |
| 745 | + timestamp = int(parsedate_to_datetime(LAST_MODIFIED).timestamp()) |
| 746 | + attachment_dir = _fleet_base_dir(cache_dir) / str(timestamp) |
| 747 | + attachment_dir.mkdir(parents=True) |
| 748 | + victim = tmp_path / "victim" |
| 749 | + victim.write_text("untouched\n") |
| 750 | + (attachment_dir / PARSER_FILE).symlink_to(victim) |
| 751 | + config_path = _write_bootstrap_config(tmp_path, cache_dir, api_port) |
| 752 | + if follow_symlinks is not None: |
| 753 | + config_path.write_text(config_path.read_text().replace( |
| 754 | + " fleet.config_dir:", |
| 755 | + f" fleet.follow_symlinks: {str(follow_symlinks).lower()}\n fleet.config_dir:", |
| 756 | + )) |
| 757 | + config = _fleet_config(cache_dir, api_port, "symlink-policy-reload") |
| 758 | + with FleetAPIServer(api_port, config, _fleet_files_payload()): |
| 759 | + service = FluentBitTestService(str(config_path), extra_env={"TZ": "UTC"}) |
| 760 | + try: |
| 761 | + service.start() |
| 762 | + _wait_for_log_contains(service, "unable to write fleet files safely" |
| 763 | + if follow_symlinks is False else "symlink-policy-reload") |
| 764 | + finally: |
| 765 | + service.stop() |
| 766 | + assert victim.read_text() == ("untouched\n" if follow_symlinks is False else PARSER_CONFIG) |
| 767 | + |
| 768 | + |
| 769 | +def test_fleet_attachment_rejects_parent_traversal(tmp_path): |
| 770 | + api_port = find_available_port() |
| 771 | + cache_dir = tmp_path / "fleet-cache" |
| 772 | + config_path = _write_bootstrap_config(tmp_path, cache_dir, api_port) |
| 773 | + config = _fleet_config(cache_dir, api_port, "must-not-reload") |
| 774 | + payload = [{"name": "../escaped", "contents": base64.b64encode(b"escape").decode()}] |
| 775 | + with FleetAPIServer(api_port, config, payload): |
| 776 | + service = FluentBitTestService(str(config_path)) |
| 777 | + try: |
| 778 | + service.start() |
| 779 | + _wait_for_log_contains(service, "unable to write fleet files safely") |
| 780 | + finally: |
| 781 | + service.stop() |
| 782 | + assert not (_fleet_base_dir(cache_dir) / "escaped").exists() |
| 783 | + |
| 784 | + |
| 785 | +def _set_follow_symlinks(config_path, follow_symlinks): |
| 786 | + config_path.write_text(config_path.read_text().replace( |
| 787 | + " fleet.config_dir:", |
| 788 | + f" fleet.follow_symlinks: {str(follow_symlinks).lower()}\n fleet.config_dir:", |
| 789 | + )) |
| 790 | + |
| 791 | + |
| 792 | +@pytest.mark.parametrize("cached", [False, True], ids=["bootstrap", "update"]) |
| 793 | +@pytest.mark.parametrize("follow_symlinks", [True, False], ids=["follow", "harden"]) |
| 794 | +def test_fleet_retries_failed_attachments(tmp_path, cached, follow_symlinks): |
| 795 | + if os.name == "nt" and not follow_symlinks: |
| 796 | + pytest.skip("POSIX cleanup containment") |
| 797 | + api_port = find_available_port() |
| 798 | + cache_dir = tmp_path / "fleet-cache" |
| 799 | + marker = "retried-revision-applied" |
| 800 | + config_path = _write_bootstrap_config(tmp_path, cache_dir, api_port, interval_sec="2") |
| 801 | + _set_follow_symlinks(config_path, follow_symlinks) |
| 802 | + victim = tmp_path / "outside" / "keep" |
| 803 | + victim.parent.mkdir() |
| 804 | + victim.write_text("untouched") |
| 805 | + old_directory = None |
| 806 | + if cached: |
| 807 | + paths = _write_nested_cache(cache_dir, api_port, "cur", "old-revision") |
| 808 | + old_directory = paths["nested_config"].parent |
| 809 | + paths["nested_config"].write_text(paths["nested_config"].read_text().replace( |
| 810 | + 'fleet.interval_sec: "3600"', 'fleet.interval_sec: "2"')) |
| 811 | + _set_follow_symlinks(paths["nested_config"], follow_symlinks) |
| 812 | + if not follow_symlinks: |
| 813 | + (old_directory / "external").symlink_to(victim.parent, target_is_directory=True) |
| 814 | + (old_directory / "dangling").symlink_to(tmp_path / "missing") |
| 815 | + (old_directory / ".hidden").write_text("remove me") |
| 816 | + config = _fleet_config(cache_dir, api_port, marker) |
| 817 | + with FleetAPIServer(api_port, config, _fleet_files_payload()) as api: |
| 818 | + api.httpd.file_failures = 1 |
| 819 | + service = FluentBitTestService(str(config_path), extra_env={"TZ": "UTC"}) |
| 820 | + try: |
| 821 | + service.start() |
| 822 | + _wait_for_log_contains(service, "unable to write fleet files safely", timeout=45) |
| 823 | + _wait_for_log_contains(service, marker, timeout=45) |
| 824 | + if old_directory is not None: |
| 825 | + service.wait_for_condition(lambda: not old_directory.exists(), timeout=45, |
| 826 | + interval=0.5, description="old revision cleanup") |
| 827 | + finally: |
| 828 | + service.stop() |
| 829 | + assert sum(r["path"].endswith("/files") for r in api.requests) >= 2 |
| 830 | + assert victim.read_text() == "untouched" |
| 831 | + |
| 832 | + |
| 833 | +@pytest.mark.skipif(os.name == "nt", reason="POSIX containment policy") |
| 834 | +def test_fleet_rejects_header_symlink_before_download(tmp_path): |
| 835 | + api_port = find_available_port() |
| 836 | + cache_dir = tmp_path / "fleet-cache" |
| 837 | + config_path = _write_bootstrap_config(tmp_path, cache_dir, api_port) |
| 838 | + _set_follow_symlinks(config_path, False) |
| 839 | + base_dir = _fleet_base_dir(cache_dir) |
| 840 | + base_dir.mkdir(parents=True) |
| 841 | + victim = tmp_path / "header-victim" |
| 842 | + victim.write_text("untouched") |
| 843 | + (base_dir / "header.yaml").symlink_to(victim) |
| 844 | + with FleetAPIServer(api_port, _fleet_config(cache_dir, api_port, "must-not-reload")) as api: |
| 845 | + service = FluentBitTestService(str(config_path)) |
| 846 | + try: |
| 847 | + service.start() |
| 848 | + _wait_for_log_contains(service, "unable to create fleet header safely") |
| 849 | + finally: |
| 850 | + service.stop() |
| 851 | + assert not any(r["path"].endswith("/config") for r in api.requests) |
| 852 | + assert victim.read_text() == "untouched" |
| 853 | + assert not (base_dir / "new.ref").exists() |
| 854 | + |
| 855 | + |
| 856 | +@pytest.mark.skipif(os.name == "nt", reason="POSIX containment policy") |
| 857 | +@pytest.mark.parametrize("follow_symlinks", [True, False], ids=["follow", "harden"]) |
| 858 | +def test_fleet_migration_source_symlink_policy(tmp_path, follow_symlinks): |
| 859 | + api_port = find_available_port() |
| 860 | + cache_dir = tmp_path / "fleet-cache" |
| 861 | + marker = "migration-source-config" |
| 862 | + paths = _write_old_cache(cache_dir, api_port, "cur", marker) |
| 863 | + source = tmp_path / "external.yaml" |
| 864 | + paths["flat_config"].rename(source) |
| 865 | + paths["flat_config"].symlink_to(source) |
| 866 | + config_path = _write_bootstrap_config(tmp_path, cache_dir, api_port) |
| 867 | + _set_follow_symlinks(config_path, follow_symlinks) |
| 868 | + service = FluentBitTestService(str(config_path)) |
| 869 | + try: |
| 870 | + service.start() |
| 871 | + _wait_for_log_contains(service, marker if follow_symlinks else "unable to read file for migration") |
| 872 | + finally: |
| 873 | + service.stop() |
| 874 | + assert paths["nested_config"].exists() == follow_symlinks |
| 875 | + assert source.is_file() |
0 commit comments