Wraith.SafeHTTP (Wraith v0.1.0)

View Source

Outbound HTTP on behalf of untrusted input — a widget's wraith.fetch, an agent's upload_from_url, an embed URL in a post. The one place the rules live, so no fetch can be pointed at the inside of the network:

  • http/https only, with a hostname;
  • the hostname is resolved here, and every address it resolves to must be public — loopback, RFC 1918, link-local (169.254.x: cloud metadata), CGNAT, multicast, unspecified and their IPv6 counterparts are refused;
  • redirects are followed by hand, at most max_redirects, and each hop is checked again (a public host that 302s to 127.0.0.1 is the classic);
  • optional host allowlist (allow: list of exact names / *.suffix);
  • the body is streamed and cut at max_bytes — a 10 GB response never lands in memory before someone checks its size;
  • a total deadline, not just an idle timeout.

Returns {:ok, %{status, headers, body, url}} (final URL after redirects) or {:error, reason} with a reason safe to show a user.

Summary

Functions

Is this URL one we would fetch at all? (scheme, host, allowlist, public address.)

Types

opt()

@type opt() ::
  {:max_bytes, pos_integer()}
  | {:max_redirects, non_neg_integer()}
  | {:timeout_ms, pos_integer()}
  | {:allow, [String.t()] | nil}
  | {:headers, [{String.t(), String.t()}]}

Functions

check(url, opts \\ [])

@spec check(String.t(), [opt()]) :: :ok | {:error, String.t()}

Is this URL one we would fetch at all? (scheme, host, allowlist, public address.)

get(url, opts \\ [])

@spec get(String.t(), [opt()]) ::
  {:ok,
   %{status: pos_integer(), headers: map(), body: binary(), url: String.t()}}
  | {:error, String.t()}