Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions docs/system-specs/modules/security.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 13 additions & 22 deletions src/kiro_crew/dashboard/chat_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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}"
Expand Down Expand Up @@ -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})
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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="",
Expand Down Expand Up @@ -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)
):
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 4 additions & 6 deletions src/kiro_crew/dashboard/chat_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
_exempt_exact_hosts,
bounded_blocked_links,
oauth_url_contains_credential,
redact,
redact_credentials,
redact_exfiltration_urls,
)
Expand Down Expand Up @@ -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"}
Expand Down Expand Up @@ -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."
)
Expand All @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions src/kiro_crew/dashboard/file_api/grep.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Expand Down
10 changes: 4 additions & 6 deletions src/kiro_crew/dashboard/handlers/artifacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
_B64_CHUNK_RE,
_HARD_CREDENTIAL_RE,
is_sensitive_path,
redact,
redact_credentials,
redact_exfiltration_urls,
)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
6 changes: 2 additions & 4 deletions src/kiro_crew/dashboard/handlers/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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",
Expand Down
5 changes: 2 additions & 3 deletions src/kiro_crew/dashboard/handlers/mcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
36 changes: 16 additions & 20 deletions src/kiro_crew/dashboard/handlers/messaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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

Expand All @@ -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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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(
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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"]
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down
Loading
Loading