Skip to content

fix(slack): guard split()[0] against whitespace-only command text#26245

Merged
teknium1 merged 1 commit into
mainfrom
hermes/hermes-2dba138f
May 15, 2026
Merged

fix(slack): guard split()[0] against whitespace-only command text#26245
teknium1 merged 1 commit into
mainfrom
hermes/hermes-2dba138f

Conversation

@teknium1

Copy link
Copy Markdown
Contributor

Salvage of #2752 by @nidhi-singh02 — slack hunk only.

Summary

A Slack user sending /hermes (trailing whitespace) crashes the slash router on text.split()[0] because ' '.split() == []. The guard if text else "" doesn't catch whitespace-only text (still truthy). Switched to a parts-list intermediate so we only index when non-empty.

Why only the slack hunk

The PR also touched tools/file_operations.py and agent/anthropic_adapter.py. Both are unreachable in current code:

  • tools/file_operations.pyLINTERS is a hardcoded constant dict (py_compile, node --check, …); no empty values, no user input path. split()[0] cannot raise IndexError.
  • agent/anthropic_adapter.py — already guarded one line up: if result.returncode == 0 and result.stdout.strip():. A non-empty stripped string always splits to ≥1 token.

Defensive guards there would be harmless but noise. Dropped them; kept the real fix.

Validation

scripts/run_tests.sh tests/gateway/test_slack.py → 186/186 passing.

Empirically:

input before after
"" "" ""
" " IndexError ""
"\\t \\n" IndexError ""
"compress now" "compress" "compress"

Closes #2745.

When a user sends a Slack message like '/hermes   ' (trailing whitespace
after the slash) the legacy subcommand router hit `text.split()[0]` with
a truthy-but-whitespace-only `text`. `'   '.split()` returns `[]` →
IndexError, blowing up the slash handler before fallthrough to `/help`.

Switch to a two-step guard that materializes the parts list first and
indexes only if non-empty.

Salvaged from PR #2752 by @nidhi-singh02. The PR's other two hunks
(`tools/file_operations.py`, `agent/anthropic_adapter.py`) are
unreachable in current code — `LINTERS` is a hardcoded constant dict
with no empty values, and the anthropic version-detection site is
already guarded by a `result.stdout.strip()` truthy check — so only the
slack hunk is taken.

Closes #2745

Co-authored-by: Teknium <127238744+teknium1@users.noreply.github.com>
@teknium1 teknium1 merged commit 8373956 into main May 15, 2026
14 of 16 checks passed
@teknium1 teknium1 deleted the hermes/hermes-2dba138f branch May 15, 2026 08:50
@github-actions

Copy link
Copy Markdown
Contributor

🔎 Lint report: hermes/hermes-2dba138f vs origin/main

ruff

Total: 0 on HEAD, 0 on base (➖ 0)

🆕 New issues: none

✅ Fixed issues: none

Unchanged: 0 pre-existing issues carried over.

ty (type checker)

Total: 8269 on HEAD, 8269 on base (➖ 0)

🆕 New issues (3):

Rule Count
invalid-argument-type 3
First entries
run_agent.py:7482: [invalid-argument-type] invalid-argument-type: Argument to function `build_anthropic_client` is incorrect: Expected `str`, found `str | dict[Unknown, Unknown] | Any | ... omitted 3 union elements`
run_agent.py:13753: [invalid-argument-type] invalid-argument-type: Argument to function `len` is incorrect: Expected `Sized`, found `(str & ~AlwaysFalsy) | (dict[Unknown, Unknown] & ~AlwaysFalsy) | (Any & ~AlwaysFalsy) | ... omitted 3 union elements`
run_agent.py:13750: [invalid-argument-type] invalid-argument-type: Argument to function `_is_oauth_token` is incorrect: Expected `str`, found `str | dict[Unknown, Unknown] | Any | ... omitted 3 union elements`

✅ Fixed issues (3):

Rule Count
invalid-argument-type 3
First entries
run_agent.py:13750: [invalid-argument-type] invalid-argument-type: Argument to function `_is_oauth_token` is incorrect: Expected `str`, found `str | dict[Unknown | str, Unknown | str | dict[str, str]] | Any | ... omitted 3 union elements`
run_agent.py:7482: [invalid-argument-type] invalid-argument-type: Argument to function `build_anthropic_client` is incorrect: Expected `str`, found `str | dict[Unknown | str, Unknown | str | dict[str, str]] | Any | ... omitted 3 union elements`
run_agent.py:13753: [invalid-argument-type] invalid-argument-type: Argument to function `len` is incorrect: Expected `Sized`, found `(str & ~AlwaysFalsy) | (dict[Unknown | str, Unknown | str | dict[str, str]] & ~AlwaysFalsy) | (Any & ~AlwaysFalsy) | ... omitted 3 union elements`

Unchanged: 4314 pre-existing issues carried over.

Diagnostics are surfaced as warnings — this check never fails the build.

@alt-glitch alt-glitch added type/bug Something isn't working platform/slack Slack app adapter P2 Medium — degraded but workaround exists labels May 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P2 Medium — degraded but workaround exists platform/slack Slack app adapter type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Unchecked str.split() index access can raise IndexError

3 participants