Debian Codegraph @

docs/topics/security.rst

raw
1 Security
2 ========
3
4 .. currentmodule:: websockets
5
6 Encryption
7 ----------
8
9 In production, you should always secure WebSocket connections with TLS.
10
11 Secure WebSocket connections provide confidentiality and integrity, as well as
12 better reliability because they reduce the risk of interference by bad proxies.
13
14 WebSocket servers are usually deployed behind a reverse proxy that terminates
15 TLS. Else, you can :doc:`configure TLS <../howto/encryption>` for the server.
16
17 Memory usage
18 ------------
19
20 .. warning::
21
22 An attacker who can open an arbitrary number of connections will be able
23 to perform a denial of service by memory exhaustion. If you're concerned
24 by denial of service attacks, you must reject suspicious connections
25 before they reach websockets, typically in a reverse proxy.
26
27 With the default settings, opening a connection uses 70 KiB of memory.
28
29 Sending some highly compressed messages could use up to 128 MiB of memory with
30 an amplification factor of 1000 between network traffic and memory usage.
31
32 Configuring a server to :doc:`optimize memory usage <memory>` will improve
33 security in addition to improving performance.
34
35 HTTP limits
36 -----------
37
38 In the opening handshake, websockets applies limits to the amount of data that
39 it accepts in order to minimize exposure to denial of service attacks.
40
41 The request or status line is limited to 8192 bytes. Each header line, including
42 the name and value, is limited to 8192 bytes too. No more than 128 HTTP headers
43 are allowed. When the HTTP response includes a body, it is limited to 1 MiB.
44
45 You may change these limits by setting the :envvar:`WEBSOCKETS_MAX_LINE_LENGTH`,
46 :envvar:`WEBSOCKETS_MAX_NUM_HEADERS`, and :envvar:`WEBSOCKETS_MAX_BODY_SIZE`
47 environment variables respectively.
48
49 When a handshake request exceeds one of these limits, the server replies with
50 HTTP 414 URI Too Long or 431 Request Header Fields Too Large, then closes the
51 connection.
52
53 When a handshake response exceeds one of these limits, the client closes the
54 connection and raises an exception.
55
56 Identification
57 --------------
58
59 By default, websockets identifies itself with a ``Server`` or ``User-Agent``
60 header in the format ``"Python/x.y.z websockets/X.Y"``.
61
62 You can set the ``server_header`` argument of :func:`~asyncio.server.serve` or
63 the ``user_agent_header`` argument of :func:`~asyncio.client.connect` to
64 configure another value. Setting them to :obj:`None` removes the header.
65
66 Alternatively, you can set the :envvar:`WEBSOCKETS_SERVER` and
67 :envvar:`WEBSOCKETS_USER_AGENT` environment variables respectively. Setting them
68 to an empty string removes the header.
69
70 If both the argument and the environment variable are set, the argument takes
71 precedence.