Skip to content

Commit

Permalink
Move log_protocol_errors to a shared option
Browse files Browse the repository at this point in the history
  • Loading branch information
mtrudel committed Mar 27, 2024
1 parent 7f7f895 commit 53822ef
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Changes

* **BREAKING CHANGE** Move `log_protocol_errors` configuration option into
shared `http_options` top-level config (and apply it to HTTP/2 errors as well)
* **BREAKING CHANGE** Remove `origin_telemetry_span_context` from WebSocket
telemetry events
* **BREAKING CHANGE** Remove `stream_id` from HTTP/2 telemetry events
Expand Down
14 changes: 7 additions & 7 deletions lib/bandit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,13 @@ defmodule Bandit do
[RFC9110§8.4](https://www.rfc-editor.org/rfc/rfc9110.html#section-8.4). Defaults to true
* `deflate_options`: A keyword list of options to set on the deflate library. A complete list can
be found at `t:deflate_options/0`
* `log_protocol_errors`: Whether or not to log protocol errors such as malformed requests.
Defaults to `true`
"""
@type http_options :: [
compress: boolean(),
deflate_opions: deflate_options()
deflate_opions: deflate_options(),
log_protocol_errors: boolean()
]

@typedoc """
Expand All @@ -116,8 +119,6 @@ defmodule Bandit do
every 5 requests). This option is currently experimental, and may change at any time
* `log_unknown_messages`: Whether or not to log unknown messages sent to the handler process.
Defaults to `false`
* `log_protocol_errors`: Whether or not to log protocol errors such as malformed requests.
Defaults to `true`
"""
@type http_1_options :: [
enabled: boolean(),
Expand All @@ -126,8 +127,7 @@ defmodule Bandit do
max_header_count: pos_integer(),
max_requests: pos_integer(),
gc_every_n_keepalive_requests: pos_integer(),
log_unknown_messages: boolean(),
log_protocol_errors: boolean()
log_unknown_messages: boolean()
]

@typedoc """
Expand Down Expand Up @@ -196,8 +196,8 @@ defmodule Bandit do
end

@top_level_keys ~w(plug scheme port ip keyfile certfile otp_app cipher_suite display_plug startup_log thousand_island_options http_options http_1_options http_2_options websocket_options)a
@http_keys ~w(compress deflate_options)a
@http_1_keys ~w(enabled max_request_line_length max_header_length max_header_count max_requests gc_every_n_keepalive_requests log_unknown_messages log_protocol_errors)a
@http_keys ~w(compress deflate_options log_protocol_errors)a
@http_1_keys ~w(enabled max_request_line_length max_header_length max_header_count max_requests gc_every_n_keepalive_requests log_unknown_messages)a
@http_2_keys ~w(enabled max_header_block_size max_requests default_local_settings)a
@websocket_keys ~w(enabled max_frame_size validate_text_frames compress)a
@thousand_island_keys ThousandIsland.ServerConfig.__struct__()
Expand Down
4 changes: 2 additions & 2 deletions lib/bandit/pipeline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ defmodule Bandit.Pipeline do
] ->
Bandit.Telemetry.stop_span(span, %{}, %{error: error.message})

if Keyword.get(opts.http_1, :log_protocol_errors, true),
if Keyword.get(opts.http, :log_protocol_errors, true),
do: Logger.error(Exception.format(:error, error, __STACKTRACE__))

Bandit.HTTPTransport.send_on_error(transport, error)
Expand All @@ -73,7 +73,7 @@ defmodule Bandit.Pipeline do
span = Bandit.Telemetry.start_span(:request, measurements, metadata)
Bandit.Telemetry.stop_span(span, %{}, %{error: error.message})

if Keyword.get(opts.http_1, :log_protocol_errors, true),
if Keyword.get(opts.http, :log_protocol_errors, true),
do: Logger.error(Exception.format(:error, error, __STACKTRACE__))

Bandit.HTTPTransport.send_on_error(transport, error)
Expand Down
2 changes: 1 addition & 1 deletion test/bandit/http1/request_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ defmodule HTTP1RequestTest do

describe "suppressing protocol error logging" do
test "errors are not logged if so configured", context do
context = http_server(context, http_1_options: [log_protocol_errors: false])
context = http_server(context, http_options: [log_protocol_errors: false])

output =
capture_log(fn ->
Expand Down
21 changes: 21 additions & 0 deletions test/bandit/http2/protocol_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,27 @@ defmodule HTTP2ProtocolTest do
"(Bandit.HTTP2.Errors.StreamError) Received trailers with pseudo headers"
end

test "stream errors are not logged if so configured", context do

Check failure on line 115 in test/bandit/http2/protocol_test.exs

View workflow job for this annotation

GitHub Actions / test / test (1.15.x, 24.x)

test errors and unexpected frames stream errors are not logged if so configured (HTTP2ProtocolTest)
context =
context
|> https_server(http_options: [log_protocol_errors: false])
|> Enum.into(context)

socket = SimpleH2Client.tls_client(context)
SimpleH2Client.exchange_prefaces(socket)

output =
capture_log(fn ->
# Send trailers with pseudo headers
{:ok, ctx} = SimpleH2Client.send_simple_headers(socket, 1, :post, "/echo", context.port)
SimpleH2Client.send_headers(socket, 1, true, [{":path", "/foo"}], ctx)
assert SimpleH2Client.recv_rst_stream(socket) == {:ok, 1, 1}
Process.sleep(100)
end)

assert output == ""
end

@tag capture_log: true
test "it should shut down the connection after read timeout has been reached with no initial data sent",
context do
Expand Down

0 comments on commit 53822ef

Please sign in to comment.