diff --git a/docs/system-specs/modules/security.md b/docs/system-specs/modules/security.md index 9c707251fbc1..e8db25c5086f 100644 --- a/docs/system-specs/modules/security.md +++ b/docs/system-specs/modules/security.md @@ -1251,8 +1251,16 @@ of its own: the sequencing lives in `redact_with_findings`, which sits beside `redact` and is the single home of the URLs-then-credentials order — redacting a credential first leaves a placeholder inside a URL that the exfiltration matcher then fails to recognise, so that sequencing is a security -property and belongs in one place rather than re-spelled per caller (several -existing callers hand-sequence the reverse order; migrating them is separate). +property and belongs in one place rather than re-spelled per caller. That property +is now gated rather than merely stated: `test_redaction_pair_order.py` refuses a NEW +hand-sequenced reversed pair anywhere under `src/kiro_crew/`, in either spelling (a +two-statement pair, or one nested expression whose inner call runs first), and pins +the sites not yet migrated in a shrink-only baseline so paying one off without +pruning its entry fails. The `dashboard` package's 37 reversed sites compose through +`redact` instead; the remaining 47 pairs in 36 files are the pinned baseline. The one +nested-expression exception, `metrics/schema.py`, is pinned NOT as a defect: it +compares the redacted text against its input only for a boolean, and both orders +answer that boolean identically for every input measured. Two further properties are deliberate: the seam is populated for EVERY app with no permission gating it (it only removes data, and an app refused the seam ships its own regexes instead — a control that looks present and is not), and it carries no diff --git a/src/kiro_crew/dashboard/chat_runner.py b/src/kiro_crew/dashboard/chat_runner.py index 8c14a89101fc..8acedaf2ad23 100644 --- a/src/kiro_crew/dashboard/chat_runner.py +++ b/src/kiro_crew/dashboard/chat_runner.py @@ -887,13 +887,12 @@ def _redact_display_text(text: str) -> str: ``event.title`` prefers the model's own ``description`` field (``_select_tool_title``), so any surface it reaches — a transcript row that is broadcast to the dashboard AND persisted to the ConversationLog, or a - SEL audit ``tool_name`` — must see it only through this helper. Both - redactors return their input unchanged when nothing matches, so clean - titles pass through byte-identical. + SEL audit ``tool_name`` — must see it only through this helper. + ``security.redact`` composes the credential and exfiltration-URL passes + in their fixed order, and each returns its input unchanged when nothing + matches, so clean titles pass through byte-identical. """ - text, _ = redact_exfiltration_urls(text) - text, _ = redact_credentials(text) - return text + return redact(text) async def _surface_agent_welcome( @@ -2144,8 +2143,7 @@ def _redact_acp_string(s: str) -> str: """ if not s: return s - s, _ = redact_credentials(s) - s, _ = redact_exfiltration_urls(s) + s = redact(s) return s @@ -4519,8 +4517,7 @@ def _resolve_prompt_mention( # line operate on exactly what the agent receives. content = _strip_yaml_frontmatter(content) - content, _ = redact_credentials(content) - content, _ = redact_exfiltration_urls(content) + content = redact(content) # Inject SOP as instructions the agent must follow expanded = f"Execute the following instructions:\n\n{content}" @@ -4675,8 +4672,7 @@ def _expand_dollar_skills( names: list[str] = [] snapshots: list[dict[str, str]] = [] for _token, name, body in resolved: - body, _ = redact_credentials(body) - body, _ = redact_exfiltration_urls(body) + body = redact(body) blocks.append(f"[Skill: {name}]\n\n{body}") names.append(name) snapshots.append({"name": name, "body": body}) @@ -6108,8 +6104,7 @@ async def _handle_workflow_command( f"`{started.get('run_id')}` from revision {started.get('revision')}. " "Its result will appear here when it finishes." ) - text, _ = redact_credentials(text) - text, _ = redact_exfiltration_urls(text) + text = redact(text) slot.append("assistant", text, "msg msg-a") sel().log_tool_invocation( session_key=session_key, @@ -9429,8 +9424,7 @@ def _wt_note_approved(event) -> None: # noqa: ANN001 -- ACP event union desc = f" — {p['description']}" if p["description"] else "" lines.append(f"- `@{p['fullName']}`{desc}") text = "\n".join(lines) - text, _ = redact_credentials(text) - text, _ = redact_exfiltration_urls(text) + text = redact(text) slot.append("assistant", text, "msg msg-a") sel().log_tool_invocation( session_key="", @@ -14563,8 +14557,7 @@ async def _refusal_fallback_retry() -> bool: state, slot, "assistant", "🗑️ Conversation cleared.", "msg msg-a" ) elif event.kind == EVENT_AGENT_SWITCHED: - new_agent, _ = redact_credentials(event.text) - new_agent, _ = redact_exfiltration_urls(new_agent) + new_agent = redact(event.text) if new_agent and ( private_member or (slot.mode == "member" and new_agent != slot.agent) ): @@ -15578,8 +15571,7 @@ def _emit_error(msg: str, *, will_retry: bool = False) -> None: logger.info("Deferred compaction result: %s", compaction_result) if compaction_result["type"] == "completed": _restore_skills_context_after_compaction() - summary, _ = redact_credentials(compaction_result.get("summary", "")) - summary, _ = redact_exfiltration_urls(summary) + summary = redact(compaction_result.get("summary", "")) msg = ( f"✅ Conversation compacted: {summary}" if summary @@ -15600,8 +15592,7 @@ def _emit_error(msg: str, *, will_retry: bool = False) -> None: # the text is backend-echoed, so it is not trusted to be # free of credentials or exfiltration URLs even though the # provider already redacts once at its own boundary. - error, _ = redact_credentials(compaction_result.get("summary", "")) - error, _ = redact_exfiltration_urls(error) + error = redact(compaction_result.get("summary", "")) error = error.strip() if len(error) > _COMPACT_FAIL_REASON_MAX_CHARS: # A notice is a one-line receipt, not a log: a provider diff --git a/src/kiro_crew/dashboard/chat_utils.py b/src/kiro_crew/dashboard/chat_utils.py index 6c0bd66bf76a..e44ba97703da 100644 --- a/src/kiro_crew/dashboard/chat_utils.py +++ b/src/kiro_crew/dashboard/chat_utils.py @@ -73,6 +73,7 @@ _exempt_exact_hosts, bounded_blocked_links, oauth_url_contains_credential, + redact, redact_credentials, redact_exfiltration_urls, ) @@ -657,8 +658,7 @@ def _append_compaction_notice(state: DashboardState, slot: _ChatSlot, msg_text: so a future caller passing unredacted LLM-derived text (e.g. a compaction summary) can never leak a credential/exfil URL. Both passes are idempotent. """ - msg_text, _ = redact_credentials(msg_text) - msg_text, _ = redact_exfiltration_urls(msg_text) + msg_text = redact(msg_text) meta = {"kind": "compaction"} append_and_surface( state, slot, "assistant", msg_text, "msg msg-a", meta=meta, extra={"kind": "compaction"} @@ -687,8 +687,7 @@ def _broadcast_compaction_result( if status_type == "completed": slot._compaction_fail_streak = 0 slot._compaction_fail_cooldown_until = 0.0 - summary, _ = redact_credentials(event.title) - summary, _ = redact_exfiltration_urls(summary) + summary = redact(event.title) msg_text = ( f"✅ Conversation compacted: {summary}" if summary else "✅ Conversation compacted." ) @@ -709,8 +708,7 @@ def _broadcast_compaction_result( # once/twice. Nothing new to say — don't spam identical notices. return None - error, _ = redact_credentials(event.title or "unknown error") - error, _ = redact_exfiltration_urls(error) + error = redact(event.title or "unknown error") if streak <= _COMPACTION_NOTICE_SHOW_FIRST_N: msg_text = f"❌ Compaction failed: {error}" else: diff --git a/src/kiro_crew/dashboard/file_api/grep.py b/src/kiro_crew/dashboard/file_api/grep.py index c023c2dcd278..f6f228aad539 100644 --- a/src/kiro_crew/dashboard/file_api/grep.py +++ b/src/kiro_crew/dashboard/file_api/grep.py @@ -139,8 +139,8 @@ def _grep_hit(path: str, line: int, preview: str, label: str = "") -> dict: sensitive-path fence covers credential STORES, not a secret pasted into an ordinary file. Redaction runs BEFORE the cut: half a token matches no pattern. """ - safe, _ = redact_credentials(preview.rstrip("\n")) - safe, _ = redact_exfiltration_urls(safe) + safe, _ = redact_exfiltration_urls(preview.rstrip("\n")) + safe, _ = redact_credentials(safe) hit: dict = { # Segment-wise so two paths that both redact to a tag stay two rows; a # clean path is returned byte-for-byte. @@ -149,8 +149,8 @@ def _grep_hit(path: str, line: int, preview: str, label: str = "") -> dict: "preview": safe[:_GREP_PREVIEW_CHARS], } if label: - safe_label, _ = redact_credentials(label) - safe_label, _ = redact_exfiltration_urls(safe_label) + safe_label, _ = redact_exfiltration_urls(label) + safe_label, _ = redact_credentials(safe_label) hit["label"] = safe_label[:_GREP_LABEL_CHARS] return hit diff --git a/src/kiro_crew/dashboard/handlers/artifacts.py b/src/kiro_crew/dashboard/handlers/artifacts.py index 60a5c486de2f..17a63b15ff3a 100644 --- a/src/kiro_crew/dashboard/handlers/artifacts.py +++ b/src/kiro_crew/dashboard/handlers/artifacts.py @@ -89,6 +89,7 @@ _B64_CHUNK_RE, _HARD_CREDENTIAL_RE, is_sensitive_path, + redact, redact_credentials, redact_exfiltration_urls, ) @@ -836,8 +837,7 @@ def _scan_session_docs( """ def _redact(text: str) -> str: - cleaned, _ = redact_credentials(text or "") - cleaned, _ = redact_exfiltration_urls(cleaned) + cleaned = redact(text or "") return cleaned out = _collect_session_docs(conversation_log, saved_map, session_key) @@ -3820,8 +3820,7 @@ def work() -> list[dict[str, Any]]: try: docs = await _run_off_loop(work) except Exception as exc: # noqa: BLE001 — audit + redacted 500 on any scan/list failure - _rc, _ = redact_credentials(str(exc)) - safe_err, _ = redact_exfiltration_urls(_rc) + safe_err = redact(str(exc)) _audit( tool="artifact_session_docs", request=request, @@ -3868,8 +3867,7 @@ async def api_artifact_materialize(request: web.Request) -> web.Response: path = path.strip() # Redacted copy for audit/error metadata — never emit a raw (LLM-influenced) # path into the SEL audit log (credential/exfiltration redaction rule). - _rc, _ = redact_credentials(path) - audit_path, _ = redact_exfiltration_urls(_rc) + audit_path = redact(path) clog = getattr(state, "conversation_log", None) if clog is None: _audit( diff --git a/src/kiro_crew/dashboard/handlers/files.py b/src/kiro_crew/dashboard/handlers/files.py index 66116b95b987..cbd7cbc8279f 100644 --- a/src/kiro_crew/dashboard/handlers/files.py +++ b/src/kiro_crew/dashboard/handlers/files.py @@ -1371,8 +1371,7 @@ async def api_slack_upload_file(request: web.Request) -> web.Response: # A Slack SDK / network exception can carry file paths, host and URL # fragments, or credentials embedded in a URL. Sanitize before it # reaches the client or the audit record (see api_slack_pins). - safe_error, _ = redact_credentials(str(e)) - safe_error, _ = redact_exfiltration_urls(safe_error) + safe_error = redact(str(e)) _audit_file_send(leg="slack", outcome="error", downstream="slack", error=safe_error) return web.json_response({"error": safe_error}, status=500) @@ -1457,8 +1456,7 @@ def _skip(reason: str) -> web.Response: # A transport / network exception can carry file paths, host and URL # fragments, or credentials embedded in a URL. Sanitize before it # reaches the client or the audit record (see api_slack_upload_file). - safe_error, _ = redact_credentials(str(e)) - safe_error, _ = redact_exfiltration_urls(safe_error) + safe_error = redact(str(e)) _audit_file_send( leg="channel", outcome="error", diff --git a/src/kiro_crew/dashboard/handlers/mcp.py b/src/kiro_crew/dashboard/handlers/mcp.py index 4191c4ce3041..38e19f233e11 100644 --- a/src/kiro_crew/dashboard/handlers/mcp.py +++ b/src/kiro_crew/dashboard/handlers/mcp.py @@ -71,7 +71,7 @@ mcp_server_alias, ) from kiro_crew.platform.governance import may_skip_gate_now -from kiro_crew.security import redact_credentials, redact_exfiltration_urls +from kiro_crew.security import redact, redact_credentials, redact_exfiltration_urls from kiro_crew.sel import sel from kiro_crew.user_json import loads_mcp_config, loads_user_json @@ -1141,8 +1141,7 @@ async def api_mcp_servers(request: web.Request) -> web.Response: _stamp_config_state(d, global_mcps, kirocrew_mcps) err = d.get("error") if err: - err, _ = redact_credentials(err) - err, _ = redact_exfiltration_urls(err) + err = redact(err) d["error"] = err result.append(d) # Annotated HERE too, not only on the probe endpoints. This is the endpoint diff --git a/src/kiro_crew/dashboard/handlers/messaging.py b/src/kiro_crew/dashboard/handlers/messaging.py index a0badc5dfb6f..6dae4a48f065 100644 --- a/src/kiro_crew/dashboard/handlers/messaging.py +++ b/src/kiro_crew/dashboard/handlers/messaging.py @@ -257,6 +257,7 @@ from kiro_crew.platform_compat import IS_MACOS # noqa: F401 from kiro_crew.security import ( # noqa: F401 is_sensitive_path, + redact, redact_credentials, redact_exfiltration_urls, ) @@ -861,7 +862,11 @@ async def api_send_message(request: web.Request) -> web.Response: and the response are built in ``messaging_api.proactive_send``. The redaction and authorization of the Slack target and the origin-session injection stay here. """ - from kiro_crew.security import redact_credentials, redact_exfiltration_urls # noqa: F811 + from kiro_crew.security import ( # noqa: F811 + redact, + redact_credentials, + redact_exfiltration_urls, + ) from kiro_crew.slack.handler import is_allowed_user, is_tracked_channel # noqa: F811 from kiro_crew.validation import USER_ID_RE # noqa: F811 @@ -885,11 +890,9 @@ async def api_send_message(request: web.Request) -> web.Response: # Redact after format validation if target_channel: - target_channel, _ = redact_exfiltration_urls(target_channel) - target_channel, _ = redact_credentials(target_channel) + target_channel = redact(target_channel) if target_user: - target_user, _ = redact_exfiltration_urls(target_user) - target_user, _ = redact_credentials(target_user) + target_user = redact(target_user) # Sanitize LLM-generated content before any external surface. # This covers all downstream paths (session injection, fallback, Slack, @@ -1047,8 +1050,7 @@ async def api_send_message(request: web.Request) -> web.Response: ) if slot: label = job_name or "cron" - label, _ = redact_exfiltration_urls(label) - label, _ = redact_credentials(label) + label = redact(label) # text and title already redacted above # Text wrapper kept for LLM context and queue detection; # cronLabel in cls JSON provides structured data for frontend. @@ -1213,8 +1215,7 @@ async def api_slack_pins(request: web.Request) -> web.Response: # it to the caller (same output contract as send_message). pins = await slack.list_pins(channel) for pin in pins: - safe_text, _ = redact_credentials(pin.get("text", "")) - safe_text, _ = redact_exfiltration_urls(safe_text) + safe_text = redact(pin.get("text", "")) pin["text"] = safe_text result["pins"] = pins _sel().log_tool_invocation( @@ -1228,8 +1229,7 @@ async def api_slack_pins(request: web.Request) -> web.Response: ) return web.json_response(result) except Exception as e: - safe_error, _ = redact_credentials(str(e)) - safe_error, _ = redact_exfiltration_urls(safe_error) + safe_error = redact(str(e)) _sel().log_tool_invocation( session_key="api", source="api", @@ -1316,8 +1316,7 @@ async def api_slack_reactions(request: web.Request) -> web.Response: ) return web.json_response({"ok": True}) except Exception as e: - safe_error, _ = redact_credentials(str(e)) - safe_error, _ = redact_exfiltration_urls(safe_error) + safe_error = redact(str(e)) _sel().log_tool_invocation( session_key="api", source="api", @@ -1353,7 +1352,7 @@ def _missing_scope_message(needed: str) -> str: async def api_slack_profile(request: web.Request) -> web.Response: """POST /api/slack-profile — read a Slack user's profile.""" - from kiro_crew.security import redact_credentials, redact_exfiltration_urls # noqa: F811 + from kiro_crew.security import redact # noqa: F811 from kiro_crew.validation import USER_ID_RE # noqa: F811 state: DashboardState = request.app["state"] @@ -1371,8 +1370,7 @@ async def api_slack_profile(request: web.Request) -> web.Response: # Validate format first, then redact (#2) if not USER_ID_RE.match(user_id): return web.json_response({"error": "invalid user ID format"}, status=400) - user_id, _ = redact_exfiltration_urls(user_id) - user_id, _ = redact_credentials(user_id) + user_id = redact(user_id) # Authorization first (deny-by-default) — reject before any side effects from kiro_crew.slack.handler import is_allowed_user # noqa: F811 @@ -1436,8 +1434,7 @@ async def api_slack_profile(request: web.Request) -> web.Response: downstream_service="slack", resources=f"user={user_id} reason=missing_scope needed={needed}", ) - needed, _ = redact_credentials(needed) - needed, _ = redact_exfiltration_urls(needed) + needed = redact(needed) return web.json_response({"error": _missing_scope_message(needed)}, status=403) logger.exception("slack-profile: failed for %s", user_id) _sel().log_tool_invocation( @@ -1453,8 +1450,7 @@ async def api_slack_profile(request: web.Request) -> web.Response: for key in list(profile): val = profile[key] if isinstance(val, str) and key not in ("id",): - val, _ = redact_exfiltration_urls(val) - val, _ = redact_credentials(val) + val = redact(val) profile[key] = val _sel().log_tool_invocation( diff --git a/src/kiro_crew/dashboard/handlers/prompts.py b/src/kiro_crew/dashboard/handlers/prompts.py index 8f27e60394e5..f1a0b59411d5 100644 --- a/src/kiro_crew/dashboard/handlers/prompts.py +++ b/src/kiro_crew/dashboard/handlers/prompts.py @@ -34,7 +34,7 @@ ) from kiro_crew.loop_lock import LoopBoundLock from kiro_crew.platform_compat import is_link_or_junction -from kiro_crew.security import redact_credentials, redact_exfiltration_urls +from kiro_crew.security import redact, redact_with_findings from kiro_crew.skill_trust import ReviewedProjectChanged as _ReviewedProjectChanged from kiro_crew.skill_trust import ( TrustStoreFull, @@ -678,8 +678,7 @@ def _local_prompt_entry(name: str, project_dir: Path | None) -> dict[str, Any] | def _redact_prompt(p: dict[str, Any]) -> None: """Redact credential patterns and exfiltration URLs from prompt metadata.""" for field in ("description", "path"): - p[field], _ = redact_credentials(p[field]) - p[field], _ = redact_exfiltration_urls(p[field]) + p[field] = redact(p[field]) async def api_prompts(request: web.Request) -> web.Response: @@ -897,8 +896,7 @@ def _resolve_and_read() -> tuple[dict[str, Any] | None, str, str]: # given, so a redacted copy must never be offered as an edit base: saving it # would replace the real token with the redaction marker. The write path # cannot detect that after the fact, so the read path says so here. - content, cred_hits = redact_credentials(content) - content, url_hits = redact_exfiltration_urls(content) + content, cred_hits, url_hits = redact_with_findings(content) out = dict(p) _redact_prompt(out) # Strip full filesystem path — return display-only relative path @@ -1772,8 +1770,7 @@ def _read() -> tuple[str | None, str, str | None, str, bool, str]: {"error": "file too large", "code": "content_too_large"}, status=413 ) body = content or "" - body, cred_hits = redact_credentials(body) - body, url_hits = redact_exfiltration_urls(body) + body, cred_hits, url_hits = redact_with_findings(body) # Metadata gets the same treatment as the unscoped branch: a description read # out of frontmatter is file content too, and can carry a token. out = { diff --git a/src/kiro_crew/dashboard/handlers/steering.py b/src/kiro_crew/dashboard/handlers/steering.py index 8a60027a3195..03925df4442d 100644 --- a/src/kiro_crew/dashboard/handlers/steering.py +++ b/src/kiro_crew/dashboard/handlers/steering.py @@ -54,8 +54,7 @@ from kiro_crew.security import ( is_sensitive_path, is_sensitive_write_path, - redact_credentials, - redact_exfiltration_urls, + redact, ) from ._shared import ( @@ -188,8 +187,7 @@ def _redact_meta(text: str) -> str: detail response is what the textarea saves back, so a redaction there would overwrite the user's own file with ``[REDACTED]`` markers. """ - out, _ = redact_credentials(text) - out, _ = redact_exfiltration_urls(out) + out = redact(text) return out diff --git a/src/kiro_crew/dashboard/handlers/updates.py b/src/kiro_crew/dashboard/handlers/updates.py index 2dfd833262da..39e6a1fbae8e 100644 --- a/src/kiro_crew/dashboard/handlers/updates.py +++ b/src/kiro_crew/dashboard/handlers/updates.py @@ -88,7 +88,7 @@ reexec_python_module, ) from kiro_crew.safety_override import flush_breadcrumb_writes -from kiro_crew.security import redact_credentials, redact_exfiltration_urls +from kiro_crew.security import redact logger = logging.getLogger(__name__) @@ -1611,8 +1611,7 @@ async def _build_frontend(proj: str, state: DashboardState) -> None: from kiro_crew import frontend def _push(step: str, msg: str) -> None: - msg, _ = redact_credentials(msg) - msg, _ = redact_exfiltration_urls(msg) + msg = redact(msg) # frontend emits ("warning", detail); show it as a non-fatal build note. state.push_update_progress("building", msg) @@ -1644,8 +1643,7 @@ async def _venv_pip_install(proj: str, state: DashboardState) -> bool: def _publish(step: str, message: str) -> None: # dep_sync hands pip's output over raw; this is the surface that publishes # it, so redaction and the length cap belong here. - message, _ = redact_credentials(message) - message, _ = redact_exfiltration_urls(message) + message = redact(message) if len(message) > 1000: message = message[:1000] + "\n…(truncated)" state.push_update_progress(step, message) diff --git a/src/kiro_crew/dashboard/messaging_api/proactive_send.py b/src/kiro_crew/dashboard/messaging_api/proactive_send.py index 063aa614abcf..c0ca2db3a683 100644 --- a/src/kiro_crew/dashboard/messaging_api/proactive_send.py +++ b/src/kiro_crew/dashboard/messaging_api/proactive_send.py @@ -59,8 +59,9 @@ def _redact_all(value: str) -> str: ``redact_for_display`` re-runs its redactor over each normalised form, so it needs the pair behind a single call rather than two sequential passes. """ - value, _ = redact_exfiltration_urls(value) - value, _ = redact_credentials(value) + from kiro_crew.security import redact # noqa: F811 + + value = redact(value) return value @@ -268,6 +269,8 @@ async def _resolve_session_link_url( async def api_delete_message(request: web.Request) -> web.Response: """POST /api/delete-message — delete a bot-authored Slack message.""" + from kiro_crew.security import redact # noqa: F811 + state: DashboardState = request.app["state"] try: body = await request.json() @@ -284,8 +287,7 @@ async def api_delete_message(request: web.Request) -> web.Response: await slack.delete_message(channel, ts) except Exception as e: safe_error = str(e).split("\n")[0][:200] - safe_error, _ = redact_credentials(safe_error) - safe_error, _ = redact_exfiltration_urls(safe_error) + safe_error = redact(safe_error) return web.json_response({"error": f"Delete failed: {safe_error}"}, status=502) return web.json_response({"ok": True}) @@ -314,6 +316,8 @@ async def api_update_message(request: web.Request) -> web.Response: message that is already there, so a wrong audience is not merely a new message they can ignore. """ + from kiro_crew.security import redact # noqa: F811 + # circular import: slack.handler imports from dashboard.* at module load from kiro_crew.slack.handler import is_tracked_channel # noqa: F811 @@ -433,8 +437,7 @@ async def api_update_message(request: web.Request) -> web.Response: await slack.update_message(channel, ts, text, blocks) except Exception as e: safe_error = str(e).split("\n")[0][:200] - safe_error, _ = redact_credentials(safe_error) - safe_error, _ = redact_exfiltration_urls(safe_error) + safe_error = redact(safe_error) return web.json_response( {"error": f"Update failed: {safe_error}", "code": "update_failed"}, status=502 ) @@ -756,7 +759,7 @@ async def _deliver_send_message_fallback( Each leg records what it reached on *outcome* as it goes. """ - from kiro_crew.security import redact_credentials, redact_exfiltration_urls # noqa: F811 + from kiro_crew.security import redact # noqa: F811 # Snapshot before the suffix below: that sentence describes the BELL's # delivery, and a channel post is a real delivery, not the @@ -766,8 +769,7 @@ async def _deliver_send_message_fallback( # path for one, not an edge case. channel_text = text if target_session and job_name: - safe_name, _ = redact_exfiltration_urls(job_name) - safe_name, _ = redact_credentials(safe_name) + safe_name = redact(job_name) title = f"⏰ {safe_name}" text += "\n\n_(session closed — delivered as notification)_" state.notify("agent", title, text) @@ -1106,11 +1108,10 @@ def _send_message_response( channel_type: str, ) -> web.Response: """The answer to a send whose legs have run: a failure, or where it was delivered.""" - from kiro_crew.security import redact_credentials, redact_exfiltration_urls # noqa: F811 + from kiro_crew.security import redact # noqa: F811 if outcome.channel_code: - safe_detail, _ = redact_credentials(outcome.channel_detail) - safe_detail, _ = redact_exfiltration_urls(safe_detail) + safe_detail = redact(outcome.channel_detail) detail = f"{channel_target} delivery failed: {safe_detail}" # Both responses are spelled out inline, with a literal status and a # literal body, rather than sharing a hoisted dict or computing the @@ -1127,8 +1128,7 @@ def _send_message_response( {"ok": False, "error": detail, "code": outcome.channel_code}, status=502 ) if outcome.slack_attempted and not outcome.sent_slack: - safe_error, _ = redact_credentials(outcome.slack_error) - safe_error, _ = redact_exfiltration_urls(safe_error) + safe_error = redact(outcome.slack_error) return web.json_response( {"ok": False, "error": f"Slack delivery failed: {safe_error}", "slack": False}, status=502, diff --git a/src/kiro_crew/dashboard/state.py b/src/kiro_crew/dashboard/state.py index 4226ecbc3f24..1f504041fcca 100644 --- a/src/kiro_crew/dashboard/state.py +++ b/src/kiro_crew/dashboard/state.py @@ -114,7 +114,7 @@ from kiro_crew.preview_text import strip_markdown_preview from kiro_crew.release_channel import channel as _release_channel_of_build from kiro_crew.safety_override import cached_disabled_approval_modes, safety_override -from kiro_crew.security import redact_credentials, redact_exfiltration_urls +from kiro_crew.security import redact, redact_credentials, redact_exfiltration_urls from kiro_crew.security.credential_sources import CredentialEvidence from kiro_crew.sel import sel from kiro_crew.session_compaction import ( @@ -770,8 +770,7 @@ def _redacted_link_target(target: str | None) -> str: """Return a non-sensitive tail hint, never a raw conversation id.""" if not target: return "…" - safe, _ = redact_exfiltration_urls(target) - safe, _ = redact_credentials(safe) + safe = redact(target) if safe != target: return "…redacted" if len(safe) <= 6: @@ -1034,8 +1033,7 @@ def _log_task_exception(task: asyncio.Task[Any]) -> None: if exc is not None: try: tb = "".join(traceback.format_exception(type(exc), exc, exc.__traceback__)) - redacted_tb, _ = redact_credentials(tb) - redacted_tb, _ = redact_exfiltration_urls(redacted_tb) + redacted_tb = redact(tb) logger.error("Background task failed:\n%s", redacted_tb) except Exception as redaction_err: # Include the redaction failure class so bugs in the redactor are visible, @@ -1069,8 +1067,7 @@ def parse_cls_meta(cls_val: str) -> dict | None: # Defence-in-depth: sanitize LLM-controlled content at every read boundary if isinstance(meta.get("tool_input"), str): - sanitized, _ = redact_exfiltration_urls(meta["tool_input"]) - sanitized, _ = redact_credentials(sanitized) + sanitized = redact(meta["tool_input"]) meta["tool_input"] = sanitized # Normalize: backend stores as request_id, frontend expects approval_id @@ -2340,8 +2337,7 @@ def build_infra_retry_prompt(error_class: str, retry_after_secs: float | None) - def _redact(text: str) -> str: """Sanitise LLM output before surfacing to dashboard.""" - text, _ = redact_exfiltration_urls(text) - text, _ = redact_credentials(text) + text = redact(text) return text @@ -7731,8 +7727,7 @@ def get_or_create_slot( # are dashboard-surfaced, so apply the same redaction as explicit # title pinning in api_chat_slot_create. ``_titled`` stays False — # auto-title and explicit pinning can still override. - pretty_title, _ = redact_exfiltration_urls(requested_name) - pretty_title, _ = redact_credentials(pretty_title) + pretty_title = redact(requested_name) slot.title = pretty_title slot._tab_id = uuid.uuid4().hex[:12] slot._on_message = self._broadcast_chat_message @@ -8039,8 +8034,7 @@ def _record_member_row(self, slot_key: str, msg: dict) -> None: # (`member_message_payload` -> `speech_preview`) so the folded # preview equals what `GET /api/members` reads back. def _sanitize_preview(text: str) -> str: - text, _ = redact_exfiltration_urls(text) - text, _ = redact_credentials(text) + text = redact(text) return text _payload = eventlog_hooks.member_message_payload( @@ -10444,8 +10438,7 @@ def _redact_note_value(value: Any) -> Any: if isinstance(value, str): if not value: return value - value, _ = redact_exfiltration_urls(value) - value, _ = redact_credentials(value) + value = redact(value) return value if isinstance(value, list): return [_redact_note_value(item) for item in value] diff --git a/src/kiro_crew/dashboard/websocket_hub.py b/src/kiro_crew/dashboard/websocket_hub.py index 8b5d50dc08a7..85b0fd6d8413 100644 --- a/src/kiro_crew/dashboard/websocket_hub.py +++ b/src/kiro_crew/dashboard/websocket_hub.py @@ -485,8 +485,11 @@ def broadcast_browser_event(self, event_type: str, data: dict[str, Any]) -> None safe_data: dict[str, Any] = {} for key, value in data.items(): if isinstance(value, str): - value, _ = redact_credentials(value) - value, _ = redact_exfiltration_urls(value) + # URL pass FIRST: it matches whole token-bearing URLs, and a credential + # replaced ahead of it destroys the pattern this pass keys on, leaving + # the destination standing. Both redactors stay the injected providers + # so a loaded companion's extra patterns still apply. + value = redact_credentials(redact_exfiltration_urls(value)[0])[0] safe_data[key] = value payload: dict[str, Any] = { "type": "browser_event", diff --git a/test/test_cse_2026_08_07_fixes.py b/test/test_cse_2026_08_07_fixes.py index 88b0afbb1c49..8d753b09fb7d 100644 --- a/test/test_cse_2026_08_07_fixes.py +++ b/test/test_cse_2026_08_07_fixes.py @@ -390,6 +390,18 @@ def test_upload_failure_is_redacted_in_body_and_audit(self): assert "error=str(e)" not in block def test_helpers_are_imported(self): + """The handler composes through the shared helper rather than its own copy. + + ``files.py`` gets its whole-string redactor as the context-aware + ``redact`` shim (``platform.redact_via_context``), so a loaded companion's + extra patterns apply. The two raw passes stay bound on the facade for the + owner modules that run on its globals (the composition contract pins every + base name), while the handler's own redaction composes through the + helper instead of hand-sequencing them: a hand-written pair is what + the shared composition exists to replace, and writing one here would + reintroduce the ordering hazard. + """ from kiro_crew.dashboard.handlers import files as files_mod + assert callable(files_mod.redact) assert callable(files_mod.redact_credentials) assert callable(files_mod.redact_exfiltration_urls) diff --git a/test/test_heartbeat_prompt_deliver.py b/test/test_heartbeat_prompt_deliver.py index 7607a1138458..aa9527affc25 100644 --- a/test/test_heartbeat_prompt_deliver.py +++ b/test/test_heartbeat_prompt_deliver.py @@ -340,7 +340,12 @@ async def leaky(): @pytest.mark.asyncio() async def test_redaction_failure_falls_back_to_raw(self, caplog, monkeypatch): - """If redact_credentials raises, fallback logs exc type+message unredacted.""" + """If the redactor raises, fallback logs exc type+message unredacted. + + The subject is the redactor ``_log_task_exception`` actually calls, which + is the shared composition rather than either pass on its own, so patching + that is what makes the failure path reachable. + """ import asyncio import logging @@ -348,7 +353,7 @@ async def test_redaction_failure_falls_back_to_raw(self, caplog, monkeypatch): from kiro_crew.dashboard.state import _log_task_exception caplog.set_level(logging.ERROR) - monkeypatch.setattr(state_mod, "redact_credentials", lambda _tb: (_ for _ in ()).throw(ValueError("boom"))) + monkeypatch.setattr(state_mod, "redact", lambda _tb: (_ for _ in ()).throw(ValueError("boom"))) async def failing(): raise RuntimeError("original error") diff --git a/test/test_redaction_pair_order.py b/test/test_redaction_pair_order.py new file mode 100644 index 000000000000..c3f6974e56e1 --- /dev/null +++ b/test/test_redaction_pair_order.py @@ -0,0 +1,656 @@ +"""GATE — the credential and exfiltration-URL passes are composed, never hand-ordered. + +``kiro_crew.security.redact`` composes the two egress passes in one place, and its +own docstring states why the order is fixed: ``redact_exfiltration_urls`` matches +whole **token-bearing** URLs, so a credential replaced ahead of it leaves a +placeholder inside the URL and the URL pass fails to recognise the shape it exists +to catch. The credential is removed either way; what the reversed order loses is +suppression of the DESTINATION, which is the half a reader acts on. + +A hand-sequenced pair is a silent leak, so the ``dashboard`` package's 37 reversed +sites compose through the helper and the remaining 47 pairs in 36 files are pinned +below rather than migrated in one change: the rule is enforced from today while the +sweep stays a separate review. + +## What this pins + +1. **The behaviour the order decides** — asserted on the shared helper, so the + rule cannot become decorative if someone later "simplifies" a pass. +2. **No NEW reversed pair** in ``src/kiro_crew``, in either spelling (a + two-statement pair, or one nested expression). +3. **No stale baseline entry.** A pinned site that has been paid off must be + pruned here, or the list stops shrinking and starts lying. +4. **The scan itself is sound** — probes below fail if a spelling stops being + recognised, and one asserts the walk really reads files rather than passing + forever on an empty set. + +## When this goes RED + +You wrote a reversed pair. Compose it through ``redact`` (or +``redact_with_findings`` when you need the warning lists) — do not relax the rule. +If you are paying off a pinned site, delete its entry and lower its count. + +## Nested order, which is easy to get backwards + +The INNERMOST call runs first, so: + + redact_credentials(redact_exfiltration_urls(x)) # CORRECT — URL pass first + redact_exfiltration_urls(redact_credentials(x)) # REVERSED — the leak + +``_inline_reversed`` looks only for the second spelling. + +## Spellings, and which ones the scan resolves + +Both passes are reachable three ways in this tree, so the scan resolves all three: +a bare name (``redact_credentials(x)``), a qualified attribute chain +(``security.redact_credentials(x)``, the form the app packages use), and a +subscript (``redact_credentials(x)[0]``). An attribute chain is credited to a pass +only when its root is ``security``/``kiro_crew``/``self`` or it is a single bare +attribute, so another object that happens to share the method name is not read as +ours. An IMPORT ALIAS +(``from kiro_crew.security import redact_credentials as _redact_credentials``) is +deliberately NOT resolved: doing so means binding names to their imports, and a +half-built alias resolver is worse than a documented limit. That shape is +uncommon — ``workflows/runner.py`` is the one site — and it composes in the correct +order, so nothing is hidden behind it today. +""" + +from __future__ import annotations + +import ast +import collections +from pathlib import Path + +import pytest + +from kiro_crew.security import redact +from kiro_crew.security.exfil import redact_exfiltration_urls +from kiro_crew.security.redaction import redact_credentials + +# One xdist worker for the whole module: several tests derive from ONE walk of +# src/, and under `--dist loadgroup` an unmarked module is spread across workers, +# so each worker re-pays that walk. +pytestmark = pytest.mark.xdist_group(name="tree_scan_test_redaction_pair_order") + +ROOT = Path(__file__).resolve().parents[1] +SRC = ROOT / "src" / "kiro_crew" + +CREDENTIAL = "redact_credentials" +URL_PASS = "redact_exfiltration_urls" + +#: A URL carrying the credential in its query: the shape the URL pass keys on, and +#: the shape a credential-first order destroys. +_URL_WITH_CREDENTIAL = "see https://evil.example.com/collect?token=AKIAIOSFODNN7EXAMPLE&x=1 now" + +#: Files still holding a reversed TWO-STATEMENT pair, with how many. Pinned rather +#: than migrated in this change: the fix and the sweep review better separately. +#: Keyed by path and COUNT rather than line number, so an unrelated edit above a +#: pinned site does not red this gate -- what is pinned is the obligation, not a +#: coordinate that moves. +_PINNED_TWO_LINE: dict[str, int] = { + "src/kiro_crew/acp/client.py": 3, + "src/kiro_crew/acp/runtime.py": 1, + "src/kiro_crew/apps/builtins/auto_research/campaign/untrusted.py": 1, + "src/kiro_crew/apps/builtins/aws_control/backend/accounts.py": 1, + "src/kiro_crew/apps/builtins/aws_control/backend/backup_parts/egress_text.py": 1, + "src/kiro_crew/apps/builtins/aws_control/backend/library.py": 2, + "src/kiro_crew/apps/builtins/aws_control/backend/routes.py": 2, + "src/kiro_crew/apps/builtins/aws_control/backend/storage.py": 2, + "src/kiro_crew/apps/builtins/dev_fleet/runtime.py": 1, + "src/kiro_crew/apps/builtins/md_notebook/server.py": 1, + "src/kiro_crew/apps/builtins/mochi/activity_log.py": 1, + "src/kiro_crew/apps/builtins/mochi/redact.py": 1, + "src/kiro_crew/apps/event_bus.py": 1, + "src/kiro_crew/apps/job_routes.py": 1, + "src/kiro_crew/apps/job_sdk.py": 1, + "src/kiro_crew/apps/routes.py": 1, + "src/kiro_crew/aws_consent.py": 1, + "src/kiro_crew/channel.py": 1, + "src/kiro_crew/decisions/points/memory_recall.py": 1, + "src/kiro_crew/decisions/points/message_steer.py": 1, + "src/kiro_crew/deploy/engine.py": 1, + "src/kiro_crew/deploy/handlers.py": 1, + "src/kiro_crew/deploy/iam.py": 1, + "src/kiro_crew/deploy/profiles.py": 1, + "src/kiro_crew/deploy/scan.py": 1, + "src/kiro_crew/discord/transport_dispatch.py": 1, + "src/kiro_crew/knowledge/agent_source.py": 1, + "src/kiro_crew/knowledge/artifact_ingest.py": 1, + "src/kiro_crew/knowledge/ingestion.py": 1, + "src/kiro_crew/mcp_discovery.py": 1, + "src/kiro_crew/mcp_tools/knowledge.py": 2, + "src/kiro_crew/snapshot_redact.py": 1, +} + +#: Files holding a reversed NESTED expression. ``metrics/schema.py`` is here on +#: purpose and is NOT a defect: it compares the redacted text against the input for +#: a boolean (``... != value``), and both orders answer that boolean the same way +#: for every input measured -- the credential is removed either way, which is all +#: that call site asks. Pinned so a second one cannot appear unnoticed, and so the +#: entry survives if that call site ever starts using the text itself instead of +#: only the comparison. +_PINNED_INLINE: dict[str, int] = { + "src/kiro_crew/metrics/schema.py": 1, +} + + +class TestTheOrderIsWhatDecidesTheOutcome: + """The leak the shared helper prevents, asserted on the helper itself.""" + + def test_the_canonical_order_suppresses_the_destination(self) -> None: + out = redact(_URL_WITH_CREDENTIAL) + assert "https://" not in out + # The host is NAMED INSIDE the tag, which is why "host absent" would be the + # wrong assertion: what matters is that no live, clickable URL survives. + assert "evil.example.com" in out + + def test_the_reversed_order_leaves_a_live_url_standing(self) -> None: + """The failure mode, reproduced deliberately so the rule is not vacuous. + + The credential is gone either way -- which is exactly why the bug reads as + harmless -- and what survives is a clickable exfiltration endpoint. + """ + text, _ = redact_credentials(_URL_WITH_CREDENTIAL) + text, _ = redact_exfiltration_urls(text) + assert "https://evil.example.com/collect" in text + assert "[REDACTED: credential]" in text + + def test_the_two_orders_disagree_on_a_url_bearing_credential(self) -> None: + """The premise: the passes are not commutative on this input.""" + text, _ = redact_credentials(_URL_WITH_CREDENTIAL) + text, _ = redact_exfiltration_urls(text) + assert text != redact(_URL_WITH_CREDENTIAL) + + def test_the_orders_agree_when_no_url_carries_the_credential(self) -> None: + """Negative control: the difference is URL-specific, not a general split. + + Without this, "the orders differ" could be satisfied by any divergence, and + the rule would tell a future reader nothing about WHEN it matters. + """ + plain = "plain AKIAIOSFODNN7EXAMPLE token, no url" + text, _ = redact_credentials(plain) + text, _ = redact_exfiltration_urls(text) + assert text == redact(plain) + + def test_the_helper_matches_the_canonical_manual_composition(self) -> None: + """``redact`` IS the URL-then-credential composition, not a third variant. + + Corpus-wide rather than one fixture: a change that made ``redact`` compose + something else would have to diverge here before it could diverge silently + at 400+ call sites. + """ + corpus = [ + "", + "plain text", + "key AKIAIOSFODNN7EXAMPLE here", + "secret wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", + "ghp_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", + _URL_WITH_CREDENTIAL, + "https://evil.example.com/plain", + "Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIn0.abcdefghijklmnopqrstuvwxyz0", + "aws_secret_access_key=BAREsecret1", + '{"token":"AKIAIOSFODNN7EXAMPLE","url":"https://exfil.test/?q=1"}', + ] + for text in corpus: + manual, _ = redact_exfiltration_urls(text) + manual, _ = redact_credentials(manual) + assert redact(text) == manual, text + + +class TestNoNewReversedPair: + """The ratchet over the real tree.""" + + @pytest.mark.timeout(600) + def test_two_line_scan_matches_the_pinned_baseline(self) -> None: + found = _count(_two_line_offenders()) + assert found == _PINNED_TWO_LINE, _diff_message(found, _PINNED_TWO_LINE) + + @pytest.mark.timeout(600) + def test_inline_scan_matches_the_pinned_baseline(self) -> None: + found = _count(_inline_reversed()) + assert found == _PINNED_INLINE, _diff_message(found, _PINNED_INLINE) + + +def _count(offenders: list[tuple[str, int]]) -> dict[str, int]: + return dict(collections.Counter(path for path, _ in offenders)) + + +def _diff_message(found: dict[str, int], pinned: dict[str, int]) -> str: + new = sorted(set(found) - set(pinned)) + grown = sorted(p for p in found if p in pinned and found[p] > pinned[p]) + stale = sorted(p for p in pinned if p not in found or found[p] < pinned[p]) + lines = ["reversed redaction pair(s) scanned against the pinned baseline:"] + if new or grown: + lines.append(" NEW (compose these through `redact`, or `redact_with_findings`):") + lines += [" %s (%d)" % (p, found[p]) for p in new + grown] + if stale: + lines.append(" PAID OFF (prune these entries; the list must keep shrinking):") + lines += [" %s (pinned %d, found %d)" % (p, pinned[p], found.get(p, 0)) for p in stale] + return "\n".join(lines) + + +# --------------------------------------------------------------------------- +# scan +# --------------------------------------------------------------------------- + + +def _source_files() -> list[Path]: + return sorted( + p for p in SRC.rglob("*.py") if "__pycache__" not in p.parts and "_vendor" not in p.parts + ) + + +#: The two pass names, as the fast reject in :func:`_call_name`. +_PASS_NAMES = frozenset({CREDENTIAL, URL_PASS}) + +#: Module tails an alias must resolve to for a qualified call to be credited. The +#: facade (``kiro_crew.security``) owns the passes, and ``dashboard.handlers`` +#: re-exports the SAME function objects — five real sites reach them as +#: ``_h.`` via `import kiro_crew.dashboard.handlers as _h`. Verified by +#: identity, not by name: `_h.redact_credentials is security.redact_credentials`. +_SECURITY_MODULES = frozenset({"security", "handlers"}) + +#: Qualifier names credited WITHOUT an import lookup, because they are the module's +#: own spelling in place. +_SECURITY_QUALIFIERS = frozenset({"security"}) + + +def _alias_targets(tree: ast.AST) -> dict[str, str]: + """Map a local module alias to the dotted module it was imported from. + + Only ``import a.b.c as x`` and ``from a.b import c as x`` qualify. Resolving the + alias is what separates a legitimate ``_h.redact_credentials`` (the dashboard + handlers facade, which re-exports the real pass) from a same-named method on an + unrelated object — a distinction a bare attribute-name check cannot make. + """ + out: dict[str, str] = {} + for node in ast.walk(tree): + if isinstance(node, ast.Import): + for alias in node.names: + if alias.asname: + out[alias.asname] = alias.name + else: + # `import a.b.c` binds `a` to the top package. + out.setdefault(alias.name.split(".")[0], alias.name.split(".")[0]) + elif isinstance(node, ast.ImportFrom) and node.module: + for alias in node.names: + if alias.asname: + out[alias.asname] = node.module + "." + alias.name + else: + out[alias.name] = node.module + "." + alias.name + return out + + +def _call_name(node: ast.AST, aliases: dict[str, str] | None = None) -> str | None: + """The redaction pass a ``Call`` invokes, or None if it is some other call. + + Recognises every spelling this tree actually uses, which is what the gate's + "either spelling" claim rests on: + + * a bare name — ``redact_credentials(x)``, the common local form; + * a qualified chain — ``security.redact_credentials(x)``, the app-package form + an ``ast.Name``-only reader silently missed; and + * an IMPORT ALIAS of that module — ``_h.redact_credentials(x)``, where ``_h`` + resolves (via :func:`_alias_targets`) to a module that really does re-export + the pass. + + *aliases* is the per-module map from :func:`_alias_targets`. Without it the + qualifier is judged by its own spelling, which still accepts ``security`` and + now also accepts a *known* facade alias. Crediting an unrelated object's + same-named method would red the gate for work unrelated to this rule. + """ + if not isinstance(node, ast.Call): + return None + func = node.func + if isinstance(func, ast.Name): + return func.id + if not isinstance(func, ast.Attribute): + return None + # Cheap reject BEFORE any chain walking: this runs for every Call in the tree + # (hundreds of thousands across src/). Without it the scan ran past the xdist + # worker's budget and workers crashed rather than failing an assertion. + if func.attr not in _PASS_NAMES: + return None + # `x.(...)`: `x` must name a module that owns or re-exports the passes. + # A bare local binding (`redact_credentials(x)`) arrives as ast.Name and was + # handled above; here the qualifier is always a NAME, so resolve it through the + # module alias map. `security` and the `_h` handlers alias resolve; a SDK object + # or an unrelated alias does not. + qualifier = func.value + if not isinstance(qualifier, ast.Name): + # A deeper chain (`mod.security.`) is credited on its inner segment. + if isinstance(qualifier, ast.Attribute) and qualifier.attr in _SECURITY_QUALIFIERS: + return func.attr + return None + if qualifier.id in _SECURITY_QUALIFIERS: + return func.attr + if aliases and _resolves_to_security(aliases.get(qualifier.id)): + return func.attr + return None + + +def _resolves_to_security(module: str | None) -> bool: + """True when *module* is (or sits directly under) the module that owns the passes.""" + if not module: + return False + tail = module.rsplit(".", 1)[-1] + return tail in _SECURITY_MODULES + + +def _unwrap(node: ast.AST) -> ast.AST: + """Strip ``[0]`` subscripts so ``f(x)[0]`` reads as ``f(x)``.""" + while isinstance(node, ast.Subscript): + node = node.value + return node + + +def _target_sources(node: ast.stmt) -> list[str]: + """The dotted source spelling of each assignment target. + + Compared as SOURCE TEXT rather than by identifier, because a target need not + be a bare name: the ledger and chip code the migration rewrote assigns into a + ``dict`` entry (``entry["path"], _ = redact_credentials(entry["path"])``), and + an identifier-only reader saw no target there at all. ``ast.unparse`` gives + both sides the same spelling so ``entry["path"]`` matches ``entry["path"]``. + """ + if not isinstance(node, (ast.Assign, ast.AnnAssign)): + return [] + targets = node.targets if isinstance(node, ast.Assign) else [node.target] + out: list[str] = [] + for target in targets: + if isinstance(target, ast.Tuple): + out.extend(ast.unparse(e) for e in target.elts) + else: + out.append(ast.unparse(target)) + return out + + +def _arg_sources(call: ast.Call) -> list[str]: + """The source spelling of each positional argument of *call*.""" + return [ast.unparse(a) for a in call.args] + + +def _body_lists(tree: ast.AST): + """Every statement list in the module, so nested and branched pairs are found. + + Each list is yielded ONCE. ``ast.walk`` also visits the ``ExceptHandler`` node + whose ``body`` the handler loop below already yielded, so without the ``seen`` + guard a pair inside an ``except`` block is matched twice -- which reads as a + second offending site and would make the pinned counts lie. + """ + seen: set[int] = set() + for node in ast.walk(tree): + for field in ("body", "orelse", "finalbody"): + body = getattr(node, field, None) + if isinstance(body, list) and body and isinstance(body[0], ast.stmt): + if id(body) not in seen: + seen.add(id(body)) + yield body + for handler in getattr(node, "handlers", []) or []: + if id(handler.body) not in seen: + seen.add(id(handler.body)) + yield handler.body + + +def _two_line_offenders_in(tree: ast.AST, rel: str) -> list[tuple[str, int]]: + out: list[tuple[str, int]] = [] + aliases = _alias_targets(tree) + for body in _body_lists(tree): + for first, second in zip(body, body[1:]): + s1 = _unwrap(first.value) if isinstance(first, (ast.Assign, ast.AnnAssign)) else None + s2 = _unwrap(second.value) if isinstance(second, (ast.Assign, ast.AnnAssign)) else None + if not (isinstance(s1, ast.Call) and isinstance(s2, ast.Call)): + continue + if _call_name(s1, aliases) != CREDENTIAL or _call_name(s2, aliases) != URL_PASS: + continue + # The second call must consume what the first one produced. That is + # what makes the two lines one composition rather than two independent + # redactions that merely sit next to each other. Both sides are + # compared as SOURCE, so a dict-entry target counts too. + if set(_target_sources(first)) & set(_arg_sources(s2)): + out.append((rel, first.lineno)) + return out + + +def _inline_offenders_in(tree: ast.AST, rel: str) -> list[tuple[str, int]]: + out: list[tuple[str, int]] = [] + aliases = _alias_targets(tree) + for node in ast.walk(tree): + if _call_name(node, aliases) != URL_PASS or not node.args: + continue + inner = _unwrap(node.args[0]) + if isinstance(inner, ast.Call) and _call_name(inner) == CREDENTIAL: + out.append((rel, node.lineno)) + return out + + +def _rel(path: Path) -> str: + """Repo-relative POSIX spelling, so a baseline entry is platform-independent.""" + return path.relative_to(ROOT).as_posix() + + +def _scan_tree() -> tuple[list[tuple[str, int]], list[tuple[str, int]]]: + """One pass over ``src/``, returning (two-line offenders, inline offenders). + + A SINGLE walk for both scanners, and each tree is dropped as soon as it is + scanned. Two independent walks meant two full parses of ~1600 files (measured + ~35s per 300 files), and holding the parsed trees alive between tests pushed an + xdist worker past its budget — which surfaces as `worker crashed`, not as an + assertion failure, so it is easy to misread as a code bug. + """ + two_line: list[tuple[str, int]] = [] + inline: list[tuple[str, int]] = [] + for path in _source_files(): + try: + tree = ast.parse(path.read_text(encoding="utf-8")) + except (SyntaxError, UnicodeDecodeError): # pragma: no cover - not our code + continue + rel = _rel(path) + two_line.extend(_two_line_offenders_in(tree, rel)) + inline.extend(_inline_offenders_in(tree, rel)) + del tree + return two_line, inline + + +#: Both scanners share ONE pass over src/. The scan is the expensive part of this +#: module (~2 min), and re-walking it per test doubled that for no coverage: the +#: two offender sets are disjoint by construction (a two-statement pair vs a single +#: nested expression), so one traversal answers both. +_OFFENDERS: tuple[list[tuple[str, int]], list[tuple[str, int]]] | None = None + + +def _offenders() -> tuple[list[tuple[str, int]], list[tuple[str, int]]]: + global _OFFENDERS + if _OFFENDERS is None: + _OFFENDERS = _scan_tree() + return _OFFENDERS + + +def _two_line_offenders() -> list[tuple[str, int]]: + return _offenders()[0] + + +def _inline_reversed() -> list[tuple[str, int]]: + return _offenders()[1] + + +class TestTheScanItselfIsSound: + """Probes: every spelling must stay caught, and the walk must read files.""" + + def test_probe_same_variable_is_caught(self) -> None: + tree = ast.parse( + "def f(x):\n" + " x, _ = redact_credentials(x)\n" + " x, _ = redact_exfiltration_urls(x)\n" + ) + assert _two_line_offenders_in(tree, "probe.py") + + def test_probe_different_target_variable_is_caught(self) -> None: + """``a, _ = cred(x)`` then ``b, _ = urls(a)`` is still one composition.""" + tree = ast.parse( + "def f(x):\n" + " a, _ = redact_credentials(x)\n" + " b, _ = redact_exfiltration_urls(a)\n" + ) + assert _two_line_offenders_in(tree, "probe.py") + + def test_probe_nested_in_a_branch_is_caught(self) -> None: + tree = ast.parse( + "def f(x, c):\n" + " if c:\n" + " x, _ = redact_credentials(x)\n" + " x, _ = redact_exfiltration_urls(x)\n" + ) + assert _two_line_offenders_in(tree, "probe.py") + + def test_probe_nested_in_a_handler_is_caught(self) -> None: + tree = ast.parse( + "def f(x):\n" + " try:\n" + " pass\n" + " except Exception:\n" + " x, _ = redact_credentials(x)\n" + " x, _ = redact_exfiltration_urls(x)\n" + ) + assert _two_line_offenders_in(tree, "probe.py") + + def test_probe_the_subscript_form_is_caught(self) -> None: + tree = ast.parse( + "def f(x):\n" + " x, _ = redact_credentials(x)\n" + " x, _ = redact_exfiltration_urls(x)[0]\n" + ) + assert _two_line_offenders_in(tree, "probe.py") + + def test_probe_the_qualified_attribute_form_is_caught(self) -> None: + """``security.redact_credentials(...)`` — the app-package spelling. + + The regression this probe exists for: an ``ast.Name``-only reader missed + the one live site in ``apps/builtins/md_notebook/server.py``, so the gate + claimed "either spelling" while covering one. + """ + tree = ast.parse( + "def f(x):\n" + " x, _ = security.redact_credentials(str(x))\n" + " x, _ = security.redact_exfiltration_urls(x)\n" + ) + assert _two_line_offenders_in(tree, "probe.py") + + def test_probe_a_deeper_qualified_chain_is_caught(self) -> None: + tree = ast.parse( + "def f(x):\n" + " x, _ = mod.security.redact_credentials(x)\n" + " x, _ = mod.security.redact_exfiltration_urls(x)\n" + ) + assert _two_line_offenders_in(tree, "probe.py") + + def test_probe_a_foreign_object_with_the_same_method_is_not_credited(self) -> None: + """A different object's ``redact_credentials`` is not this package's pass. + + Without qualifier resolution, ANY SDK or test double that happens to share + the method name would be read as ours and reported as a reversed pair. + """ + tree = ast.parse( + "def f(x):\n" + " x, _ = vendor_sdk.redact_credentials(x)\n" + " x, _ = vendor_sdk.redact_exfiltration_urls(x)\n" + ) + assert not _two_line_offenders_in(tree, "probe.py") + + def test_probe_the_dashboard_handlers_alias_is_credited(self) -> None: + """``_h.`` where ``_h`` aliases a module that re-exports them. + + Five real sites reach the passes this way. Missing them would mean the gate + silently ignores a whole spelling, which is the regression it exists to stop. + """ + tree = ast.parse( + "import kiro_crew.dashboard.handlers as _h\n" + "def f(x):\n" + " x, _ = _h.redact_credentials(x)\n" + " x, _ = _h.redact_exfiltration_urls(x)\n" + ) + assert _two_line_offenders_in(tree, "probe.py") + + def test_probe_an_unrelated_alias_is_not_credited(self) -> None: + tree = ast.parse( + "import some.other.sdk as _h\n" + "def f(x):\n" + " x, _ = _h.redact_credentials(x)\n" + " x, _ = _h.redact_exfiltration_urls(x)\n" + ) + assert not _two_line_offenders_in(tree, "probe.py") + + def test_probe_a_dict_entry_target_is_caught(self) -> None: + """``entry["path"], _ = ...`` — the form the migration itself rewrote. + + An identifier-only reader saw no target on that line, so this exact shape + could have been reintroduced anywhere without a red gate. + """ + tree = ast.parse( + "def f(entry):\n" + ' entry["path"], _ = redact_credentials(entry["path"])\n' + ' entry["path"], _ = redact_exfiltration_urls(entry["path"])\n' + ) + assert _two_line_offenders_in(tree, "probe.py") + + def test_probe_an_attribute_target_is_caught(self) -> None: + tree = ast.parse( + "def f(obj):\n" + " obj.text, _ = redact_credentials(obj.text)\n" + " obj.text, _ = redact_exfiltration_urls(obj.text)\n" + ) + assert _two_line_offenders_in(tree, "probe.py") + + def test_probe_the_correct_order_is_not_flagged(self) -> None: + tree = ast.parse( + "def f(x):\n" + " x, _ = redact_exfiltration_urls(x)\n" + " x, _ = redact_credentials(x)\n" + ) + assert not _two_line_offenders_in(tree, "probe.py") + + def test_probe_independent_redactions_are_not_flagged(self) -> None: + """Two calls that do not share a value are not a composition.""" + tree = ast.parse( + "def f(x, y):\n" + " a, _ = redact_credentials(x)\n" + " b, _ = redact_exfiltration_urls(y)\n" + ) + assert not _two_line_offenders_in(tree, "probe.py") + + def test_probe_the_reversed_inline_nesting_is_caught(self) -> None: + tree = ast.parse( + "def f(x):\n return redact_exfiltration_urls(redact_credentials(x)[0])[0]\n" + ) + assert _inline_offenders_in(tree, "probe.py") + + def test_probe_the_correct_inline_nesting_is_not_flagged(self) -> None: + """Innermost runs first: ``cred(urls(x))`` is the CORRECT order.""" + tree = ast.parse( + "def f(x):\n return redact_credentials(redact_exfiltration_urls(x)[0])[0]\n" + ) + assert not _inline_offenders_in(tree, "probe.py") + + def test_probe_a_comment_quoting_the_pattern_is_not_flagged(self) -> None: + """The scan parses the AST, so literals and comments can never count.""" + tree = ast.parse( + "def f(x):\n" + " # x, _ = redact_credentials(x)\n" + " # x, _ = redact_exfiltration_urls(x)\n" + ' note = "redact_exfiltration_urls(redact_credentials(x))"\n' + " return redact(x) + note\n" + ) + assert not _two_line_offenders_in(tree, "probe.py") + assert not _inline_offenders_in(tree, "probe.py") + + def test_the_tree_scan_actually_reads_files(self) -> None: + """A scan that silently found no files would pass forever. + + Asserts the walk is non-trivial and reaches the package that owns the two + passes, so an over-eager exclusion (or a moved source root) fails here + rather than voiding every baseline comparison above. + """ + files = _source_files() + assert len(files) > 100, "the source walk found implausibly few files" + assert any(p.parent.name == "security" for p in files), files[:5] + assert SRC.is_dir(), SRC