forked from crystaldba/postgres-mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add CLI flags and tests for transport security #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
EliShteinman
wants to merge
3
commits into
DavidFHCh:feature/configurable-transport-security
Choose a base branch
from
EliShteinman:feature/transport-security-enhancements
base: feature/configurable-transport-security
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,224 @@ | ||
| import sys | ||
| from unittest.mock import AsyncMock | ||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
|
|
||
| _TRANSPORT_MOCK_MAP = { | ||
| "sse": "postgres_mcp.server.mcp.run_sse_async", | ||
| "streamable-http": "postgres_mcp.server.mcp.run_streamable_http_async", | ||
| } | ||
|
|
||
| _MCP_ENV_KEYS = [ | ||
| "MCP_ENABLE_DNS_REBINDING_PROTECTION", | ||
| "MCP_ALLOWED_HOSTS", | ||
| "MCP_ALLOWED_ORIGINS", | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("transport", ["sse", "streamable-http"]) | ||
| class TestTransportSecurityIntegration: | ||
| @pytest.fixture(autouse=True) | ||
| def _preserve_mcp_state(self, monkeypatch: pytest.MonkeyPatch): | ||
| from postgres_mcp.server import mcp | ||
|
|
||
| original_argv = sys.argv | ||
| original_security = mcp.settings.transport_security | ||
| for key in _MCP_ENV_KEYS: | ||
| monkeypatch.delenv(key, raising=False) | ||
| yield | ||
| sys.argv = original_argv | ||
| mcp.settings.transport_security = original_security | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_disable_dns_rebinding_via_cli_flag(self, transport: str): | ||
EliShteinman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| from postgres_mcp.server import main | ||
| from postgres_mcp.server import mcp | ||
|
|
||
| sys.argv = [ | ||
| "postgres_mcp", | ||
| "postgresql://user:password@localhost/db", | ||
| f"--transport={transport}", | ||
| "--disable-dns-rebinding-protection", | ||
| ] | ||
|
|
||
| with ( | ||
| patch("postgres_mcp.server.db_connection.pool_connect", AsyncMock()), | ||
| patch(_TRANSPORT_MOCK_MAP[transport], AsyncMock()), | ||
| ): | ||
| await main() | ||
| assert mcp.settings.transport_security is not None | ||
| assert mcp.settings.transport_security.enable_dns_rebinding_protection is False | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_disable_dns_rebinding_via_env(self, transport: str): | ||
| from postgres_mcp.server import main | ||
| from postgres_mcp.server import mcp | ||
|
|
||
| sys.argv = [ | ||
| "postgres_mcp", | ||
| "postgresql://user:password@localhost/db", | ||
| f"--transport={transport}", | ||
| ] | ||
|
|
||
| with ( | ||
| patch("postgres_mcp.server.db_connection.pool_connect", AsyncMock()), | ||
| patch(_TRANSPORT_MOCK_MAP[transport], AsyncMock()), | ||
| patch.dict("os.environ", {"MCP_ENABLE_DNS_REBINDING_PROTECTION": "false"}), | ||
| ): | ||
| await main() | ||
| assert mcp.settings.transport_security is not None | ||
| assert mcp.settings.transport_security.enable_dns_rebinding_protection is False | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_allowed_hosts_via_cli(self, transport: str): | ||
| from postgres_mcp.server import main | ||
| from postgres_mcp.server import mcp | ||
|
|
||
| sys.argv = [ | ||
| "postgres_mcp", | ||
| "postgresql://user:password@localhost/db", | ||
| f"--transport={transport}", | ||
| "--allowed-hosts", | ||
| "localhost:*,127.0.0.1:*", | ||
| ] | ||
|
|
||
| with ( | ||
| patch("postgres_mcp.server.db_connection.pool_connect", AsyncMock()), | ||
| patch(_TRANSPORT_MOCK_MAP[transport], AsyncMock()), | ||
| ): | ||
| await main() | ||
| assert mcp.settings.transport_security is not None | ||
| assert "localhost:*" in mcp.settings.transport_security.allowed_hosts | ||
| assert "127.0.0.1:*" in mcp.settings.transport_security.allowed_hosts | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_allowed_hosts_env_overrides_cli(self, transport: str): | ||
| from postgres_mcp.server import main | ||
| from postgres_mcp.server import mcp | ||
|
|
||
| sys.argv = [ | ||
| "postgres_mcp", | ||
| "postgresql://user:password@localhost/db", | ||
| f"--transport={transport}", | ||
| "--allowed-hosts", | ||
| "cli-host:*", | ||
| ] | ||
|
|
||
| with ( | ||
| patch("postgres_mcp.server.db_connection.pool_connect", AsyncMock()), | ||
| patch(_TRANSPORT_MOCK_MAP[transport], AsyncMock()), | ||
| patch.dict("os.environ", {"MCP_ALLOWED_HOSTS": "env-host:*"}), | ||
| ): | ||
| await main() | ||
| assert mcp.settings.transport_security is not None | ||
| assert "env-host:*" in mcp.settings.transport_security.allowed_hosts | ||
| assert "cli-host:*" not in mcp.settings.transport_security.allowed_hosts | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_allowed_origins_via_cli(self, transport: str): | ||
| from postgres_mcp.server import main | ||
| from postgres_mcp.server import mcp | ||
|
|
||
| sys.argv = [ | ||
| "postgres_mcp", | ||
| "postgresql://user:password@localhost/db", | ||
| f"--transport={transport}", | ||
| "--allowed-origins", | ||
| "http://localhost:*", | ||
| ] | ||
|
|
||
| with ( | ||
| patch("postgres_mcp.server.db_connection.pool_connect", AsyncMock()), | ||
| patch(_TRANSPORT_MOCK_MAP[transport], AsyncMock()), | ||
| ): | ||
| await main() | ||
| assert mcp.settings.transport_security is not None | ||
| assert "http://localhost:*" in mcp.settings.transport_security.allowed_origins | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_allowed_origins_env_overrides_cli(self, transport: str): | ||
| from postgres_mcp.server import main | ||
| from postgres_mcp.server import mcp | ||
|
|
||
| sys.argv = [ | ||
| "postgres_mcp", | ||
| "postgresql://user:password@localhost/db", | ||
| f"--transport={transport}", | ||
| "--allowed-origins", | ||
| "http://cli-origin:*", | ||
| ] | ||
|
|
||
| with ( | ||
| patch("postgres_mcp.server.db_connection.pool_connect", AsyncMock()), | ||
| patch(_TRANSPORT_MOCK_MAP[transport], AsyncMock()), | ||
| patch.dict("os.environ", {"MCP_ALLOWED_ORIGINS": "http://env-origin:*"}), | ||
| ): | ||
| await main() | ||
| assert mcp.settings.transport_security is not None | ||
| assert "http://env-origin:*" in mcp.settings.transport_security.allowed_origins | ||
| assert "http://cli-origin:*" not in mcp.settings.transport_security.allowed_origins | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_env_protection_true_overrides_cli_disable(self, transport: str): | ||
| from postgres_mcp.server import main | ||
| from postgres_mcp.server import mcp | ||
|
|
||
| sys.argv = [ | ||
| "postgres_mcp", | ||
| "postgresql://user:password@localhost/db", | ||
| f"--transport={transport}", | ||
| "--disable-dns-rebinding-protection", | ||
| ] | ||
|
|
||
| with ( | ||
| patch("postgres_mcp.server.db_connection.pool_connect", AsyncMock()), | ||
| patch(_TRANSPORT_MOCK_MAP[transport], AsyncMock()), | ||
| patch.dict("os.environ", {"MCP_ENABLE_DNS_REBINDING_PROTECTION": "true"}), | ||
| ): | ||
| await main() | ||
| assert mcp.settings.transport_security is not None | ||
| assert mcp.settings.transport_security.enable_dns_rebinding_protection is True | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_default_defers_to_fastmcp(self, transport: str): | ||
| from postgres_mcp.server import main | ||
| from postgres_mcp.server import mcp | ||
|
|
||
| sys.argv = [ | ||
| "postgres_mcp", | ||
| "postgresql://user:password@localhost/db", | ||
| f"--transport={transport}", | ||
| ] | ||
|
|
||
| with ( | ||
| patch("postgres_mcp.server.db_connection.pool_connect", AsyncMock()), | ||
| patch(_TRANSPORT_MOCK_MAP[transport], AsyncMock()), | ||
| ): | ||
| await main() | ||
| assert mcp.settings.transport_security is not None | ||
| assert mcp.settings.transport_security.enable_dns_rebinding_protection is True | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_database_url_after_flags_not_consumed(self, transport: str): | ||
| from postgres_mcp.server import main | ||
| from postgres_mcp.server import mcp | ||
|
|
||
| sys.argv = [ | ||
| "postgres_mcp", | ||
| f"--transport={transport}", | ||
| "--allowed-hosts", | ||
| "localhost:*,my-gateway:8080", | ||
| "--allowed-origins", | ||
| "http://localhost:*,http://my-gateway:*", | ||
| "postgresql://user:password@localhost/db", | ||
| ] | ||
|
|
||
| with ( | ||
| patch("postgres_mcp.server.db_connection.pool_connect", AsyncMock()), | ||
| patch(_TRANSPORT_MOCK_MAP[transport], AsyncMock()), | ||
| ): | ||
| await main() | ||
| assert mcp.settings.transport_security is not None | ||
| assert "localhost:*" in mcp.settings.transport_security.allowed_hosts | ||
| assert "my-gateway:8080" in mcp.settings.transport_security.allowed_hosts | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.