Skip to content

[high] fix: [malshare_upload] avoid KeyError when config is missing - #884

Open
elhoim wants to merge 1 commit into
MISP:mainfrom
elhoim:fix/075-malshare-upload-config-keyerror
Open

[high] fix: [malshare_upload] avoid KeyError when config is missing#884
elhoim wants to merge 1 commit into
MISP:mainfrom
elhoim:fix/075-malshare-upload-config-keyerror

Conversation

@elhoim

@elhoim elhoim commented Aug 31, 2026

Copy link
Copy Markdown
Member

BLUF — malshare_upload.py raises KeyError on a config-less request instead of its missing-key message.

  • Problemmalshare_upload.py indexes request["config"] directly, so a request carrying no config key at all raises an unhandled KeyError instead of the module's intended "Missing MalShare API key" message.
  • Fix — Reads (request.get("config") or {}).get("malshare_apikey") so the friendly error is returned.
  • Effect — Analysts who run the module before a MalShare API key is configured now get a clear setup message rather than a traceback surfaced through MISP.
if request["config"].get("malshare_apikey") is None:

Direct indexing into request["config"] raises a KeyError whenever the incoming request has no "config" key at all, instead of returning the module's intended friendly error message "Missing MalShare API key".

Impact: An analyst who invokes the malshare_upload expansion module without any configuration (e.g. no MalShare API key set up in MISP module settings, so config is entirely absent from the request) gets an unhandled KeyError traceback surfaced back through MISP instead of a clear "Missing MalShare API key" error explaining what to fix.

Fix: Changed the lookup to (request.get("config") or {}).get("malshare_apikey"), which falls back to an empty dict when "config" is missing, so the friendly error message is returned as intended.

Verification

  • flake8 misp_modules/modules/expansion/malshare_upload.py — clean (no output)
  • Full test suite: 161 passed, 4 skipped, 5 subtests passed in 25.92s

Found during a review of the repository; other findings are being submitted as separate PRs.

🤖 Generated with Claude Code

handler() checked request["config"].get("malshare_apikey") to decide whether
to report the friendly "Missing MalShare API key" error, but indexing
request["config"] directly raises a KeyError when the request has no
"config" key at all (e.g. the module is invoked before any configuration is
set). Instead of returning the intended misperrors message, the module
crashes with an unhandled exception.

Use request.get("config") or {} so a missing config dict falls back to an
empty one and the existing API-key check reports the expected error instead
of raising.

Verified with flake8 (clean) and the full pytest suite against a local
misp-modules server on port 6775: 161 passed, 4 skipped, 5 subtests passed,
matching the baseline.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018dfYpyaSZd1nxSRLr8suj8
@elhoim elhoim changed the title fix: [malshare_upload] avoid KeyError when config is missing [high] fix: [malshare_upload] avoid KeyError when config is missing Sep 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant