Philter.Config (Philter v0.4.0)

Copy Markdown View Source

Configuration management for Philter proxy library.

Configuration can be set at the application level and overridden per-request.

Application Configuration

config :philter,
  receive_timeout: 15_000,
  max_payload_size: 1_048_576,
  persistable_content_types: ["application/json", "text/xml", "text/*"]

Per-Request Overrides

Any option can be overridden when calling Philter.proxy/2:

Philter.proxy(conn,
  upstream: "http://api.example.com",
  receive_timeout: 30_000,
  max_payload_size: 5_242_880
)

Options

  • :finch_name - Deprecated and ignored. The transport uses no connection pool. Still accepted so existing configuration does not crash (default: Philter.Finch)
  • :receive_timeout - Timeout in ms for receiving response (default: 15_000)
  • :max_payload_size - Max size in bytes for full body accumulation (default: 1_048_576 / 1MB)
  • :persistable_content_types - Content types eligible for full body storage (default: see below)
  • :log_level - Logger level for lifecycle events, or false to disable (default: :debug)
  • :block_private_networks - Reject upstreams that resolve to private, loopback, link-local or otherwise internal ranges (SSRF egress guard, default: true)
  • :allowed_hosts - Hosts that bypass the egress block check entirely. Exact match after downcase + trailing-dot strip (default: [])
  • :dns_timeout - Milliseconds to bound upstream DNS resolution (default: 5_000)
  • :connect_timeout - Milliseconds to bound the connection phase to a validated upstream address (default: 5_000)
  • :transport_opts - Extra Mint transport options merged into the connection, e.g. a custom CA bundle. Cannot be used to disable TLS certificate verification (default: [])

Default Persistable Content Types

By default, the following content types are eligible for full body accumulation:

  • application/json
  • application/xml
  • text/xml
  • text/plain
  • text/html

Wildcards like text/* are supported.

Summary

Functions

Returns the list of hosts that bypass the egress block check.

Returns whether upstreams resolving to internal ranges are blocked.

Returns the connection-phase timeout in milliseconds.

Checks if a content type is eligible for body accumulation.

Returns the DNS resolution timeout in milliseconds.

Returns the configured :finch_name. Deprecated and ignored.

Returns the log level for lifecycle events.

Returns the maximum payload size for full body accumulation in bytes.

Returns the list of content types eligible for full body accumulation.

Returns the receive timeout in milliseconds.

Returns all configuration as a map, with per-request overrides applied.

Returns extra Mint transport options merged into the connection.

Types

t()

@type t() :: %{
  finch_name: atom(),
  receive_timeout: pos_integer(),
  max_payload_size: pos_integer(),
  persistable_content_types: [String.t()],
  log_level: Logger.level() | false,
  block_private_networks: boolean(),
  allowed_hosts: [String.t()],
  dns_timeout: pos_integer(),
  connect_timeout: pos_integer(),
  transport_opts: keyword()
}

Functions

allowed_hosts(opts \\ [])

@spec allowed_hosts(keyword()) :: [String.t()]

Returns the list of hosts that bypass the egress block check.

Examples

iex> Philter.Config.allowed_hosts()
[]

iex> Philter.Config.allowed_hosts(allowed_hosts: ["api.internal"])
["api.internal"]

block_private_networks(opts \\ [])

@spec block_private_networks(keyword()) :: boolean()

Returns whether upstreams resolving to internal ranges are blocked.

Examples

iex> Philter.Config.block_private_networks()
true

iex> Philter.Config.block_private_networks(block_private_networks: false)
false

connect_timeout(opts \\ [])

@spec connect_timeout(keyword()) :: pos_integer()

Returns the connection-phase timeout in milliseconds.

Examples

iex> Philter.Config.connect_timeout()
5_000

iex> Philter.Config.connect_timeout(connect_timeout: 1_000)
1_000

content_type_persistable?(content_type, allowed)

@spec content_type_persistable?(String.t() | nil, [String.t()]) :: boolean()

Checks if a content type is eligible for body accumulation.

Supports exact matches and wildcard patterns (e.g., text/*).

Examples

iex> Philter.Config.content_type_persistable?("application/json", ["application/json", "text/*"])
true

iex> Philter.Config.content_type_persistable?("text/plain", ["application/json", "text/*"])
true

iex> Philter.Config.content_type_persistable?("image/png", ["application/json", "text/*"])
false

dns_timeout(opts \\ [])

@spec dns_timeout(keyword()) :: pos_integer()

Returns the DNS resolution timeout in milliseconds.

Examples

iex> Philter.Config.dns_timeout()
5_000

iex> Philter.Config.dns_timeout(dns_timeout: 1_000)
1_000

finch_name(opts \\ [])

@spec finch_name(keyword()) :: atom()

Returns the configured :finch_name. Deprecated and ignored.

Examples

iex> Philter.Config.finch_name()
Philter.Finch

iex> Philter.Config.finch_name(finch_name: MyApp.Finch)
MyApp.Finch

log_level(opts \\ [])

@spec log_level(keyword()) :: Logger.level() | false

Returns the log level for lifecycle events.

Set to false to disable all logging. Defaults to :debug.

Examples

iex> Philter.Config.log_level()
:debug

iex> Philter.Config.log_level(log_level: :info)
:info

iex> Philter.Config.log_level(log_level: false)
false

max_payload_size(opts \\ [])

@spec max_payload_size(keyword()) :: pos_integer()

Returns the maximum payload size for full body accumulation in bytes.

Examples

iex> Philter.Config.max_payload_size()
1_048_576

iex> Philter.Config.max_payload_size(max_payload_size: 5_242_880)
5_242_880

persistable_content_types(opts \\ [])

@spec persistable_content_types(keyword()) :: [String.t()]

Returns the list of content types eligible for full body accumulation.

Supports wildcard patterns like "text/*".

Examples

iex> Philter.Config.persistable_content_types() |> Enum.member?("application/json")
true

iex> Philter.Config.persistable_content_types(persistable_content_types: ["application/json"])
["application/json"]

receive_timeout(opts \\ [])

@spec receive_timeout(keyword()) :: pos_integer()

Returns the receive timeout in milliseconds.

Examples

iex> Philter.Config.receive_timeout()
15_000

iex> Philter.Config.receive_timeout(receive_timeout: 30_000)
30_000

resolve(opts \\ [])

@spec resolve(keyword()) :: t()

Returns all configuration as a map, with per-request overrides applied.

Useful for getting the full resolved config in one call.

Examples

iex> config = Philter.Config.resolve(receive_timeout: 30_000)
iex> config.receive_timeout
30_000

transport_opts(opts \\ [])

@spec transport_opts(keyword()) :: keyword()

Returns extra Mint transport options merged into the connection.

Cannot be used to disable TLS certificate verification.

Examples

iex> Philter.Config.transport_opts()
[]

iex> Philter.Config.transport_opts(transport_opts: [cacertfile: "/etc/ssl/cert.pem"])
[cacertfile: "/etc/ssl/cert.pem"]