diff --git a/reme/steps/file_io/_file_io.py b/reme/steps/file_io/_file_io.py index 613e0ac0..22e945b0 100644 --- a/reme/steps/file_io/_file_io.py +++ b/reme/steps/file_io/_file_io.py @@ -3,8 +3,8 @@ import asyncio import os from pathlib import Path +from secrets import token_hex from stat import S_IMODE -import tempfile from typing import Iterable import aiofiles @@ -156,7 +156,7 @@ async def detect_file_encoding(file_path, sniff_bytes: int = 8192) -> str: async def write_file_safe(file_path: Path, content: str | bytes, encoding: str = "utf-8") -> None: - """Publish complete bytes atomically so index readers never see a partial write.""" + """Publish complete bytes atomically while retaining normal file permissions.""" file_path.parent.mkdir(parents=True, exist_ok=True) if isinstance(content, str): try: @@ -171,8 +171,20 @@ async def write_file_safe(file_path: Path, content: str | bytes, encoding: str = payload = content def publish(): - mode = S_IMODE(file_path.stat().st_mode) if file_path.exists() else None - fd, temporary = tempfile.mkstemp(prefix=".reme-", suffix=".tmp", dir=file_path.parent) + flags = os.O_WRONLY | getattr(os, "O_BINARY", 0) + # Check OS write access without truncating the existing bytes. + try: + existing_fd = os.open(file_path, flags) + except FileNotFoundError: + mode = None + else: + try: + mode = S_IMODE(os.fstat(existing_fd).st_mode) + finally: + os.close(existing_fd) + + temporary = file_path.parent / f".reme-{token_hex(16)}.tmp" + fd = os.open(temporary, flags | os.O_CREAT | os.O_EXCL, 0o600 if mode is not None else 0o666) try: with os.fdopen(fd, "wb") as handle: handle.write(payload) diff --git a/reme/utils/async_utils.py b/reme/utils/async_utils.py index a694d1ec..8f54fbcb 100644 --- a/reme/utils/async_utils.py +++ b/reme/utils/async_utils.py @@ -9,18 +9,19 @@ from typing import Any async def complete_in_thread(func: Callable[..., Any], /, *args) -> Any: """Finish a side-effecting thread call, including through repeated cancellation.""" task = asyncio.create_task(asyncio.to_thread(func, *args)) - cancelled = False + cancellation: asyncio.CancelledError | None = None while not task.done(): try: await asyncio.shield(task) - except asyncio.CancelledError: - cancelled = True + except asyncio.CancelledError as exc: + if cancellation is None: + cancellation = exc except Exception: - if not cancelled: + if cancellation is None: raise break - if cancelled: + if cancellation is not None: with suppress(Exception): task.result() - raise asyncio.CancelledError + raise cancellation return task.result() diff --git a/tests/unit/test_async_utils.py b/tests/unit/test_async_utils.py index b39674fa..70b90adc 100644 --- a/tests/unit/test_async_utils.py +++ b/tests/unit/test_async_utils.py @@ -2,7 +2,9 @@ import asyncio import threading +import time +import anyio import pytest from reme.utils.async_utils import complete_in_thread @@ -46,3 +48,53 @@ async def test_complete_in_thread_finishes_before_propagating_result(cancellatio task.result() else: assert task.result() == "complete" + + +@pytest.mark.asyncio +@pytest.mark.parametrize("scope_name", ["fail_after", "move_on_after"]) +@pytest.mark.parametrize("fail", [False, True]) +async def test_complete_in_thread_preserves_anyio_cancel_scope(scope_name, fail): + """AnyIO must recognize its original cancellation after the thread finishes.""" + finished = threading.Event() + + def operation(): + time.sleep(0.05) + finished.set() + if fail: + raise RuntimeError("thread failed after cancellation") + + async def run_scope(): + with getattr(anyio, scope_name)(0.01) as scope: + await complete_in_thread(operation) + assert scope.cancelled_caught + + if scope_name == "fail_after": + with pytest.raises(TimeoutError): + await run_scope() + else: + await run_scope() + assert finished.is_set() + + +@pytest.mark.asyncio +async def test_complete_in_thread_retains_first_cancellation_message(): + """Repeated cancellation must retain the original caller's reason.""" + started, release = threading.Event(), threading.Event() + + def operation(): + started.set() + assert release.wait(5) + + task = asyncio.create_task(complete_in_thread(operation)) + try: + assert await asyncio.to_thread(started.wait, 5) + for message in ("original cancellation", "cancel again", "cancel once more"): + task.cancel(message) + await asyncio.sleep(0) + assert not task.done() + release.set() + with pytest.raises(asyncio.CancelledError, match="original cancellation"): + await task + finally: + release.set() + await asyncio.gather(task, return_exceptions=True) diff --git a/tests/unit/test_atomic_file_write.py b/tests/unit/test_atomic_file_write.py index 5bde4773..b2a68dbd 100644 --- a/tests/unit/test_atomic_file_write.py +++ b/tests/unit/test_atomic_file_write.py @@ -2,7 +2,10 @@ import asyncio import os +from pathlib import Path import stat +import subprocess +import sys import threading import pytest @@ -107,3 +110,75 @@ async def test_cancelled_save_keeps_path_lock_until_publication(tmp_path, monkey assert results[1].success assert target.read_text(encoding="utf-8") == "successful write" assert list(tmp_path.iterdir()) == [target] + + +@pytest.mark.asyncio +@pytest.mark.skipif(os.name != "posix", reason="POSIX file permissions") +async def test_save_rejects_read_only_target_without_replacing_it(tmp_path): + """A writable parent must not let saving bypass the target's access rights.""" + if os.geteuid() == 0: + pytest.skip("Root bypasses ordinary file write permissions") + target = tmp_path / "readonly.md" + target.write_bytes(b"original") + target.chmod(0o444) + try: + response = await SaveStep(app_context=ApplicationContext(workspace_dir=str(tmp_path)))( + path=target.name, + content="replacement", + ) + assert response.success is False + assert target.read_bytes() == b"original" + assert stat.S_IMODE(target.stat().st_mode) == 0o444 + assert list(tmp_path.iterdir()) == [target] + finally: + target.chmod(0o644) + + +@pytest.mark.asyncio +@pytest.mark.skipif(os.name != "posix", reason="POSIX file permissions") +async def test_atomic_write_does_not_require_target_read_access(tmp_path): + """Permission checks retain the existing write-only file contract.""" + target = tmp_path / "writeonly.md" + target.write_bytes(b"original") + target.chmod(0o200) + try: + await write_file_safe(target, b"replacement") + assert stat.S_IMODE(target.stat().st_mode) == 0o200 + finally: + target.chmod(0o600) + assert target.read_bytes() == b"replacement" + + +@pytest.mark.skipif(os.name != "posix", reason="POSIX umask semantics") +@pytest.mark.parametrize("mask, expected", [(0o002, 0o664), (0o077, 0o600)]) +def test_new_save_respects_process_umask(tmp_path, mask, expected): + """Check real creation modes without changing the test process's umask.""" + script = """ +import asyncio +import os +from pathlib import Path +import stat +import sys +from reme.components.application_context import ApplicationContext +from reme.steps.file_io.save import SaveStep + +workspace = Path(sys.argv[1]) +os.umask(int(sys.argv[2])) +response = asyncio.run(SaveStep(app_context=ApplicationContext(workspace_dir=str(workspace)))( + path="new.md", content="memory" +)) +assert response.success, response.answer +target = workspace / "new.md" +assert target.read_text() == "memory" +assert list(workspace.iterdir()) == [target] +print(stat.S_IMODE(target.stat().st_mode)) +""" + result = subprocess.run( + [sys.executable, "-c", script, str(tmp_path), str(mask)], + cwd=Path(__file__).resolve().parents[2], + capture_output=True, + text=True, + check=True, + timeout=15, + ) + assert int(result.stdout.splitlines()[-1]) == expected