Prevent service degradation or outage due to malicious requests with excessively large Content-Length headers
defmodule VulnerableApp do
use Plug.Router
plug :match
plug :dispatch
get '/' do
send_resp(conn, 200, 'Hello, world!')
end
end
This basic Plug-based Elixir application doesn't impose any limits on the Content-Length of incoming requests, making it vulnerable to DoS attacks.
defmodule SecureApp do
use Plug.Router
plug Plug.Parsers, parsers: [:urlencoded, :multipart, :json], pass: ['*/*'], json_decoder: Poison, length: 1_000_000
plug :match
plug :dispatch
get '/' do
send_resp(conn, 200, 'Hello, world!')
end
end
This Elixir application uses the Cowboy HTTP server with a configuration that limits the maximum request body size, mitigating the DoS vulnerability.