Repository navigation
fix: check connections to discovered backends - #347
Conversation
Send requests to TES backends discovered in service registries through FOCA's GuardedClient, which checks the address of each connection, so that hosts whose addresses change after discovery (DNS rebinding) can't lead requests and proTES's credentials to private addresses. The per-request host check is dropped. Responses of discovered backends are limited to 64 MiB; errors of the guarded client map onto those of requests (not sent, or maybe sent). Remove the DNS rebinding caveat from the docs. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Treat TLS errors of the guarded client as not sent only for refused certificates, as for configured backends; other TLS errors may come after the request was sent, so the outcome is unknown and the task isn't sent elsewhere. Convert guarded responses with their reason and encoding, refuse arguments the guarded client doesn't support, and document that the forward budget doesn't shorten connecting to discovered backends yet. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Report connect timeouts of discovered backends as connect_timeout, and treat invalid headers and URLs reported by the guarded client as an unknown outcome, as FOCA also reports them for responses (e.g., of redirects) after the request was sent. Note in the docs that FOCA 0.18 reads bodies of redirects whole, beyond the response limit, and why the forward budget doesn't shorten connecting to discovered backends. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideDiscovered TES backends now use FOCA 0.18's per-connection guarded client to prevent DNS-rebinding requests to private addresses, with registry-specific policy, bounded responses, requests-compatible response conversion, and careful error mapping that preserves failover and unknown-outcome semantics. Sequence diagram for guarded requests to discovered TES backendssequenceDiagram
participant TesClient
participant GuardedClient
participant DNS
participant TES
TesClient->>GuardedClient: request(method, url, params, headers, data, auth)
GuardedClient->>DNS: Resolve host for connection
DNS-->>GuardedClient: Address
alt Private or unresolved address
GuardedClient-->>TesClient: AddressNotAllowed or HostNotResolved
TesClient-->>TesClient: BackendAddressNotAllowed
else Allowed address
GuardedClient->>TES: HTTPS request
TES-->>GuardedClient: Response up to 64 MiB
GuardedClient-->>TesClient: HttpResponse
TesClient-->>TesClient: as_response()
end
Flow diagram for discovered backend error mappingflowchart TD
A["GuardedClient.request()"] --> B{Request outcome}
B -->|AddressNotAllowed or HostNotResolved| C[BackendAddressNotAllowed]
B -->|RequestNotSent| D[BackendRequestNotSent]
B -->|RequestMaybeSent read_timeout| E[requests.ReadTimeout]
B -->|RequestMaybeSent other error| F[requests.ConnectionError]
B -->|Valid response| G["as_response()"]
C --> H[Fail over]
D --> H
E --> I[Unknown outcome 502]
F --> I
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #347 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 23 23
Lines 2099 2136 +37
=========================================
+ Hits 2099 2136 +37 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="pro_tes/ga4gh/tes/backends.py" line_range="790" />
<code_context>
+ if unsupported:
+ raise TypeError(f"Unsupported arguments of guarded requests: {unsupported}")
+ try:
+ guarded = self.guarded.request(
+ method,
+ url,
</code_context>
<issue_to_address>
**Discovered requests outlive forwarding budget**
When a discovered-backend request takes longer than the forwarding attempt's remaining budget, especially while connecting or when the configured TES timeout exceeds that budget, `TesClient._request` lets `GuardedClient.request` use its configured timeout instead of the remaining forwarding budget, so the request continues past the deadline, tying up worker capacity and delaying task processing; a late task submission can then be followed by a retry that creates a second remote task.
Pass the forwarding attempt's remaining budget as the timeout to `GuardedClient.request`.
</issue_to_address>Linked issues
#339 fix: check connections to discovered backends · 2 of 2 met
| # | Requirement | Status | Notes |
|---|---|---|---|
| 1 | Check each connection to discovered TES instances, refusing private addresses unless allowed, with tests. | ✅ Met | Discovered backends use FOCA's GuardedClient with the registry entry's allow_private_addresses setting; tests cover refusal before connecting and the allowed-private case. |
| 2 | Remove the DNS rebinding caveat from the docs. | ✅ Met | The configuration docs replace the caveat with a description of the per-connection check and its DNS-rebinding protection. |
Sourcery assessment
Needs a human reviewer. 1 finding to address first, and if the guarded client checks one address but connects to another, a discovered backend that changes DNS could still receive proTES credentials at a private address. Any credentials exposed before the mistake is found cannot be recovered by reverting this change.
Blocking findings: pro_tes/ga4gh/tes/backends.py:790
Summary
Requests to TES instances discovered in service registries now go through FOCA 0.18's guarded HTTP client (
foca.utils.http.GuardedClient), which checks the address of each connection itself. A host whose addresses change after discovery (DNS rebinding, TTL 0) can no longer lead requests, with proTES's token, to private addresses (unlessallow_private_addressesis set on the registry entry).TesClientcreates aGuardedClientfor discovered backends (allow_private_addressesof the registry entry, https only, as FOCA discovers https services only, responses up to 64 MiB).TesClient._request()sends creates, gets and cancels through it. Configured backends still use plainrequests, since they are trusted and may be internal.check_public_host()is dropped, because the guard covers it. Discovery still checks hosts.requests:BackendRequestNotSent, orBackendAddressNotAllowedfor private or unresolved hosts; these fail over;ReadTimeoutorConnectionError; these give an unknown outcome (502) and no failover.connect_timeout. Invalid headers and URLs that the guarded client reports count as an unknown outcome, since FOCA also reports them for responses, e.g. of redirects, after the request was sent.docs/configuration.md. The docs now describe the per-connection check, the response limit and the timeouts of discovered backends.docs/plugins.mdnotes the forward-budget limitation below.Closes #339
Verification
coverage run -m pytest: 999 passed, 142 skipped (Helm not installed locally); 100% coverage.127.0.0.1at connection time is refused before connecting, and no connection reaches the local listener.allow_private_addresses, the connection is made.ruff check .,ruff format --check .,mypy pro_tes tests,ast-grep scan,semgrep --config=.semgrep --errorandmkdocs build --strictall pass.Declined review findings
Quality (low): drop
allow_redirects=Falsefrom_request_paramsfor registry entries, which the guarded path ignores. Kept as a defence in depth for the plain path.Adversarial (medium, partly): the forward budget (
plugins.defaults.forward_timeout) doesn't shorten connects to discovered backends. FOCA'sGuardedClient.request()has no per-request connect timeout; its connect limit ismin(custom.tes.timeout, 10)s. This is documented. Fixing it needs a FOCA change (aconnect_timeoutargument), which this PR doesn't make (tracked under feat: add gateway plugin engine foca#485).Adversarial (medium, FOCA): FOCA 0.18's
GuardedClientreads the bodies of 3xx responses whole before its size check, becauserequestsresolves redirect targets even withallow_redirects=False. The 64 MiB limit therefore doesn't hold for redirects. This is documented as a limitation and needs a FOCA fix (e.g. sending through the adapter directly), tracked under feat: add gateway plugin engine foca#485.Adversarial (medium, FOCA): FOCA classifies invalid response headers and redirect URLs as
RequestNotSent. proTES works around this by treating them as an unknown outcome; the classification itself should be fixed in FOCA.Open questions
None.
🤖 Generated with Claude Code
Summary by Sourcery
Protect requests to discovered TES backends with per-connection address validation and consistent guarded-client error handling.
New Features:
Bug Fixes:
Enhancements:
Documentation:
Tests: