Skip to content

[Test Improver] test: add unit tests for install/mcp_warnings.py (0% -> ~100%)#828

Open
danielmeppiel wants to merge 2 commits intomainfrom
test-assist/mcp-warnings-coverage-24754768992-c4c01f3f5ab2590c
Open

[Test Improver] test: add unit tests for install/mcp_warnings.py (0% -> ~100%)#828
danielmeppiel wants to merge 2 commits intomainfrom
test-assist/mcp-warnings-coverage-24754768992-c4c01f3f5ab2590c

Conversation

@danielmeppiel
Copy link
Copy Markdown
Collaborator

🤖 Test Improver — automated AI assistant

Goal and rationale

src/apm_cli/install/mcp_warnings.py had zero tests despite being a security-critical module. It implements two non-blocking safety checks that fire during apm install --mcp:

  • F5 (SSRF): warn_ssrf_url — warns when a self-defined remote MCP URL points at loopback, link-local, RFC1918, or cloud metadata addresses (AWS/Azure/GCP IMDS, Alibaba Cloud IMDS).
  • F7 (Shell metachars): warn_shell_metachars — warns when env values or the command field contain shell metacharacters that would be passed literally through execve-style MCP stdio spawning rather than being shell-evaluated.

These are the front-line guards against misconfigured MCP server installs surfacing confused/dangerous config to users.

Approach

50 new tests across 4 classes covering every branch:

Class Functions tested Tests
TestIsInternalOrMetadataHost _is_internal_or_metadata_host 18
TestWarnSsrfUrl warn_ssrf_url 9
TestWarnShellMetachars warn_shell_metachars 23

Key scenarios:

  • IPv4/IPv6 loopback, link-local, RFC1918, cloud metadata IPs
  • IPv6 bracket-quoted literals ([::1], [fc00::1])
  • Hostname DNS resolution to internal/public addresses
  • Resolution failures (OSError, UnicodeError) — no crash
  • All 9 _SHELL_METACHAR_TOKENS: $(, `, ;, &&, ||, |, >>, >, <
  • None/integer env values coerced safely
  • Non-string command (e.g. list) skipped without crash
  • Env and command both warn independently in the same call

Coverage impact

Module Before After
install/mcp_warnings.py 0% ~100%

Test status

50 passed in 0.28s
4821 passed, 1 warning, 13 subtests passed (full unit suite)

Reproducibility

uv run pytest tests/unit/install/test_mcp_warnings.py -v
uv run pytest tests/unit tests/test_console.py -x -q

Generated by Daily Test Improver ·

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/daily-test-improver.md@b87234850bf9664d198f28a02df0f937d0447295

Cover all three public-facing functions in the F5/F7 MCP safety warning
module: _is_internal_or_metadata_host, warn_ssrf_url, and
warn_shell_metachars.  50 new tests across four test classes.

Key scenarios covered:
- Loopback, link-local, RFC1918 private, and cloud metadata IPs
- IPv6 literals (raw and bracket-quoted)
- Hostname resolution to internal/public addresses
- Hostname resolution failures (OSError, UnicodeError)
- SSRF warnings for all internal URL patterns
- No warning for public URLs
- Shell metachar warnings for all nine metachar tokens
- Multiple env keys each warned independently
- None/integer env values do not crash
- Non-string command skipped gracefully

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@danielmeppiel danielmeppiel marked this pull request as ready for review April 23, 2026 00:04
Copilot AI review requested due to automatic review settings April 23, 2026 00:04
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds unit test coverage for apm_cli.install.mcp_warnings (install-time, non-blocking safety warnings for SSRF-ish URLs and shell metacharacters) to move the module from untested to effectively fully covered.

Changes:

  • Adds branch-complete tests for _is_internal_or_metadata_host (IP literals, bracketed IPv6, hostname resolution, and resolution failures).
  • Adds tests for warn_ssrf_url covering internal/metadata/private/public URLs plus malformed/no-host cases.
  • Adds tests for warn_shell_metachars across all metacharacter tokens, env typing edge-cases, and command handling.
Show a summary per file
File Description
tests/unit/install/test_mcp_warnings.py New unit test module covering F5/F7 warning helpers and host classification logic.

Copilot's findings

  • Files reviewed: 1/1 changed files
  • Comments generated: 2

Comment on lines +128 to +134
def test_internal_url_warns(self):
logger = self._make_logger()
warn_ssrf_url("http://127.0.0.1:8080/api", logger)
logger.warning.assert_called_once()
msg = logger.warning.call_args[0][0]
assert "127.0.0.1" in msg

Copy link

Copilot AI Apr 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid substring-matching a URL/host inside the warning text (assert "127.0.0.1" in msg). Our test convention (and CodeQL py/incomplete-url-substring-sanitization) requires parsing the URL from the message (e.g., extract the quoted URL and compare urlparse(...).hostname) or asserting the full expected message shape in a way that doesn't use in against URL-like data.

Copilot generated this review using guidance from repository custom instructions.
Comment on lines +9 to +13
import socket
from unittest.mock import MagicMock, patch

import pytest

Copy link

Copilot AI Apr 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

socket and pytest are imported but unused in this test module. Please remove unused imports to keep the test file minimal and avoid future lint/static-analysis noise.

Suggested change
import socket
from unittest.mock import MagicMock, patch
import pytest
from unittest.mock import MagicMock, patch

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants