|
| 1 | +from unittest.mock import ( |
| 2 | + AsyncMock, |
| 3 | + MagicMock, |
| 4 | + patch, |
| 5 | +) |
| 6 | + |
1 | 7 | import pytest |
2 | 8 |
|
3 | 9 | from galaxy.util.unittest_utils import skip_if_github_down |
@@ -88,3 +94,80 @@ def test_proxy_handles_encoding(self): |
88 | 94 | assert len(response.content) > 0 |
89 | 95 | # Verify content-encoding header was properly filtered out (no double decompression) |
90 | 96 | assert "content-encoding" not in response.headers |
| 97 | + |
| 98 | + @patch("galaxy.webapps.galaxy.api.proxy.httpx.AsyncClient") |
| 99 | + def test_proxy_validates_redirects(self, mock_client_class): |
| 100 | + """Test that redirects are validated.""" |
| 101 | + # Create mock responses - redirect to a local file (invalid scheme) |
| 102 | + redirect_response = MagicMock() |
| 103 | + redirect_response.status_code = 302 |
| 104 | + redirect_response.headers = {"location": "file://internal-server/secret-files"} |
| 105 | + redirect_response.aclose = AsyncMock() |
| 106 | + |
| 107 | + # Setup mock client |
| 108 | + mock_client = MagicMock() |
| 109 | + mock_client.request = AsyncMock(return_value=redirect_response) |
| 110 | + mock_client.aclose = AsyncMock() |
| 111 | + mock_client_class.return_value = mock_client |
| 112 | + |
| 113 | + # Attempt to proxy a URL that redirects to file:// (should be blocked) |
| 114 | + response = self._get("proxy?url=https://evil.com/redirect") |
| 115 | + |
| 116 | + # Should fail with 400 Bad Request due to invalid redirect URL scheme |
| 117 | + self._assert_status_code_is(response, 400) |
| 118 | + assert "Invalid URL format" in response.json()["err_msg"] |
| 119 | + |
| 120 | + @patch("galaxy.webapps.galaxy.api.proxy.httpx.AsyncClient") |
| 121 | + def test_proxy_follows_valid_redirects(self, mock_client_class): |
| 122 | + """Test that valid redirects are followed after validation.""" |
| 123 | + # Create mock responses |
| 124 | + redirect_response = MagicMock() |
| 125 | + redirect_response.status_code = 301 |
| 126 | + redirect_response.headers = {"location": "https://example.com/final"} |
| 127 | + redirect_response.aclose = AsyncMock() |
| 128 | + |
| 129 | + final_response = MagicMock() |
| 130 | + final_response.status_code = 200 |
| 131 | + final_response.headers = {"content-type": "text/plain"} |
| 132 | + final_response.aclose = AsyncMock() |
| 133 | + |
| 134 | + # Create async generator for streaming |
| 135 | + async def mock_stream(): |
| 136 | + yield b"test content" |
| 137 | + |
| 138 | + final_response.aiter_bytes = mock_stream |
| 139 | + |
| 140 | + # Setup mock client to return redirect first, then final response |
| 141 | + mock_client = MagicMock() |
| 142 | + mock_client.request = AsyncMock(side_effect=[redirect_response, final_response]) |
| 143 | + mock_client.aclose = AsyncMock() |
| 144 | + mock_client_class.return_value = mock_client |
| 145 | + |
| 146 | + # Proxy a URL that redirects to a valid external URL |
| 147 | + response = self._get("proxy?url=https://example.com/redirect") |
| 148 | + |
| 149 | + # Should succeed and follow the redirect |
| 150 | + self._assert_status_code_is_ok(response) |
| 151 | + assert b"test content" in response.content |
| 152 | + |
| 153 | + @patch("galaxy.webapps.galaxy.api.proxy.httpx.AsyncClient") |
| 154 | + def test_proxy_blocks_too_many_redirects(self, mock_client_class): |
| 155 | + """Test that excessive redirects are blocked to prevent redirect loops.""" |
| 156 | + # Create a mock response that always redirects |
| 157 | + redirect_response = MagicMock() |
| 158 | + redirect_response.status_code = 302 |
| 159 | + redirect_response.headers = {"location": "https://example.com/loop"} |
| 160 | + redirect_response.aclose = AsyncMock() |
| 161 | + |
| 162 | + # Setup mock client |
| 163 | + mock_client = MagicMock() |
| 164 | + mock_client.request = AsyncMock(return_value=redirect_response) |
| 165 | + mock_client.aclose = AsyncMock() |
| 166 | + mock_client_class.return_value = mock_client |
| 167 | + |
| 168 | + # Attempt to proxy a URL that loops redirects |
| 169 | + response = self._get("proxy?url=https://example.com/loop") |
| 170 | + |
| 171 | + # Should fail with 400 Bad Request due to too many redirects |
| 172 | + self._assert_status_code_is(response, 400) |
| 173 | + assert "Too many redirects" in response.json()["err_msg"] |
0 commit comments