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(http): redact app passwords and session JWTs from recorded fixtures (#1115)
Ilia Alshanetsky52e1f51b4d96
diff --git a/changelog.d/+fixture-credential-redaction.security.md b/changelog.d/+fixture-credential-redaction.security.mdnew file mode 100644index 00000000..ac8402d6--- /dev/null+++ b/changelog.d/+fixture-credential-redaction.security.md@@ -0,0 +1 @@+`--record-fixtures` no longer writes live credentials to disk. Fixture redaction is driven by key name, and the recognized set omitted `password`, `accessJwt`, and `refreshJwt` — so a Bluesky session exchange recorded the app password from the request body and both session JWTs from the response in cleartext, in a file created world-readable. The key set now covers those names, recorded files are created `0600`, and env-derived secret values are scrubbed from responses on this path as they already were for source records.diff --git a/skills/last30days/scripts/lib/http.py b/skills/last30days/scripts/lib/http.pyindex 7c1b5ce3..e67957a1 100644--- a/skills/last30days/scripts/lib/http.py+++ b/skills/last30days/scripts/lib/http.py@@ -146,7 +146,17 @@ _expected_miss_statuses: ContextVar[frozenset[int]] = ContextVar(_FIXTURE_FORMAT = "last30days-http-fixture/v1"_FIXTURE_SECRET_KEYS = frozenset(- {"api_key", "apikey", "authorization", "cookie", "key", "secret", "token"}+ {+ "api_key", "apikey", "authorization", "cookie", "key", "secret", "token",+ "password", "passwd", "passphrase", "credential", "bearer", "jwt",+ }+)+# Suffixes are matched on the normalized key, where camelCase collapses without+# a separator ("accessJwt" -> "accessjwt"), so these are bare rather than+# underscore-prefixed. "key" is deliberately absent: it would redact "monkey".+_FIXTURE_SECRET_KEY_SUFFIXES = (+ "_api_key", "apikey", "_authorization", "_cookie", "_secret", "_token",+ "password", "passwd", "passphrase", "credential", "jwt",)_fixture_lock = threading.Lock()_fixture_state: Optional[dict[str, Any]] = None@@ -161,7 +171,7 @@ def _is_secret_key(value: object) -> bool:key = re.sub(r"[^a-z0-9]+", "_", str(value).lower()).strip("_")return (key in _FIXTURE_SECRET_KEYS- or key.endswith(("_api_key", "_authorization", "_cookie", "_secret", "_token"))+ or key.endswith(_FIXTURE_SECRET_KEY_SUFFIXES))@@ -361,13 +371,26 @@ def recording_requests(path: str | Path):"exchanges": state["exchanges"],"source_exchanges": state["source_exchanges"],}+ # A recorded exchange is credential-adjacent by construction:+ # redaction is key-name driven, so an unrecognized key name leaves+ # a real value on disk. Create the temp file 0600 at open time+ # rather than chmod-ing after the write, or the credentials sit in+ # a world-readable file for the length of the write (the parent+ # directory is caller-supplied and not guaranteed private).+ # Mirrors last30days.save_output. Unlink first so a stale or+ # pre-planted temp file cannot be reused with its own wider mode --+ # O_CREAT does not alter the mode of an existing file.temporary = target.with_name(f".{target.name}.tmp")- temporary.write_text(- json.dumps(payload, indent=2, ensure_ascii=False) + "\n",- encoding="utf-8",+ temporary.unlink(missing_ok=True)+ fd = os.open(+ temporary,+ os.O_CREAT | os.O_EXCL | os.O_WRONLY,+ 0o600,)- if os.name != "nt":- temporary.chmod(0o644)+ with os.fdopen(fd, "w", encoding="utf-8") as handle:+ handle.write(+ json.dumps(payload, indent=2, ensure_ascii=False) + "\n"+ )temporary.replace(target)@@ -456,6 +479,12 @@ def _fixture_record(state = _fixture_stateif state is None or state["mode"] != "record":return+ # Union the session's env-derived secret VALUES in, so a credential+ # echoed back inside an ordinary response field is scrubbed on this+ # path too, not only on the source-record path. Response scrubbing does+ # not feed _fixture_key, so this cannot make a replay key+ # machine-dependent.+ redactions = redactions | (state.get("redactions") or frozenset())response: dict[str, Any]if error is None:response = {"value": _scrub_fixture_value(value, redactions=redactions)}diff --git a/tests/test_http_fixtures.py b/tests/test_http_fixtures.pyindex 5a1076e0..970ade6b 100644--- a/tests/test_http_fixtures.py+++ b/tests/test_http_fixtures.py@@ -1,6 +1,7 @@from __future__ import annotationsimport json+import osfrom unittest.mock import MagicMockimport pytest@@ -59,6 +60,85 @@ def test_http_recording_redacts_credentials_echoed_in_response_values(tmp_path,assert '"echo": "<redacted>"' in fixture_text+def test_http_recording_scrubs_app_password_and_session_jwts(tmp_path, monkeypatch):+ """A Bluesky session exchange puts the app password in the request body and+ both JWTs in the response. Redaction is key-name driven, so every one of+ those names has to be recognized."""+ monkeypatch.setattr(+ http.urllib.request,+ "urlopen",+ lambda *_args, **_kwargs: _response(json.dumps({+ "accessJwt": "eyJhbGciOi.ACCESS-SENTINEL",+ "refreshJwt": "eyJhbGciOi.REFRESH-SENTINEL",+ "handle": "me.bsky.social",+ })),+ )+ fixture_dir = tmp_path / "fixture"++ with http.recording_requests(fixture_dir):+ http.post(+ "https://bsky.social/xrpc/com.atproto.server.createSession",+ json_data={+ "identifier": "me.bsky.social",+ "password": "abcd-efgh-ijkl-SENTINEL",+ },+ )++ fixture_path = fixture_dir / "http.json"+ fixture_text = fixture_path.read_text(encoding="utf-8")+ assert "abcd-efgh-ijkl-SENTINEL" not in fixture_text+ assert "ACCESS-SENTINEL" not in fixture_text+ assert "REFRESH-SENTINEL" not in fixture_text+ # Non-secret fields still round-trip, so the fixture stays useful.+ assert "me.bsky.social" in fixture_text+ # And the file is not world-readable.+ if os.name != "nt":+ assert fixture_path.stat().st_mode & 0o777 == 0o600+++def test_recorded_fixture_is_private_from_creation_not_after_a_chmod(+ tmp_path, monkeypatch+):+ """Tightening the mode after writing leaves the credentials in a+ world-readable file for the length of the write. Assert the temp file is+ opened 0600, since a final-mode check passes either way."""+ monkeypatch.setattr(+ http.urllib.request,+ "urlopen",+ lambda *_args, **_kwargs: _response('{"ok": true}'),+ )+ opened: list[tuple[str, int]] = []+ real_open = os.open++ def _recording_open(path, flags, mode=0o777, **kwargs):+ opened.append((str(path), mode))+ return real_open(path, flags, mode, **kwargs)++ monkeypatch.setattr(http.os, "open", _recording_open)++ fixture_dir = tmp_path / "fixture"+ with http.recording_requests(fixture_dir):+ http.get("https://api.example.test/thing")++ tmp_opens = [+ (path, mode) for path, mode in opened if path.endswith(".http.json.tmp")+ ]+ assert tmp_opens, "the fixture temp file must be created via os.open with a mode"+ assert all(mode == 0o600 for _path, mode in tmp_opens), tmp_opens+++def test_is_secret_key_covers_credential_names_without_over_matching():+ for name in (+ "password", "passwd", "app_password", "BSKY_APP_PASSWORD",+ "accessJwt", "refreshJwt", "jwt", "passphrase", "credential",+ "api_key", "apiKey", "x_api_key", "Authorization", "cookie",+ "secret", "token", "access_token",+ ):+ assert http._is_secret_key(name), name+ for name in ("monkey", "handle", "identifier", "url", "title", "did"):+ assert not http._is_secret_key(name), name++def test_aborted_recording_does_not_overwrite_existing_fixture(tmp_path):fixture = tmp_path / "http.json"fixture.write_text("existing fixture\n", encoding="utf-8")