from __future__ import annotations import asyncio import os from pathlib import Path import pytest from core.workspace_artifacts import ArtifactWorkspace, owner_ref_from_secret def test_owner_ref_is_stable_and_does_not_reveal_secret(): ref = owner_ref_from_secret("smesh-secret-value") assert ref == owner_ref_from_secret("smesh-secret-value") assert "secret" not in ref assert ref != owner_ref_from_secret("other") def test_put_list_read_delete_roundtrip(tmp_path: Path): ws = ArtifactWorkspace(tmp_path) owner = owner_ref_from_secret("alpha") rec = ws.put(owner, "demo", "src/main.py", b"print('hi')\n", mime_type="text/x-python") assert rec.path == "src/main.py" assert rec.size == 12 assert rec.mime_type == "text/x-python" items = ws.list(owner, "demo") assert [item["path"] for item in items] == ["src/main.py"] got_rec, body = ws.read(owner, "demo", "src/main.py") assert got_rec.sha256 == rec.sha256 assert body == b"print('hi')\n" assert ws.delete(owner, "demo", "src/main.py") is True assert ws.delete(owner, "demo", "src/main.py") is False def test_owner_namespaces_are_physically_isolated(tmp_path: Path): ws = ArtifactWorkspace(tmp_path) a = owner_ref_from_secret("a") b = owner_ref_from_secret("b") ws.put(a, "same-project", "answer.txt", b"A") ws.put(b, "same-project", "answer.txt", b"B") assert ws.read(a, "same-project", "answer.txt")[1] == b"A" assert ws.read(b, "same-project", "answer.txt")[1] == b"B" @pytest.mark.parametrize("bad", ["../x", "/etc/passwd", "a/../../x", "./x", "a//../b"]) def test_path_traversal_is_refused(tmp_path: Path, bad: str): ws = ArtifactWorkspace(tmp_path) owner = owner_ref_from_secret("alpha") with pytest.raises(ValueError): ws.put(owner, "demo", bad, b"no") def test_symlink_escape_is_refused(tmp_path: Path): if not hasattr(os, "symlink"): pytest.skip("symlinks unavailable") ws = ArtifactWorkspace(tmp_path) owner = owner_ref_from_secret("alpha") root = tmp_path / owner / "demo" root.mkdir(parents=True) outside = tmp_path / "outside" outside.mkdir() (root / "escape").symlink_to(outside, target_is_directory=True) with pytest.raises(ValueError, match="symlink"): ws.put(owner, "demo", "escape/pwn.txt", b"no") def test_file_and_workspace_quota(monkeypatch, tmp_path: Path): monkeypatch.setenv("SIGNALMESH_ARTIFACT_MAX_FILE_BYTES", "4") monkeypatch.setenv("SIGNALMESH_ARTIFACT_MAX_WORKSPACE_BYTES", "6") ws = ArtifactWorkspace(tmp_path) owner = owner_ref_from_secret("alpha") with pytest.raises(ValueError, match="per-file"): ws.put(owner, "demo", "big.bin", b"12345") ws.put(owner, "demo", "a.bin", b"1234") with pytest.raises(ValueError, match="storage"): ws.put(owner, "demo", "b.bin", b"789") class _Response: def raise_for_status(self): return None def json(self): return { "success": True, "link": "https://file.io/demo", "expires": "tomorrow", "maxDownloads": 1, "autoDelete": True, } class _Client: def __init__(self): self.calls = [] async def post(self, url, **kwargs): self.calls.append((url, kwargs)) return _Response() def test_fileio_export_is_opt_in_and_keeps_canonical_file(monkeypatch, tmp_path: Path): ws = ArtifactWorkspace(tmp_path) owner = owner_ref_from_secret("alpha") ws.put(owner, "demo", "build/app.zip", b"ZIP", mime_type="application/zip") client = _Client() monkeypatch.delenv("SIGNALMESH_FILEIO_ENABLED", raising=False) with pytest.raises(PermissionError): asyncio.run(ws.export_fileio(owner, "demo", "build/app.zip", client=client)) monkeypatch.setenv("SIGNALMESH_FILEIO_ENABLED", "1") result = asyncio.run(ws.export_fileio(owner, "demo", "build/app.zip", client=client)) assert result["temporary"] is True assert result["link"] == "https://file.io/demo" assert result["canonical_address"] == "artifact://demo/build/app.zip" assert ws.read(owner, "demo", "build/app.zip")[1] == b"ZIP" assert client.calls[0][0] == "https://file.io"