fix: restrict Web Push endpoint egress (Closes #653)
This commit is contained in:
1 parent
dc202bde11
commit
85043b4af6
6 files changed
+190
No files matched your search
@@ -0,0 +1,37 @@
|
||||
import pytest
|
||||
|
||||
from src.push_endpoint_policy import UnsafePushEndpoint, validate_public_push_endpoint
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
@pytest.mark.parametrize(
|
||||
"address",
|
||||
["127.0.0.1", "::1", "10.0.0.4", "169.254.169.254", "fc00::1"],
|
||||
)
|
||||
async def test_push_endpoint_rejects_every_non_public_resolved_address(address):
|
||||
async def resolve(_host, _port):
|
||||
return [address]
|
||||
|
||||
with pytest.raises(UnsafePushEndpoint, match="public Web Push service"):
|
||||
await validate_public_push_endpoint(
|
||||
"https://push.example/device", resolver=resolve
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
@pytest.mark.parametrize(
|
||||
"endpoint",
|
||||
[
|
||||
"http://push.example/device",
|
||||
"https://user@push.example/device",
|
||||
"https://push.example:8443/device",
|
||||
"https://push.example:not-a-port/device",
|
||||
"https://push.example/device#fragment",
|
||||
],
|
||||
)
|
||||
async def test_push_endpoint_rejects_noncanonical_authorities_without_resolving(endpoint):
|
||||
async def must_not_resolve(_host, _port):
|
||||
raise AssertionError("invalid URL reached DNS")
|
||||
|
||||
with pytest.raises(UnsafePushEndpoint, match="canonical public Web Push"):
|
||||
await validate_public_push_endpoint(endpoint, resolver=must_not_resolve)
|
||||
@@ -10,6 +10,15 @@ import pytest
|
||||
from src import dashboard_auth, gitea_proxy, main
|
||||
from src.push_notifications import PushConfiguration, dispatch_unread_updates
|
||||
from src.push_subscription_store import PushSubscriptionStore
|
||||
from src.push_endpoint_policy import UnsafePushEndpoint
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def resolve_test_push_endpoints_publicly(monkeypatch):
|
||||
async def accept(endpoint):
|
||||
return endpoint
|
||||
|
||||
monkeypatch.setattr(main, "validate_public_push_endpoint", accept)
|
||||
|
||||
|
||||
def test_dispatch_lease_is_exclusive_recoverable_and_owner_fenced(tmp_path):
|
||||
@@ -508,6 +517,32 @@ async def test_dispatch_removes_an_expired_push_endpoint(tmp_path):
|
||||
assert store.is_subscribed("session-a") is False
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_dispatch_removes_an_endpoint_that_rebinds_private_without_contacting_it(tmp_path):
|
||||
store = PushSubscriptionStore(tmp_path / "push.sqlite3")
|
||||
store.upsert("unsafe-device", {
|
||||
"endpoint": "https://push.example/rebound",
|
||||
"keys": {"p256dh": "public-key", "auth": "auth-secret"},
|
||||
})
|
||||
contacted = []
|
||||
|
||||
async def unread():
|
||||
return {"items": [{"id": 8}]}
|
||||
|
||||
async def reject_rebound(_endpoint):
|
||||
raise UnsafePushEndpoint("Endpoint must resolve to a public Web Push service")
|
||||
|
||||
async def send(subscription, _payload):
|
||||
contacted.append(subscription["endpoint"])
|
||||
|
||||
config = PushConfiguration("public", "private", "mailto:ops@example.com")
|
||||
assert await dispatch_unread_updates(
|
||||
store, config, unread, send, endpoint_validator=reject_rebound
|
||||
) == 0
|
||||
assert contacted == []
|
||||
assert store.is_subscribed("unsafe-device") is False
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_transient_endpoint_failure_does_not_block_healthy_devices(tmp_path):
|
||||
store = PushSubscriptionStore(tmp_path / "push.sqlite3")
|
||||
@@ -722,6 +757,37 @@ async def test_authenticated_device_can_subscribe_report_status_and_unsubscribe(
|
||||
assert (await main.push_status(request))["subscribed"] is False
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_subscription_rejects_an_unsafe_endpoint_before_persistence(tmp_path, monkeypatch):
|
||||
store = PushSubscriptionStore(tmp_path / "push.sqlite3")
|
||||
monkeypatch.setattr(main, "_push_subscription_store", store)
|
||||
monkeypatch.setattr(
|
||||
main, "_push_configuration",
|
||||
lambda: PushConfiguration("public", "private", "mailto:ops@example.com"),
|
||||
)
|
||||
|
||||
async def management_id(_session):
|
||||
return "unsafe-device"
|
||||
|
||||
async def reject_private(_endpoint):
|
||||
raise UnsafePushEndpoint("Endpoint must resolve to a public Web Push service")
|
||||
|
||||
monkeypatch.setattr(main.dashboard_auth, "session_management_id", management_id)
|
||||
monkeypatch.setattr(main, "validate_public_push_endpoint", reject_private)
|
||||
request = SimpleNamespace(state=SimpleNamespace(dashboard_session=object()))
|
||||
payload = main.PushSubscriptionPayload(
|
||||
endpoint="https://push.example/device-a",
|
||||
keys={"p256dh": "public-key", "auth": "auth-secret"},
|
||||
)
|
||||
|
||||
with pytest.raises(main.HTTPException) as rejected:
|
||||
await main.subscribe_push(payload, request)
|
||||
|
||||
assert rejected.value.status_code == 422
|
||||
assert rejected.value.detail == "Endpoint must resolve to a public Web Push service"
|
||||
assert store.is_subscribed("unsafe-device") is False
|
||||
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_subscription_baselines_every_revision_from_the_complete_snapshot(tmp_path, monkeypatch):
|
||||
store = PushSubscriptionStore(tmp_path / "push.sqlite3")
|
||||
|
||||
Reference in new issue
Block a user