시험 작업 경로 누출을 막음
- 작성자
- 코노모리 카논
- 작성 시각
- 커밋
486cd8438fe0afa4d428f42cc5399cfc5d13bb7f- 부모
- e755a6e
변경된 파일 6개
+107-18| @@ -42,6 +42,15 @@ def work_directory(root, name): | ||
| 42 | 42 | return path |
| 43 | 43 | |
| 44 | 44 | |
| 45 | 추가: def output_directory(work, name, create=False): | |
| 46 | 추가: path = work / name | |
| 47 | 추가: if not path.resolve().is_relative_to(work) or (path.exists() and not path.is_dir()): | |
| 48 | 추가: raise ValueError(f"작업 폴더 밖의 출력 경로는 허용하지 않음: {path}") | |
| 49 | 추가: if create: | |
| 50 | 추가: path.mkdir(mode=0o700, exist_ok=True) | |
| 51 | 추가: return path | |
| 52 | 추가: | |
| 53 | 추가: | |
| 45 | 54 | def test_work_directory(root): |
| 46 | 55 | return Path(os.environ.get("ZIPELO_TEST_WORK_DIR", root / "tests/work/rust-local")) |
| 47 | 56 | |
| @@ -226,8 +235,8 @@ def main(): | ||
| 226 | 235 | if args.phase == "local" and sys.platform != "linux": |
| 227 | 236 | parser.error("local 모드는 Linux에서 실행해야 함") |
| 228 | 237 | work = work_directory(ROOT, args.job) |
| 229 | 삭제: temporary = work / "tmp" | |
| 230 | 삭제: temporary.mkdir(mode=0o700, exist_ok=True) | |
| 238 | 추가: temporary = output_directory(work, "tmp", create=True) | |
| 239 | 추가: target = output_directory(work, "target") | |
| 231 | 240 | os.environ["ZIPELO_CI_JOB"] = args.job |
| 232 | 241 | os.environ["ZIPELO_TEST_WORK_DIR"] = str(work) |
| 233 | 242 | os.environ["TMPDIR"] = str(temporary) |
| @@ -235,7 +244,7 @@ def main(): | ||
| 235 | 244 | os.environ["TEMP"] = str(temporary) |
| 236 | 245 | os.environ["PYTHONDONTWRITEBYTECODE"] = "1" |
| 237 | 246 | os.environ.setdefault("CARGO_BUILD_JOBS", "2") |
| 238 | 삭제: os.environ["CARGO_TARGET_DIR"] = str(work / "target") | |
| 247 | 추가: os.environ["CARGO_TARGET_DIR"] = str(target) | |
| 239 | 248 | prepare_tools(ROOT) |
| 240 | 249 | phases = ["build", "test", "cross-build"] if args.phase == "local" else [args.phase] |
| 241 | 250 | checks = subprocess.run( |
| @@ -2,10 +2,13 @@ import gzip | ||
| 2 | 2 | import importlib.util |
| 3 | 3 | import io |
| 4 | 4 | import os |
| 5 | 추가: import subprocess | |
| 6 | 추가: import sys | |
| 5 | 7 | from pathlib import Path |
| 6 | 8 | import tarfile |
| 7 | 9 | import tempfile |
| 8 | 10 | import unittest |
| 11 | 추가: from unittest.mock import patch | |
| 9 | 12 | import zipfile |
| 10 | 13 | |
| 11 | 14 | |
| @@ -18,6 +21,32 @@ SPEC.loader.exec_module(GENERATOR) | ||
| 18 | 21 | |
| 19 | 22 | |
| 20 | 23 | class LargeArchiveFixtureTests(unittest.TestCase): |
| 24 | 추가: def test_standalone_generator_uses_common_work_directory(self): | |
| 25 | 추가: expected = Path(os.environ["ZIPELO_TEST_WORK_DIR"]) | |
| 26 | 추가: with tempfile.TemporaryDirectory(dir=expected) as scratch: | |
| 27 | 추가: relative = Path(scratch).name + "/corpus" | |
| 28 | 추가: environment = dict(os.environ, ZIPELO_CI_JOB=expected.name) | |
| 29 | 추가: environment.pop("ZIPELO_TEST_WORK_DIR", None) | |
| 30 | 추가: result = subprocess.run( | |
| 31 | 추가: ["python3", str(ROOT / "tests/large-archive-fixtures/generate.py"), | |
| 32 | 추가: "--output", relative], | |
| 33 | 추가: cwd=ROOT, env=environment, capture_output=True, text=True, check=True, | |
| 34 | 추가: ) | |
| 35 | 추가: self.assertTrue((Path(scratch) / "corpus/expectations.json").is_file()) | |
| 36 | 추가: self.assertIn("11 cases", result.stdout) | |
| 37 | 추가: | |
| 38 | 추가: def test_standalone_verifier_uses_work_target(self): | |
| 39 | 추가: job = Path(os.environ["ZIPELO_TEST_WORK_DIR"]).name | |
| 40 | 추가: with patch.dict(os.environ, {"ZIPELO_CI_JOB": job}), patch.dict(sys.modules, {"generate": GENERATOR}): | |
| 41 | 추가: os.environ.pop("ZIPELO_TEST_WORK_DIR", None) | |
| 42 | 추가: os.environ.pop("CARGO_TARGET_DIR", None) | |
| 43 | 추가: spec = importlib.util.spec_from_file_location( | |
| 44 | 추가: "large_fixture_verifier", ROOT / "tests/large-archive-fixtures/verify.py" | |
| 45 | 추가: ) | |
| 46 | 추가: verifier = importlib.util.module_from_spec(spec) | |
| 47 | 추가: spec.loader.exec_module(verifier) | |
| 48 | 추가: self.assertEqual(verifier.default_target(), GENERATOR.work_root() / "target/debug") | |
| 49 | 추가: | |
| 21 | 50 | def test_boundary_archives_are_valid_and_expectations_straddle_limit(self): |
| 22 | 51 | with tempfile.TemporaryDirectory(dir=os.environ["ZIPELO_TEST_WORK_DIR"]) as scratch: |
| 23 | 52 | directory = Path(scratch) / "corpus" |
| @@ -24,6 +24,28 @@ class RunnerTests(unittest.TestCase): | ||
| 24 | 24 | run_rust.work_directory(run_rust.ROOT, name) |
| 25 | 25 | self.assertEqual(run_rust.work_directory(run_rust.ROOT, os.environ["ZIPELO_CI_JOB"]), TEST_WORK) |
| 26 | 26 | |
| 27 | 추가: def test_output_paths_reject_symlinks_outside_job(self): | |
| 28 | 추가: with tempfile.TemporaryDirectory(dir=TEST_WORK) as directory: | |
| 29 | 추가: work = Path(directory) | |
| 30 | 추가: outside = TEST_WORK / "outside" | |
| 31 | 추가: for name in ("tmp", "target"): | |
| 32 | 추가: with self.subTest(name=name): | |
| 33 | 추가: link = work / name | |
| 34 | 추가: link.symlink_to(outside, target_is_directory=True) | |
| 35 | 추가: with self.assertRaisesRegex(ValueError, "작업 폴더 밖"): | |
| 36 | 추가: run_rust.output_directory(work, name, create=True) | |
| 37 | 추가: self.assertFalse(outside.exists()) | |
| 38 | 추가: link.unlink() | |
| 39 | 추가: | |
| 40 | 추가: def test_output_paths_allow_symlinks_within_job(self): | |
| 41 | 추가: with tempfile.TemporaryDirectory(dir=TEST_WORK) as directory: | |
| 42 | 추가: work = Path(directory) | |
| 43 | 추가: inner = work / "inner" | |
| 44 | 추가: inner.mkdir() | |
| 45 | 추가: link = work / "tmp" | |
| 46 | 추가: link.symlink_to(inner, target_is_directory=True) | |
| 47 | 추가: self.assertEqual(run_rust.output_directory(work, "tmp", create=True), link) | |
| 48 | 추가: | |
| 27 | 49 | def test_cargo_and_temp_paths_are_kept_in_job_directory(self): |
| 28 | 50 | for key in ("CARGO_TARGET_DIR", "TMPDIR", "TMP", "TEMP", "ZIPELO_TEST_WORK_DIR"): |
| 29 | 51 | with self.subTest(key=key): |
| @@ -14,20 +14,20 @@ Python 표준 라이브러리로 실행할 때 합성 표본을 생성합니다. | ||
| 14 | 14 | |
| 15 | 15 | ## 실행 |
| 16 | 16 | |
| 17 | 삭제: 저장소 루트에서 실행합니다. --large는 1 GiB 성공 2건과 16 GiB 초과 거부를 포함해 총 14건을 검사합니다. 생성을 포함한 기본 실행은 tests 안의 전용 임시 디렉터리를 쓰고 종료 시 정리합니다. 압축 표본, 입력 사본과 출력에 수 GiB의 임시 디스크 공간이 필요합니다. | |
| 17 | 추가: 저장소 루트에서 실행합니다. --large는 1 GiB 성공 2건과 16 GiB 초과 거부를 포함해 총 14건을 검사합니다. 생성을 포함한 기본 실행은 tests/work/rust-local 안의 전용 임시 디렉터리를 쓰고 종료 시 정리합니다. 압축 표본, 입력 사본과 출력에 수 GiB의 임시 디스크 공간이 필요합니다. | |
| 18 | 18 | |
| 19 | 19 | ```sh |
| 20 | 20 | python3 tests/ci/run_rust.py local |
| 21 | 21 | python3 tests/large-archive-fixtures/verify.py --large |
| 22 | 22 | ``` |
| 23 | 23 | |
| 24 | 삭제: CARGO_TARGET_DIR을 지정했다면 검증기도 같은 환경 값을 사용합니다. --probe와 --worker로 바이너리 경로를 따로 지정할 수도 있습니다. --large를 생략하면 작은 경계 및 고압축 호환성 11건만 검사합니다. 기본 local 실행에는 작은 표본의 참조 무결성, 경계 독립성과 생성기 경로 및 버퍼 제약 검사가 포함됩니다. | |
| 24 | 추가: 검증기는 기본적으로 tests/work/rust-local/target의 빌드를 사용합니다. CARGO_TARGET_DIR을 지정했다면 같은 환경 값을 사용합니다. --probe와 --worker로 바이너리 경로를 따로 지정할 수도 있습니다. --large를 생략하면 작은 경계 및 고압축 호환성 11건만 검사합니다. 기본 local 실행에는 작은 표본의 참조 무결성, 경계 독립성과 생성기 경로 및 버퍼 제약 검사가 포함됩니다. | |
| 25 | 25 | |
| 26 | 삭제: 표본을 보존하려면 아래처럼 새 경로를 지정합니다. 기존 디렉터리는 생성기가 덮어쓰지 않으며, 검증기는 기존 expectations.json이 있으면 재사용합니다. | |
| 26 | 추가: 표본을 보존하려면 아래처럼 작업 폴더 안의 새 상대 경로를 지정합니다. 기존 디렉터리는 생성기가 덮어쓰지 않으며, 검증기는 기존 expectations.json이 있으면 재사용합니다. | |
| 27 | 27 | |
| 28 | 28 | ```sh |
| 29 | 삭제: python3 tests/large-archive-fixtures/generate.py --large --output tests/large-archive-fixtures/runs/full | |
| 30 | 삭제: python3 tests/large-archive-fixtures/verify.py --large --directory tests/large-archive-fixtures/runs/full | |
| 29 | 추가: python3 tests/large-archive-fixtures/generate.py --large --output large-archive-fixtures/full | |
| 30 | 추가: python3 tests/large-archive-fixtures/verify.py --large --directory large-archive-fixtures/full | |
| 31 | 31 | ``` |
| 32 | 32 | |
| 33 | 33 | 검증기는 전체 표본을 참조 판독기로 읽어 합성 내용, CRC와 TAR 구조를 확인한 후 실제 작업자의 종료 코드, Terminal, 오류, 게시 상태, Output 개수와 정리를 확인합니다. 성공 파일은 길이와 모든 바이트를 검사하며 거부한 경우 출력 폴더가 비어 있어야 합니다. 입력의 전후 SHA-256도 확인합니다. 작업자 피크 RSS, 디스크 부족과 고아 작업자 정리는 tests/rust-archive-worker/hardening.rs에서 별도로 검사합니다. Windows 실기는 별도 검증이 필요합니다. |
| @@ -6,6 +6,8 @@ import hashlib | ||
| 6 | 6 | import io |
| 7 | 7 | import json |
| 8 | 8 | import os |
| 9 | 추가: import re | |
| 10 | 추가: import subprocess | |
| 9 | 11 | from pathlib import Path |
| 10 | 12 | import struct |
| 11 | 13 | import tarfile |
| @@ -19,6 +21,25 @@ CHUNK = 64 * 1024 | ||
| 19 | 21 | INPUT_LIMIT = 2 ** 64 - 1 |
| 20 | 22 | OUTPUT_LIMIT = 16 * GIB |
| 21 | 23 | NAME = "payload.bin" |
| 24 | 추가: JOB_NAME = re.compile(r"[A-Za-z0-9][A-Za-z0-9_-]{0,63}\Z") | |
| 25 | 추가: | |
| 26 | 추가: | |
| 27 | 추가: def work_root(): | |
| 28 | 추가: job = os.environ.get("ZIPELO_CI_JOB", "rust-local") | |
| 29 | 추가: if not JOB_NAME.fullmatch(job): | |
| 30 | 추가: raise ValueError("안전한 작업명이 필요함") | |
| 31 | 추가: common = Path(subprocess.check_output( | |
| 32 | 추가: ["git", "rev-parse", "--path-format=absolute", "--git-common-dir"], | |
| 33 | 추가: cwd=ROOT, text=True, | |
| 34 | 추가: ).strip()).resolve() | |
| 35 | 추가: project = common.parent if common.name == ".git" else ROOT.resolve() | |
| 36 | 추가: work = project / "tests/work" / job | |
| 37 | 추가: configured = os.environ.get("ZIPELO_TEST_WORK_DIR") | |
| 38 | 추가: if configured and Path(configured).resolve() != work: | |
| 39 | 추가: raise ValueError("작업 폴더 밖의 표본 경로는 허용하지 않음") | |
| 40 | 추가: if work.resolve() != work: | |
| 41 | 추가: raise ValueError("안전한 작업 폴더가 필요함") | |
| 42 | 추가: return work | |
| 22 | 43 | |
| 23 | 44 | |
| 24 | 45 | class ZeroStream(io.RawIOBase): |
| @@ -104,7 +125,7 @@ def violations(artifact, limits): | ||
| 104 | 125 | |
| 105 | 126 | def build(directory, large=False): |
| 106 | 127 | directory = Path(directory).resolve() |
| 107 | 삭제: directory.relative_to(Path(os.environ["ZIPELO_TEST_WORK_DIR"]).resolve()) | |
| 128 | 추가: directory.relative_to(work_root()) | |
| 108 | 129 | directory.mkdir(mode=0o700, parents=True, exist_ok=False) |
| 109 | 130 | cases = [] |
| 110 | 131 | artifacts = {} |
| @@ -197,8 +218,9 @@ def main(): | ||
| 197 | 218 | parser.add_argument("--output", required=True, type=Path, help="tests 아래의 새 디렉터리") |
| 198 | 219 | parser.add_argument("--large", action="store_true", help="1 GiB 성공과 16 GiB 초과 표본도 생성함") |
| 199 | 220 | args = parser.parse_args() |
| 200 | 삭제: manifest = build(args.output, args.large) | |
| 201 | 삭제: print(f"{len(manifest['cases'])} cases: {args.output / 'expectations.json'}") | |
| 221 | 추가: output = args.output if args.output.is_absolute() else work_root() / args.output | |
| 222 | 추가: manifest = build(output, args.large) | |
| 223 | 추가: print(f"{len(manifest['cases'])} cases: {output / 'expectations.json'}") | |
| 202 | 224 | |
| 203 | 225 | |
| 204 | 226 | if __name__ == "__main__": |
| @@ -11,7 +11,7 @@ import tempfile | ||
| 11 | 11 | import tarfile |
| 12 | 12 | import zipfile |
| 13 | 13 | |
| 14 | 삭제: from generate import CHUNK, NAME, ROOT, build, digest, violations | |
| 14 | 추가: from generate import CHUNK, NAME, ROOT, build, digest, violations, work_root | |
| 15 | 15 | |
| 16 | 16 | |
| 17 | 17 | def check_payload(source, size): |
| @@ -49,7 +49,7 @@ def check_reference(path, artifact): | ||
| 49 | 49 | |
| 50 | 50 | def verify(directory, probe, worker, large=False): |
| 51 | 51 | directory = directory.resolve() |
| 52 | 삭제: directory.relative_to(ROOT / "tests") | |
| 52 | 추가: directory.relative_to(work_root()) | |
| 53 | 53 | manifest = json.loads((directory / "expectations.json").read_text(encoding="utf-8")) |
| 54 | 54 | assert not large or manifest["large"] |
| 55 | 55 | for name, artifact in manifest["artifacts"].items(): |
| @@ -108,22 +108,29 @@ def verify(directory, probe, worker, large=False): | ||
| 108 | 108 | return results |
| 109 | 109 | |
| 110 | 110 | |
| 111 | 추가: def default_target(): | |
| 112 | 추가: return Path(os.environ.get("CARGO_TARGET_DIR", work_root() / "target")) / "debug" | |
| 113 | 추가: | |
| 114 | 추가: | |
| 111 | 115 | def main(): |
| 112 | 116 | parser = argparse.ArgumentParser() |
| 113 | 117 | parser.add_argument("--directory", type=Path, help="기존 표본 또는 새 표본 경로") |
| 114 | 118 | parser.add_argument("--large", action="store_true", help="1 GiB 성공과 16 GiB 초과도 검사함") |
| 115 | 삭제: target = Path(os.environ.get("CARGO_TARGET_DIR", ROOT / "tests/ci/target")) / "debug" | |
| 119 | 추가: target = default_target() | |
| 116 | 120 | suffix = ".exe" if os.name == "nt" else "" |
| 117 | 121 | parser.add_argument("--probe", type=Path, default=target / ("zipelo-large-archive-probe" + suffix)) |
| 118 | 122 | parser.add_argument("--worker", type=Path, default=target / ("zipelo-archive-worker" + suffix)) |
| 119 | 123 | args = parser.parse_args() |
| 120 | 124 | assert args.probe.is_file() and args.worker.is_file(), "local 빌드를 먼저 실행해야 함" |
| 121 | 125 | if args.directory: |
| 122 | 삭제: if not args.directory.exists(): | |
| 123 | 삭제: build(args.directory, args.large) | |
| 124 | 삭제: verify(args.directory, args.probe, args.worker, args.large) | |
| 126 | 추가: directory = args.directory if args.directory.is_absolute() else work_root() / args.directory | |
| 127 | 추가: if not directory.exists(): | |
| 128 | 추가: build(directory, args.large) | |
| 129 | 추가: verify(directory, args.probe, args.worker, args.large) | |
| 125 | 130 | else: |
| 126 | 삭제: with tempfile.TemporaryDirectory(prefix="fixture-", dir=ROOT / "tests/large-archive-fixtures") as scratch: | |
| 131 | 추가: work = work_root() | |
| 132 | 추가: work.mkdir(mode=0o700, parents=True, exist_ok=True) | |
| 133 | 추가: with tempfile.TemporaryDirectory(prefix="fixture-", dir=work) as scratch: | |
| 127 | 134 | directory = Path(scratch) / "corpus" |
| 128 | 135 | build(directory, args.large) |
| 129 | 136 | verify(directory, args.probe, args.worker, args.large) |