Merge pull request #139 from Team-LJY/fix/test-tool-path-guards
시험 도구 경로와 창 탐색 보호 수정
- 작성자
- LeeJunyoung
- 작성 시각
- 커밋
c8fee9f9dd4a11158ea8678be1285ad5a7e80ba0
변경된 파일 5개
+218-16| @@ -98,10 +98,13 @@ def command(manifest, phase): | ||
| 98 | 98 | |
| 99 | 99 | def prepare_fixtures(root): |
| 100 | 100 | skipped = {} |
| 101 | 추가: work = test_work_directory(root) | |
| 102 | 추가: if work.resolve() != work: | |
| 103 | 추가: raise ValueError(f"안전한 작업 디렉터리가 필요함: {work}") | |
| 104 | 추가: work.mkdir(mode=0o700, parents=True, exist_ok=True) | |
| 105 | 추가: fixtures = output_directory(work, "fixtures", create=True) | |
| 101 | 106 | for name in sorted(FILE_FIXTURES): |
| 102 | 삭제: path = test_work_directory(root) / "fixtures" / name | |
| 103 | 삭제: if not path.exists() and not path.is_symlink(): | |
| 104 | 삭제: path.mkdir(mode=0o700, parents=True) | |
| 107 | 추가: path = output_directory(fixtures, name, create=True) | |
| 105 | 108 | info = path.lstat() |
| 106 | 109 | if ( |
| 107 | 110 | not stat.S_ISDIR(info.st_mode) |
| @@ -10,8 +10,57 @@ if [[ ! "$job" =~ ^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$ ]]; then | ||
| 10 | 10 | exit 2 |
| 11 | 11 | fi |
| 12 | 12 | ci_tools="${ZIPELO_CI_TOOLS:-$project_root/tests/work/$job/tools}" |
| 13 | 삭제: mkdir -p "$ci_tools/downloads" "$ci_tools/mingw" | |
| 14 | 삭제: ci_tools="$(cd "$ci_tools" && pwd)" | |
| 13 | 추가: validate_write_tree() { | |
| 14 | 추가: python3 - "$project_root" "$job" "$ci_tools" <<'PYTHON' | |
| 15 | 추가: import os | |
| 16 | 추가: from pathlib import Path | |
| 17 | 추가: import sys | |
| 18 | 추가: | |
| 19 | 추가: project = Path(sys.argv[1]).resolve() | |
| 20 | 추가: allowed = project / "tests/work" / sys.argv[2] | |
| 21 | 추가: tools = Path(os.path.abspath(sys.argv[3])) | |
| 22 | 추가: | |
| 23 | 추가: | |
| 24 | 추가: def reject(path): | |
| 25 | 추가: raise SystemExit(f"작업 폴더 밖의 경로는 허용하지 않음: {path}") | |
| 26 | 추가: | |
| 27 | 추가: | |
| 28 | 추가: for path in (project / "tests", project / "tests/work", allowed): | |
| 29 | 추가: if path.resolve() != path: | |
| 30 | 추가: reject(path) | |
| 31 | 추가: if not tools.is_relative_to(allowed) or tools.resolve() != tools: | |
| 32 | 추가: reject(tools) | |
| 33 | 추가: | |
| 34 | 추가: seen = set() | |
| 35 | 추가: | |
| 36 | 추가: | |
| 37 | 추가: def check(path): | |
| 38 | 추가: try: | |
| 39 | 추가: resolved = path.resolve() | |
| 40 | 추가: except (OSError, RuntimeError): | |
| 41 | 추가: reject(path) | |
| 42 | 추가: if not resolved.is_relative_to(allowed): | |
| 43 | 추가: reject(path) | |
| 44 | 추가: if path.is_dir(): | |
| 45 | 추가: identity = (path.stat().st_dev, path.stat().st_ino) | |
| 46 | 추가: if identity in seen: | |
| 47 | 추가: return | |
| 48 | 추가: seen.add(identity) | |
| 49 | 추가: for child in path.iterdir(): | |
| 50 | 추가: check(child) | |
| 51 | 추가: | |
| 52 | 추가: | |
| 53 | 추가: if tools.is_dir(): | |
| 54 | 추가: check(tools) | |
| 55 | 추가: print(tools) | |
| 56 | 추가: PYTHON | |
| 57 | 추가: } | |
| 58 | 추가: ci_tools="$(validate_write_tree)" | |
| 59 | 추가: mkdir -p "$ci_tools/downloads" "$ci_tools/mingw" "$ci_tools/cargo" \ | |
| 60 | 추가: "$ci_tools/rustup" "$ci_tools/tmp" | |
| 61 | 추가: export CARGO_HOME="$ci_tools/cargo" | |
| 62 | 추가: export RUSTUP_HOME="$ci_tools/rustup" | |
| 63 | 추가: export TMPDIR="$ci_tools/tmp" | |
| 15 | 64 | |
| 16 | 65 | # 도구를 프로젝트 안에 풀며 시스템 패키지는 설치하지 않음. |
| 17 | 66 | ( |
| @@ -26,8 +75,10 @@ ci_tools="$(cd "$ci_tools" && pwd)" | ||
| 26 | 75 | mingw-w64-x86-64-dev \ |
| 27 | 76 | mingw-w64-common |
| 28 | 77 | for package in ./*.deb; do |
| 78 | 추가: validate_write_tree >/dev/null | |
| 29 | 79 | dpkg-deb -x "$package" "$ci_tools/mingw" |
| 30 | 80 | done |
| 31 | 81 | ) |
| 32 | 삭제: RUSTUP_HOME="$ci_tools/rustup" rustup toolchain install 1.99.1 \ | |
| 82 | 추가: validate_write_tree >/dev/null | |
| 83 | 추가: rustup toolchain install 1.99.1 \ | |
| 33 | 84 | --profile minimal --no-self-update --target x86_64-pc-windows-gnu |
| @@ -47,6 +47,100 @@ class RunnerTests(unittest.TestCase): | ||
| 47 | 47 | self.assertFalse((outside / "task").exists()) |
| 48 | 48 | self.assertFalse((outside / "work").exists()) |
| 49 | 49 | |
| 50 | 추가: def test_fixture_parent_symlink_is_rejected_before_creating_children(self): | |
| 51 | 추가: with tempfile.TemporaryDirectory(dir=TEST_WORK) as directory: | |
| 52 | 추가: base = Path(directory) | |
| 53 | 추가: work = base / "work" | |
| 54 | 추가: work.mkdir() | |
| 55 | 추가: outside = base / "outside" | |
| 56 | 추가: outside.mkdir() | |
| 57 | 추가: (work / "fixtures").symlink_to(outside, target_is_directory=True) | |
| 58 | 추가: with ( | |
| 59 | 추가: patch.object(run_rust, "test_work_directory", return_value=work), | |
| 60 | 추가: self.assertRaisesRegex(ValueError, "작업 폴더 밖"), | |
| 61 | 추가: ): | |
| 62 | 추가: run_rust.prepare_fixtures(run_rust.ROOT) | |
| 63 | 추가: self.assertEqual(list(outside.iterdir()), []) | |
| 64 | 추가: | |
| 65 | 추가: def test_windows_setup_rejects_symlinked_parent_before_creating_tools(self): | |
| 66 | 추가: with tempfile.TemporaryDirectory(dir=TEST_WORK) as directory: | |
| 67 | 추가: base = Path(directory) | |
| 68 | 추가: project = base / "project" | |
| 69 | 추가: ci = project / "tests/ci" | |
| 70 | 추가: ci.mkdir(parents=True) | |
| 71 | 추가: work = project / "tests/work/task" | |
| 72 | 추가: work.mkdir(parents=True) | |
| 73 | 추가: script = ci / "setup_windows.sh" | |
| 74 | 추가: script.write_bytes((run_rust.ROOT / "tests/ci/setup_windows.sh").read_bytes()) | |
| 75 | 추가: commands = base / "bin" | |
| 76 | 추가: commands.mkdir() | |
| 77 | 추가: git = commands / "git" | |
| 78 | 추가: git.write_text('#!/bin/sh\nprintf "%s\n" "$FAKE_GIT_COMMON"\n') | |
| 79 | 추가: git.chmod(0o700) | |
| 80 | 추가: outside = base / "outside" | |
| 81 | 추가: outside.mkdir() | |
| 82 | 추가: (work / "linked").symlink_to(outside, target_is_directory=True) | |
| 83 | 추가: environment = { | |
| 84 | 추가: **os.environ, | |
| 85 | 추가: "PATH": str(commands) + os.pathsep + os.environ["PATH"], | |
| 86 | 추가: "FAKE_GIT_COMMON": str(project / ".git"), | |
| 87 | 추가: "ZIPELO_CI_JOB": "task", | |
| 88 | 추가: "ZIPELO_CI_TOOLS": str(work / "linked/tools"), | |
| 89 | 추가: } | |
| 90 | 추가: result = subprocess.run(["bash", str(script)], env=environment, | |
| 91 | 추가: capture_output=True, text=True) | |
| 92 | 추가: self.assertNotEqual(result.returncode, 0) | |
| 93 | 추가: self.assertIn("작업 폴더 밖", result.stderr) | |
| 94 | 추가: self.assertEqual(list(outside.iterdir()), []) | |
| 95 | 추가: | |
| 96 | 추가: def test_windows_setup_rejects_external_rustup_before_install(self): | |
| 97 | 추가: with tempfile.TemporaryDirectory(dir=TEST_WORK) as directory: | |
| 98 | 추가: base = Path(directory) | |
| 99 | 추가: project = base / "project" | |
| 100 | 추가: ci = project / "tests/ci" | |
| 101 | 추가: ci.mkdir(parents=True) | |
| 102 | 추가: work = project / "tests/work/task" | |
| 103 | 추가: tools = work / "tools" | |
| 104 | 추가: tools.mkdir(parents=True) | |
| 105 | 추가: script = ci / "setup_windows.sh" | |
| 106 | 추가: script.write_bytes((run_rust.ROOT / "tests/ci/setup_windows.sh").read_bytes()) | |
| 107 | 추가: commands = base / "bin" | |
| 108 | 추가: commands.mkdir() | |
| 109 | 추가: git = commands / "git" | |
| 110 | 추가: git.write_text('#!/bin/sh\nprintf "%s\n" "$FAKE_GIT_COMMON"\n') | |
| 111 | 추가: git.chmod(0o700) | |
| 112 | 추가: for name in ("apt-get", "dpkg-deb"): | |
| 113 | 추가: command = commands / name | |
| 114 | 추가: command.write_text("#!/bin/sh\nexit 0\n") | |
| 115 | 추가: command.chmod(0o700) | |
| 116 | 추가: rustup = commands / "rustup" | |
| 117 | 추가: rustup.write_text( | |
| 118 | 추가: '#!/bin/sh\nmkdir -p "$RUSTUP_HOME"\nprintf marker > "$RUSTUP_HOME/marker"\n' | |
| 119 | 추가: ) | |
| 120 | 추가: rustup.chmod(0o700) | |
| 121 | 추가: outside = base / "outside" | |
| 122 | 추가: outside.mkdir() | |
| 123 | 추가: environment = { | |
| 124 | 추가: **os.environ, | |
| 125 | 추가: "PATH": str(commands) + os.pathsep + os.environ["PATH"], | |
| 126 | 추가: "FAKE_GIT_COMMON": str(project / ".git"), | |
| 127 | 추가: "ZIPELO_CI_JOB": "task", | |
| 128 | 추가: "ZIPELO_CI_TOOLS": str(tools), | |
| 129 | 추가: } | |
| 130 | 추가: for name in ("rustup", "cargo", "downloads/nested"): | |
| 131 | 추가: with self.subTest(name=name): | |
| 132 | 추가: link = tools / name | |
| 133 | 추가: link.parent.mkdir(parents=True, exist_ok=True) | |
| 134 | 추가: link.symlink_to(outside, target_is_directory=True) | |
| 135 | 추가: result = subprocess.run( | |
| 136 | 추가: ["bash", str(script)], env=environment, | |
| 137 | 추가: capture_output=True, text=True, | |
| 138 | 추가: ) | |
| 139 | 추가: self.assertNotEqual(result.returncode, 0) | |
| 140 | 추가: self.assertIn("작업 폴더 밖", result.stderr) | |
| 141 | 추가: self.assertEqual(list(outside.iterdir()), []) | |
| 142 | 추가: link.unlink() | |
| 143 | 추가: | |
| 50 | 144 | def test_output_paths_reject_symlinks_outside_job(self): |
| 51 | 145 | with tempfile.TemporaryDirectory(dir=TEST_WORK) as directory: |
| 52 | 146 | work = Path(directory) |
| @@ -98,6 +98,48 @@ with patch.object(module.subprocess, "run", return_value=subprocess.CompletedPro | ||
| 98 | 98 | self.assertEqual(result.returncode, 0, result.stderr) |
| 99 | 99 | self.assertEqual(result.stdout.strip(), "blocked") |
| 100 | 100 | |
| 101 | 추가: def test_window_guards_stay_active_with_python_optimization(self): | |
| 102 | 추가: script = """import importlib.util | |
| 103 | 추가: import subprocess | |
| 104 | 추가: import sys | |
| 105 | 추가: from unittest.mock import Mock, patch | |
| 106 | 추가: | |
| 107 | 추가: spec = importlib.util.spec_from_file_location("ubuntu_smoke", sys.argv[1]) | |
| 108 | 추가: module = importlib.util.module_from_spec(spec) | |
| 109 | 추가: spec.loader.exec_module(module) | |
| 110 | 추가: app = Mock(pid=123) | |
| 111 | 추가: app.poll.return_value = 1 | |
| 112 | 추가: with patch.object(module.subprocess, "run") as search: | |
| 113 | 추가: try: | |
| 114 | 추가: module.wait_for_window(app, {}, timeout=0.5) | |
| 115 | 추가: except RuntimeError as error: | |
| 116 | 추가: if "조기에 종료" not in str(error): | |
| 117 | 추가: raise | |
| 118 | 추가: else: | |
| 119 | 추가: raise SystemExit("종료된 앱을 통과시킴") | |
| 120 | 추가: search.assert_not_called() | |
| 121 | 추가: app.poll.return_value = None | |
| 122 | 추가: with patch.object(module.subprocess, "run", return_value=subprocess.CompletedProcess([], 1)): | |
| 123 | 추가: with patch.object(module.time, "monotonic", side_effect=[0, 1]): | |
| 124 | 추가: with patch.object(module.time, "sleep") as sleep: | |
| 125 | 추가: try: | |
| 126 | 추가: module.wait_for_window(app, {}, timeout=0.5) | |
| 127 | 추가: except RuntimeError as error: | |
| 128 | 추가: if "시간 초과" not in str(error): | |
| 129 | 추가: raise | |
| 130 | 추가: else: | |
| 131 | 추가: raise SystemExit("창 탐색 제한 시간을 통과시킴") | |
| 132 | 추가: sleep.assert_not_called() | |
| 133 | 추가: print("guards active") | |
| 134 | 추가: """ | |
| 135 | 추가: result = subprocess.run( | |
| 136 | 추가: [sys.executable, "-B", "-O", "-c", script, str(SMOKE.ROOT / "tests/release-packages/ubuntu_smoke.py")], | |
| 137 | 추가: env=dict(os.environ, PYTHONDONTWRITEBYTECODE="1"), cwd=ROOT, | |
| 138 | 추가: capture_output=True, text=True, timeout=10, | |
| 139 | 추가: ) | |
| 140 | 추가: self.assertEqual(result.returncode, 0, result.stderr) | |
| 141 | 추가: self.assertEqual(result.stdout.strip(), "guards active") | |
| 142 | 추가: | |
| 101 | 143 | def test_existing_installation_files_are_blocked(self): |
| 102 | 144 | with ( |
| 103 | 145 | patch.object(SMOKE.subprocess, "run", return_value=subprocess.CompletedProcess([], 1, b"")), |
| @@ -109,6 +109,26 @@ def installed_package(package, home): | ||
| 109 | 109 | raise BaseExceptionGroup("패키지 시험 또는 정리 실패", failures) |
| 110 | 110 | |
| 111 | 111 | |
| 112 | 추가: def ensure_app_running(app): | |
| 113 | 추가: if app.poll() is not None: | |
| 114 | 추가: raise RuntimeError("설치된 앱이 조기에 종료됨") | |
| 115 | 추가: | |
| 116 | 추가: | |
| 117 | 추가: def wait_for_window(app, env, timeout=30): | |
| 118 | 추가: deadline = time.monotonic() + timeout | |
| 119 | 추가: while True: | |
| 120 | 추가: ensure_app_running(app) | |
| 121 | 추가: window = subprocess.run( | |
| 122 | 추가: ["xdotool", "search", "--all", "--pid", str(app.pid), "--name", "Zipelo"], | |
| 123 | 추가: env=env, capture_output=True, text=True, | |
| 124 | 추가: ) | |
| 125 | 추가: if window.returncode == 0: | |
| 126 | 추가: return window | |
| 127 | 추가: if time.monotonic() >= deadline: | |
| 128 | 추가: raise RuntimeError("앱 창 표시 시간 초과") | |
| 129 | 추가: time.sleep(0.2) | |
| 130 | 추가: | |
| 131 | 추가: | |
| 112 | 132 | def main(): |
| 113 | 133 | parser = argparse.ArgumentParser(description=__doc__) |
| 114 | 134 | parser.add_argument("package", type=Path) |
| @@ -147,17 +167,9 @@ def main(): | ||
| 147 | 167 | env.pop("WAYLAND_DISPLAY", None) |
| 148 | 168 | with (runs / "app.stdout").open("wb") as stdout, (runs / "app.stderr").open("wb") as stderr: |
| 149 | 169 | app = subprocess.Popen(["/usr/bin/zipelo", str(archive)], env=env, stdout=stdout, stderr=stderr) |
| 150 | 삭제: deadline = time.monotonic() + 30 | |
| 151 | 삭제: while True: | |
| 152 | 삭제: assert app.poll() is None, "설치된 앱이 조기에 종료됨" | |
| 153 | 삭제: window = subprocess.run(["xdotool", "search", "--all", "--pid", str(app.pid), "--name", "Zipelo"], | |
| 154 | 삭제: env=env, capture_output=True, text=True) | |
| 155 | 삭제: if window.returncode == 0: | |
| 156 | 삭제: break | |
| 157 | 삭제: assert time.monotonic() < deadline, "앱 창 표시 시간 초과" | |
| 158 | 삭제: time.sleep(0.2) | |
| 170 | 추가: window = wait_for_window(app, env) | |
| 159 | 171 | time.sleep(3) |
| 160 | 삭제: assert app.poll() is None | |
| 172 | 추가: ensure_app_running(app) | |
| 161 | 173 | subprocess.run(["import", "-window", window.stdout.splitlines()[0], |
| 162 | 174 | str(runs / "installed-window.png")], env=env, check=True) |
| 163 | 175 | print("PASS installed package, ZIP/7z/TAR.GZ defaults and native X11 window") |