Skip to content

Commit 5e07a39

Browse files
committed
Re-vendor Mint v1.11.0 and hpax v1.1.0
All four Mint changes Hex depended on are released upstream (elixir-mint/mint#478, #479, #480 and #481), so vendor from Mint main (fb850d3, v1.11.0) instead of the integration branch and drop the list of pending patches from scripts/vendor_mint.sh. Mint.Application no longer exists upstream, so the skip list for it is gone too. Mint v1.11.0 reads `:proxy_headers` from the top-level connect options and only uses the `:proxy` tuple's options for the connection to the proxy (elixir-mint/mint#494), so Hex.HTTP now passes the proxy-authorization header there.
1 parent a683957 commit 5e07a39

29 files changed

Lines changed: 2298 additions & 578 deletions

‎lib/hex/http.ex‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ defmodule Hex.HTTP do
316316
[]
317317

318318
%URI{host: phost, port: pport} = proxy when not is_nil(phost) and not is_nil(pport) ->
319-
proxy_opts =
319+
proxy_headers =
320320
case proxy.userinfo do
321321
nil ->
322322
[]
@@ -326,7 +326,7 @@ defmodule Hex.HTTP do
326326
[proxy_headers: [{"proxy-authorization", "Basic #{encoded}"}]]
327327
end
328328

329-
[proxy: {:http, phost, pport, proxy_opts}]
329+
[proxy: {:http, phost, pport, []}] ++ proxy_headers
330330

331331
_ ->
332332
[]

‎lib/hex/mint/core/conn.ex‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Vendored from mint v1.7.1 (d30d2cf), do not edit manually
1+
# Vendored from mint v1.11.0 (fb850d3), do not edit manually
22

33
defmodule Hex.Mint.Core.Conn do
44
@moduledoc false
@@ -64,4 +64,6 @@ defmodule Hex.Mint.Core.Conn do
6464
@callback put_proxy_headers(conn(), Hex.Mint.Types.headers()) :: conn()
6565

6666
@callback put_log(conn(), boolean()) :: conn()
67+
68+
@callback request_body_window(conn(), Types.request_ref()) :: non_neg_integer() | :infinity
6769
end

‎lib/hex/mint/core/headers.ex‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Vendored from mint v1.7.1 (d30d2cf), do not edit manually
1+
# Vendored from mint v1.11.0 (fb850d3), do not edit manually
22

33
defmodule Hex.Mint.Core.Headers do
44
@moduledoc false

‎lib/hex/mint/core/transport.ex‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Vendored from mint v1.7.1 (d30d2cf), do not edit manually
1+
# Vendored from mint v1.11.0 (fb850d3), do not edit manually
22

33
defmodule Hex.Mint.Core.Transport do
44
@moduledoc false

‎lib/hex/mint/core/transport/ssl.ex‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Vendored from mint v1.7.1 (d30d2cf), do not edit manually
1+
# Vendored from mint v1.11.0 (fb850d3), do not edit manually
22

33
defmodule Hex.Mint.Core.Transport.SSL do
44
@moduledoc false
@@ -295,6 +295,8 @@ defmodule Hex.Mint.Core.Transport.SSL do
295295
@default_versions [:"tlsv1.3", :"tlsv1.2"]
296296
@default_timeout 30_000
297297

298+
@tunnel_cb_info {Hex.Mint.Core.Transport.SSL.Tunnel, :ssl, :ssl_closed, :ssl_error, :ssl_passive}
299+
298300
Record.defrecordp(
299301
:certificate,
300302
:Certificate,
@@ -349,6 +351,10 @@ defmodule Hex.Mint.Core.Transport.SSL do
349351
end
350352

351353
@impl true
354+
# :ssl.connect/3's spec doesn't cover upgrading an :ssl socket, but the
355+
# function supports any socket type that the :cb_info callback module can
356+
# handle.
357+
@dialyzer {:nowarn_function, upgrade: 5}
352358
def upgrade(socket, :http, hostname, _port, opts) do
353359
hostname = String.to_charlist(hostname)
354360
timeout = Keyword.get(opts, :timeout, @default_timeout)
@@ -359,8 +365,21 @@ defmodule Hex.Mint.Core.Transport.SSL do
359365
wrap_err(:ssl.connect(socket, ssl_opts(hostname, opts), timeout))
360366
end
361367

362-
def upgrade(_socket, :https, _hostname, _port, _opts) do
363-
raise "nested SSL sessions are not supported"
368+
def upgrade(socket, :https, hostname, _port, opts) do
369+
hostname = String.to_charlist(hostname)
370+
timeout = Keyword.get(opts, :timeout, @default_timeout)
371+
372+
with :ok <- setopts(socket, active: false) do
373+
# The TLS session to the host is nested inside the TLS session to the
374+
# proxy: the established :ssl socket acts as the transport for the new
375+
# connection, so :ssl needs a callback module that speaks :ssl instead
376+
# of :gen_tcp. Forced with Keyword.put/3 so that user-provided transport
377+
# options cannot override it.
378+
opts = Keyword.delete(opts, :hostname)
379+
opts = Keyword.put(ssl_opts(hostname, opts), :cb_info, @tunnel_cb_info)
380+
381+
wrap_err(:ssl.connect(socket, opts, timeout))
382+
end
364383
end
365384

366385
@impl true
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Vendored from mint v1.11.0 (fb850d3), do not edit manually
2+
3+
defmodule Hex.Mint.Core.Transport.SSL.Tunnel do
4+
@moduledoc false
5+
6+
# The transport callback module given to `:ssl.connect/3` through the
7+
# `:cb_info` option when nesting a TLS session inside an established
8+
# `:ssl` socket. `:ssl` requires the callback module to behave like
9+
# `:gen_tcp`, with functions corresponding to `:inet.setopts/2`,
10+
# `:inet.getopts/2`, `:inet.peername/1`, `:inet.sockname/1`, and
11+
# `:inet.port/1`.
12+
13+
defdelegate setopts(socket, opts), to: :ssl
14+
defdelegate getopts(socket, opts), to: :ssl
15+
defdelegate send(socket, data), to: :ssl
16+
defdelegate recv(socket, length), to: :ssl
17+
defdelegate recv(socket, length, timeout), to: :ssl
18+
defdelegate controlling_process(socket, pid), to: :ssl
19+
defdelegate close(socket), to: :ssl
20+
defdelegate shutdown(socket, how), to: :ssl
21+
defdelegate getstat(socket, options), to: :ssl
22+
defdelegate peername(socket), to: :ssl
23+
defdelegate sockname(socket), to: :ssl
24+
25+
def port(socket) do
26+
with {:ok, {_address, port}} <- :ssl.sockname(socket) do
27+
{:ok, port}
28+
end
29+
end
30+
end

‎lib/hex/mint/core/transport/tcp.ex‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Vendored from mint v1.7.1 (d30d2cf), do not edit manually
1+
# Vendored from mint v1.11.0 (fb850d3), do not edit manually
22

33
defmodule Hex.Mint.Core.Transport.TCP do
44
@moduledoc false

‎lib/hex/mint/core/util.ex‎

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# Vendored from mint v1.7.1 (d30d2cf), do not edit manually
1+
# Vendored from mint v1.11.0 (fb850d3), do not edit manually
22

33
defmodule Hex.Mint.Core.Util do
44
@moduledoc false
55

66
alias Hex.Mint.Types
77

8-
@spec hostname(keyword(), String.t()) :: String.t()
8+
@spec hostname(keyword(), Types.address()) :: String.t()
99
def hostname(opts, address) when is_list(opts) do
1010
case Keyword.fetch(opts, :hostname) do
1111
{:ok, hostname} ->
@@ -19,6 +19,36 @@ defmodule Hex.Mint.Core.Util do
1919
end
2020
end
2121

22+
# RFC 9112 3.2.2 and RFC 3986 3.2.2: an IPv6 literal in the Host header, the
23+
# :authority pseudo-header or a request target must be enclosed in square
24+
# brackets.
25+
@spec uri_host(String.t()) :: String.t()
26+
def uri_host(hostname) do
27+
case without_zone_id(hostname) do
28+
{:ipv6, address} -> "[" <> address <> "]"
29+
:other -> hostname
30+
end
31+
end
32+
33+
# The RFC 3986 IP-literal has no zone ID ("fe80::1%eth0"), so it's left out of
34+
# IPv6 literals sent to the server.
35+
@spec uri_host_without_brackets(String.t()) :: String.t()
36+
def uri_host_without_brackets(hostname) do
37+
case without_zone_id(hostname) do
38+
{:ipv6, address} -> address
39+
:other -> hostname
40+
end
41+
end
42+
43+
defp without_zone_id(hostname) do
44+
[address | _zone_id] = String.split(hostname, "%", parts: 2)
45+
46+
case :inet.parse_ipv6strict_address(String.to_charlist(address)) do
47+
{:ok, _address} -> {:ipv6, address}
48+
{:error, :einval} -> :other
49+
end
50+
end
51+
2252
@spec inet_opts(:gen_tcp | :ssl, :gen_tcp.socket() | :ssl.sslsocket()) :: :ok | {:error, term()}
2353
def inet_opts(transport, socket) do
2454
with {:ok, opts} <- transport.getopts(socket, [:sndbuf, :recbuf, :buffer]),

‎lib/hex/mint/hpax.ex‎

Lines changed: 77 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Vendored from hpax v1.0.3, do not edit manually
1+
# Vendored from hpax v1.1.0, do not edit manually
22

33
defmodule Hex.Mint.HPAX do
44
_ = """
@@ -84,30 +84,62 @@ defmodule Hex.Mint.HPAX do
8484
end
8585

8686
@doc """
87-
Resizes the given table to the given maximum size.
87+
Resizes the given encoding table to the given maximum size.
8888
89-
This is intended for use where the overlying protocol has signaled a change to the table's
90-
maximum size, such as when an HTTP/2 `SETTINGS` frame is received.
89+
This is intended for use on an encoding context where the overlying protocol has signaled a
90+
change to the maximum size the encoder is permitted to use, such as when an HTTP/2 `SETTINGS`
91+
frame is received. The table takes the whole size it is permitted, which
92+
[RFC7541§4.2](https://datatracker.ietf.org/doc/html/rfc7541#section-4.2) requires it to declare
93+
to the decoder, so a "dynamic table size update" command is prefixed to the next block encoded
94+
with this table.
9195
9296
If the indicated size is less than the table's current size, entries
9397
will be evicted as needed to fit within the specified size, and the table's
94-
maximum size will be decreased to the specified value. A flag will also be
95-
set which will enqueue a "dynamic table size update" command to be prefixed
96-
to the next block encoded with this table, per
97-
[RFC9113§4.3.1](https://www.rfc-editor.org/rfc/rfc9113.html#section-4.3.1).
98+
maximum size will be decreased to the specified value.
9899
99100
If the indicated size is greater than or equal to the table's current max size, no entries are evicted
100101
and the table's maximum size changes to the specified value.
101102
103+
Use `protocol_resize/2` for a decoding context, whose maximum size is chosen by the peer's
104+
encoder rather than by the protocol.
105+
102106
## Examples
103107
104-
decoding_context = Hex.Mint.HPAX.new(4096)
105-
Hex.Mint.HPAX.resize(decoding_context, 8192)
108+
encoding_context = Hex.Mint.HPAX.new(4096)
109+
Hex.Mint.HPAX.resize(encoding_context, 8192)
106110
107111
"""
108112
@spec resize(table(), non_neg_integer()) :: table()
109113
defdelegate resize(table, new_max_size), to: Table
110114

115+
@doc """
116+
Changes the maximum size the peer's encoder is permitted to use for the given decoding table.
117+
118+
This is intended for use on a decoding context when the overlying protocol communicates a new
119+
maximum to the peer, such as when an HTTP/2 client sends `SETTINGS_HEADER_TABLE_SIZE` and the
120+
server acknowledges it.
121+
[RFC7541§4.2](https://datatracker.ietf.org/doc/html/rfc7541#section-4.2) leaves the table's own
122+
maximum size to the encoder, which declares it with a "dynamic table size update" command, so
123+
raising the permitted maximum doesn't change the size of the table.
124+
125+
If the new maximum is below the maximum the encoder declared, the table is resized to it and
126+
entries are evicted as needed to fit. The encoder then has to declare a size of at most the new
127+
maximum at the start of its next block, so `decode/2` returns `{:error, :missing_size_update}`
128+
for a block that doesn't start with one, per
129+
[RFC9113§4.3.1](https://www.rfc-editor.org/rfc/rfc9113.html#section-4.3.1). When the maximum
130+
changes more than once before that block, the smallest of them is the one that has to be
131+
declared.
132+
133+
## Examples
134+
135+
decoding_context = Hex.Mint.HPAX.new(4096)
136+
Hex.Mint.HPAX.protocol_resize(decoding_context, 8192)
137+
138+
"""
139+
@doc since: "1.1.0"
140+
@spec protocol_resize(table(), non_neg_integer()) :: table()
141+
defdelegate protocol_resize(table, new_protocol_max_size), to: Table
142+
111143
@doc """
112144
Decodes a header block fragment (HBF) through a given table.
113145
@@ -131,13 +163,31 @@ defmodule Hex.Mint.HPAX do
131163
def decode(<<0b001::3, rest::bitstring>>, %Table{} = table) do
132164
{new_max_size, rest} = decode_integer(rest, 5)
133165

134-
# Dynamic resizes must be less than protocol max table size
135-
# https://datatracker.ietf.org/doc/html/rfc7541#section-6.3
136-
if new_max_size <= table.protocol_max_table_size do
137-
decode(rest, Table.dynamic_resize(table, new_max_size))
138-
else
139-
{:error, :protocol_error}
166+
cond do
167+
# Dynamic resizes must be less than protocol max table size
168+
# https://datatracker.ietf.org/doc/html/rfc7541#section-6.3
169+
new_max_size > table.protocol_max_table_size ->
170+
{:error, :protocol_error}
171+
172+
# The smallest maximum size the table was reduced to has to be signalled first
173+
# https://datatracker.ietf.org/doc/html/rfc7541#section-4.2
174+
is_integer(table.required_minimum_resize) and new_max_size > table.required_minimum_resize ->
175+
{:error, :missing_size_update}
176+
177+
true ->
178+
table = %{table | required_minimum_resize: nil}
179+
decode(rest, Table.dynamic_resize(table, new_max_size))
140180
end
181+
catch
182+
:throw, {:hpax, error} -> {:error, error}
183+
end
184+
185+
# A reduction of the maximum size below the size of the table has to be acknowledged with a
186+
# dynamic table size update at the start of the next block
187+
# https://www.rfc-editor.org/rfc/rfc9113.html#section-4.3.1
188+
def decode(block, %Table{required_minimum_resize: required})
189+
when is_binary(block) and is_integer(required) do
190+
{:error, :missing_size_update}
141191
end
142192

143193
def decode(block, %Table{} = table) when is_binary(block) do
@@ -300,7 +350,8 @@ defmodule Hex.Mint.HPAX do
300350
end
301351

302352
defp encode_headers([{action, name, value} | rest], table, acc)
303-
when action in @valid_header_actions and is_binary(name) and is_binary(value) do
353+
when action in @valid_header_actions do
354+
validate_header(name, value)
304355
huffman? = table.huffman_encoding == :always
305356

306357
{encoded, table} =
@@ -332,6 +383,15 @@ defmodule Hex.Mint.HPAX do
332383
encode_headers(rest, table, [acc, encoded])
333384
end
334385

386+
defp validate_header(name, value) when is_binary(name) and is_binary(value) do
387+
:ok
388+
end
389+
390+
defp validate_header(name, value) do
391+
raise ArgumentError,
392+
"expected header name/value to be strings, got: #{inspect(name)}/#{inspect(value)}"
393+
end
394+
335395
defp encode_indexed_header(index) do
336396
<<1::1, Types.encode_integer(index, 7)::bitstring>>
337397
end

‎lib/hex/mint/hpax/huffman.ex‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Vendored from hpax v1.0.3, do not edit manually
1+
# Vendored from hpax v1.1.0, do not edit manually
22

33
defmodule Hex.Mint.HPAX.Huffman do
44
@moduledoc false
@@ -63,30 +63,40 @@ defmodule Hex.Mint.HPAX.Huffman do
6363
## Decoding
6464

6565
@spec decode(binary()) :: binary()
66-
def decode(binary)
66+
def decode(binary) when is_bitstring(binary) do
67+
decode(binary, <<>>)
68+
end
6769

6870
for {byte_value, bits, bit_count} <- regular_entries do
69-
def decode(<<unquote(bits)::size(unquote(bit_count)), rest::bitstring>>) do
70-
<<unquote(byte_value), decode(rest)::binary>>
71+
defp decode(<<unquote(bits)::size(unquote(bit_count)), rest::bitstring>>, acc) do
72+
decode(rest, <<acc::binary, unquote(byte_value)>>)
7173
end
7274
end
7375

74-
def decode(<<>>) do
75-
<<>>
76+
defp decode(<<>>, acc) do
77+
acc
7678
end
7779

7880
# Use binary syntax for single match context optimization.
79-
def decode(<<padding::bitstring>>) when bit_size(padding) in 1..7 do
81+
defp decode(<<padding::bitstring>>, acc) when bit_size(padding) in 1..7 do
8082
padding_size = bit_size(padding)
81-
<<padding::size(padding_size)>> = padding
83+
<<padding::size(^padding_size)>> = padding
8284

8385
if take_significant_bits(unquote(eos_bits), unquote(eos_bit_count), padding_size) == padding do
84-
<<>>
86+
acc
8587
else
8688
throw({:hpax, {:protocol_error, :invalid_huffman_encoding}})
8789
end
8890
end
8991

92+
# Anything left over here is 8 or more bits that don't match any Huffman code (the clauses
93+
# above only match a complete code, the empty binary, or up to 7 bits of valid EOS padding).
94+
# This can only happen with a malformed/malicious encoding, since a real encoder never
95+
# produces output with more than 7 trailing bits that aren't a complete code.
96+
defp decode(<<_rest::bitstring>>, _acc) do
97+
throw({:hpax, {:protocol_error, :invalid_huffman_encoding}})
98+
end
99+
90100
## Helpers
91101

92102
@compile {:inline, take_significant_bits: 3}

0 commit comments

Comments
 (0)