|
23 | 23 | get_error_message, |
24 | 24 | get_git_revision, |
25 | 25 | get_lines_from_file, |
| 26 | + is_localhost, |
26 | 27 | is_sentry_url, |
27 | 28 | is_valid_sample_rate, |
28 | 29 | logger, |
@@ -1409,3 +1410,50 @@ def __init__(self): |
1409 | 1410 |
|
1410 | 1411 | def test_package_version_is_none(): |
1411 | 1412 | assert package_version("non_existent_package") is None |
| 1413 | + |
| 1414 | + |
| 1415 | +@pytest.mark.parametrize( |
| 1416 | + "kwargs, expected", |
| 1417 | + [ |
| 1418 | + # Client IP matches |
| 1419 | + ({"client_ip": "127.0.0.1"}, True), |
| 1420 | + ({"client_ip": "::1"}, True), |
| 1421 | + ({"client_ip": "192.168.1.1"}, False), |
| 1422 | + ({"client_ip": "10.0.0.1"}, False), |
| 1423 | + ({"client_ip": "8.8.8.8"}, False), |
| 1424 | + ({"client_ip": "::2"}, False), |
| 1425 | + # URL host (subdomain-aware matching) |
| 1426 | + ({"url_host": "localhost"}, True), |
| 1427 | + ({"url_host": "127.0.0.1"}, True), |
| 1428 | + ({"url_host": "foo.localhost"}, True), |
| 1429 | + ({"url_host": "bar.foo.localhost"}, True), |
| 1430 | + ({"url_host": "foolocalhost"}, False), |
| 1431 | + ({"url_host": "notlocalhost"}, False), |
| 1432 | + ({"url_host": "example.com"}, False), |
| 1433 | + # Host header (exact match after port strip, no subdomain matching) |
| 1434 | + ({"host_header": "localhost"}, True), |
| 1435 | + ({"host_header": "localhost:3000"}, True), |
| 1436 | + ({"host_header": "127.0.0.1:8080"}, True), |
| 1437 | + ({"host_header": "example.com"}, False), |
| 1438 | + ({"host_header": "localhost.example.com"}, False), |
| 1439 | + # X-Forwarded-Host header |
| 1440 | + ({"forwarded_host_header": "localhost"}, True), |
| 1441 | + ({"forwarded_host_header": "localhost:3000"}, True), |
| 1442 | + ({"forwarded_host_header": "127.0.0.1:8080"}, True), |
| 1443 | + ({"forwarded_host_header": "example.com"}, False), |
| 1444 | + # No args |
| 1445 | + ({}, False), |
| 1446 | + # All non-local |
| 1447 | + ( |
| 1448 | + { |
| 1449 | + "client_ip": "8.8.8.8", |
| 1450 | + "url_host": "example.com", |
| 1451 | + "host_header": "example.com", |
| 1452 | + "forwarded_host_header": "example.com", |
| 1453 | + }, |
| 1454 | + False, |
| 1455 | + ), |
| 1456 | + ], |
| 1457 | +) |
| 1458 | +def test_is_localhost(kwargs, expected): |
| 1459 | + assert is_localhost(**kwargs) is expected |
0 commit comments