From 7f3fdec31351f6d6adfb77b7a97c6cd2d2b53b19 Mon Sep 17 00:00:00 2001 From: Omkar Gaikwad Date: Fri, 20 Nov 2026 07:29:59 +0000 Subject: [PATCH] fix(reporting): skip symlinks and special files in the GCS artifact zip The agent under test writes the sandbox directory. Add only regular files to the zip, so that a symlink cannot add a file from outside the sandbox and a FIFO cannot block the reporter. --- evalbench/reporting/gcs_artifact.py | 30 ++++++++++++++- evalbench/test/gcs_artifact_test.py | 59 +++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) diff --git a/evalbench/reporting/gcs_artifact.py b/evalbench/reporting/gcs_artifact.py index f5b1ddded..cbb80e936 100644 --- a/evalbench/reporting/gcs_artifact.py +++ b/evalbench/reporting/gcs_artifact.py @@ -1,4 +1,5 @@ import os +import stat import tempfile import zipfile import logging @@ -102,6 +103,18 @@ def store(self, results: pd.DataFrame, type: STORETYPE) -> None: eval_id = rows["eval_id"].iloc[0] self._zip_and_upload(fake_home, eval_id, bucket) + @staticmethod + def _is_regular_file(path: str) -> bool: + """Returns True for a regular file. A symlink is not a regular file. + + The agent under test writes the sandbox. A symlink there can point to + a host file, and zipfile copies the target. A FIFO blocks the read. + """ + try: + return stat.S_ISREG(os.lstat(path).st_mode) + except OSError: + return False + def _zip_and_upload( self, src_dir: str, eval_id: str, bucket: storage.Bucket ) -> None: @@ -128,8 +141,12 @@ def _zip_and_upload( ) as tmp_file: zip_path = tmp_file.name + skipped = 0 with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zipf: - for root, dirs, files in os.walk(src_dir): + # os.walk does not enter a symlinked directory, and + # _is_regular_file rejects a symlinked file. Thus each file + # in the zip is inside src_dir. + for root, dirs, files in os.walk(src_dir, followlinks=False): # Exclude hidden directories and common heavy/cache directories dirs[:] = [ d @@ -142,9 +159,20 @@ def _zip_and_upload( if file.startswith("."): continue file_path = os.path.join(root, file) + if not self._is_regular_file(file_path): + skipped += 1 + continue arcname = os.path.relpath(file_path, src_dir) zipf.write(file_path, arcname) + if skipped: + logging.warning( + "GcsReporter._zip_and_upload: skipped %d symlinks or " + "special files in %s.", + skipped, + src_dir, + ) + blob_name = f"{self.path_prefix}/{self.job_id}/{eval_id}.zip" logging.info( "GcsReporter._zip_and_upload: Zip created. Size=%d bytes. Uploading to gs://%s/%s ...", diff --git a/evalbench/test/gcs_artifact_test.py b/evalbench/test/gcs_artifact_test.py index 2f971dc1a..c14b670c3 100644 --- a/evalbench/test/gcs_artifact_test.py +++ b/evalbench/test/gcs_artifact_test.py @@ -1,4 +1,7 @@ +import os +import tempfile import unittest +import zipfile from unittest.mock import patch, MagicMock import pandas as pd import datetime @@ -111,6 +114,62 @@ def test_store_shared(self, mock_remove, mock_tempfile, mock_zipfile, mock_exist mock_bucket.blob.assert_called_once_with("results/test-job-id/fake_home.zip") mock_blob.upload_from_filename.assert_called_once_with("/tmp/temp.zip") + def _zip_names_and_data(self, src_dir): + """Runs _zip_and_upload and returns the names and bytes in the zip.""" + captured = {} + + def read_zip(zip_path): + with zipfile.ZipFile(zip_path) as zipf: + captured["names"] = sorted(zipf.namelist()) + captured["data"] = b"".join( + zipf.read(name) for name in zipf.namelist()) + + bucket = MagicMock() + bucket.blob.return_value.upload_from_filename.side_effect = read_zip + reporter = GcsReporter(self.reporting_config, self.job_id, self.run_time) + reporter._zip_and_upload(src_dir, "eval1", bucket) + return captured["names"], captured["data"] + + def _make_sandbox(self): + temp_dir = tempfile.TemporaryDirectory() + self.addCleanup(temp_dir.cleanup) + fake_home = os.path.join(temp_dir.name, "fake_home") + os.makedirs(os.path.join(fake_home, "sub")) + for name in ("note.txt", os.path.join("sub", "kept.txt")): + with open(os.path.join(fake_home, name), "w") as f: + f.write("sandbox file") + return temp_dir.name, fake_home + + @patch('reporting.gcs_artifact.storage.Client') + def test_zip_does_not_follow_symlinks(self, mock_storage_client): + """A symlink in the sandbox must not copy a host file into the zip.""" + host_dir, fake_home = self._make_sandbox() + host_file = os.path.join(host_dir, "host_secret.txt") + with open(host_file, "w") as f: + f.write("HOST_SECRET") + host_subdir = os.path.join(host_dir, "host_subdir") + os.makedirs(host_subdir) + with open(os.path.join(host_subdir, "inner.txt"), "w") as f: + f.write("HOST_SECRET") + os.symlink(host_file, os.path.join(fake_home, "link_to_file.txt")) + os.symlink(host_file, os.path.join(fake_home, "sub", "nested_link.txt")) + os.symlink(host_subdir, os.path.join(fake_home, "link_to_dir")) + + names, data = self._zip_names_and_data(fake_home) + + self.assertEqual(names, ["note.txt", "sub/kept.txt"]) + self.assertNotIn(b"HOST_SECRET", data) + + @patch('reporting.gcs_artifact.storage.Client') + def test_zip_skips_special_files(self, mock_storage_client): + """A FIFO in the sandbox must not block the reporter.""" + _, fake_home = self._make_sandbox() + os.mkfifo(os.path.join(fake_home, "pipe")) + + names, _ = self._zip_names_and_data(fake_home) + + self.assertEqual(names, ["note.txt", "sub/kept.txt"]) + if __name__ == '__main__': unittest.main()