Skip to content

Commit afe48bf

Browse files
committed
tests: integration: reject unsafe fleet attachment paths
Signed-off-by: Hiroshi Hatake <hiroshi@chronosphere.io>
1 parent 712b07a commit afe48bf

1 file changed

Lines changed: 144 additions & 0 deletions

File tree

‎tests/integration/scenarios/in_calyptia_fleet/tests/test_in_calyptia_fleet_001.py‎

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pytest
2+
import os
13
import base64
24
import json
35
import threading
@@ -6,6 +8,7 @@
68
from urllib.parse import urlparse
79

810
from utils.data_utils import read_file
11+
from utils.filesystem import make_link
912
from utils.network import find_available_port, wait_for_port_to_be_free
1013
from utils.test_service import FluentBitTestService
1114

@@ -58,6 +61,10 @@ def do_GET(self):
5861
return
5962

6063
if parsed.path == f"/v1/fleets/{FLEET_ID}/files":
64+
if self.server.file_failures > 0:
65+
self.server.file_failures -= 1
66+
self._send_response(503, "temporary failure", "text/plain")
67+
return
6168
self._send_response(
6269
200,
6370
json.dumps(self.server.fleet_files),
@@ -92,6 +99,7 @@ def __init__(self, port, fleet_config, fleet_files=None):
9299
self.httpd.fleet_files = fleet_files or []
93100
self.httpd.last_modified = LAST_MODIFIED
94101
self.httpd.requests = []
102+
self.httpd.file_failures = 0
95103
self.thread = threading.Thread(target=self.httpd.serve_forever, daemon=True)
96104

97105
def __enter__(self):
@@ -723,3 +731,139 @@ def test_fleet_startup_keeps_flat_ref_when_migration_target_is_blocked(tmp_path)
723731
assert paths["timestamp_blocker"].is_file()
724732
assert _read_ref_path(paths["ref_file"]) == paths["flat_config"]
725733
assert marker in log_text
734+
735+
736+
@pytest.mark.parametrize("follow_symlinks", [None, True, False], ids=["default", "follow", "harden"])
737+
def test_fleet_attachment_symlink_policy(tmp_path, follow_symlinks):
738+
from email.utils import parsedate_to_datetime
739+
740+
api_port = find_available_port()
741+
cache_dir = tmp_path / "fleet-cache"
742+
timestamp = int(parsedate_to_datetime(LAST_MODIFIED).timestamp())
743+
attachment_dir = _fleet_base_dir(cache_dir) / str(timestamp)
744+
attachment_dir.mkdir(parents=True)
745+
victim = tmp_path / "victim"
746+
victim.write_text("untouched\n")
747+
make_link(attachment_dir / PARSER_FILE, victim)
748+
config_path = _write_bootstrap_config(tmp_path, cache_dir, api_port)
749+
if follow_symlinks is not None:
750+
config_path.write_text(config_path.read_text().replace(
751+
" fleet.config_dir:",
752+
f" fleet.follow_symlinks: {str(follow_symlinks).lower()}\n fleet.config_dir:",
753+
))
754+
config = _fleet_config(cache_dir, api_port, "symlink-policy-reload")
755+
with FleetAPIServer(api_port, config, _fleet_files_payload()):
756+
service = FluentBitTestService(str(config_path), extra_env={"TZ": "UTC"})
757+
try:
758+
service.start()
759+
_wait_for_log_contains(service, "unable to write fleet files safely"
760+
if follow_symlinks is False else "symlink-policy-reload")
761+
finally:
762+
service.stop()
763+
assert victim.read_text() == ("untouched\n" if follow_symlinks is False else PARSER_CONFIG)
764+
765+
766+
def test_fleet_attachment_rejects_parent_traversal(tmp_path):
767+
api_port = find_available_port()
768+
cache_dir = tmp_path / "fleet-cache"
769+
config_path = _write_bootstrap_config(tmp_path, cache_dir, api_port)
770+
config = _fleet_config(cache_dir, api_port, "must-not-reload")
771+
payload = [{"name": "../escaped", "contents": base64.b64encode(b"escape").decode()}]
772+
with FleetAPIServer(api_port, config, payload):
773+
service = FluentBitTestService(str(config_path))
774+
try:
775+
service.start()
776+
_wait_for_log_contains(service, "unable to write fleet files safely")
777+
finally:
778+
service.stop()
779+
assert not (_fleet_base_dir(cache_dir) / "escaped").exists()
780+
781+
782+
def _set_follow_symlinks(config_path, follow_symlinks):
783+
config_path.write_text(config_path.read_text().replace(
784+
" fleet.config_dir:",
785+
f" fleet.follow_symlinks: {str(follow_symlinks).lower()}\n fleet.config_dir:",
786+
))
787+
788+
789+
@pytest.mark.parametrize("cached", [False, True], ids=["bootstrap", "update"])
790+
@pytest.mark.parametrize("follow_symlinks", [True, False], ids=["follow", "harden"])
791+
def test_fleet_retries_failed_attachments(tmp_path, cached, follow_symlinks):
792+
api_port = find_available_port()
793+
cache_dir = tmp_path / "fleet-cache"
794+
marker = "retried-revision-applied"
795+
config_path = _write_bootstrap_config(tmp_path, cache_dir, api_port, interval_sec="2")
796+
_set_follow_symlinks(config_path, follow_symlinks)
797+
victim = tmp_path / "outside" / "keep"
798+
victim.parent.mkdir()
799+
victim.write_text("untouched")
800+
old_directory = None
801+
if cached:
802+
paths = _write_nested_cache(cache_dir, api_port, "cur", "old-revision")
803+
old_directory = paths["nested_config"].parent
804+
paths["nested_config"].write_text(paths["nested_config"].read_text().replace(
805+
'fleet.interval_sec: "3600"', 'fleet.interval_sec: "2"'))
806+
_set_follow_symlinks(paths["nested_config"], follow_symlinks)
807+
if not follow_symlinks:
808+
make_link(old_directory / "external", victim.parent, directory=True)
809+
if os.name != "nt":
810+
make_link(old_directory / "dangling", tmp_path / "missing")
811+
(old_directory / ".hidden").write_text("remove me")
812+
config = _fleet_config(cache_dir, api_port, marker)
813+
with FleetAPIServer(api_port, config, _fleet_files_payload()) as api:
814+
api.httpd.file_failures = 1
815+
service = FluentBitTestService(str(config_path), extra_env={"TZ": "UTC"})
816+
try:
817+
service.start()
818+
_wait_for_log_contains(service, "unable to write fleet files safely", timeout=45)
819+
_wait_for_log_contains(service, marker, timeout=45)
820+
if old_directory is not None:
821+
service.wait_for_condition(lambda: not old_directory.exists(), timeout=45,
822+
interval=0.5, description="old revision cleanup")
823+
finally:
824+
service.stop()
825+
assert sum(r["path"].endswith("/files") for r in api.requests) >= 2
826+
assert victim.read_text() == "untouched"
827+
828+
829+
def test_fleet_rejects_header_symlink_before_download(tmp_path):
830+
api_port = find_available_port()
831+
cache_dir = tmp_path / "fleet-cache"
832+
config_path = _write_bootstrap_config(tmp_path, cache_dir, api_port)
833+
_set_follow_symlinks(config_path, False)
834+
base_dir = _fleet_base_dir(cache_dir)
835+
base_dir.mkdir(parents=True)
836+
victim = tmp_path / "header-victim"
837+
victim.write_text("untouched")
838+
make_link(base_dir / "header.yaml", victim)
839+
with FleetAPIServer(api_port, _fleet_config(cache_dir, api_port, "must-not-reload")) as api:
840+
service = FluentBitTestService(str(config_path))
841+
try:
842+
service.start()
843+
_wait_for_log_contains(service, "unable to create fleet header safely")
844+
finally:
845+
service.stop()
846+
assert not any(r["path"].endswith("/config") for r in api.requests)
847+
assert victim.read_text() == "untouched"
848+
assert not (base_dir / "new.ref").exists()
849+
850+
851+
@pytest.mark.parametrize("follow_symlinks", [True, False], ids=["follow", "harden"])
852+
def test_fleet_migration_source_symlink_policy(tmp_path, follow_symlinks):
853+
api_port = find_available_port()
854+
cache_dir = tmp_path / "fleet-cache"
855+
marker = "migration-source-config"
856+
paths = _write_old_cache(cache_dir, api_port, "cur", marker)
857+
source = tmp_path / "external.yaml"
858+
paths["flat_config"].rename(source)
859+
make_link(paths["flat_config"], source)
860+
config_path = _write_bootstrap_config(tmp_path, cache_dir, api_port)
861+
_set_follow_symlinks(config_path, follow_symlinks)
862+
service = FluentBitTestService(str(config_path))
863+
try:
864+
service.start()
865+
_wait_for_log_contains(service, marker if follow_symlinks else "unable to read file for migration")
866+
finally:
867+
service.stop()
868+
assert paths["nested_config"].exists() == follow_symlinks
869+
assert source.is_file()

0 commit comments

Comments
 (0)