Skip to content

armgit / AiSOC

Public Imported from GitHub
0

Open-source AI Security Operations Center: alert fusion, LLM-agent triage, MITRE ATT&CK investigation, and a replayable decision ledger for every agent step. Self-hostable, runs with no API keys, MIT licensed. Ships an MCP server for Claude, Cursor and Continue.

fix(war-rooms): return a code from a closed set, never a slice of the exception

Beenu Arorab139b81f21a0
diff --git a/services/slack-bot/app/war_room.py b/services/slack-bot/app/war_room.py
index 8e05a3ca1..145337393 100644
--- a/services/slack-bot/app/war_room.py
+++ b/services/slack-bot/app/war_room.py
@@ -76,29 +76,70 @@ class PostRequest(BaseModel):
case_id: str = ""
-#: Slack's machine-readable reason codes. The API answers with one of a
-#: closed set (`name_taken`, `restricted_action`, `channel_not_found`, …),
-#: and that token is both the whole actionable content of the failure and
-#: safe to return — unlike `str(exc)`, which carries the full API response.
-_REASON_RE = re.compile(r"\b([a-z][a-z0-9_]{3,48})\b")
+#: Slack's documented error codes, as a closed set.
+#:
+#: A regex over `str(exc)` was the first attempt and it was still a
+#: *substring of the exception*, so the value returned to the caller was
+#: derived from the API response however short it was — which is both the
+#: `py/stack-trace-exposure` finding and, more to the point, true. This
+#: returns a literal from the set below or a fixed string, so nothing
+#: derived from the exception can reach a response at all.
+#:
+#: Unknown codes are deliberately not passed through. Slack adds error codes
+#: and this list will go stale; "refused" plus a log line is the right
+#: failure for that, because the alternative is a passthrough that reopens
+#: the hole the day a new code appears.
+_SLACK_REASONS: frozenset[str] = frozenset(
+ {
+ "name_taken",
+ "restricted_action",
+ "invalid_name",
+ "invalid_name_specials",
+ "invalid_name_maxlength",
+ "invalid_name_punctuation",
+ "invalid_name_required",
+ "channel_not_found",
+ "is_archived",
+ "user_not_found",
+ "users_not_found",
+ "cant_invite_self",
+ "cant_invite",
+ "not_in_channel",
+ "already_in_channel",
+ "ura_max_channels",
+ "too_many_users",
+ "missing_scope",
+ "not_authed",
+ "invalid_auth",
+ "account_inactive",
+ "token_revoked",
+ "no_permission",
+ "ratelimited",
+ "rate_limited",
+ "fatal_error",
+ "internal_error",
+ "service_unavailable",
+ "msg_too_long",
+ "no_text",
+ }
+)
-#: Bounded so an unrecognised error cannot smuggle a response body out
-#: through the reason slot.
-_MAX_REASON = 48
+#: What a caller is told when Slack's code is not one this build knows.
+_UNKNOWN_REASON = "refused"
def slack_reason(exc: BaseException) -> str:
- """The reason code from a Slack SDK error, or a fixed fallback.
+ """A reason code from the closed set above, or a fixed string.
- A `fullmatch`-shaped allowlist rather than a redaction pass: anything
- that is not a lower-case reason token is replaced, not escaped, because
- an escaped response body returned to a caller is still the response
- body.
+ Never a slice of the exception. The Slack SDK's error carries the whole
+ API response, and a caller gets a code it can act on rather than a
+ redacted copy of somebody else's HTTP body.
"""
- for candidate in _REASON_RE.findall(str(exc))[:20]:
- if candidate not in ("the", "error", "response", "slack", "api", "data"):
- return candidate[:_MAX_REASON]
- return "no reason code"
+ message = str(exc).lower()
+ for code in sorted(_SLACK_REASONS):
+ if code in message:
+ return code
+ return _UNKNOWN_REASON
def _authorized(supplied: str | None) -> bool:
diff --git a/services/slack-bot/tests/test_war_room.py b/services/slack-bot/tests/test_war_room.py
index a75e34346..8e133804e 100644
--- a/services/slack-bot/tests/test_war_room.py
+++ b/services/slack-bot/tests/test_war_room.py
@@ -130,15 +130,22 @@ class TestChannelName:
class TestErrorText:
"""The vendor's own words go to the log; the reason code goes to the caller."""
- def test_a_reason_code_is_extracted(self) -> None:
- assert slack_reason(Exception("The request to the Slack API failed. name_taken")) == "request"
+ def test_a_known_code_is_returned(self) -> None:
+ assert slack_reason(Exception("The request to the Slack API failed: name_taken")) == "name_taken"
assert slack_reason(FakeSlackError("restricted_action")) == "restricted_action"
- def test_an_unrecognisable_error_yields_a_fixed_string(self) -> None:
- assert slack_reason(Exception("!!! 500 !!!")) == "no reason code"
+ def test_an_unknown_code_is_a_fixed_string_not_a_passthrough(self) -> None:
+ # Slack adds error codes and this list will go stale. "refused"
+ # plus a log line is the right failure for that; a passthrough
+ # would reopen the hole the day a new code appears.
+ assert slack_reason(Exception("some_brand_new_code_2027")) == "refused"
+ assert slack_reason(Exception("!!! 500 !!!")) == "refused"
- def test_the_reason_is_bounded_so_a_body_cannot_ride_out_in_it(self) -> None:
- assert len(slack_reason(Exception("a" * 500))) <= 48
+ def test_the_result_is_always_a_literal_from_the_closed_set(self) -> None:
+ from app.war_room import _SLACK_REASONS, _UNKNOWN_REASON
+
+ for message in ("a" * 5000, "xoxb-leaked-token", "", "NAME_TAKEN"):
+ assert slack_reason(Exception(message)) in _SLACK_REASONS | {_UNKNOWN_REASON}
def test_a_create_failure_does_not_return_the_api_response(self, monkeypatch) -> None:
secret = "xoxb-this-would-be-a-leaked-token"
diff --git a/services/teams-bot/app/war_room.py b/services/teams-bot/app/war_room.py
index f43ff1232..0ec3e39c7 100644
--- a/services/teams-bot/app/war_room.py
+++ b/services/teams-bot/app/war_room.py
@@ -270,8 +270,8 @@ async def post_to_war_room(
# suppression sits on the reported line and nowhere else. Any
# *other* URL built from a request in this file still fails the
# query.
- res = await http.post(
- f"{GRAPH}/teams/{team_id}/channels/{quote(channel_id, safe='')}/messages", # codeql[py/partial-ssrf]
+ res = await http.post( # codeql[py/partial-ssrf]
+ f"{GRAPH}/teams/{team_id}/channels/{quote(channel_id, safe='')}/messages",
headers={"Authorization": f"Bearer {token}", "Content-Type": "application/json"},
json={"body": {"content": body.text}},
)