Skip to content

[Bug]: Replace assert statements with explicit error handling in translate.py and fix bandit lint issues #412

@crivetimihai

Description

@crivetimihai

Description

Bandit detected multiple issues including assert statements that are removed with optimization flags, hardcoded bind to all interfaces, and HTTP requests without timeouts.

Affected Code

# mcpgateway/translate.py:225, 299, 614
assert self._proc.stdin and self._proc.stdout  # B101
assert self._proc and self._proc.stdout        # B101
assert process.stdout                           # B101

# mcpgateway/translate.py:550
host="0.0.0.0",  # B104: hardcoded bind all interfaces

# mcpgateway/translate.py:605
async with httpx.AsyncClient(headers=headers, timeout=None)  # B113: no timeout

Impact

  • Severity: Low-Medium
  • CWEs: CWE-703, CWE-605, CWE-400
  • Potential issues:
    • Assert checks removed with python -O causing AttributeErrors
    • Binding to 0.0.0.0 exposes service to all network interfaces
    • HTTP requests without timeout can hang indefinitely

Fix

  1. Replace all assert statements with explicit runtime checks
  2. Make bind interface configurable with secure default (127.0.0.1)
  3. Add proper timeout to HTTP client (30s default)
  4. Add --host CLI argument for bind interface configuration
  5. Updated all docstrings with new parameters and Raises sections

Steps to Reproduce

  1. Run bandit mcpgateway/translate.py
  2. Observe B101, B104, and B113 warnings

Expected Behavior

Secure defaults with configurable options and proper error handling regardless of Python optimization level.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingmcpgateway.translateIssues and features related to the translate componentpythonPython / backend development (FastAPI)securityImproves securitytriageIssues / Features awaiting triage

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions