codeant-ai-for-open-source[bot] commented on code in PR #40673: URL: https://github.com/apache/superset/pull/40673#discussion_r3415481464
########## superset/migrations/versions/2026-06-03_10-00_78a40c08b4be_add_server_host_key_to_ssh_tunnels.py: ########## @@ -0,0 +1,48 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +"""add server_host_key to ssh_tunnels + +Adds a nullable ``server_host_key`` column to the ``ssh_tunnels`` table. It stores the +expected SSH server host key in authorized-key form (e.g. "ssh-ed25519 AAAA...") so +operators can opt in to verifying the SSH server's host key before a tunnel is opened. +This is a public key and is stored in plaintext (not encrypted). The column is +nullable, so existing tunnels are unaffected. + +Revision ID: 78a40c08b4be +Revises: b7c9d1e2f3a4 +Create Date: 2026-06-03 10:00:00.000000 + +""" + +import sqlalchemy as sa + +from superset.migrations.shared.utils import add_columns, drop_columns + +# revision identifiers, used by Alembic. +revision = "78a40c08b4be" +down_revision = "b7c9d1e2f3a4" + + +def upgrade() -> None: + add_columns( + "ssh_tunnels", + sa.Column("server_host_key", sa.Text(), nullable=True), + ) Review Comment: **Suggestion:** Add a short docstring to this newly added function describing the schema change it applies. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> This is a newly added Python function in a newly created file, and it has no docstring. The custom rule requires newly added Python functions and classes to include docstrings, so the suggestion correctly identifies a real violation. </details> [Fix in Cursor](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=aa1fa4dece264ed2ac1318cad4aff58a&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) | [Fix in VSCode Claude](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=aa1fa4dece264ed2ac1318cad4aff58a&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <details> <summary><b>Prompt for AI Agent 🤖 </b></summary> ```mdx This is a comment left during a code review. **Path:** superset/migrations/versions/2026-06-03_10-00_78a40c08b4be_add_server_host_key_to_ssh_tunnels.py **Line:** 40:44 **Comment:** *Custom Rule: Add a short docstring to this newly added function describing the schema change it applies. Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40673&comment_hash=73b725ff730e548569974992a3cad5a8ef4445fc7141dda5043c849779b23b7a&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40673&comment_hash=73b725ff730e548569974992a3cad5a8ef4445fc7141dda5043c849779b23b7a&reaction=dislike'>👎</a> ########## superset/migrations/versions/2026-06-03_10-00_78a40c08b4be_add_server_host_key_to_ssh_tunnels.py: ########## @@ -0,0 +1,48 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +"""add server_host_key to ssh_tunnels + +Adds a nullable ``server_host_key`` column to the ``ssh_tunnels`` table. It stores the +expected SSH server host key in authorized-key form (e.g. "ssh-ed25519 AAAA...") so +operators can opt in to verifying the SSH server's host key before a tunnel is opened. +This is a public key and is stored in plaintext (not encrypted). The column is +nullable, so existing tunnels are unaffected. + +Revision ID: 78a40c08b4be +Revises: b7c9d1e2f3a4 +Create Date: 2026-06-03 10:00:00.000000 + +""" + +import sqlalchemy as sa + +from superset.migrations.shared.utils import add_columns, drop_columns + +# revision identifiers, used by Alembic. +revision = "78a40c08b4be" +down_revision = "b7c9d1e2f3a4" + + +def upgrade() -> None: + add_columns( + "ssh_tunnels", + sa.Column("server_host_key", sa.Text(), nullable=True), + ) + + +def downgrade() -> None: + drop_columns("ssh_tunnels", "server_host_key") Review Comment: **Suggestion:** Add a short docstring to this newly added function describing the rollback behavior. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> This is also a newly added Python function in a newly created file, and it lacks a docstring. That matches the custom rule requiring newly added Python functions and classes to be documented inline. </details> [Fix in Cursor](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=d141795fcd8347fcbc480c50e017c636&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) | [Fix in VSCode Claude](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=d141795fcd8347fcbc480c50e017c636&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <details> <summary><b>Prompt for AI Agent 🤖 </b></summary> ```mdx This is a comment left during a code review. **Path:** superset/migrations/versions/2026-06-03_10-00_78a40c08b4be_add_server_host_key_to_ssh_tunnels.py **Line:** 47:48 **Comment:** *Custom Rule: Add a short docstring to this newly added function describing the rollback behavior. Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40673&comment_hash=8161b98eb0e89d464375defd7625ad0b2143f82bb738575a451c42f7cf58a7b5&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40673&comment_hash=8161b98eb0e89d464375defd7625ad0b2143f82bb738575a451c42f7cf58a7b5&reaction=dislike'>👎</a> ########## tests/unit_tests/extensions/ssh_test.py: ########## @@ -34,3 +67,196 @@ def test_ssh_tunnel_timeout_setting() -> None: factory.init_app(app) assert sshtunnel.TUNNEL_TIMEOUT == 123.0 assert sshtunnel.SSH_TIMEOUT == 321.0 + + +@patch("superset.extensions.ssh.socket.create_connection") +@patch("superset.extensions.ssh.paramiko.Transport") +def test_verify_host_key_match( + mock_transport_cls: Mock, mock_create_connection: Mock +) -> None: + """The server presents the same key we expect: verification passes.""" + server_key = paramiko.RSAKey.generate(2048) + manager = _make_manager(strict=False) + tunnel = _ssh_tunnel(_authorized_key(server_key)) + + transport = mock_transport_cls.return_value + transport.get_remote_server_key.return_value = server_key + + result = manager._verify_host_key(tunnel) # should not raise + + # The TCP connect is bounded by an explicit timeout, and the resulting + # socket is handed to Transport. + mock_create_connection.assert_called_once_with( + ("ssh.example.com", 22), timeout=321.0 + ) + mock_transport_cls.assert_called_once_with(mock_create_connection.return_value) + transport.start_client.assert_called_once() + transport.close.assert_called_once() + # The parsed expected key is returned so the caller can pin it on the tunnel. + assert result == server_key + + +@patch("superset.extensions.ssh.socket.create_connection") +@patch("superset.extensions.ssh.paramiko.Transport") +def test_verify_host_key_mismatch_raises( + mock_transport_cls: Mock, mock_create_connection: Mock +) -> None: + """The server presents a different key than expected: verification fails.""" + expected_key = paramiko.RSAKey.generate(2048) + presented_key = paramiko.RSAKey.generate(2048) + manager = _make_manager(strict=False) + tunnel = _ssh_tunnel(_authorized_key(expected_key)) + + transport = mock_transport_cls.return_value + transport.get_remote_server_key.return_value = presented_key + + with pytest.raises(SSHTunnelHostKeyVerificationError): + manager._verify_host_key(tunnel) + + mock_create_connection.assert_called_once() + transport.close.assert_called_once() + + +@patch("superset.extensions.ssh.socket.create_connection") +def test_verify_host_key_connect_failure_raises( + mock_create_connection: Mock, +) -> None: Review Comment: **Suggestion:** Add a docstring directly under this new test function to describe the scenario and expected error behavior. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> This is a newly added test function and it does not have a docstring; it only has a comment. The custom rule requires new Python functions/classes to include docstrings inline, so the suggestion correctly identifies a real violation. </details> [Fix in Cursor](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=e32a2a6f1aa34678882b70eea7af6e7e&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) | [Fix in VSCode Claude](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=e32a2a6f1aa34678882b70eea7af6e7e&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <details> <summary><b>Prompt for AI Agent 🤖 </b></summary> ```mdx This is a comment left during a code review. **Path:** tests/unit_tests/extensions/ssh_test.py **Line:** 121:123 **Comment:** *Custom Rule: Add a docstring directly under this new test function to describe the scenario and expected error behavior. Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40673&comment_hash=eec04559f47bbde9ec7c7a97281c06faacadc72c565a5bf20e75833e8ee606ba&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40673&comment_hash=eec04559f47bbde9ec7c7a97281c06faacadc72c565a5bf20e75833e8ee606ba&reaction=dislike'>👎</a> ########## tests/unit_tests/extensions/ssh_test.py: ########## @@ -34,3 +67,196 @@ def test_ssh_tunnel_timeout_setting() -> None: factory.init_app(app) assert sshtunnel.TUNNEL_TIMEOUT == 123.0 assert sshtunnel.SSH_TIMEOUT == 321.0 + + +@patch("superset.extensions.ssh.socket.create_connection") +@patch("superset.extensions.ssh.paramiko.Transport") +def test_verify_host_key_match( + mock_transport_cls: Mock, mock_create_connection: Mock +) -> None: + """The server presents the same key we expect: verification passes.""" + server_key = paramiko.RSAKey.generate(2048) + manager = _make_manager(strict=False) + tunnel = _ssh_tunnel(_authorized_key(server_key)) + + transport = mock_transport_cls.return_value + transport.get_remote_server_key.return_value = server_key + + result = manager._verify_host_key(tunnel) # should not raise + + # The TCP connect is bounded by an explicit timeout, and the resulting + # socket is handed to Transport. + mock_create_connection.assert_called_once_with( + ("ssh.example.com", 22), timeout=321.0 + ) + mock_transport_cls.assert_called_once_with(mock_create_connection.return_value) + transport.start_client.assert_called_once() + transport.close.assert_called_once() + # The parsed expected key is returned so the caller can pin it on the tunnel. + assert result == server_key + + +@patch("superset.extensions.ssh.socket.create_connection") +@patch("superset.extensions.ssh.paramiko.Transport") +def test_verify_host_key_mismatch_raises( + mock_transport_cls: Mock, mock_create_connection: Mock +) -> None: + """The server presents a different key than expected: verification fails.""" + expected_key = paramiko.RSAKey.generate(2048) + presented_key = paramiko.RSAKey.generate(2048) + manager = _make_manager(strict=False) + tunnel = _ssh_tunnel(_authorized_key(expected_key)) + + transport = mock_transport_cls.return_value + transport.get_remote_server_key.return_value = presented_key + + with pytest.raises(SSHTunnelHostKeyVerificationError): + manager._verify_host_key(tunnel) + + mock_create_connection.assert_called_once() + transport.close.assert_called_once() + + +@patch("superset.extensions.ssh.socket.create_connection") +def test_verify_host_key_connect_failure_raises( + mock_create_connection: Mock, +) -> None: + # A bounded TCP connect failure surfaces as a host-key verification error. + manager = _make_manager(strict=False) + server_key = paramiko.RSAKey.generate(2048) + tunnel = _ssh_tunnel(_authorized_key(server_key)) + + mock_create_connection.side_effect = OSError("connection refused") + + with pytest.raises(SSHTunnelHostKeyVerificationError): + manager._verify_host_key(tunnel) + + +@patch("superset.extensions.ssh.paramiko.Transport") +def test_verify_host_key_unset_non_strict_skips(mock_transport_cls: Mock) -> None: Review Comment: **Suggestion:** Add a docstring to this new test function so its non-strict behavior is explicitly documented inline. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> The function is newly introduced and has no docstring. A comment follows the signature, but that does not satisfy the rule requiring docstrings for new functions/classes. </details> [Fix in Cursor](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=21f5624aa12a477c8a24343fc8225551&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) | [Fix in VSCode Claude](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=21f5624aa12a477c8a24343fc8225551&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <details> <summary><b>Prompt for AI Agent 🤖 </b></summary> ```mdx This is a comment left during a code review. **Path:** tests/unit_tests/extensions/ssh_test.py **Line:** 136:136 **Comment:** *Custom Rule: Add a docstring to this new test function so its non-strict behavior is explicitly documented inline. Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40673&comment_hash=c835ca7df43db344666f745ab913492f3ecf7672552d225c12c67c4a45d6aa87&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40673&comment_hash=c835ca7df43db344666f745ab913492f3ecf7672552d225c12c67c4a45d6aa87&reaction=dislike'>👎</a> ########## tests/unit_tests/extensions/ssh_test.py: ########## @@ -34,3 +67,196 @@ def test_ssh_tunnel_timeout_setting() -> None: factory.init_app(app) assert sshtunnel.TUNNEL_TIMEOUT == 123.0 assert sshtunnel.SSH_TIMEOUT == 321.0 + + +@patch("superset.extensions.ssh.socket.create_connection") +@patch("superset.extensions.ssh.paramiko.Transport") +def test_verify_host_key_match( + mock_transport_cls: Mock, mock_create_connection: Mock +) -> None: + """The server presents the same key we expect: verification passes.""" + server_key = paramiko.RSAKey.generate(2048) + manager = _make_manager(strict=False) + tunnel = _ssh_tunnel(_authorized_key(server_key)) + + transport = mock_transport_cls.return_value + transport.get_remote_server_key.return_value = server_key + + result = manager._verify_host_key(tunnel) # should not raise + + # The TCP connect is bounded by an explicit timeout, and the resulting + # socket is handed to Transport. + mock_create_connection.assert_called_once_with( + ("ssh.example.com", 22), timeout=321.0 + ) + mock_transport_cls.assert_called_once_with(mock_create_connection.return_value) + transport.start_client.assert_called_once() + transport.close.assert_called_once() + # The parsed expected key is returned so the caller can pin it on the tunnel. + assert result == server_key + + +@patch("superset.extensions.ssh.socket.create_connection") +@patch("superset.extensions.ssh.paramiko.Transport") +def test_verify_host_key_mismatch_raises( + mock_transport_cls: Mock, mock_create_connection: Mock +) -> None: + """The server presents a different key than expected: verification fails.""" + expected_key = paramiko.RSAKey.generate(2048) + presented_key = paramiko.RSAKey.generate(2048) + manager = _make_manager(strict=False) + tunnel = _ssh_tunnel(_authorized_key(expected_key)) + + transport = mock_transport_cls.return_value + transport.get_remote_server_key.return_value = presented_key + + with pytest.raises(SSHTunnelHostKeyVerificationError): + manager._verify_host_key(tunnel) + + mock_create_connection.assert_called_once() + transport.close.assert_called_once() + + +@patch("superset.extensions.ssh.socket.create_connection") +def test_verify_host_key_connect_failure_raises( + mock_create_connection: Mock, +) -> None: + # A bounded TCP connect failure surfaces as a host-key verification error. + manager = _make_manager(strict=False) + server_key = paramiko.RSAKey.generate(2048) + tunnel = _ssh_tunnel(_authorized_key(server_key)) + + mock_create_connection.side_effect = OSError("connection refused") + + with pytest.raises(SSHTunnelHostKeyVerificationError): + manager._verify_host_key(tunnel) + + +@patch("superset.extensions.ssh.paramiko.Transport") +def test_verify_host_key_unset_non_strict_skips(mock_transport_cls: Mock) -> None: + # Back-compat: no expected key + strict checking off => no verification at all. + manager = _make_manager(strict=False) + tunnel = _ssh_tunnel(None) + + assert manager._verify_host_key(tunnel) is None # should not raise + + mock_transport_cls.assert_not_called() + + +@patch("superset.extensions.ssh.paramiko.Transport") +def test_verify_host_key_unset_strict_raises(mock_transport_cls: Mock) -> None: + # Fail-closed: no expected key + strict checking on => reject. + manager = _make_manager(strict=True) + tunnel = _ssh_tunnel(None) + + with pytest.raises(SSHTunnelHostKeyVerificationError): + manager._verify_host_key(tunnel) + + mock_transport_cls.assert_not_called() + + +@patch("superset.extensions.ssh.socket.create_connection") +@patch("superset.extensions.ssh.paramiko.Transport") +def test_verify_host_key_match_ignores_comment_and_whitespace( + mock_transport_cls: Mock, + mock_create_connection: Mock, +) -> None: + # The stored key may carry a trailing comment and extra whitespace. + server_key = paramiko.RSAKey.generate(2048) + manager = _make_manager(strict=False) + stored = f" {_authorized_key(server_key)} user@host " + tunnel = _ssh_tunnel(stored) + + transport = mock_transport_cls.return_value + transport.get_remote_server_key.return_value = server_key + + manager._verify_host_key(tunnel) # should not raise + + # Whitespace/comment stripping must not short-circuit verification: the + # bounded TCP connect and Transport handshake still run as in the plain + # match case. + mock_create_connection.assert_called_once_with( + ("ssh.example.com", 22), timeout=321.0 + ) + mock_transport_cls.assert_called_once_with(mock_create_connection.return_value) + transport.start_client.assert_called_once() + transport.close.assert_called_once() + + +def test_verify_host_key_invalid_expected_raises() -> None: + # A malformed expected key is rejected before any network connection. + manager = _make_manager(strict=False) + tunnel = _ssh_tunnel("not-a-valid-key") + + with pytest.raises(SSHTunnelHostKeyVerificationError): + manager._verify_host_key(tunnel) + + +def test_verify_host_key_unknown_key_type_raises() -> None: + """An unsupported key type is wrapped in the verification error, not leaked.""" + manager = _make_manager(strict=False) + server_key = paramiko.RSAKey.generate(2048) + tunnel = _ssh_tunnel(f"ssh-bogus {server_key.get_base64()}") + + with pytest.raises(SSHTunnelHostKeyVerificationError): + manager._verify_host_key(tunnel) + + +@patch("superset.extensions.ssh.sshtunnel.open_tunnel") +@patch("superset.extensions.ssh.socket.create_connection") +@patch("superset.extensions.ssh.paramiko.Transport") +def test_create_tunnel_pins_verified_host_key( + mock_transport_cls: Mock, + mock_create_connection: Mock, + mock_open_tunnel: Mock, +) -> None: Review Comment: **Suggestion:** Add a docstring for this new multi-mock test function to document the host-key pinning behavior being validated. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> The test function is newly added and lacks a docstring. The surrounding comments do not satisfy the custom rule, so this is a real docstring omission. </details> [Fix in Cursor](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=49472bda265b41beab24bc2d7527e50d&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) | [Fix in VSCode Claude](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=49472bda265b41beab24bc2d7527e50d&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <details> <summary><b>Prompt for AI Agent 🤖 </b></summary> ```mdx This is a comment left during a code review. **Path:** tests/unit_tests/extensions/ssh_test.py **Line:** 206:212 **Comment:** *Custom Rule: Add a docstring for this new multi-mock test function to document the host-key pinning behavior being validated. Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40673&comment_hash=7677689972ff4725cc3066fbbf9d817e2006b44536ce943659fe1156e6d230b5&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40673&comment_hash=7677689972ff4725cc3066fbbf9d817e2006b44536ce943659fe1156e6d230b5&reaction=dislike'>👎</a> ########## tests/unit_tests/extensions/ssh_test.py: ########## @@ -34,3 +67,196 @@ def test_ssh_tunnel_timeout_setting() -> None: factory.init_app(app) assert sshtunnel.TUNNEL_TIMEOUT == 123.0 assert sshtunnel.SSH_TIMEOUT == 321.0 + + +@patch("superset.extensions.ssh.socket.create_connection") +@patch("superset.extensions.ssh.paramiko.Transport") +def test_verify_host_key_match( + mock_transport_cls: Mock, mock_create_connection: Mock +) -> None: + """The server presents the same key we expect: verification passes.""" + server_key = paramiko.RSAKey.generate(2048) + manager = _make_manager(strict=False) + tunnel = _ssh_tunnel(_authorized_key(server_key)) + + transport = mock_transport_cls.return_value + transport.get_remote_server_key.return_value = server_key + + result = manager._verify_host_key(tunnel) # should not raise + + # The TCP connect is bounded by an explicit timeout, and the resulting + # socket is handed to Transport. + mock_create_connection.assert_called_once_with( + ("ssh.example.com", 22), timeout=321.0 + ) + mock_transport_cls.assert_called_once_with(mock_create_connection.return_value) + transport.start_client.assert_called_once() + transport.close.assert_called_once() + # The parsed expected key is returned so the caller can pin it on the tunnel. + assert result == server_key + + +@patch("superset.extensions.ssh.socket.create_connection") +@patch("superset.extensions.ssh.paramiko.Transport") +def test_verify_host_key_mismatch_raises( + mock_transport_cls: Mock, mock_create_connection: Mock +) -> None: + """The server presents a different key than expected: verification fails.""" + expected_key = paramiko.RSAKey.generate(2048) + presented_key = paramiko.RSAKey.generate(2048) + manager = _make_manager(strict=False) + tunnel = _ssh_tunnel(_authorized_key(expected_key)) + + transport = mock_transport_cls.return_value + transport.get_remote_server_key.return_value = presented_key + + with pytest.raises(SSHTunnelHostKeyVerificationError): + manager._verify_host_key(tunnel) + + mock_create_connection.assert_called_once() + transport.close.assert_called_once() + + +@patch("superset.extensions.ssh.socket.create_connection") +def test_verify_host_key_connect_failure_raises( + mock_create_connection: Mock, +) -> None: + # A bounded TCP connect failure surfaces as a host-key verification error. + manager = _make_manager(strict=False) + server_key = paramiko.RSAKey.generate(2048) + tunnel = _ssh_tunnel(_authorized_key(server_key)) + + mock_create_connection.side_effect = OSError("connection refused") + + with pytest.raises(SSHTunnelHostKeyVerificationError): + manager._verify_host_key(tunnel) + + +@patch("superset.extensions.ssh.paramiko.Transport") +def test_verify_host_key_unset_non_strict_skips(mock_transport_cls: Mock) -> None: + # Back-compat: no expected key + strict checking off => no verification at all. + manager = _make_manager(strict=False) + tunnel = _ssh_tunnel(None) + + assert manager._verify_host_key(tunnel) is None # should not raise + + mock_transport_cls.assert_not_called() + + +@patch("superset.extensions.ssh.paramiko.Transport") +def test_verify_host_key_unset_strict_raises(mock_transport_cls: Mock) -> None: + # Fail-closed: no expected key + strict checking on => reject. + manager = _make_manager(strict=True) + tunnel = _ssh_tunnel(None) + + with pytest.raises(SSHTunnelHostKeyVerificationError): + manager._verify_host_key(tunnel) + + mock_transport_cls.assert_not_called() + + +@patch("superset.extensions.ssh.socket.create_connection") +@patch("superset.extensions.ssh.paramiko.Transport") +def test_verify_host_key_match_ignores_comment_and_whitespace( + mock_transport_cls: Mock, + mock_create_connection: Mock, +) -> None: + # The stored key may carry a trailing comment and extra whitespace. + server_key = paramiko.RSAKey.generate(2048) + manager = _make_manager(strict=False) + stored = f" {_authorized_key(server_key)} user@host " + tunnel = _ssh_tunnel(stored) + + transport = mock_transport_cls.return_value + transport.get_remote_server_key.return_value = server_key + + manager._verify_host_key(tunnel) # should not raise + + # Whitespace/comment stripping must not short-circuit verification: the + # bounded TCP connect and Transport handshake still run as in the plain + # match case. + mock_create_connection.assert_called_once_with( + ("ssh.example.com", 22), timeout=321.0 + ) + mock_transport_cls.assert_called_once_with(mock_create_connection.return_value) + transport.start_client.assert_called_once() + transport.close.assert_called_once() + + +def test_verify_host_key_invalid_expected_raises() -> None: + # A malformed expected key is rejected before any network connection. + manager = _make_manager(strict=False) + tunnel = _ssh_tunnel("not-a-valid-key") + + with pytest.raises(SSHTunnelHostKeyVerificationError): + manager._verify_host_key(tunnel) + + +def test_verify_host_key_unknown_key_type_raises() -> None: + """An unsupported key type is wrapped in the verification error, not leaked.""" + manager = _make_manager(strict=False) + server_key = paramiko.RSAKey.generate(2048) + tunnel = _ssh_tunnel(f"ssh-bogus {server_key.get_base64()}") + + with pytest.raises(SSHTunnelHostKeyVerificationError): + manager._verify_host_key(tunnel) + + +@patch("superset.extensions.ssh.sshtunnel.open_tunnel") +@patch("superset.extensions.ssh.socket.create_connection") +@patch("superset.extensions.ssh.paramiko.Transport") +def test_create_tunnel_pins_verified_host_key( + mock_transport_cls: Mock, + mock_create_connection: Mock, + mock_open_tunnel: Mock, +) -> None: + # When an expected host key is configured and verified, it is also pinned on the + # tunnel's own connection (``ssh_host_key``) so paramiko verifies the host that + # actually carries traffic on the same transport — closing the probe-vs-tunnel + # TOCTOU gap rather than trusting only the pre-flight probe. + server_key = paramiko.RSAKey.generate(2048) + manager = _make_manager(strict=False) + tunnel = _ssh_tunnel(_authorized_key(server_key)) + tunnel.username = "user" + tunnel.password = None + tunnel.private_key = None + + mock_transport_cls.return_value.get_remote_server_key.return_value = server_key + + manager.create_tunnel(tunnel, "postgresql://u:p@db:5432/ex") + + _, kwargs = mock_open_tunnel.call_args + assert kwargs["ssh_host_key"] == server_key + + +@patch("superset.extensions.ssh.sshtunnel.open_tunnel") +def test_create_tunnel_without_host_key_does_not_pin(mock_open_tunnel: Mock) -> None: + # No expected key configured (non-strict): nothing is pinned, preserving the + # prior behavior. + manager = _make_manager(strict=False) + tunnel = _ssh_tunnel(None) + tunnel.username = "user" + tunnel.password = None + tunnel.private_key = None + + manager.create_tunnel(tunnel, "postgresql://u:p@db:5432/ex") + + _, kwargs = mock_open_tunnel.call_args + assert "ssh_host_key" not in kwargs + + +def test_ssh_tunnel_schema_round_trips_server_host_key() -> None: Review Comment: **Suggestion:** Add a docstring to this new test function to document that schema serialization preserves the configured host key. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> This newly added test function has no docstring. Because the rule requires docstrings for new functions and classes, the suggestion accurately flags a real violation. </details> [Fix in Cursor](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=9743ac651bfd43c0a462b7e687826eba&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) | [Fix in VSCode Claude](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=9743ac651bfd43c0a462b7e687826eba&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <details> <summary><b>Prompt for AI Agent 🤖 </b></summary> ```mdx This is a comment left during a code review. **Path:** tests/unit_tests/extensions/ssh_test.py **Line:** 248:248 **Comment:** *Custom Rule: Add a docstring to this new test function to document that schema serialization preserves the configured host key. Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40673&comment_hash=ea4c23adbec669afdb216c52ad594af421cab9879b7f2dcfa1823d42cdf0762d&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40673&comment_hash=ea4c23adbec669afdb216c52ad594af421cab9879b7f2dcfa1823d42cdf0762d&reaction=dislike'>👎</a> ########## tests/unit_tests/extensions/ssh_test.py: ########## @@ -34,3 +67,196 @@ def test_ssh_tunnel_timeout_setting() -> None: factory.init_app(app) assert sshtunnel.TUNNEL_TIMEOUT == 123.0 assert sshtunnel.SSH_TIMEOUT == 321.0 + + +@patch("superset.extensions.ssh.socket.create_connection") +@patch("superset.extensions.ssh.paramiko.Transport") +def test_verify_host_key_match( + mock_transport_cls: Mock, mock_create_connection: Mock +) -> None: + """The server presents the same key we expect: verification passes.""" + server_key = paramiko.RSAKey.generate(2048) + manager = _make_manager(strict=False) + tunnel = _ssh_tunnel(_authorized_key(server_key)) + + transport = mock_transport_cls.return_value + transport.get_remote_server_key.return_value = server_key + + result = manager._verify_host_key(tunnel) # should not raise + + # The TCP connect is bounded by an explicit timeout, and the resulting + # socket is handed to Transport. + mock_create_connection.assert_called_once_with( + ("ssh.example.com", 22), timeout=321.0 + ) + mock_transport_cls.assert_called_once_with(mock_create_connection.return_value) + transport.start_client.assert_called_once() + transport.close.assert_called_once() + # The parsed expected key is returned so the caller can pin it on the tunnel. + assert result == server_key + + +@patch("superset.extensions.ssh.socket.create_connection") +@patch("superset.extensions.ssh.paramiko.Transport") +def test_verify_host_key_mismatch_raises( + mock_transport_cls: Mock, mock_create_connection: Mock +) -> None: + """The server presents a different key than expected: verification fails.""" + expected_key = paramiko.RSAKey.generate(2048) + presented_key = paramiko.RSAKey.generate(2048) + manager = _make_manager(strict=False) + tunnel = _ssh_tunnel(_authorized_key(expected_key)) + + transport = mock_transport_cls.return_value + transport.get_remote_server_key.return_value = presented_key + + with pytest.raises(SSHTunnelHostKeyVerificationError): + manager._verify_host_key(tunnel) + + mock_create_connection.assert_called_once() + transport.close.assert_called_once() + + +@patch("superset.extensions.ssh.socket.create_connection") +def test_verify_host_key_connect_failure_raises( + mock_create_connection: Mock, +) -> None: + # A bounded TCP connect failure surfaces as a host-key verification error. + manager = _make_manager(strict=False) + server_key = paramiko.RSAKey.generate(2048) + tunnel = _ssh_tunnel(_authorized_key(server_key)) + + mock_create_connection.side_effect = OSError("connection refused") + + with pytest.raises(SSHTunnelHostKeyVerificationError): + manager._verify_host_key(tunnel) + + +@patch("superset.extensions.ssh.paramiko.Transport") +def test_verify_host_key_unset_non_strict_skips(mock_transport_cls: Mock) -> None: + # Back-compat: no expected key + strict checking off => no verification at all. + manager = _make_manager(strict=False) + tunnel = _ssh_tunnel(None) + + assert manager._verify_host_key(tunnel) is None # should not raise + + mock_transport_cls.assert_not_called() + + +@patch("superset.extensions.ssh.paramiko.Transport") +def test_verify_host_key_unset_strict_raises(mock_transport_cls: Mock) -> None: Review Comment: **Suggestion:** Add a docstring immediately after the function definition to document the strict-mode failure expectation. [custom_rule] **Severity Level:** Minor ⚠️ <details> <summary><b>Why it matters? 🤔 </b></summary> This is another newly added test function without a docstring. Since the rule requires newly added Python functions and classes to be documented inline, the suggestion is valid. </details> [Fix in Cursor](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=b604ce5da9654e819bf7ab5b2180f4ee&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) | [Fix in VSCode Claude](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=b604ce5da9654e819bf7ab5b2180f4ee&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <details> <summary><b>Prompt for AI Agent 🤖 </b></summary> ```mdx This is a comment left during a code review. **Path:** tests/unit_tests/extensions/ssh_test.py **Line:** 147:147 **Comment:** *Custom Rule: Add a docstring immediately after the function definition to document the strict-mode failure expectation. Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40673&comment_hash=70ed181f2cfec7dfba3ae79c329e105d3a0b5915035ea2f428d604820a8e87e9&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40673&comment_hash=70ed181f2cfec7dfba3ae79c329e105d3a0b5915035ea2f428d604820a8e87e9&reaction=dislike'>👎</a> -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
