keep a precise failure precise, and state one redirect policy

Two problems of the same shape: information about *why* something failed being
replaced by something vaguer.

get_dataset_geojson wrapped the JSON parse, the metadata read, the CRS
resolution and the canonicalisation in one try and reported all of it as
"Stored dataset is not valid JSON" with a 500. An operator whose dataset had an
unusable CRS was sent to inspect a file that parses perfectly well, and the
canonicaliser's own AppError — with its code and its status — never reached
them. Only the parse is now inside that handler; everything after it keeps the
error it raised, and a genuine bug becomes a distinct 500 rather than a
mislabelled client error. A guard finds the same shape elsewhere: catching
Exception around a call into another component and relabelling what it
reported. Wrapping one's own private helper stays legitimate and the guard
says so.

The redirect policy was split without anyone saying so. Two acquisition
services rejected every redirect through a hand-rolled opener, while eight
allowed a same-origin one through the shared guard — and only the latter
checked where the response came from. Both live in the guard now, and the
strict path uses the rejecting handler rather than the guard's after-the-fact
check: objecting to response.url means urllib already opened the connection and
read the body, which for a metadata endpoint is the whole attack. That was a
weakening I introduced in this same commit's first draft.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Jens
2026-08-22 23:35:59 +02:00
co-authored by Claude Opus 5
parent c6837ec1b2
commit 7c052a339e
8 changed files with 375 additions and 26 deletions
@@ -162,3 +162,96 @@ class TestTheGuardIsWiredIntoAcquisition:
OrthophotoAcquisitionService._fetch("http://192.168.10.150/wms", self._settings())
assert exc_info.value.code == "OUTBOUND_URL_NOT_ALLOWED"
class TestOneRedirectPolicy:
"""Two acquisition services rejected every redirect through their own
opener while eight allowed a same-origin one through this guard. Two
policies with no stated reason, and only one of them checked where the
response actually came from."""
def test_the_strict_policy_refuses_any_redirect(self) -> None:
from app.services.outbound_request_guard import guarded_opener
opener = guarded_opener("https://geo.api.vlaanderen.be/GRB/wfs", allow_redirect=False)
class _Redirected:
url = "https://geo.api.vlaanderen.be/GRB/wfs/v2"
def __enter__(self):
return self
def __exit__(self, *_args):
return False
with pytest.raises(AppError) as exc_info:
with opener(object(), timeout=1, _transport=lambda *_a, **_k: _Redirected()):
pass
assert exc_info.value.code == "OUTBOUND_REDIRECT_NOT_ALLOWED"
def test_the_strict_policy_still_allows_the_response_it_asked_for(self) -> None:
from app.services.outbound_request_guard import guarded_opener
url = "https://geo.api.vlaanderen.be/GRB/wfs"
opener = guarded_opener(url, allow_redirect=False)
class _Direct:
def __init__(self) -> None:
self.url = url
def __enter__(self):
return self
def __exit__(self, *_args):
return False
with opener(object(), timeout=1, _transport=lambda *_a, **_k: _Direct()) as response:
assert response.url == url
def test_both_policies_refuse_a_private_destination(self) -> None:
from app.services.outbound_request_guard import guarded_opener
for allow_redirect in (True, False):
with pytest.raises(AppError) as exc_info:
guarded_opener("http://10.0.0.5/wfs", allow_redirect=allow_redirect)
assert exc_info.value.code == "OUTBOUND_URL_NOT_ALLOWED"
def test_the_strict_services_use_the_shared_guard(self) -> None:
"""Behavioural: their own fetch paths refuse a private endpoint, which
the hand-rolled opener never checked."""
from app.core.config import Settings
from app.services.grb_acquisition_service import GrbAcquisitionService
from app.services.official_vector_acquisition_service import OfficialVectorAcquisitionService
settings = Settings(_env_file=None)
for service in (GrbAcquisitionService, OfficialVectorAcquisitionService):
with pytest.raises(AppError) as exc_info:
service._read_page("http://127.0.0.1:9/wfs", settings, None)
assert exc_info.value.code == "OUTBOUND_URL_NOT_ALLOWED"
def test_a_refused_redirect_is_never_requested() -> None:
"""Rejecting after the fact still sends the request.
Checking ``response.url`` means urllib has already followed the chain: the
connection to the redirect target was opened and the response read. For a
destination like a metadata endpoint that is the whole attack. The strict
policy must refuse to follow, not refuse afterwards.
"""
from app.services.outbound_request_guard import no_redirect_opener
opener = no_redirect_opener()
handlers = [type(handler).__name__ for handler in opener.handlers]
assert "_RejectRedirects" in handlers
def test_the_rejecting_handler_returns_no_new_request() -> None:
from app.services.outbound_request_guard import _RejectRedirects
handler = _RejectRedirects()
assert handler.redirect_request(None, None, 302, "Found", {}, "http://169.254.169.254/") is None