Commit Graph

7827 Commits

Author SHA1 Message Date
Maxim Dounin
5348706fe6 gRPC: generate error when response size is wrong.
As long as the "Content-Length" header is given, we now make sure
it exactly matches the size of the response.  If it doesn't,
the response is considered malformed and must not be forwarded
(https://tools.ietf.org/html/rfc7540#section-8.1.2.6).  While it
is not really possible to "not forward" the response which is already
being forwarded, we generate an error instead, which is the closest
equivalent.

Previous behaviour was to pass everything to the client, but this
seems to be suboptimal and causes issues (ticket #1695).  Also this
directly contradicts HTTP/2 specification requirements.

Note that the new behaviour for the gRPC proxy is more strict than that
applied in other variants of proxying.  This is intentional, as HTTP/2
specification requires us to do so, while in other types of proxying
malformed responses from backends are well known and historically
tolerated.
2020-07-06 18:36:25 +03:00
Maxim Dounin
1194ba36a0 FastCGI: protection from responses with wrong length.
Previous behaviour was to pass everything to the client, but this
seems to be suboptimal and causes issues (ticket #1695).  Fix is to
drop extra data instead, as it naturally happens in most clients.

Additionally, we now also issue a warning if the response is too
short, and make sure the fact it is truncated is propagated to the
client.  The u->error flag is introduced to make it possible to
propagate the error to the client in case of unbuffered proxying.

For responses to HEAD requests there is an exception: we do allow
both responses without body and responses with body matching the
Content-Length header.
2020-07-06 18:36:23 +03:00
Maxim Dounin
dfcfcc5a88 Upstream: drop extra data sent by upstream.
Previous behaviour was to pass everything to the client, but this
seems to be suboptimal and causes issues (ticket #1695).  Fix is to
drop extra data instead, as it naturally happens in most clients.

This change covers generic buffered and unbuffered filters as used
in the scgi and uwsgi modules.  Appropriate input filter init
handlers are provided by the scgi and uwsgi modules to set corresponding
lengths.

Note that for responses to HEAD requests there is an exception:
we do allow any response length.  This is because responses to HEAD
requests might be actual full responses, and it is up to nginx
to remove the response body.  If caching is enabled, only full
responses matching the Content-Length header will be cached
(see b779728b180c).
2020-07-06 18:36:22 +03:00
Maxim Dounin
7f2490c43c Proxy: style. 2020-07-06 18:36:21 +03:00
Maxim Dounin
156e193408 Proxy: detection of data after final chunk.
Previously, additional data after final chunk was either ignored
(in the same buffer, or during unbuffered proxying) or sent to the
client (in the next buffer already if it was already read from the
socket).  Now additional data are properly detected and ignored
in all cases.  Additionally, a warning is now logged and keepalive
is disabled in the connection.
2020-07-06 18:36:20 +03:00
Maxim Dounin
a2abe31a85 Proxy: drop extra data sent by upstream.
Previous behaviour was to pass everything to the client, but this
seems to be suboptimal and causes issues (ticket #1695).  Fix is to
drop extra data instead, as it naturally happens in most clients.
2020-07-06 18:36:19 +03:00
Maxim Dounin
b835b57184 Memcached: protect from too long responses.
If a memcached response was followed by a correct trailer, and then
the NUL character followed by some extra data - this was accepted by
the trailer checking code.  This in turn resulted in ctx->rest underflow
and caused negative size buffer on the next reading from the upstream,
followed by the "negative size buf in writer" alert.

Fix is to always check for too long responses, so a correct trailer cannot
be followed by extra data.
2020-07-06 18:36:17 +03:00
Ruslan Ermilov
829c9d5981 HTTP/2: lingering close after GOAWAY.
After sending the GOAWAY frame, a connection is now closed using
the lingering close mechanism.

This allows for the reliable delivery of the GOAWAY frames, while
also fixing connection resets observed when http2_max_requests is
reached (ticket #1250), or with graceful shutdown (ticket #1544),
when some additional data from the client is received on a fully
closed connection.

For HTTP/2, the settings lingering_close, lingering_timeout, and
lingering_time are taken from the "server" level.
2020-07-03 16:16:47 +03:00
Roman Arutyunyan
3db00b4da7 HTTP/3: simplified handling return codes from parse functions. 2020-07-02 20:07:24 +03:00
Roman Arutyunyan
ac9c1dcad8 HTTP/3: put ngx_http_v3_parse_varlen_int() return code in variable.
This makes calling this function similar to other parse functions.
2020-07-03 12:07:43 +03:00
Roman Arutyunyan
8d1875d39a HTTP/3: simplifed handling ngx_http_v3_parse_literal() return code. 2020-07-03 12:05:05 +03:00
Roman Arutyunyan
3a9874c4ab HTTP/3: limited prefixed integer size by 62 bits. 2020-07-03 09:26:12 +03:00
Roman Arutyunyan
3b2eabde0b HTTP/3: fixed overflow in prefixed integer parser.
Previously, the expression (ch & 0x7f) was promoted to a signed integer.
Depending on the platform, the size of this integer could be less than 8 bytes,
leading to overflow when handling the higher bits of the result.  Also, sign
bit of this integer could be replicated when adding to the 64-bit st->value.
2020-07-03 16:41:31 +03:00
Sergey Kandaurov
0ebcffcf14 HTTP/3: fixed prefix in decoding Section Acknowledgement. 2020-07-02 17:35:57 +03:00
Roman Arutyunyan
d839fee75f HTTP/3: set r->headers_in.chunked flag after parsing headers.
Previously it was set when creating the request object.  The side-effect was
trying to discard the request body in case of header parse error.
2020-06-30 15:32:09 +03:00
Roman Arutyunyan
707117276e HTTP/3: close QUIC connection with HTTP/QPACK errors when needed.
Previously errors led only to closing streams.

To simplify closing QUIC connection from a QUIC stream context, new macro
ngx_http_v3_finalize_connection() is introduced.  It calls
ngx_quic_finalize_connection() for the parent connection.
2020-07-02 16:47:51 +03:00
Roman Arutyunyan
fd35d92232 HTTP/3: error code definitions for HTTP/3 and QPACK. 2020-06-30 12:30:57 +03:00
Roman Arutyunyan
90f5c334f1 QUIC: Introduced ngx_quic_finalize_connection().
The function finalizes QUIC connection with an application protocol error
code and sends a CONNECTION_CLOSE frame with type=0x1d.

Also, renamed NGX_QUIC_FT_CONNECTION_CLOSE2 to NGX_QUIC_FT_CONNECTION_CLOSE_APP.
2020-07-02 16:33:59 +03:00
Roman Arutyunyan
86e287a2de HTTP/3: downgraded literal size error level to NGX_LOG_INFO.
Now it's similar to HTTP/2.
2020-07-02 16:33:36 +03:00
Roman Arutyunyan
a687d08062 HTTP/3: refactored dynamic table implementation.
Previously dynamic table was not functional because of zero limit on its size
set by default.  Now the following changes enable it:

- new directives to set SETTINGS_QPACK_MAX_TABLE_CAPACITY and
  SETTINGS_QPACK_BLOCKED_STREAMS
- send settings with SETTINGS_QPACK_MAX_TABLE_CAPACITY and
  SETTINGS_QPACK_BLOCKED_STREAMS to the client
- send Insert Count Increment to the client
- send Header Acknowledgement to the client
- evict old dynamic table entries on overflow
- decode Required Insert Count from client
- block stream if Required Insert Count is not reached
2020-07-02 15:34:05 +03:00
Roman Arutyunyan
a7ef0da3c8 HTTP/3: fixed prefixed integer encoding and decoding.
Previously bytes were ordered from MSB to LSB, but the right order is the
reverse.
2020-07-02 15:15:55 +03:00
Maxim Dounin
fa2f2e3508 SSL: fixed unexpected certificate requests (ticket #2008).
Using SSL_CTX_set_verify(SSL_VERIFY_PEER) implies that OpenSSL will
send a certificate request during an SSL handshake, leading to unexpected
certificate requests from browsers as long as there are any client
certificates installed.  Given that ngx_ssl_trusted_certificate()
is called unconditionally by the ngx_http_ssl_module, this affected
all HTTPS servers.  Broken by 699f6e55bbb4 (not released yet).

Fix is to set verify callback in the ngx_ssl_trusted_certificate() function
without changing the verify mode.
2020-06-29 17:15:51 +03:00
Roman Arutyunyan
8d41d17a12 HTTP/3: http3_max_field_size directive to limit string size.
Client streams may send literal strings which are now limited in size by the
new directive.  The default value is 4096.

The directive is similar to HTTP/2 directive http2_max_field_size.
2020-06-29 15:56:14 +03:00
Roman Arutyunyan
b7662c0e80 HTTP/3: introduced ngx_http_v3_get_module_srv_conf() macro.
The macro helps to access a module's server configuration from a QUIC
stream context.
2020-06-26 11:58:00 +03:00
Roman Arutyunyan
690e1bc50c HTTP/3: fixed dropping first non-pseudo header. 2020-06-26 10:05:28 +03:00
Sergey Kandaurov
3168f58306 HTTP/3: do not emit a DATA frame header for header_only responses.
This resulted in the frame error due to the invalid DATA frame length.
2020-06-25 20:31:13 +03:00
Vladimir Homutov
9022fc686d Style. 2020-06-19 11:29:30 +03:00
Sergey Kandaurov
cbfecb46fe README: documented draft-28, draft-29 support. 2020-06-23 11:57:00 +03:00
Sergey Kandaurov
439fad4df5 Update Initial salt and Retry secret from quic-tls-29.
See sections 5.2 and 5.8 for the current values.
2020-06-23 11:57:00 +03:00
Sergey Kandaurov
3d27c55ae3 Get rid of hardcoded numbers used for quic handshake errors. 2020-06-23 11:57:00 +03:00
Sergey Kandaurov
fc0036bdd6 Discard short packets which could not be decrypted.
So that connections are protected from failing from on-path attacks.
Decryption failure of long packets used during handshake still leads
to connection close since it barely makes sense to handle them there.
2020-06-23 11:57:00 +03:00
Sergey Kandaurov
d7baead1e8 Close connection with PROTOCOL_VIOLATION on decryption failure.
A previously used undefined error code is now replaced with the generic one.

Note that quic-transport prescribes keeping connection intact, discarding such
QUIC packets individually, in the sense that coalesced packets could be there.
This is selectively handled in the next change.
2020-06-23 11:57:00 +03:00
Sergey Kandaurov
82519e1af2 Define KEY_UPDATE_ERROR from quic-tls-24. 2020-06-23 11:57:00 +03:00
Sergey Kandaurov
157da97d7a Reject new QUIC connection with CONNECTION_REFUSED on shutdown. 2020-06-23 11:57:00 +03:00
Sergey Kandaurov
1cfd67c44a Close QUIC connection with NO_ERROR on c->close.
That way it makes more sense.  Previously it was closed with INTERNAL_ERROR.
2020-06-23 11:57:00 +03:00
Sergey Kandaurov
9d46500914 Do not close QUIC sockets in ngx_close_listening_sockets().
This breaks graceful shutdown of QUIC connections in terms of quic-transport.
2020-06-23 11:57:00 +03:00
Sergey Kandaurov
c31e105403 QUIC error SERVER_BUSY renamed to CONNECTION_REFUSED in draft-29. 2020-06-23 11:57:00 +03:00
Vladimir Homutov
f1fdf6901b QUIC: cleaned up quic encryption state tracking.
The patch removes remnants of the old state tracking mechanism, which did
not take into account assimetry of read/write states and was not very
useful.

The encryption state now is entirely tracked using SSL_quic_read/write_level().
2020-06-18 14:29:24 +03:00
Vladimir Homutov
28f1acdb6f QUIC: added ALPN checks.
quic-transport draft 29:

    section 7:

    *  authenticated negotiation of an application protocol (TLS uses
       ALPN [RFC7301] for this purpose)

    ...

    Endpoints MUST explicitly negotiate an application protocol.  This
    avoids situations where there is a disagreement about the protocol
    that is in use.

    section 8.1:

    When using ALPN, endpoints MUST immediately close a connection (see
    Section 10.3 of [QUIC-TRANSPORT]) with a no_application_protocol TLS
    alert (QUIC error code 0x178; see Section 4.10) if an application
    protocol is not negotiated.

Changes in ngx_quic_close_quic() function are required to avoid attempts
to generated and send packets without proper keys, what happens in case
of failed ALPN check.
2020-06-18 13:58:46 +03:00
Vladimir Homutov
a213258b5b QUIC: fixed off-by-one in frame range handler.
The ctx->pnum is incremented after the packet is sent, thus pointing to the
next packet number, which should not be used in comparison.
2020-06-18 11:16:35 +03:00
Vladimir Homutov
3b3264dc25 QUIC: further limiting maximum QUIC packet size.
quic-transport draft 29, section 14:

    QUIC depends upon a minimum IP packet size of at least 1280 bytes.
    This is the IPv6 minimum size [RFC8200] and is also supported by most
    modern IPv4 networks.  Assuming the minimum IP header size, this
    results in a QUIC maximum packet size of 1232 bytes for IPv6 and 1252
    bytes for IPv4.

Since the packet size can change during connection lifetime, the
ngx_quic_max_udp_payload() function is introduced that currently
returns minimal allowed size, depending on address family.
2020-06-16 11:54:05 +03:00
Vladimir Homutov
d6d7838c79 QUIC: raise error on missing transport parameters.
quic-tls, 8.2:

    The quic_transport_parameters extension is carried in the ClientHello
    and the EncryptedExtensions messages during the handshake.  Endpoints
    MUST send the quic_transport_parameters extension; endpoints that
    receive ClientHello or EncryptedExtensions messages without the
    quic_transport_parameters extension MUST close the connection with an
    error of type 0x16d (equivalent to a fatal TLS missing_extension
    alert, see Section 4.10).
2020-06-15 17:06:40 +03:00
Vladimir Homutov
6c2712f781 QUIC: Fixed connection cleanup.
A posted event need to be deleted during the connection close.
2020-06-15 16:59:53 +03:00
Eran Kornblau
1bbc37d35c Fixed potential leak of temp pool.
In case ngx_hash_add_key() fails, need to goto failed instead of returning,
so that temp_pool will be destoryed.
2020-06-15 03:58:31 -04:00
Maxim Dounin
0a683fdd93 Cache: introduced min_free cache clearing.
Clearing cache based on free space left on a file system is
expected to allow better disk utilization in some cases, notably
when disk space might be also used for something other than nginx
cache (including nginx own temporary files) and while loading
cache (when cache size might be inaccurate for a while, effectively
disabling max_size cache clearing).

Based on a patch by Adam Bambuch.
2020-06-22 18:03:00 +03:00
Maxim Dounin
6bb4336196 Too large st_blocks values are now ignored (ticket #157).
With XFS, using "allocsize=64m" mount option results in large preallocation
being reported in the st_blocks as returned by fstat() till the file is
closed.  This in turn results in incorrect cache size calculations and
wrong clearing based on max_size.

To avoid too aggressive cache clearing on such volumes, st_blocks values
which result in sizes larger than st_size and eight blocks (an arbitrary
limit) are no longer trusted, and we use st_size instead.

The ngx_de_fs_size() counterpart is intentionally not modified, as
it is used on closed files and hence not affected by this problem.
2020-06-22 18:02:59 +03:00
Maxim Dounin
cd69bf51ca Large block sizes on Linux are now ignored (ticket #1168).
NFS on Linux is known to report wsize as a block size (in both f_bsize
and f_frsize, both in statfs() and statvfs()).  On the other hand,
typical file system block sizes on Linux (ext2/ext3/ext4, XFS) are limited
to pagesize.  (With FAT, block sizes can be at least up to 512k in
extreme cases, but this doesn't really matter, see below.)
To avoid too aggressive cache clearing on NFS volumes on Linux, block
sizes larger than pagesize are now ignored.

Note that it is safe to ignore large block sizes.  Since 3899:e7cd13b7f759
(1.0.1) cache size is calculated based on fstat() st_blocks, and rounding
to file system block size is preserved mostly for Windows.

Note well that on other OSes valid block sizes seen are at least up
to 65536.  In particular, UFS on FreeBSD is known to work well with block
and fragment sizes set to 65536.
2020-06-22 18:02:58 +03:00
Roman Arutyunyan
7547581bbc OCSP: fixed use-after-free on error.
When validating second and further certificates, ssl callback could be called
twice to report the error.  After the first call client connection is
terminated and its memory is released.  Prior to the second call and in it
released connection memory is accessed.

Errors triggering this behavior:
- failure to create the request
- failure to start resolving OCSP responder name
- failure to start connecting to the OCSP responder

The fix is to rearrange the code to eliminate the second call.
2020-06-15 20:17:16 +03:00
Quantum
2afc050bd0 Correctly flush request body to uwsgi with SSL.
The flush flag was not set when forwarding the request body to the uwsgi
server. When using uwsgi_pass suwsgi://..., this causes the uwsgi server
to wait indefinitely for the request body and eventually time out due to
SSL buffering.

This is essentially the same change as 4009:3183165283cc, which was made
to ngx_http_proxy_module.c.

This will fix the uwsgi bug https://github.com/unbit/uwsgi/issues/1490.
2020-06-15 17:35:26 -04:00
Vladimir Homutov
966a867971 Style. 2020-06-10 21:37:48 +03:00