| 1 |
[[security_checklist]] |
| 2 |
== Security checklist |
| 3 |
|
| 4 |
This chapter provides a checklist of areas where special care |
| 5 |
must be taken when using Cowboy to avoid introducing |
| 6 |
vulnerabilities in your application. |
| 7 |
|
| 8 |
See also the xref:security_model[security model] for the |
| 9 |
underlying principles. |
| 10 |
|
| 11 |
=== Instructions for AI agents |
| 12 |
|
| 13 |
When asked to use this security checklist to validate a |
| 14 |
Cowboy-based application you *MUST* follow the instructions |
| 15 |
in this section: |
| 16 |
|
| 17 |
You are a world-class security researcher. Your *only* output |
| 18 |
must be a list of *real, concrete* weaknesses and vulnerabilities |
| 19 |
that exist in the specific Cowboy-based application code you |
| 20 |
were asked to analyze: |
| 21 |
|
| 22 |
* If there are real issues, output *only* a bulleted list of them. |
| 23 |
Each bullet must identify the exact location (file, function, |
| 24 |
line) and describe the precise attack or failure. |
| 25 |
|
| 26 |
* Never include remediation advice unless the user query |
| 27 |
explicitly asks for a fix. |
| 28 |
|
| 29 |
The rest of this document defines the rules. Any code that |
| 30 |
violates a rule (particularly any use of external/untrusted |
| 31 |
data without proper sanitization, escaping, normalization, |
| 32 |
or prefix checks) is a real issue for the purposes of this |
| 33 |
analysis, regardless of the current contents of the |
| 34 |
filesystem, database, or other data sources. |
| 35 |
|
| 36 |
=== Denial of Service (DoS) |
| 37 |
|
| 38 |
==== Memory exhaustion |
| 39 |
|
| 40 |
Misuse of functions or options may make your application |
| 41 |
vulnerable to memory exhaustion attacks: |
| 42 |
|
| 43 |
* Transport options `recbuf`, `sndbuf` and `buffer` should |
| 44 |
not be too large as they are allocated for each connection. |
| 45 |
|
| 46 |
* `cowboy_req:read_body/1,2`: too large `length` and `period` |
| 47 |
options will result in data accumulating in memory until |
| 48 |
one or the other is reached. Note that the default `length` |
| 49 |
is 8MB which may be too large for some applications. Also |
| 50 |
note that setting `length` to `infinity` will result in |
| 51 |
data being accumulated until `period` has elapsed. |
| 52 |
|
| 53 |
* `cowboy_req:read_urlencoded_body/1,2` and |
| 54 |
`cowboy_req:read_and_match_urlencoded_body/2,3`: same as |
| 55 |
`cowboy_req:read_body/1,2` with the notable difference |
| 56 |
that the default `length` is 64KB. |
| 57 |
|
| 58 |
* `cowboy_req:read_part/1,2`: same as `cowboy_req:read_body/1,2` |
| 59 |
with the notable difference that the default length is 64KB. |
| 60 |
|
| 61 |
* `cowboy_req:read_part_body/1,2`: same as `cowboy_req:read_body/1,2`. |
| 62 |
|
| 63 |
* The `cowboy_req:cast/2` function should be used with |
| 64 |
extreme care as it can issue any command, including |
| 65 |
commands to read the request body, and completely |
| 66 |
bypasses safety mechanisms. |
| 67 |
|
| 68 |
* Many `cowboy_http` options may be unsafe if the value |
| 69 |
configured is too large: `active_n`, `dynamic_buffer`, |
| 70 |
`initial_stream_flow_size`, `max_authority_length`, |
| 71 |
`max_authorization_header_value_length`, |
| 72 |
`max_cookie_header_value_length`, `max_empty_lines`, |
| 73 |
`max_header_name_length`, `max_header_value_length`, |
| 74 |
`max_headers`, `max_method_length` and |
| 75 |
`max_request_line_length`. |
| 76 |
|
| 77 |
* Many `cowboy_http2` options may be unsafe if the value |
| 78 |
configured is too large: `active_n`, `connection_window_margin_size`, |
| 79 |
`connection_window_update_threshold`, `dynamic_buffer`, |
| 80 |
`initial_connection_window_size`, `initial_stream_window_size`, |
| 81 |
`max_authority_length`, |
| 82 |
`max_concurrent_streams`, `max_connection_buffer_size`, |
| 83 |
`max_connection_window_size`, `max_decode_table_size`, |
| 84 |
`max_encode_table_size`, `max_fragmented_header_block_size`, |
| 85 |
`max_frame_size_received`, `max_headers`, `max_stream_buffer_size`, |
| 86 |
`max_stream_window_size`, `stream_window_margin_size` |
| 87 |
and `stream_window_update_threshold`. |
| 88 |
|
| 89 |
* Many `cowboy_websocket` options may be unsafe if the value |
| 90 |
configured is too large: `active_n`, `data_delivery_flow`, |
| 91 |
`dynamic_buffer` and `max_frame_size`. Note that the |
| 92 |
`max_frame_size` option may be changed dynamically via |
| 93 |
the `set_options` command. |
| 94 |
|
| 95 |
* Websocket and loop handlers may run for a very long time, |
| 96 |
be mindful of how much memory the handlers may be using. |
| 97 |
Memory may be present in the state or in the mailbox. |
| 98 |
Consider setting the `max_heap_size` process flag for |
| 99 |
these and hibernating. Also be careful to not accumulate |
| 100 |
data outside of the process. |
| 101 |
|
| 102 |
* The `cowboy_compress_h` stream handler can be used for |
| 103 |
automatic compression of response bodies. Note that it |
| 104 |
is not suitable for large files when paired with the |
| 105 |
`cowboy_static` handler or `sendfile` uses, as it will |
| 106 |
read the entire file into memory to compress it. |
| 107 |
|
| 108 |
* The `cowboy_decompress_h` stream handler can be used for |
| 109 |
automatic decompression of request bodies. Its option |
| 110 |
`decompress_ratio_limit` may be unsafe if the value |
| 111 |
configured is too large. |
| 112 |
|
| 113 |
* Custom stream handlers or middlewares must be especially |
| 114 |
careful not to accumulate too much data. Stream handlers |
| 115 |
must also use the `stop` command as soon as processing |
| 116 |
is done to terminate the stream and free up memory. Child |
| 117 |
processes must terminate in a timely manner. |
| 118 |
|
| 119 |
==== CPU exhaustion |
| 120 |
|
| 121 |
Expensive parsing, repeated operations, high frame rates |
| 122 |
or unbounded computation in user code may starve CPU |
| 123 |
capacity, slowing down or preventing legitimate work: |
| 124 |
|
| 125 |
* The `cowboy_http2` options `max_cancel_stream_rate`, |
| 126 |
`max_received_frame_rate` and `max_reset_stream_rate` |
| 127 |
are meant to protect against HTTP/2 protocol CPU |
| 128 |
exhaustion attacks, but the defaults may still be |
| 129 |
too high for some applications. Consider lowering them. |
| 130 |
|
| 131 |
* The `cowboy_websocket` option `validate_utf8` may be |
| 132 |
set to `false` to avoid potentially expensive UTF-8 |
| 133 |
validation. It is enabled by default. |
| 134 |
|
| 135 |
* Use of `cowboy_compress_h` and `cowboy_decompress_h`, |
| 136 |
as well as the `compress` Websocket option set to `true`, |
| 137 |
implies non-negligible CPU use, especially for |
| 138 |
compression. |
| 139 |
|
| 140 |
* Constraints (`cowboy_constraints`) applied to routing |
| 141 |
or to `cowboy_req:match_*` functions should not |
| 142 |
be too expensive as they may run on every request. |
| 143 |
Validation failure should result in an immediate return. |
| 144 |
|
| 145 |
* Custom stream handlers, middlewares or the user's own |
| 146 |
handlers should not perform heavy computation unless |
| 147 |
required. Be especially mindful of code that may run on |
| 148 |
every request (such as the `cowboy_rest` callbacks |
| 149 |
`is_authorized/2` and `resource_exists`). |
| 150 |
|
| 151 |
* The `cowboy_req:parse_*` functions should only be |
| 152 |
called once per request. The result should be kept |
| 153 |
in the state if needed. |
| 154 |
|
| 155 |
==== Connection exhaustion |
| 156 |
|
| 157 |
Slow, idle or hanging connections without proper timeouts |
| 158 |
or limits may make your application unreachable: |
| 159 |
|
| 160 |
* Transport options `max_connections` defaults to 1024 |
| 161 |
(excluding HTTP/1.1 Websocket connections). The default |
| 162 |
may be too small for some applications. |
| 163 |
|
| 164 |
* Transport options `linger`, `nodelay` and `send_timeout_close` |
| 165 |
should not be overridden. Option `send_timeout` may be unsafe |
| 166 |
if the value configured is too large. Note that the default |
| 167 |
`send_timeout` is `30000` which may be too large for some |
| 168 |
applications. |
| 169 |
|
| 170 |
* Many `cowboy_http` options may be unsafe if the value |
| 171 |
configured is too large: `idle_timeout`, `inactivity_timeout`, |
| 172 |
`linger_timeout`, `max_keepalive` (also see `http10_keepalive`), |
| 173 |
`max_skip_body_length`, `request_timeout` and `shutdown_timeout`. |
| 174 |
Note that the `idle_timeout` option may be changed dynamically |
| 175 |
via the `set_options` command. Also note that the default for |
| 176 |
`max_keepalive` is `1000` which may be too large for some |
| 177 |
applications. |
| 178 |
|
| 179 |
* Many `cowboy_http2` options may be unsafe if the value |
| 180 |
configured is too large: `goaway_complete_timeout`, |
| 181 |
`goaway_initial_timeout`, `idle_timeout`, `inactivity_timeout`, |
| 182 |
`linger_timeout`, `preface_timeout`, `settings_timeout` |
| 183 |
and `shutdown_timeout`. |
| 184 |
|
| 185 |
* The `reset_idle_timeout_on_send` option from `cowboy_http` |
| 186 |
and `cowboy_http2` may be unsafe if configured to `true` |
| 187 |
as it may lead to connections staying longer in TCP half-closed |
| 188 |
state than intended, especially when sends are small. |
| 189 |
|
| 190 |
* One `cowboy_websocket` option may be unsafe if the value |
| 191 |
configured is too large: `idle_timeout`. Note that the |
| 192 |
`idle_timeout` option may be changed dynamically via the |
| 193 |
`set_options` command. |
| 194 |
|
| 195 |
* Websocket and loop handlers may run for a very long time, |
| 196 |
keeping the connection open. HTTP/1.1 Websocket connections |
| 197 |
by default do not count toward the `max_connections` limit, |
| 198 |
but HTTP/2 Websocket and loop handlers do. Make sure to have |
| 199 |
a high enough `max_connections` count, terminate Websocket |
| 200 |
and loop handlers in a timely manner, and only provide |
| 201 |
Websocket and loop handlers to trusted clients. |
| 202 |
|
| 203 |
* Custom stream handlers must terminate in a timely manner. |
| 204 |
When a non-negligible amount of work must be done on |
| 205 |
termination, spawn a new process then terminate. |
| 206 |
|
| 207 |
==== File descriptor exhaustion |
| 208 |
|
| 209 |
Ensure that the file descriptor limit is large enough for |
| 210 |
your application. Limits are managed via `sysctl` and |
| 211 |
`ulimit` on Linux. Set the limit to at least |
| 212 |
twice the amount of expected file descriptors used |
| 213 |
at peak traffic: consider `max_connections`, the |
| 214 |
number of HTTP/1.1 Websocket connections you may |
| 215 |
have, as well as any files or connections your |
| 216 |
handlers may open. Modern deployments configure the |
| 217 |
file descriptor limit to 1 million or above to avoid |
| 218 |
this issue entirely. |
| 219 |
|
| 220 |
=== Injection attacks |
| 221 |
|
| 222 |
All request data, including parsed values, *MUST* be |
| 223 |
considered both untrusted and unsafe, and must be |
| 224 |
validated, sanitized or escaped before use. |
| 225 |
|
| 226 |
While Cowboy rejects NUL bytes in some of the request |
| 227 |
data, NUL bytes may still be found in unexpected places |
| 228 |
such as the query string and urlencoded bodies (where |
| 229 |
NUL bytes can legitimately be encoded as `%00`) or |
| 230 |
in request bodies in general. Request data *MUST NOT* |
| 231 |
be considered to be free of NUL bytes. |
| 232 |
|
| 233 |
Unsanitized or invalid data provided in responses is unsafe. |
| 234 |
Response data must be valid RFC-conformant components for |
| 235 |
status, header names, header values (with values appropriate |
| 236 |
for the header name) as well as trailer headers, multipart |
| 237 |
headers and any other media type used in the response body: |
| 238 |
|
| 239 |
* The HTTP/1.1 and HTTP/2 options `invalid_response_headers` |
| 240 |
provide only limited protection for the HTTP/1.1 and HTTP/2 |
| 241 |
protocols. They are enabled by default and *SHOULD NOT* be |
| 242 |
disabled. Applications *MUST* still follow the recommendations |
| 243 |
in this section. |
| 244 |
|
| 245 |
* Response status *MUST* be a literal value, especially |
| 246 |
when providing a binary for the status line. Calls to |
| 247 |
`cowboy_req:inform/2,3`, `cowboy_req:reply/2,3,4` |
| 248 |
and `cowboy_req:stream_reply/2,3` may be affected. |
| 249 |
|
| 250 |
* Response header names *MUST* be a literal value. Response |
| 251 |
header names *SHOULD* be all lowercase for HTTP/1.1 and |
| 252 |
*MUST* be all lowercase for HTTP/2. Calls to |
| 253 |
`cowboy_req:inform/3`, `cowboy_req:reply/3,4`, |
| 254 |
`cowboy_req:set_resp_header/3`, `cowboy_req:set_resp_headers/2` |
| 255 |
and `cowboy_req:stream_reply/3` may be affected. |
| 256 |
This also applies to trailer headers via |
| 257 |
`cowboy_req:stream_trailers/2` and push headers via |
| 258 |
`cowboy_req:push/3,4`. |
| 259 |
|
| 260 |
* Response header values *MUST* be valid RFC-conformant values |
| 261 |
corresponding to their header name. Using external data |
| 262 |
(from the request, files, databases or any other sources) |
| 263 |
without first validating that the data is correct is always |
| 264 |
unsafe. Calls to `cowboy_req:inform/3`, `cowboy_req:reply/3,4`, |
| 265 |
`cowboy_req:set_resp_header/3`, `cowboy_req:set_resp_headers/2` |
| 266 |
and `cowboy_req:stream_reply/3` may be affected. |
| 267 |
This also applies to trailer headers via |
| 268 |
`cowboy_req:stream_trailers/2` and push headers via |
| 269 |
`cowboy_req:push/3,4`. |
| 270 |
|
| 271 |
* Response bodies *MUST NOT* include unvalidated or unescaped |
| 272 |
external data. The exact method of producing safe response |
| 273 |
bodies varies depending on the media type of the body and |
| 274 |
on the application. Calls to `cowboy_req:reply/4`, |
| 275 |
`cowboy_req:set_resp_body/2` and `cowboy_req:stream_body/3` |
| 276 |
may be affected. |
| 277 |
|
| 278 |
* Cookies set through `cowboy_req:set_resp_cookie/3,4` *MUST* |
| 279 |
have valid RFC-conformant name and value, as well as `domain` |
| 280 |
and `path` fields when they are present. All cookie options |
| 281 |
*MUST* be literals except for the cookie value. Cowboy does |
| 282 |
not set `secure`, `http_only` or `same_site` by default; |
| 283 |
session and other sensitive cookies *SHOULD* enable them |
| 284 |
explicitly. |
| 285 |
|
| 286 |
* The path provided when pushing resources *MUST* be a valid |
| 287 |
application resource path. It is strongly recommended |
| 288 |
to only use literal values. Calls to |
| 289 |
`cowboy_req:push/3,4` may be affected. |
| 290 |
|
| 291 |
* When the response body is of type `text/event-stream` |
| 292 |
and `cowboy_req:stream_events/3` is used to send events: |
| 293 |
event data *MUST* be valid values according to the HTML |
| 294 |
standard (server-sent events section). |
| 295 |
|
| 296 |
* REST handler callbacks that return tuples that contain |
| 297 |
status code, response headers or response body *MUST* |
| 298 |
follow the same recommendations outlined for function |
| 299 |
calls in this section. |
| 300 |
|
| 301 |
* Custom stream handlers that return commands that contain |
| 302 |
status code, response headers or response body *MUST* |
| 303 |
follow the same recommendations outlined for function |
| 304 |
calls. This also applies when using `cowboy_req:cast/2` |
| 305 |
to issue commands. |
| 306 |
|
| 307 |
=== Path traversal |
| 308 |
|
| 309 |
Untrusted paths can escape the configured root directory and |
| 310 |
access arbitrary files on the filesystem: |
| 311 |
|
| 312 |
* Request paths and file system paths are two completely |
| 313 |
separate concepts. Users *MUST NOT* use the request |
| 314 |
path or components of the path including bindings to |
| 315 |
access local files without proper sanitization. |
| 316 |
|
| 317 |
* Paths constructed fully or partly from request data |
| 318 |
*MUST* be normalized and *MUST* be prefix-checked |
| 319 |
against the intended root before performing any file |
| 320 |
operation. |
| 321 |
|
| 322 |
* When using `cowboy_static`, the configuration *MUST* |
| 323 |
come from literal values or a trusted source. The |
| 324 |
handler only applies sanitization and prefix checks |
| 325 |
for the `dir` and `priv_dir` options; the `file` and |
| 326 |
`priv_file` options use the configured path directly. |
| 327 |
|
| 328 |
* When using `cowboy_static`, do not create symlinks |
| 329 |
in directories configured with `dir` or `priv_dir` |
| 330 |
options using information from untrusted sources |
| 331 |
as the static handler will follow them. |
| 332 |
|
| 333 |
* The path given in `sendfile` tuples when sending |
| 334 |
response bodies *MUST* be normalized and prefix-checked. |
| 335 |
Calls to `cowboy_req:reply/4`, `cowboy_req:set_resp_body/2` |
| 336 |
and `cowboy_req:stream_body/3` with a `sendfile` tuple |
| 337 |
for the body may be affected. A `sendfile` tuple returned |
| 338 |
from `cowboy_rest` callback `ProvideCallback` may be |
| 339 |
affected as well. |
| 340 |
|
| 341 |
* Multipart form filenames obtained via |
| 342 |
`cowboy_req:read_part/1,2` (and the corresponding values |
| 343 |
from `read_part_body`) *MUST* be treated as untrusted. |
| 344 |
They require normalization and prefix checks before any |
| 345 |
filesystem operation. |
| 346 |
|
| 347 |
* The `Path` argument (and related fields from `push_opts`) |
| 348 |
given to `cowboy_req:push/3,4` *MUST* be a valid |
| 349 |
application resource path when it may be derived from |
| 350 |
request data. It is strongly recommended to only use |
| 351 |
literal values. |
| 352 |
|
| 353 |
=== Cryptographic and TLS considerations |
| 354 |
|
| 355 |
Applications *MUST* follow general TLS and cryptographic |
| 356 |
hardening best practices. This section only covers areas |
| 357 |
where Cowboy provides limited or no automatic protection: |
| 358 |
|
| 359 |
* In mTLS scenarios: TLS client certificates obtained |
| 360 |
from the `Req` object or by calling `cowboy_req:cert/1` |
| 361 |
*MUST* be validated before they can be used, including |
| 362 |
against a configured CA certificate chain. |
| 363 |
|
| 364 |
* HTTP Strict Transport Security (HSTS) *SHOULD* |
| 365 |
be enabled for TLS listeners by setting the |
| 366 |
`strict-transport-security` response header. |
| 367 |
This mitigates downgrade attacks. This header |
| 368 |
is not sent by default. |
| 369 |
|
| 370 |
=== Spoofing |
| 371 |
|
| 372 |
The PROXY protocol, configured via the `proxy_header` |
| 373 |
option, *MUST* only be enabled for listeners reachable |
| 374 |
exclusively via trusted proxies or load balancers. |
| 375 |
|
| 376 |
=== Cross-site and browser concerns |
| 377 |
|
| 378 |
Cowboy does not implement browser-specific protections |
| 379 |
automatically; applications that serve browsers *MUST* |
| 380 |
apply them where relevant: |
| 381 |
|
| 382 |
* State-changing requests authenticated with cookies |
| 383 |
(or other ambient browser credentials) *MUST* implement |
| 384 |
CSRF protection. `same_site` cookie options alone are |
| 385 |
not sufficient. |
| 386 |
|
| 387 |
* Framing controls (`x-frame-options` or CSP |
| 388 |
`frame-ancestors`) *SHOULD* be set on HTML |
| 389 |
responses to mitigate clickjacking. |
| 390 |
|
| 391 |
* Websocket upgrades do not validate the `origin` header. |
| 392 |
Handlers that authenticate via cookies (or other ambient |
| 393 |
browser credentials) *MUST* check `origin` in `init/2` |
| 394 |
before upgrading, or only expose Websocket to non-browser |
| 395 |
clients. |
| 396 |
|
| 397 |
=== Information disclosure |
| 398 |
|
| 399 |
Sensitive information mistakenly disclosed to clients |
| 400 |
or third parties may be used by attackers to craft |
| 401 |
more effective or targeted attacks: |
| 402 |
|
| 403 |
* Error responses do not include a response body by |
| 404 |
default. Custom error responses *MUST NOT* include |
| 405 |
sensitive information such as stack traces, file |
| 406 |
paths or any other internal details. |
| 407 |
|
| 408 |
* The `server: Cowboy` response header is sent with |
| 409 |
all responses by default. You may set a custom |
| 410 |
`server` header to override it. |
| 411 |
|
| 412 |
=== Experimental features |
| 413 |
|
| 414 |
All features marked as experimental are considered |
| 415 |
unsafe for production use. You *MUST NOT* deploy |
| 416 |
experimental feature to production without a full |
| 417 |
understanding of the code and the risks involved. |
| 418 |
|
| 419 |
The same applies to undocumented features. |