|
4 | 4 | import unittest |
5 | 5 | from unittest import mock |
6 | 6 |
|
7 | | -from azure.monitor.opentelemetry.exporter._quickpulse._policy import _QuickpulseRedirectPolicy |
| 7 | +from azure.monitor.opentelemetry.exporter._quickpulse._policy import ( |
| 8 | + _QuickpulseRedirectPolicy, |
| 9 | + _is_redirect_target_allowed, |
| 10 | +) |
8 | 11 |
|
9 | 12 |
|
10 | 13 | # pylint: disable=line-too-long |
@@ -76,3 +79,131 @@ def test_get_redirect_location_no_client(self): |
76 | 79 | redirect_location = policy.get_redirect_location(pipeline_resp_mock) |
77 | 80 | self.assertEqual(redirect_location, "https://eastus.livediagnostics.monitor.azure.com/QuickPulseService.svc") |
78 | 81 | self.assertIsNone(policy._qp_client_ref) |
| 82 | + |
| 83 | + def test_get_redirect_location_rejects_untrusted_host(self): |
| 84 | + policy = _QuickpulseRedirectPolicy() |
| 85 | + pipeline_resp_mock = mock.Mock() |
| 86 | + http_resp_mock = mock.Mock() |
| 87 | + headers = { |
| 88 | + "x-ms-qps-service-endpoint-redirect-v2": "https://evil.attacker.com/exfiltrate" |
| 89 | + } |
| 90 | + http_resp_mock.headers = headers |
| 91 | + pipeline_resp_mock.http_response = http_resp_mock |
| 92 | + qp_client_mock = mock.Mock() |
| 93 | + client_mock = mock.Mock() |
| 94 | + client_mock._base_url = "https://original.livediagnostics.monitor.azure.com" |
| 95 | + qp_client_mock._client = client_mock |
| 96 | + qp_client_ref = weakref.ref(qp_client_mock) |
| 97 | + policy._qp_client_ref = qp_client_ref |
| 98 | + redirect_location = policy.get_redirect_location(pipeline_resp_mock) |
| 99 | + # Redirect should be rejected and return None |
| 100 | + self.assertIsNone(redirect_location) |
| 101 | + # Base URL must not be changed |
| 102 | + self.assertEqual(client_mock._base_url, "https://original.livediagnostics.monitor.azure.com") |
| 103 | + |
| 104 | + def test_get_redirect_location_rejects_http_scheme(self): |
| 105 | + policy = _QuickpulseRedirectPolicy() |
| 106 | + pipeline_resp_mock = mock.Mock() |
| 107 | + http_resp_mock = mock.Mock() |
| 108 | + headers = { |
| 109 | + "x-ms-qps-service-endpoint-redirect-v2": "http://eastus.livediagnostics.monitor.azure.com/QuickPulseService.svc" |
| 110 | + } |
| 111 | + http_resp_mock.headers = headers |
| 112 | + pipeline_resp_mock.http_response = http_resp_mock |
| 113 | + qp_client_mock = mock.Mock() |
| 114 | + client_mock = mock.Mock() |
| 115 | + client_mock._base_url = "https://original.livediagnostics.monitor.azure.com" |
| 116 | + qp_client_mock._client = client_mock |
| 117 | + qp_client_ref = weakref.ref(qp_client_mock) |
| 118 | + policy._qp_client_ref = qp_client_ref |
| 119 | + redirect_location = policy.get_redirect_location(pipeline_resp_mock) |
| 120 | + # HTTP downgrade should be rejected |
| 121 | + self.assertIsNone(redirect_location) |
| 122 | + self.assertEqual(client_mock._base_url, "https://original.livediagnostics.monitor.azure.com") |
| 123 | + |
| 124 | + def test_get_redirect_location_allows_visualstudio_domain(self): |
| 125 | + policy = _QuickpulseRedirectPolicy() |
| 126 | + pipeline_resp_mock = mock.Mock() |
| 127 | + http_resp_mock = mock.Mock() |
| 128 | + headers = { |
| 129 | + "x-ms-qps-service-endpoint-redirect-v2": "https://rt.services.visualstudio.com/QuickPulseService.svc" |
| 130 | + } |
| 131 | + http_resp_mock.headers = headers |
| 132 | + pipeline_resp_mock.http_response = http_resp_mock |
| 133 | + qp_client_mock = mock.Mock() |
| 134 | + client_mock = mock.Mock() |
| 135 | + client_mock._base_url = "https://original.livediagnostics.monitor.azure.com" |
| 136 | + qp_client_mock._client = client_mock |
| 137 | + qp_client_ref = weakref.ref(qp_client_mock) |
| 138 | + policy._qp_client_ref = qp_client_ref |
| 139 | + redirect_location = policy.get_redirect_location(pipeline_resp_mock) |
| 140 | + self.assertEqual(redirect_location, "https://rt.services.visualstudio.com/QuickPulseService.svc") |
| 141 | + self.assertEqual(client_mock._base_url, "https://rt.services.visualstudio.com") |
| 142 | + |
| 143 | + def test_get_redirect_location_rejects_spoofed_suffix(self): |
| 144 | + """Attacker uses a domain that contains an allowed suffix but is not actually that domain.""" |
| 145 | + policy = _QuickpulseRedirectPolicy() |
| 146 | + pipeline_resp_mock = mock.Mock() |
| 147 | + http_resp_mock = mock.Mock() |
| 148 | + # Reject a host like "monitor.azure.com.evil.com": it starts with the allowed-looking |
| 149 | + # "monitor.azure.com" string, but the actual hostname is a subdomain of "evil.com". |
| 150 | + headers = { |
| 151 | + "x-ms-qps-service-endpoint-redirect-v2": "https://monitor.azure.com.evil.com/exfiltrate" |
| 152 | + } |
| 153 | + http_resp_mock.headers = headers |
| 154 | + pipeline_resp_mock.http_response = http_resp_mock |
| 155 | + qp_client_mock = mock.Mock() |
| 156 | + client_mock = mock.Mock() |
| 157 | + client_mock._base_url = "https://original.livediagnostics.monitor.azure.com" |
| 158 | + qp_client_mock._client = client_mock |
| 159 | + qp_client_ref = weakref.ref(qp_client_mock) |
| 160 | + policy._qp_client_ref = qp_client_ref |
| 161 | + redirect_location = policy.get_redirect_location(pipeline_resp_mock) |
| 162 | + self.assertIsNone(redirect_location) |
| 163 | + self.assertEqual(client_mock._base_url, "https://original.livediagnostics.monitor.azure.com") |
| 164 | + |
| 165 | + def test_get_redirect_location_rejects_userinfo_bypass(self): |
| 166 | + """Reject redirect URLs that use userinfo (@) to disguise the real host.""" |
| 167 | + policy = _QuickpulseRedirectPolicy() |
| 168 | + pipeline_resp_mock = mock.Mock() |
| 169 | + http_resp_mock = mock.Mock() |
| 170 | + # URL with userinfo: urlparse sees "evil.com" as the real host, not the allowed domain. |
| 171 | + headers = { |
| 172 | + "x-ms-qps-service-endpoint-redirect-v2": "https://eastus.livediagnostics.monitor.azure.com:443@evil.com/exfiltrate" |
| 173 | + } |
| 174 | + http_resp_mock.headers = headers |
| 175 | + pipeline_resp_mock.http_response = http_resp_mock |
| 176 | + qp_client_mock = mock.Mock() |
| 177 | + client_mock = mock.Mock() |
| 178 | + client_mock._base_url = "https://original.livediagnostics.monitor.azure.com" |
| 179 | + qp_client_mock._client = client_mock |
| 180 | + qp_client_ref = weakref.ref(qp_client_mock) |
| 181 | + policy._qp_client_ref = qp_client_ref |
| 182 | + redirect_location = policy.get_redirect_location(pipeline_resp_mock) |
| 183 | + self.assertIsNone(redirect_location) |
| 184 | + self.assertEqual(client_mock._base_url, "https://original.livediagnostics.monitor.azure.com") |
| 185 | + |
| 186 | + |
| 187 | +class TestIsRedirectTargetAllowed(unittest.TestCase): |
| 188 | + def test_allowed_domains(self): |
| 189 | + self.assertTrue(_is_redirect_target_allowed("eastus.livediagnostics.monitor.azure.com")) |
| 190 | + self.assertTrue(_is_redirect_target_allowed("global.livediagnostics.monitor.azure.com")) |
| 191 | + self.assertTrue(_is_redirect_target_allowed("rt.services.visualstudio.com")) |
| 192 | + self.assertTrue(_is_redirect_target_allowed("westus.in.applicationinsights.azure.com")) |
| 193 | + self.assertTrue(_is_redirect_target_allowed("settings.monitor.azure.com")) |
| 194 | + self.assertTrue(_is_redirect_target_allowed("eastus.monitor.azure.us")) |
| 195 | + self.assertTrue(_is_redirect_target_allowed("eastus.monitor.azure.cn")) |
| 196 | + |
| 197 | + def test_allowed_domains_with_port(self): |
| 198 | + self.assertTrue(_is_redirect_target_allowed("eastus.livediagnostics.monitor.azure.com:443")) |
| 199 | + |
| 200 | + def test_disallowed_domains(self): |
| 201 | + self.assertFalse(_is_redirect_target_allowed("evil.attacker.com")) |
| 202 | + self.assertFalse(_is_redirect_target_allowed("monitor.azure.com.evil.com")) |
| 203 | + self.assertFalse(_is_redirect_target_allowed("localhost")) |
| 204 | + self.assertFalse(_is_redirect_target_allowed("192.168.1.1")) |
| 205 | + self.assertFalse(_is_redirect_target_allowed("attacker.com")) |
| 206 | + |
| 207 | + def test_disallowed_userinfo_bypass(self): |
| 208 | + self.assertFalse(_is_redirect_target_allowed("eastus.livediagnostics.monitor.azure.com:443@evil.com")) |
| 209 | + self.assertFalse(_is_redirect_target_allowed("user:pass@evil.com")) |
0 commit comments