Skip to content
Draft
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
2 changes: 1 addition & 1 deletion docs/architecture/mcp.md
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ quarantine exclusion, and the discover-and-sync step (`sync_discovered_servers`,

| Owner | Owns |
|---|---|
| `mcp_discovery_server_info.py` | `McpServerInfo`, the scope names and config-source tables, and `redact_mcp_headers` / `redact_mcp_error` |
| `mcp_discovery_server_info.py` | `McpServerInfo`, the scope names and config-source tables, and `redact_mcp_headers` / `redact_mcp_error` (the configured header values are masked first, then the generic credential scanners) |
| `mcp_discovery_probe_state.py` | the protocol revision every client handshake offers and steps down to, the probe timeouts and error cap, the once-only warning ledgers, and the probe-result cache |
| `mcp_discovery_managed.py` | the managed servers' subcommand and tool-module tables, `managed_server_is_session_bound`, the in-process tool read, and stale-command repair |
| `mcp_discovery_config.py` | loading the agent config and the `mcp.json` sources per scope, and `list_servers` |
Expand Down
2 changes: 1 addition & 1 deletion docs/system-specs/modules/security.md
Original file line number Diff line number Diff line change
Expand Up @@ -2612,7 +2612,7 @@ the SDK's answer, which a copy could not satisfy, and pins the public surface at
exactly `outbound` so a second spelling cannot accrete.

**Credential output redaction** — catches raw credential patterns in LLM/tool output:
- `redact_credentials(text)`: the credential plan, the rule table alone (`security/redaction_structured/`, rows in `table.py`): vendor formats by prefix, length, charset and checksum (Telegram and Discord bot tokens, npm and PyPI tokens and OpenAI `sk-proj-` keys among them), the `Authorization: Bearer` token (a documented placeholder excepted), the `token` URL parameter, and an explicit list of well-known credential names (`known_key`), matched ignoring case and the separators `_ - .`. A name the list does not hold is a known gap, as is a format no row reads. Nothing else runs: a value none of these reads stays as written, and no run is masked on a guess from its entropy or shape. The recorded direction is [the redaction tier decision](../../decisions/2026-10-20-redaction-classifies-by-what-a-value-can-do.md): a value is masked for what it can do, so a precise format check that holds is masked and an identifier is not
- `redact_credentials(text)`: the credential plan, the rule table alone (`security/redaction_structured/`, rows in `table.py`): vendor formats by prefix, length, charset and checksum (Telegram and Discord bot tokens, npm and PyPI tokens and OpenAI `sk-proj-` keys among them), a `Bearer` token, with or without the `Authorization` name before it (a documented placeholder excepted, and prose after the word such as `Bearer of bad news`), the `token` URL parameter, and an explicit list of well-known credential names (`known_key`), matched ignoring case and the separators `_ - .`. A name the list does not hold is a known gap, as is a format no row reads. Nothing else runs: a value none of these reads stays as written, and no run is masked on a guess from its entropy or shape. The recorded direction is [the redaction tier decision](../../decisions/2026-10-20-redaction-classifies-by-what-a-value-can-do.md): a value is masked for what it can do, so a precise format check that holds is masked and an identifier is not
- **Only the VALUE is masked.** The label, header name, separator, quotes, URL scheme, host, path and every other parameter stay as written, so `{"Authorization": "Bearer <tok>"}` redacts to `{"Authorization": "Bearer [REDACTED: credential]"}` and still parses, `password=<value>` keeps `password=`, and a URL keeps everything but the credential value in it. A base64 or hex run whose decoded text holds a credential becomes `[REDACTED: encoded credential]`
- **What the table masks** (values that are credentials by themselves): vendor tokens by published format, with the checksum where the vendor publishes one (GitHub, GitLab, Slack with `xapp-`, Stripe, Anthropic, OpenAI legacy keys, Hugging Face `hf_`, Docker `dckr_pat_`, Google, SendGrid, HashiCorp Vault and others); private-key blocks (PEM and PGP); signed JWTs and JWEs; an AWS secret access key beside its key ID or after its label, and a session token after its label; the password in URL userinfo; `Authorization: Basic` values and API-key header values; session cookies; labelled secrets (`password=`, `api_key=`, `DB_PASSWORD=` and similar), behind a gate that leaves a placeholder or a code expression alone; curl `-u user:pass` and `.netrc` passwords; secret and signature URL parameters (`client_secret`, `access_token`, `refresh_token`, `id_token`, `code_verifier`, `password`, `passwd`, `api_key`, `apikey`, `private_token`, `sig`, `signature`, `X-Amz-Signature`, `X-Amz-Security-Token`, `X-Goog-Signature` and similar); the secret segment of a Slack or Discord webhook URL; and a base64 or hex run that decodes to one of these
- **What it never masks**: identifiers (AWS access key IDs, account IDs, ARNs, request ids, `x-amz-id-2`, UUIDs, git object names and commit permalinks, docker digests, lockfile hashes), documentation example keys, public certificates, SSH public keys and fingerprints, image data URIs, temp paths, an OAuth `code` or `state`, an unsigned JWT, and a token that fails its format check
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ def test_MUTATION_credential_filename(tmp_path):
"""Disable refused_by_name; nothing then blocks a skill carrying a .env."""
src = make_crew(
tmp_path / "home",
skills={"withenv": {"SKILL.md": "# ok", ".env": "SECRET=hunter2"}},
skills={"withenv": {"SKILL.md": "# ok", ".env": "SECRET=hunt2"}},
)
good = load_build()
crew = good.resolve_crew("frontdesk", src)
Expand Down
4 changes: 2 additions & 2 deletions src/kiro_crew/dashboard/handlers/agent_panel_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,8 @@ def _scrubbed(text: str) -> str:
def _scrubbed_under(key: str, value: str) -> str:
"""*value* scrubbed as the value UNDER *key*.

A credential pattern can need its key to match: ``Bearer <token>`` alone passes
the redactor while ``authorization: Bearer <token>`` is caught, and a script that
A credential pattern can need its key to match: ``Basic <token>`` alone passes
the redactor while ``authorization: Basic <token>`` is caught, and a script that
records an upstream response's headers writes exactly that pair. So the value is
judged twice -- alone, then the cleaned result spelled beside its key as a header
line spells it -- and a pair the second judgement catches is answered with the
Expand Down
24 changes: 14 additions & 10 deletions src/kiro_crew/mcp_discovery_server_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,22 +195,20 @@ def redact_mcp_error(error: object, headers: object, extra_values: Iterable[str]

Two layers, so every consumer (``to_dict``, the probe cache, the probe
endpoints) satisfies one invariant — no credential-shaped text survives to
serialized output:
serialized output. They run in this order, so a generic match never masks
only part of a configured value before the exact scrubber looks for it:

1. The site-wide scanners (``redact_credentials`` /
1. The configured-value scrubber below catches the exact header values and
Authorization suffixes, including encoded spellings the generic
scanners cannot know about.
2. The site-wide scanners (``redact_credentials`` /
``redact_exfiltration_urls``) catch anything credential-SHAPED that a
remote server reflects, whether or not it matches configured values —
the same pass ``_sanitize_probe_error`` applies to probe exceptions.
2. The configured-value scrubber below catches the exact header values and
Authorization suffixes, including encoded spellings the generic
scanners cannot know about.
"""
if not isinstance(error, str) or not isinstance(headers, dict):
return error if isinstance(error, str) else ""

error, _ = redact_exfiltration_urls(error)
error, _ = redact_credentials(error)

# Values map to whether they require lexical boundaries. Full header values
# and long credentials are bare substring matches; only SHORT credentials
# need boundaries, since only they could collide with ordinary words.
Expand Down Expand Up @@ -246,9 +244,15 @@ def redact_mcp_error(error: object, headers: object, extra_values: Iterable[str]
continue
values.setdefault(extra, len(extra) < _MCP_CREDENTIAL_UNANCHORED_MIN_LENGTH)

if not values:
return error
if values:
error = _scrub_configured_values(error, values)
error, _ = redact_exfiltration_urls(error)
error, _ = redact_credentials(error)
return error


def _scrub_configured_values(error: str, values: dict[str, bool]) -> str:
"""*error* with every configured value in *values* (value -> needs boundaries) masked."""
# Check the characters outside a suffix instead of using \b: base64 padding
# ends in a non-word "=", so \b would fail between that padding and ordinary
# punctuation. Longest-first keeps a full header ahead of its own suffix.
Expand Down
34 changes: 34 additions & 0 deletions src/kiro_crew/security/redaction_structured/headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
client prompt, and the next word names a database) and ``--password=<password>``;
- ``docker_login``: ``-p <password>`` and ``--password <password>`` (or ``=``) of a
``login`` subcommand;
- ``bearer``: ``Bearer <token>`` with no ``Authorization`` name before it;
- ``netrc``: ``password <value>`` in a ``.netrc`` entry, after ``machine <host>`` or
``default`` with only ``login`` and ``account`` pairs between, on one line or across
lines.
Expand Down Expand Up @@ -64,6 +65,7 @@
_LINE_MAX = 512
_COMMAND_STOPS = "\r\n|;&"
_NETRC_KEYWORD = "password"
_BEARER_WORD = "bearer"
_NETRC_ENTRY = "machine"
_NETRC_DEFAULT = "default"
_NETRC_PAIRS = frozenset({"login", "account"})
Expand Down Expand Up @@ -324,13 +326,45 @@ def _netrc(text: str, at: int, row: HeaderRow, final: bool) -> Hit | None:
return _word_value(text, i, row, final, "netrc")


def _bearer_bare(text: str, at: int, row: HeaderRow, final: bool) -> Hit | None:
"""``Bearer <token>`` where no ``Authorization`` name leads it (module docstring)."""
i = at + len(_BEARER_WORD)
if i >= len(text):
if not final:
raise _Open
return None
k = _skip(text, i, _ASCII_BLANKS)
if k == i:
return None
end = _value(text, k, None, final)
# A percent escape (``%2F``) is part of the token: stopping at it would leave the tail of
# an encoded credential visible.
while end < len(text) and text[end] == "%":
end = _value(text, end + 1, None, final)
value = text[k:end]
if not _keep(text, k, end, row) or reads_as_words(value) or _capitalised_word(value):
return None
if row.allow_values and value.rsplit(".", 1)[0] in row.allow_values:
return None
return Hit(k, end, row.id, row.tier, "header", "known_key")


def _capitalised_word(value: str) -> bool:
"""Letters only, upper case at most in the first place or throughout (``Credentials``,
``AUTHORIZATION``): prose after the word ``bearer``, not a token."""
if not value.isalpha() or not value.isascii():
return False
return not any(ch.isupper() for ch in value[1:]) or value.isupper()


_Reader = Callable[[str, int, HeaderRow, bool], "Hit | None"]
#: Readers that anchor on their own fixed literals, and those literals.
_FIXED: dict[str, tuple[tuple[str, ...], _Reader]] = {
"curl_user": (_CURL_FLAGS, _curl_user),
"mysql_password": (_MYSQL_FLAGS, _mysql_password),
"docker_login": (_DOCKER_FLAGS, _docker_login),
"netrc": ((_NETRC_KEYWORD,), _netrc),
"bearer": ((_BEARER_WORD,), _bearer_bare),
}


Expand Down
10 changes: 8 additions & 2 deletions src/kiro_crew/security/redaction_structured/known_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
The value gate then decides: ``aws_secret_40`` takes a run of exactly 40
AWS-secret-alphabet characters ending the value; ``b64_run`` takes a base64 run of at
least 100 characters (padding included); ``secret_literal`` takes a written value (the
text inside its quotes, or an unquoted word) of 8 to 512 characters that is not a
text inside its quotes, or an unquoted word) of 6 to 512 characters that is not a
placeholder, does not read as code and does not read as words (:func:`_literal_gate`).
Only that run is replaced.
"""
Expand All @@ -45,7 +45,9 @@
# A session-token run still running after this many characters is decided a Secret
# (fail closed); the stream then discards the rest of the run.
SESSION_FAIL_CLOSED = 4096
LITERAL_MIN = 8
LITERAL_MIN = 6
# An elided value (``sk-...``) is documentation only when its head is shorter than this.
_ELIDED_HEAD_MAX = 8
LITERAL_MAX = 512
# An unquoted literal ends at a blank, a separator another field starts after, or a quote.
_LITERAL_STOPS = frozenset(" \t\r\n;&,\"'")
Expand Down Expand Up @@ -229,6 +231,10 @@ def _literal_gate(
return None
if not LITERAL_MIN <= len(value) <= LITERAL_MAX or is_placeholder(value):
return None
# An elided example (``sk-...``) is read as documentation. Only a short head counts: a
# long token cut by a log's own ellipsis is still a credential and is masked.
if value.endswith(("...", "\u2026")) and len(value.rstrip(".\u2026")) < _ELIDED_HEAD_MAX:
return None
if any(text.startswith(tag, vs) for tag in PLAN_TAGS):
return None # already replaced, byte for byte: redaction is a fixed point
if value.startswith(_ARMOUR_HEAD) or reads_as_example_words(value):
Expand Down
15 changes: 13 additions & 2 deletions src/kiro_crew/security/redaction_structured/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ class TableError(ValueError):
"password",
"passwd",
"secret",
"token",
"api_key",
"apikey",
"api_secret",
Expand Down Expand Up @@ -486,6 +487,16 @@ class TableError(ValueError):
# A published JWT example after ``Bearer`` is documentation too.
"allow_values": list(JOSE_EXAMPLES),
},
{
# ``Bearer <token>`` with no ``Authorization`` name before it (a logged request line,
# a pasted curl ``-H`` value, a prose sentence). Words ("Bearer of bad news") stay.
"id": "http.bearer_bare",
"tier": SECRET,
"detector": "header",
"reader": "bearer",
"min_length": 8,
"allow_values": list(JOSE_EXAMPLES),
},
{
"id": "http.basic_credentials",
"tier": SECRET,
Expand Down Expand Up @@ -1258,10 +1269,10 @@ def _build_url_userinfo(row: Mapping[str, Any]) -> UrlUserinfoRow:


_HEADER_READERS = frozenset(
{"header", "cookie", "curl_user", "mysql_password", "docker_login", "netrc"}
{"header", "cookie", "curl_user", "mysql_password", "docker_login", "netrc", "bearer"}
)
#: Readers that anchor on their own fixed flags or keyword and read no ``names``.
_NAMELESS_READERS = frozenset({"curl_user", "netrc"})
_NAMELESS_READERS = frozenset({"curl_user", "netrc", "bearer"})


def _build_header(row: Mapping[str, Any]) -> HeaderRow:
Expand Down
18 changes: 9 additions & 9 deletions test/test_agent_panel_routes_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,29 +476,29 @@ async def test_the_body_cannot_set_who_wrote_it_or_for_whom(bound_cron):
async def test_the_door_scrubs_the_record_even_when_the_client_did_not(bound_cron):
"""``ScriptContext`` redacts before it posts, but it is one caller and not the gate:
a script posting through ``ctx._post`` sends whatever it holds. The DOOR scrubs
what lands -- a credential on its own, and a bearer token that only matches beside
what lands -- a credential on its own, and a Basic credential that only matches beside
its ``authorization`` key -- and the fold answers the scrubbed form."""
from kiro_crew.security import REDACTED_CREDENTIAL_TAG

secret = SECRET
bearer = "Bearer " + "abcdefghijklmnopqrstuvwxyz0123"
basic = "Basic " + "ZmFrZXVzZXI6ZmFrZXBhc3MtMTIzNDU2Nw=="
async with _client() as c:
resp = await _post(
c,
{
"type": "stock_check",
"data": {
"note": f"key {secret} seen",
"headers": {"authorization": bearer, "accept": "text/plain"},
"rows": [{"authorization": bearer}, 7],
"headers": {"authorization": basic, "accept": "text/plain"},
"rows": [{"authorization": basic}, 7],
"n": 1,
},
},
)
assert resp.status == 200, await resp.text()
row = _custom_fold()["latest"]["stock_check"]
blob = json.dumps(row["data"])
assert secret not in blob and "abcdefghijklmnopqrstuvwxyz0123" not in blob
assert secret not in blob and "ZmFrZXVzZXI6ZmFrZXBhc3MtMTIzNDU2Nw==" not in blob
assert row["data"]["headers"]["authorization"] == REDACTED_CREDENTIAL_TAG
assert row["data"]["headers"]["accept"] == "text/plain"
assert row["data"]["rows"][0]["authorization"] == REDACTED_CREDENTIAL_TAG
Expand All @@ -508,19 +508,19 @@ async def test_the_door_scrubs_the_record_even_when_the_client_did_not(bound_cro
@pytest.mark.asyncio
async def test_a_value_with_two_tokens_loses_both_at_the_door(bound_cron):
"""The keyed judgement runs on the CLEANED value: a header value carrying a token
the bare scrub catches AND a bearer only its key reveals keeps neither."""
the bare scrub catches AND a Basic credential only its key reveals keeps neither."""
from kiro_crew.security import REDACTED_CREDENTIAL_TAG

secret = SECRET
bearer = "Bearer " + "abcdefghijklmnopqrstuvwxyz0123"
basic = "Basic " + "ZmFrZXVzZXI6ZmFrZXBhc3MtMTIzNDU2Nw=="
async with _client() as c:
resp = await _post(
c, {"type": "stock_check", "data": {"authorization": f"{bearer} {secret}"}}
c, {"type": "stock_check", "data": {"authorization": f"{basic} {secret}"}}
)
assert resp.status == 200, await resp.text()
row = _custom_fold()["latest"]["stock_check"]
assert row["data"]["authorization"] == REDACTED_CREDENTIAL_TAG
assert "abcdefghijklmnopqrstuvwxyz0123" not in json.dumps(row["data"])
assert "ZmFrZXVzZXI6ZmFrZXBhc3MtMTIzNDU2Nw==" not in json.dumps(row["data"])


@pytest.mark.asyncio
Expand Down
9 changes: 8 additions & 1 deletion test/test_chat_runner_redact_across_tool_boundary.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,6 @@ async def test_no_credential_continues_a_sentence_end_released_at_the_cut(
_CONTINUATIONS = (
f"{_TAIL} after.",
_PARAM + _VALUE,
_PARAM[1:] + _VALUE,
"M" + "a" * 23 + ".abcdef." + "b" * 27,
"ey" + "J" + "hbGciOiJIUzI1NiJ9." + "ey" + "JzdWIiOiIxIn0.sig" + "0" * 30,
"aB3dE5fG7hJ9kL1mN3pQ5rS7tU9vW1xY3zA5bC7dE9fG",
Expand All @@ -454,6 +453,14 @@ def test_a_plain_sentence_end_is_released_and_joins_no_credential(run: str) -> N
assert turn_reply_seams._stands_alone(run, after), (run, after)


@pytest.mark.parametrize("run", _PLAIN_ENDS)
def test_a_named_token_after_a_plain_sentence_end_does_not_stand_alone(run: str) -> None:
"""``token=<value>`` is masked on its own, but written straight after a ``.`` it joins
that run into one longer name and stays visible: the held run is not released early."""
assert turn_reply_seams._ends_plain_sentence(run)
assert not turn_reply_seams._stands_alone(run, _PARAM[1:] + _VALUE), run


@pytest.mark.parametrize("run", _NOT_PLAIN_ENDS)
def test_a_run_that_could_start_a_credential_is_still_held(run: str) -> None:
assert not turn_reply_seams._ends_plain_sentence(run)
Expand Down
Loading
Loading