Skip to content

feat(utils): add a guarded HTTP client #490

Closed

Description

@uniqueg

Problem

Plugins, the registry discovery and gateways make outbound HTTP calls
whose targets may come from users or registries. Without guards they
are SSRF vectors (private addresses, redirects to internal hosts), can
hang past their deadline, or read unbounded responses. proTES has these
guards (check_public_host, is_public_address, fetch_json) in its
own code; FOCA should own them so all gateways and plugins share them.

Proposal

foca.utils.http, built on requests/urllib3 (no Werkzeug):

  • is_public_address(ip) (globally reachable, not multicast; embedded
    IPv4 in IPv6 checked) and check_public_host(url) (resolve; all
    addresses public; zone IDs refused), moved from proTES.
  • GuardedClient(deadline=..., allow_private_addresses=False, max_response_size=...) with get/post/request: never follows
    redirects, checks the host before each request, timeout = the remaining
    deadline (connect timeout capped), streams responses up to the size
    cap, https only unless allowed; errors as foca.utils.http exception
    classes (not-sent vs. maybe-sent distinguished, for gateway
    classification).
  • fetch_json(url, timeout, ...) for registry reads.
  • Engine: ctx.http is a GuardedClient bound to the hook's deadline.
  • Documented limitation: resolution-time check (DNS rebinding between
    check and connect is mitigated by checking each request, not
    eliminated).

Acceptance criteria

  • Tests for public/private/special addresses (v4, v6, mapped, NAT64),
    redirects refused, deadline honoured, size cap, unresolvable hosts;
    no network access in tests (mocked resolver/adapters).
  • Docs: utilities page section; plugin author guide points to
    ctx.http.
  • 100% coverage, mypy, ruff, semgrep (TLS verification never disabled),
    mkdocs build --strict.

Surviving the Connexion 3 migration

Independent of the web framework (requests/urllib3 only). In 1.0 an
async variant (httpx) may be added for async hooks (P8); the sync
client stays.

Dependencies

Open questions

  • None.

Added during pre-review: the deadline bounds the whole request (a watchdog shuts sockets down at the deadline; DNS resolution is bounded by the time left); each host is resolved once and the connection goes to a checked address (peer checked again); malformed URLs, ports, host labels and methods raise InvalidRequestURL; check_public_host(url, timeout=); HostNotResolved has reason dns_error; HttpResponse.url is removed; urllib3 is declared as a dependency; the engine closes each hook call's client.

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority: highstatus: blockedtype: featuretype: securityworkload: days

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions