AI agent skill that researches any topic across Reddit, X, YouTube, HN, Polymarket, and the web - then synthesizes a grounded summary
Python ai-promptsai-skillblueskyclaudeclaude-codeclawhubdeep-researchhackernewsinstagramopenclawpolymarketrecencyredditresearchsocial-mediatiktoktrendstwitterweb-searchyoutube
fix(security): require https for the hosted API and keep local state owner-only (#1267)
Ilia Alshanetsky928f405455ab
diff --git a/changelog.d/+security-hardening.security.md b/changelog.d/+security-hardening.security.mdnew file mode 100644index 00000000..a28d173c--- /dev/null+++ b/changelog.d/+security-hardening.security.md@@ -0,0 +1 @@+Cached research reports and the GitHub sign-in handle are now written with owner-only permissions on macOS and Linux. Hosted research through `LAST30DAYS_API_BASE` now requires HTTPS, except for loopback addresses.diff --git a/skills/last30days/scripts/last30days.py b/skills/last30days/scripts/last30days.pyindex 1bdd8942..e1978379 100644--- a/skills/last30days/scripts/last30days.py+++ b/skills/last30days/scripts/last30days.py@@ -51,6 +51,7 @@ SCRIPT_DIR = Path(__file__).parent.resolve()sys.path.insert(0, str(SCRIPT_DIR))from lib import competitors as competitors_mod, corpus, dates, discovery_handoff, env, freshness, html_render, http, permission_preflight, pipeline, reddit, registers, render, schema, subproc, ui, x_envelope+from lib.private_files import write_private_textatexit.register(subproc.cleanup_children)@@ -1334,7 +1335,7 @@ def _write_last_run("report_cache": str(target / "last-report.json"),"comparison": bool(entity_reports),}- (target / "last-run.json").write_text(json.dumps(payload, indent=2))+ write_private_text(target / "last-run.json", json.dumps(payload, indent=2))cache_payload = {"schema": REPORT_CACHE_VERSION,"topic": topic,@@ -1347,8 +1348,8 @@ def _write_last_run(],}report_cache_path = target / "last-report.json"- report_cache_path.write_text(json.dumps(cache_payload, indent=2))- if has_private_corpus:+ write_private_text(report_cache_path, json.dumps(cache_payload, indent=2))+ if has_private_corpus and os.name != "posix":report_cache_path.chmod(0o600)return Trueexcept Exception as exc:diff --git a/skills/last30days/scripts/lib/hosted.py b/skills/last30days/scripts/lib/hosted.pyindex addae36e..819c4767 100644--- a/skills/last30days/scripts/lib/hosted.py+++ b/skills/last30days/scripts/lib/hosted.py@@ -33,7 +33,7 @@ import reimport sysimport time-from . import env, http, usage+from . import env, http, providers, usagefrom .log import source_log# Distinct exit code for the clarify gate so the invoking model can tell@@ -60,7 +60,13 @@ def _api_base() -> str:# Endpoint comes only from the environment - no built-in default. Hosted# mode is gated on this being set (see last30days.py), so by the time this# is called it is populated; an empty value means "not configured".- return (os.environ.get("LAST30DAYS_API_BASE") or "").rstrip("/")+ base = (os.environ.get("LAST30DAYS_API_BASE") or "").rstrip("/")+ if not providers.allowed_base_url_override(base):+ raise ValueError(+ "LAST30DAYS_API_BASE must use https:// "+ "(http:// is allowed only for loopback hosts)."+ )+ return basedef _billing_url() -> str:@@ -269,7 +275,12 @@ def run_hosted(register: str = "default",) -> int:"""Submit topic to the remote API, poll to terminal status, render report."""- _err(f"Running via last30days API ({_api_base()}), depth={depth}")+ try:+ base = _api_base()+ except ValueError as exc:+ _err(str(exc))+ return 1+ _err(f"Running via last30days API ({base}), depth={depth}")try:resp = submit(topic, depth, register=register)except http.HTTPError as exc:diff --git a/skills/last30days/scripts/lib/private_files.py b/skills/last30days/scripts/lib/private_files.pynew file mode 100644index 00000000..9e1beaec--- /dev/null+++ b/skills/last30days/scripts/lib/private_files.py@@ -0,0 +1,21 @@+"""Private local text files."""++import os+from pathlib import Path+++def write_private_text(path: Path, content: str, *, encoding: str | None = None) -> int:+ """Write text with owner-only permissions before any bytes on POSIX."""+ if os.name != "posix":+ return path.write_text(content, encoding=encoding)++ fd = os.open(path, os.O_CREAT | os.O_WRONLY, 0o600)+ try:+ os.fchmod(fd, 0o600)+ stream = os.fdopen(fd, "w", encoding=encoding)+ except Exception:+ os.close(fd)+ raise+ with stream:+ stream.truncate(0)+ return stream.write(content)diff --git a/skills/last30days/scripts/lib/setup_wizard.py b/skills/last30days/scripts/lib/setup_wizard.pyindex 76c32990..f125d773 100644--- a/skills/last30days/scripts/lib/setup_wizard.py+++ b/skills/last30days/scripts/lib/setup_wizard.py@@ -18,6 +18,7 @@ from urllib.error import HTTPError, URLErrorfrom urllib.request import Request, urlopenfrom . import brightdata+from .private_files import write_private_textlogger = logging.getLogger(__name__)@@ -1374,8 +1375,9 @@ def run_github_start() -> Dict[str, Any]:path = _device_handle_path()try:path.parent.mkdir(parents=True, exist_ok=True)- path.write_text(json.dumps(handle), encoding="utf-8")- os.chmod(path, 0o600)+ write_private_text(path, json.dumps(handle), encoding="utf-8")+ if os.name != "posix":+ os.chmod(path, 0o600)except Exception as exc:logger.warning("Could not persist device handle: %s", exc)return publicdiff --git a/tests/test_hosted.py b/tests/test_hosted.pyindex 2a929628..9f477448 100644--- a/tests/test_hosted.py+++ b/tests/test_hosted.py@@ -451,6 +451,63 @@ def test_api_base_override(remote_env, monkeypatch, capsys):assert urls == ["https://staging.example.dev/api/v1/search"][email protected]("base", [+ TEST_BASE,+ "http://localhost:8080/api/v1",+ "http://127.0.0.1:8080/api/v1",+ "http://[::1]:8080/api/v1",+])+def test_api_base_allows_https_and_loopback_http(remote_env, monkeypatch, base):+ monkeypatch.setenv("LAST30DAYS_API_BASE", base)+ with mock.patch.object(hosted.http, "post", return_value=SUBMIT_OK) as post, \+ mock.patch.object(hosted.http, "get", return_value=POLL_COMPLETE) as get:+ assert hosted.run_hosted("test topic", "quick") == 0++ assert post.call_args.args == (f"{base}/search",)+ assert get.call_args.args == (f"{base}/search",)+ assert post.call_args.kwargs["headers"] == {"Authorization": f"Bearer {TEST_KEY}"}+ assert get.call_args.kwargs["headers"] == {"Authorization": f"Bearer {TEST_KEY}"}++[email protected]("base", [+ "http://remote.example.test/api/v1",+ f"http://remote.example.test/{TEST_KEY}",+ "ftp://remote.example.test/api/v1",+])+def test_api_base_rejects_insecure_remote_without_request(+ remote_env, monkeypatch, capsys, base,+):+ monkeypatch.setenv("LAST30DAYS_API_BASE", base)+ with mock.patch.object(hosted.http, "post", return_value=SUBMIT_OK) as post, \+ mock.patch.object(hosted.http, "get", return_value=POLL_COMPLETE) as get:+ rc = hosted.run_hosted("test topic", "quick")++ captured = capsys.readouterr()+ post.assert_not_called()+ get.assert_not_called()+ assert rc == 1+ assert "LAST30DAYS_API_BASE" in captured.err+ assert "https://" in captured.err+ assert "loopback" in captured.err+ assert TEST_KEY not in captured.out + captured.err++[email protected]("operation", ["submit", "poll"])+def test_direct_hosted_requests_reject_insecure_remote(remote_env, monkeypatch, operation):+ monkeypatch.setenv("LAST30DAYS_API_BASE", "http://remote.example.test/api/v1")+ with mock.patch.object(hosted.http, "post") as post, \+ mock.patch.object(hosted.http, "get") as get:+ with pytest.raises(ValueError, match="LAST30DAYS_API_BASE.*https://") as exc:+ if operation == "submit":+ hosted.submit("test topic", "quick")+ else:+ hosted.poll(SEARCH_ID)++ post.assert_not_called()+ get.assert_not_called()+ assert TEST_KEY not in str(exc.value)++# ---------------------------------------------------------------------------# Error handling# ---------------------------------------------------------------------------diff --git a/tests/test_last_run_state.py b/tests/test_last_run_state.pyindex 8b9db8ec..3ae05d8a 100644--- a/tests/test_last_run_state.py+++ b/tests/test_last_run_state.py@@ -117,6 +117,39 @@ class LastRunStateTests(unittest.TestCase):self.assertIsNone(entity_reports)self.assertEqual(config_dir / "last-report.json", cache_path)+ @unittest.skipUnless(os.name == "posix", "POSIX file permissions")+ def test_run_cache_files_are_private_before_writing(self):+ for existing in (False, True):+ with self.subTest(existing=existing), tempfile.TemporaryDirectory() as tmp:+ config_dir = Path(tmp)+ paths = [config_dir / name for name in ("last-run.json", "last-report.json")]+ if existing:+ for path in paths:+ path.write_text("old evidence " * 1000)+ path.chmod(0o644)+ modes_at_open = []+ real_open = io.open++ def observe_open(*args, **kwargs):+ stream = real_open(*args, **kwargs)+ if "w" in stream.mode:+ modes_at_open.append(os.fstat(stream.fileno()).st_mode & 0o777)+ return stream++ old_umask = os.umask(0o022)+ try:+ with mock.patch.object(cli.env, "CONFIG_DIR", config_dir), \+ mock.patch("io.open", side_effect=observe_open):+ written = cli._write_last_run("OpenClaw", _report("OpenClaw"))+ finally:+ os.umask(old_umask)++ self.assertTrue(written)+ self.assertEqual([0o600, 0o600], modes_at_open)+ for path in paths:+ self.assertEqual(0o600, path.stat().st_mode & 0o777)+ self.assertEqual("OpenClaw", json.loads(path.read_text())["topic"])+def test_last_report_cache_expires_after_ttl(self):with tempfile.TemporaryDirectory() as tmp:config_dir = Path(tmp) / "config"diff --git a/tests/test_private_files.py b/tests/test_private_files.pynew file mode 100644index 00000000..0dfa58b3--- /dev/null+++ b/tests/test_private_files.py@@ -0,0 +1,56 @@+import os+from types import SimpleNamespace+from unittest.mock import patch++import pytest++from lib import private_files++[email protected](os.name != "posix", reason="POSIX file permissions")+def test_new_file_is_private_at_creation(tmp_path):+ path = tmp_path / "state.json"+ modes_at_creation = []+ real_open = os.open++ def observe_open(*args, **kwargs):+ fd = real_open(*args, **kwargs)+ modes_at_creation.append(os.fstat(fd).st_mode & 0o777)+ return fd++ old_umask = os.umask(0o022)+ try:+ with patch.object(private_files.os, "open", side_effect=observe_open):+ private_files.write_private_text(path, "private evidence")+ finally:+ os.umask(old_umask)++ assert modes_at_creation == [0o600]+ assert path.read_text() == "private evidence"+++def test_windows_write_keeps_existing_file_mode(tmp_path, monkeypatch):+ path = tmp_path / "state.json"+ path.write_text("old content" * 20, encoding="utf-8")+ original_mode = path.stat().st_mode+ monkeypatch.setattr(private_files, "os", SimpleNamespace(name="nt"))++ private_files.write_private_text(path, "new café", encoding="utf-8")++ assert path.read_text(encoding="utf-8") == "new café"+ assert path.stat().st_mode == original_mode++[email protected](os.name != "posix", reason="POSIX file permissions")+def test_permission_failure_closes_descriptor_without_writing(tmp_path):+ path = tmp_path / "state.json"+ path.write_text("old content", encoding="utf-8")+ descriptor = os.open(path, os.O_WRONLY)+ with patch.object(private_files.os, "open", return_value=descriptor), \+ patch.object(private_files.os, "fchmod", side_effect=PermissionError("denied")):+ with pytest.raises(PermissionError, match="denied"):+ private_files.write_private_text(path, "new content")++ with pytest.raises(OSError):+ os.fstat(descriptor)+ assert path.read_text(encoding="utf-8") == "old content"diff --git a/tests/test_setup_wizard.py b/tests/test_setup_wizard.pyindex 58526ee6..8cadc3e1 100644--- a/tests/test_setup_wizard.py+++ b/tests/test_setup_wizard.py@@ -1,5 +1,6 @@"""Tests for the first-run setup wizard module."""+import ioimport jsonimport osimport subprocess@@ -1181,6 +1182,39 @@ def _device_code_response(interval):return mock_resp[email protected](os.name != "posix", reason="POSIX file permissions")[email protected]("existing", [False, True])+def test_github_device_handle_is_private_before_writing(tmp_path, existing):+ path = tmp_path / ".github-device-handle.json"+ if existing:+ path.write_text("old handle " * 100)+ path.chmod(0o644)+ public = {"status": "awaiting_authorization", "user_code": "ABCD-1234"}+ handle = {"device_code": "dummy-device-code", "interval": 5}+ modes_at_open = []+ real_open = io.open++ def observe_open(*args, **kwargs):+ stream = real_open(*args, **kwargs)+ if "w" in stream.mode:+ modes_at_open.append(os.fstat(stream.fileno()).st_mode & 0o777)+ return stream++ old_umask = os.umask(0o022)+ try:+ with patch.object(setup_wizard, "_start_device_flow", return_value=(public, handle)), \+ patch.object(setup_wizard, "_device_handle_path", return_value=path), \+ patch("io.open", side_effect=observe_open):+ result = setup_wizard.run_github_start()+ finally:+ os.umask(old_umask)++ assert result == public+ assert modes_at_open == [0o600]+ assert path.stat().st_mode & 0o777 == 0o600+ assert json.loads(path.read_text()) == handle++class TestDeviceIntervalClamp:"""Server-controlled poll intervals are clamped to [1, 30] before use."""