]> git.feebdaed.xyz Git - 0xmirror/nginx.git/log
0xmirror/nginx.git
4 years agoHTTP: connections with wrong ALPN protocols are now rejected.
Vladimir Homutov [Wed, 20 Oct 2021 06:50:02 +0000 (09:50 +0300)]
HTTP: connections with wrong ALPN protocols are now rejected.

This is a recommended behavior by RFC 7301 and is useful
for mitigation of protocol confusion attacks [1].

To avoid possible negative effects, list of supported protocols
was extended to include all possible HTTP protocol ALPN IDs
registered by IANA [2], i.e. "http/1.0" and "http/0.9".

[1] https://alpaca-attack.com/
[2] https://www.iana.org/assignments/tls-extensiontype-values/

4 years agoStream: the "ssl_alpn" directive.
Vladimir Homutov [Tue, 19 Oct 2021 09:19:59 +0000 (12:19 +0300)]
Stream: the "ssl_alpn" directive.

The directive sets the server list of supported application protocols
and requires one of this protocols to be negotiated if client is using
ALPN.

4 years agoSSL: added $ssl_alpn_protocol variable.
Vladimir Homutov [Thu, 14 Oct 2021 08:46:23 +0000 (11:46 +0300)]
SSL: added $ssl_alpn_protocol variable.

The variable contains protocol selected by ALPN during handshake and
is empty otherwise.

4 years agoHTTP/2: removed support for NPN.
Vladimir Homutov [Fri, 15 Oct 2021 07:02:15 +0000 (10:02 +0300)]
HTTP/2: removed support for NPN.

NPN was replaced with ALPN, published as RFC 7301 in July 2014.
It used to negotiate SPDY (and, in transition, HTTP/2).

NPN supported appeared in OpenSSL 1.0.1. It does not work with TLSv1.3 [1].
ALPN is supported since OpenSSL 1.0.2.

The NPN support was dropped in Firefox 53 [2] and Chrome 51 [3].

[1] https://github.com/openssl/openssl/issues/3665.
[2] https://bugzilla.mozilla.org/show_bug.cgi?id=1248198
[3] https://www.chromestatus.com/feature/5767920709795840

4 years agoUpstream: fixed logging level of upstream invalid header errors.
Maxim Dounin [Mon, 18 Oct 2021 13:46:59 +0000 (16:46 +0300)]
Upstream: fixed logging level of upstream invalid header errors.

In b87b7092cedb (nginx 1.21.1), logging level of "upstream sent invalid
header" errors was accidentally changed to "info".  This change restores
the "error" level, which is a proper logging level for upstream-side
errors.

4 years agoSynced ngx_http_subrequest() argument names (ticket #2255).
Maxim Dounin [Tue, 12 Oct 2021 20:18:18 +0000 (23:18 +0300)]
Synced ngx_http_subrequest() argument names (ticket #2255).

4 years agoProxy: disabled keepalive on extra data in non-buffered mode.
Awdhesh Mathpal [Fri, 8 Oct 2021 02:23:11 +0000 (19:23 -0700)]
Proxy: disabled keepalive on extra data in non-buffered mode.

The u->keepalive flag is initialized early if the response has no body
(or an empty body), and needs to be reset if there are any extra data,
similarly to how it is done in ngx_http_proxy_copy_filter().  Missed
in 83c4622053b0.

4 years agoFixed $content_length cacheability with chunked (ticket #2252).
Maxim Dounin [Wed, 6 Oct 2021 15:01:42 +0000 (18:01 +0300)]
Fixed $content_length cacheability with chunked (ticket #2252).

4 years agoStream: added half-close support.
Vladimir Homutov [Wed, 22 Sep 2021 07:20:00 +0000 (10:20 +0300)]
Stream: added half-close support.

The "proxy_half_close" directive enables handling of TCP half close.  If
enabled, connection to proxied server is kept open until both read ends get
EOF.  Write end shutdown is properly transmitted via proxy.

4 years agoRequest body: do not create temp file if there's nothing to write.
Roman Arutyunyan [Fri, 10 Sep 2021 09:59:22 +0000 (12:59 +0300)]
Request body: do not create temp file if there's nothing to write.

Do this only when the entire request body is empty and
r->request_body_in_file_only is set.

The issue manifested itself with missing warning "a client request body is
buffered to a temporary file" when the entire rb->buf is full and all buffers
are delayed by a filter.

4 years agoVersion bump.
Roman Arutyunyan [Tue, 14 Sep 2021 09:12:02 +0000 (12:12 +0300)]
Version bump.

4 years agorelease-1.21.3 tag
Maxim Dounin [Tue, 7 Sep 2021 15:21:03 +0000 (18:21 +0300)]
release-1.21.3 tag

4 years agonginx-1.21.3-RELEASE
Maxim Dounin [Tue, 7 Sep 2021 15:21:02 +0000 (18:21 +0300)]
nginx-1.21.3-RELEASE

4 years agoHTTP/2: optimized processing of small DATA frames.
Maxim Dounin [Mon, 6 Sep 2021 11:54:50 +0000 (14:54 +0300)]
HTTP/2: optimized processing of small DATA frames.

The request body filter chain is no longer called after processing
a DATA frame.  Instead, we now post a read event to do this.  This
ensures that multiple small DATA frames read during the same event loop
iteration are coalesced together, resulting in much faster processing.

Since rb->buf can now contain unprocessed data, window update is no
longer sent in ngx_http_v2_state_read_data() in case of flow control
being used due to filter buffering.  Instead, window will be updated
by ngx_http_v2_read_client_request_body_handler() in the posted read
event.

4 years agoHTTP/2: fixed timers left after request body reading.
Maxim Dounin [Mon, 6 Sep 2021 11:54:48 +0000 (14:54 +0300)]
HTTP/2: fixed timers left after request body reading.

Following rb->filter_need_buffering changes, request body reading is
only finished after the filter chain is called and rb->last_saved is set.
As such, with r->request_body_no_buffering, timer on fc->read is no
longer removed when the last part of the body is received, potentially
resulting in incorrect behaviour.

The fix is to call ngx_http_v2_process_request_body() from the
ngx_http_v2_read_unbuffered_request_body() function instead of
directly calling ngx_http_v2_filter_request_body(), so the timer
is properly removed.

4 years agoHTTP/2: fixed window updates when buffering in filters.
Maxim Dounin [Mon, 6 Sep 2021 11:54:47 +0000 (14:54 +0300)]
HTTP/2: fixed window updates when buffering in filters.

In the body read handler, the window was incorrectly calculated
based on the full buffer size instead of the amount of free space
in the buffer.  If the request body is buffered by a filter, and
the buffer is not empty after the read event is generated by the
filter to resume request body processing, this could result in
"http2 negative window update" alerts.

Further, in the body ready handler and in ngx_http_v2_state_read_data()
the buffer wasn't cleared when the data were already written to disk,
so the client might stuck without window updates.

4 years agoFixed debug logging.
Roman Arutyunyan [Thu, 2 Sep 2021 09:25:37 +0000 (12:25 +0300)]
Fixed debug logging.

4 years agoVersion bump.
Roman Arutyunyan [Fri, 3 Sep 2021 14:19:33 +0000 (17:19 +0300)]
Version bump.

4 years agorelease-1.21.2 tag
Maxim Dounin [Tue, 31 Aug 2021 15:13:47 +0000 (18:13 +0300)]
release-1.21.2 tag

4 years agonginx-1.21.2-RELEASE
Maxim Dounin [Tue, 31 Aug 2021 15:13:46 +0000 (18:13 +0300)]
nginx-1.21.2-RELEASE

4 years agoUpdated OpenSSL used for win32 builds.
Maxim Dounin [Tue, 31 Aug 2021 14:54:54 +0000 (17:54 +0300)]
Updated OpenSSL used for win32 builds.

4 years agoHTTP/2: avoid memcpy() with NULL source and zero length.
Maxim Dounin [Tue, 31 Aug 2021 13:44:13 +0000 (16:44 +0300)]
HTTP/2: avoid memcpy() with NULL source and zero length.

Prodded by Clang Static Analyzer.

4 years agoGive GCC atomics precedence over deprecated Darwin atomic(3).
Sergey Kandaurov [Mon, 30 Aug 2021 11:45:21 +0000 (14:45 +0300)]
Give GCC atomics precedence over deprecated Darwin atomic(3).

This allows to build nginx on macOS with -Wdeprecated-declarations.

4 years agoRequest body: reading body buffering in filters.
Maxim Dounin [Sun, 29 Aug 2021 19:22:02 +0000 (22:22 +0300)]
Request body: reading body buffering in filters.

If a filter wants to buffer the request body during reading (for
example, to check an external scanner), it can now do so.  To make
it possible, the code now checks rb->last_saved (introduced in the
previous change) along with rb->rest == 0.

Since in HTTP/2 this requires flow control to avoid overflowing the
request body buffer, so filters which need buffering have to set
the rb->filter_need_buffering flag on the first filter call.  (Note
that each filter is expected to call the next filter, so all filters
will be able set the flag if needed.)

4 years agoRequest body: introduced rb->last_saved flag.
Maxim Dounin [Sun, 29 Aug 2021 19:21:03 +0000 (22:21 +0300)]
Request body: introduced rb->last_saved flag.

It indicates that the last buffer was received by the save filter,
and can be used to check this at higher levels.  To be used in the
following changes.

4 years agoRequest body: added alert to catch duplicate body saving.
Maxim Dounin [Sun, 29 Aug 2021 19:20:54 +0000 (22:20 +0300)]
Request body: added alert to catch duplicate body saving.

If due to an error ngx_http_request_body_save_filter() is called
more than once with rb->rest == 0, this used to result in a segmentation
fault.  Added an alert to catch such errors, just in case.

4 years agoRequest body: missing comments about initialization.
Maxim Dounin [Sun, 29 Aug 2021 19:20:49 +0000 (22:20 +0300)]
Request body: missing comments about initialization.

4 years agoHTTP/2: improved handling of preread unbuffered requests.
Maxim Dounin [Sun, 29 Aug 2021 19:20:44 +0000 (22:20 +0300)]
HTTP/2: improved handling of preread unbuffered requests.

Previously, fully preread unbuffered requests larger than client body
buffer size were saved to disk, despite the fact that "unbuffered" is
expected to imply no disk buffering.

4 years agoHTTP/2: improved handling of END_STREAM in a separate DATA frame.
Maxim Dounin [Sun, 29 Aug 2021 19:20:38 +0000 (22:20 +0300)]
HTTP/2: improved handling of END_STREAM in a separate DATA frame.

The save body filter saves the request body to disk once the buffer is full.
Yet in HTTP/2 this might happen even if there is no need to save anything
to disk, notably when content length is known and the END_STREAM flag is
sent in a separate empty DATA frame.  Workaround is to provide additional
byte in the buffer, so saving the request body won't be triggered.

This fixes unexpected request body disk buffering in HTTP/2 observed after
the previous change when content length is known and the END_STREAM flag
is sent in a separate empty DATA frame.

4 years agoHTTP/2: reworked body reading to better match HTTP/1.x code.
Maxim Dounin [Sun, 29 Aug 2021 19:20:36 +0000 (22:20 +0300)]
HTTP/2: reworked body reading to better match HTTP/1.x code.

In particular, now the code always uses a buffer limited by
client_body_buffer_size.  At the cost of an additional copy it
ensures that small DATA frames are not directly mapped to small
write() syscalls, but rather buffered in memory before writing.
Further, requests without Content-Length are no longer forced
to use temporary files.

4 years agoHTTP/2: improved body reading logging.
Maxim Dounin [Sun, 29 Aug 2021 19:20:34 +0000 (22:20 +0300)]
HTTP/2: improved body reading logging.

4 years agoUpstream: fixed timeouts with gRPC, SSL and select (ticket #2229).
Maxim Dounin [Fri, 20 Aug 2021 00:53:56 +0000 (03:53 +0300)]
Upstream: fixed timeouts with gRPC, SSL and select (ticket #2229).

With SSL it is possible that an established connection is ready for
reading after the handshake.  Further, events might be already disabled
in case of level-triggered event methods.  If this happens and
ngx_http_upstream_send_request() blocks waiting for some data from
the upstream, such as flow control in case of gRPC, the connection
will time out due to no read events on the upstream connection.

Fix is to explicitly check the c->read->ready flag if sending request
blocks and post a read event if it is set.

Note that while it is possible to modify ngx_ssl_handshake() to keep
read events active, this won't completely resolve the issue, since
there can be data already received during the SSL handshake
(see 573bd30e46b4).

4 years agoMail: Auth-SSL-Protocol and Auth-SSL-Cipher headers (ticket #2134).
Rob Mueller [Fri, 13 Aug 2021 07:57:47 +0000 (03:57 -0400)]
Mail: Auth-SSL-Protocol and Auth-SSL-Cipher headers (ticket #2134).

This adds new Auth-SSL-Protocol and Auth-SSL-Cipher headers to
the mail proxy auth protocol when SSL is enabled.

This can be useful for detecting users using older clients that
negotiate old ciphers when you want to upgrade to newer
TLS versions of remove suppport for old and insecure ciphers.
You can use your auth backend to notify these users before the
upgrade that they either need to upgrade their client software
or contact your support team to work out an upgrade path.

4 years agoSSL: ciphers now set before loading certificates (ticket #2035).
Maxim Dounin [Mon, 16 Aug 2021 19:40:31 +0000 (22:40 +0300)]
SSL: ciphers now set before loading certificates (ticket #2035).

To load old/weak server or client certificates it might be needed to adjust
the security level, as introduced in OpenSSL 1.1.0.  This change ensures that
ciphers are set before loading the certificates, so security level changes
via the cipher string apply to certificate loading.

4 years agoDark mode support in welcome and 50x error pages.
Maxim Dounin [Mon, 16 Aug 2021 13:36:08 +0000 (16:36 +0300)]
Dark mode support in welcome and 50x error pages.

Prodded by Duncan Lock.

4 years agoWelcome and 50x error pages style.
Maxim Dounin [Mon, 16 Aug 2021 13:36:06 +0000 (16:36 +0300)]
Welcome and 50x error pages style.

Indentation of the CSS code removed to match style of the HTML code.

4 years agoSSL: removed use of the SSL_OP_MSIE_SSLV2_RSA_PADDING option.
Sergey Kandaurov [Tue, 10 Aug 2021 20:43:17 +0000 (23:43 +0300)]
SSL: removed use of the SSL_OP_MSIE_SSLV2_RSA_PADDING option.

It has no effect since OpenSSL 0.9.7h and 0.9.8a.

4 years agoSSL: removed export ciphers support.
Sergey Kandaurov [Tue, 10 Aug 2021 20:43:17 +0000 (23:43 +0300)]
SSL: removed export ciphers support.

Export ciphers are forbidden to negotiate in TLS 1.1 and later protocol modes.
They are disabled since OpenSSL 1.0.2g by default unless explicitly configured
with "enable-weak-ssl-ciphers", and completely removed in OpenSSL 1.1.0.

4 years agoSSL: use of the SSL_OP_IGNORE_UNEXPECTED_EOF option.
Sergey Kandaurov [Tue, 10 Aug 2021 20:43:17 +0000 (23:43 +0300)]
SSL: use of the SSL_OP_IGNORE_UNEXPECTED_EOF option.

A new behaviour was introduced in OpenSSL 1.1.1e, when a peer does not send
close_notify before closing the connection.  Previously, it was to return
SSL_ERROR_SYSCALL with errno 0, known since at least OpenSSL 0.9.7, and is
handled gracefully in nginx.  Now it returns SSL_ERROR_SSL with a distinct
reason SSL_R_UNEXPECTED_EOF_WHILE_READING ("unexpected eof while reading").
This leads to critical errors seen in nginx within various routines such as
SSL_do_handshake(), SSL_read(), SSL_shutdown().  The behaviour was restored
in OpenSSL 1.1.1f, but presents in OpenSSL 3.0 by default.

Use of the SSL_OP_IGNORE_UNEXPECTED_EOF option added in OpenSSL 3.0 allows
to set a compatible behaviour to return SSL_ERROR_ZERO_RETURN:
https://git.openssl.org/?p=openssl.git;a=commitdiff;h=09b90e0

See for additional details: https://github.com/openssl/openssl/issues/11381

4 years agoSSL: silenced warnings when building with OpenSSL 3.0.
Sergey Kandaurov [Tue, 10 Aug 2021 20:43:16 +0000 (23:43 +0300)]
SSL: silenced warnings when building with OpenSSL 3.0.

The OPENSSL_SUPPRESS_DEPRECATED macro is used to suppress deprecation warnings.
This covers Session Tickets keys, SSL Engine, DH low level API for DHE ciphers.

Unlike OPENSSL_API_COMPAT, it works well with OpenSSL built with no-deprecated.
In particular, it doesn't unhide various macros in OpenSSL includes, which are
meant to be hidden under OPENSSL_NO_DEPRECATED.

4 years agoSSL: ERR_peek_error_line_data() compatibility with OpenSSL 3.0.
Sergey Kandaurov [Tue, 10 Aug 2021 20:43:16 +0000 (23:43 +0300)]
SSL: ERR_peek_error_line_data() compatibility with OpenSSL 3.0.

ERR_peek_error_line_data() was deprecated in favour of ERR_peek_error_all().
Here we use the ERR_peek_error_data() helper to pass only used arguments.

4 years agoSSL: using SSL_CTX_set0_tmp_dh_pkey() with OpenSSL 3.0 in dhparam.
Sergey Kandaurov [Tue, 10 Aug 2021 20:43:16 +0000 (23:43 +0300)]
SSL: using SSL_CTX_set0_tmp_dh_pkey() with OpenSSL 3.0 in dhparam.

Using PEM_read_bio_DHparams() and SSL_CTX_set_tmp_dh() is deprecated
as part of deprecating the low level DH functions in favor of EVP_PKEY:
https://git.openssl.org/?p=openssl.git;a=commitdiff;h=163f6dc

4 years agoSSL: SSL_get_peer_certificate() is deprecated in OpenSSL 3.0.
Sergey Kandaurov [Tue, 10 Aug 2021 20:43:16 +0000 (23:43 +0300)]
SSL: SSL_get_peer_certificate() is deprecated in OpenSSL 3.0.

Switch to SSL_get1_peer_certificate() when building with OpenSSL 3.0
and OPENSSL_NO_DEPRECATED defined.

4 years agoSSL: RSA data type is deprecated in OpenSSL 3.0.
Sergey Kandaurov [Tue, 10 Aug 2021 20:42:59 +0000 (23:42 +0300)]
SSL: RSA data type is deprecated in OpenSSL 3.0.

The only consumer is a callback function for SSL_CTX_set_tmp_rsa_callback()
deprecated in OpenSSL 1.1.0.  Now the function is conditionally compiled too.

4 years agoDisabled HTTP/1.0 requests with Transfer-Encoding.
Sergey Kandaurov [Mon, 9 Aug 2021 15:12:12 +0000 (18:12 +0300)]
Disabled HTTP/1.0 requests with Transfer-Encoding.

The latest HTTP/1.1 draft describes Transfer-Encoding in HTTP/1.0 as having
potentially faulty message framing as that could have been forwarded without
handling of the chunked encoding, and forbids processing subsequest requests
over that connection: https://github.com/httpwg/http-core/issues/879.

While handling of such requests is permitted, the most secure approach seems
to reject them.

4 years agoSSL: SSL_CTX_set_tmp_dh() error handling.
Sergey Kandaurov [Wed, 4 Aug 2021 18:27:51 +0000 (21:27 +0300)]
SSL: SSL_CTX_set_tmp_dh() error handling.

For example, it can fail due to weak DH parameters.

4 years agoSSL: set events ready flags after handshake.
Maxim Dounin [Tue, 3 Aug 2021 17:50:30 +0000 (20:50 +0300)]
SSL: set events ready flags after handshake.

The c->read->ready and c->write->ready flags might be reset during
the handshake, and not set again if the handshake was finished on
the other event.  At the same time, some data might be read from
the socket during the handshake, so missing c->read->ready flag might
result in a connection hang, for example, when waiting for an SMTP
greeting (which was already received during the handshake).

Found by Sergey Kandaurov.

4 years agoVersion bump.
Maxim Dounin [Tue, 3 Aug 2021 17:50:08 +0000 (20:50 +0300)]
Version bump.

4 years agorelease-1.21.1 tag
Maxim Dounin [Tue, 6 Jul 2021 14:59:17 +0000 (17:59 +0300)]
release-1.21.1 tag

4 years agonginx-1.21.1-RELEASE
Maxim Dounin [Tue, 6 Jul 2021 14:59:16 +0000 (17:59 +0300)]
nginx-1.21.1-RELEASE

4 years agoWin32: use only preallocated memory in send/recv chain functions.
Ruslan Ermilov [Mon, 5 Jul 2021 10:26:49 +0000 (13:26 +0300)]
Win32: use only preallocated memory in send/recv chain functions.

The ngx_wsasend_chain() and ngx_wsarecv_chain() functions were
modified to use only preallocated memory, and the number of
preallocated wsabufs was increased to 64.

4 years agoUse only preallocated memory in ngx_readv_chain() (ticket #1408).
Ruslan Ermilov [Mon, 5 Jul 2021 10:09:23 +0000 (13:09 +0300)]
Use only preallocated memory in ngx_readv_chain() (ticket #1408).

In d1bde5c3c5d2, the number of preallocated iovec's for ngx_readv_chain()
was increased.  Still, in some setups, the function might allocate memory
for iovec's from a connection pool, which is only freed when closing the
connection.

The ngx_readv_chain() function was modified to use only preallocated
memory, similarly to the ngx_writev_chain() change in 8e903522c17a.

4 years agoDisabled control characters in the Host header.
Maxim Dounin [Mon, 28 Jun 2021 15:01:24 +0000 (18:01 +0300)]
Disabled control characters in the Host header.

Control characters (0x00-0x1f, 0x7f) and space are not expected to appear
in the Host header.  Requests with such characters in the Host header are
now unconditionally rejected.

4 years agoImproved logging of invalid headers.
Maxim Dounin [Mon, 28 Jun 2021 15:01:20 +0000 (18:01 +0300)]
Improved logging of invalid headers.

In 71edd9192f24 logging of invalid headers which were rejected with the
NGX_HTTP_PARSE_INVALID_HEADER error was restricted to just the "client
sent invalid header line" message, without any attempts to log the header
itself.

This patch returns logging of the header up to the invalid character and
the character itself.  The r->header_end pointer is now properly set
in all cases to make logging possible.

The same logging is also introduced when parsing headers from upstream
servers.

4 years agoDisabled control characters and space in header names.
Maxim Dounin [Mon, 28 Jun 2021 15:01:18 +0000 (18:01 +0300)]
Disabled control characters and space in header names.

Control characters (0x00-0x1f, 0x7f), space, and colon were never allowed in
header names.  The only somewhat valid use is header continuation which nginx
never supported and which is explicitly obsolete by RFC 7230.

Previously, such headers were considered invalid and were ignored by default
(as per ignore_invalid_headers directive).  With this change, such headers
are unconditionally rejected.

It is expected to make nginx more resilient to various attacks, in particular,
with ignore_invalid_headers switched off (which is inherently unsecure, though
nevertheless sometimes used in the wild).

4 years agoDisabled control characters in URIs.
Maxim Dounin [Mon, 28 Jun 2021 15:01:15 +0000 (18:01 +0300)]
Disabled control characters in URIs.

Control characters (0x00-0x1f, 0x7f) were never allowed in URIs, and must
be percent-encoded by clients.  Further, these are not believed to appear
in practice.  On the other hand, passing such characters might make various
attacks possible or easier, despite the fact that currently allowed control
characters are not significant for HTTP request parsing.

4 years agoDisabled spaces in URIs (ticket #196).
Maxim Dounin [Mon, 28 Jun 2021 15:01:13 +0000 (18:01 +0300)]
Disabled spaces in URIs (ticket #196).

From now on, requests with spaces in URIs are immediately rejected rather
than allowed.  Spaces were allowed in 31e9677b15a1 (0.8.41) to handle bad
clients.  It is believed that now this behaviour causes more harm than
good.

4 years agoCore: escaping of chars not allowed in URIs per RFC 3986.
Maxim Dounin [Mon, 28 Jun 2021 15:01:11 +0000 (18:01 +0300)]
Core: escaping of chars not allowed in URIs per RFC 3986.

Per RFC 3986 only the following characters are allowed in URIs unescaped:

unreserved    = ALPHA / DIGIT / "-" / "." / "_" / "~"
gen-delims    = ":" / "/" / "?" / "#" / "[" / "]" / "@"
sub-delims    = "!" / "$" / "&" / "'" / "(" / ")"
              / "*" / "+" / "," / ";" / "="

And "%" can appear as a part of escaping itself.  The following
characters are not allowed and need to be escaped: %00-%1F, %7F-%FF,
" ", """, "<", ">", "\", "^", "`", "{", "|", "}".

Not escaping ">" is known to cause problems at least with MS Exchange (see
http://nginx.org/pipermail/nginx-ru/2010-January/031261.html) and in
Tomcat (ticket #2191).

The patch adds escaping of the following chars in all URI parts: """, "<",
">", "\", "^", "`", "{", "|", "}".  Note that comments are mostly preserved
to outline important characters being escaped.

4 years agoCore: fixed comment about escaping in arguments.
Maxim Dounin [Mon, 28 Jun 2021 15:01:09 +0000 (18:01 +0300)]
Core: fixed comment about escaping in arguments.

After 4954530db2af, the ";" character is escaped by
ngx_escape_uri(NGX_ESCAPE_ARGS).

4 years agoDisabled requests with both Content-Length and Transfer-Encoding.
Maxim Dounin [Mon, 28 Jun 2021 15:01:06 +0000 (18:01 +0300)]
Disabled requests with both Content-Length and Transfer-Encoding.

HTTP clients are not allowed to generate such requests since Transfer-Encoding
introduction in RFC 2068, and they are not expected to appear in practice
except in attempts to perform a request smuggling attack.  While handling of
such requests is strictly defined, the most secure approach seems to reject
them.

4 years agoAdded CONNECT method rejection.
Maxim Dounin [Mon, 28 Jun 2021 15:01:04 +0000 (18:01 +0300)]
Added CONNECT method rejection.

No valid CONNECT requests are expected to appear within nginx, since it
is not a forward proxy.  Further, request line parsing will reject
proper CONNECT requests anyway, since we don't allow authority-form of
request-target.  On the other hand, RFC 7230 specifies separate message
length rules for CONNECT which we don't support, so make sure to always
reject CONNECTs to avoid potential abuse.

4 years agoMoved TRACE method rejection to a better place.
Maxim Dounin [Mon, 28 Jun 2021 15:01:00 +0000 (18:01 +0300)]
Moved TRACE method rejection to a better place.

Previously, TRACE requests were rejected before parsing Transfer-Encoding.
This is not important since keepalive is not enabled at this point anyway,
though rejecting such requests after properly parsing other headers is
less likely to cause issues in case of further code changes.

4 years agoCore: added the ngx_rbtree_data() macro.
Vladimir Homutov [Mon, 21 Jun 2021 06:42:43 +0000 (09:42 +0300)]
Core: added the ngx_rbtree_data() macro.

4 years agoFixed format strings for ngx_win32_version.
Maxim Dounin [Fri, 18 Jun 2021 01:00:21 +0000 (04:00 +0300)]
Fixed format strings for ngx_win32_version.

4 years agogRPC: RST_STREAM(NO_ERROR) handling micro-optimization.
Sergey Kandaurov [Thu, 17 Jun 2021 08:44:06 +0000 (11:44 +0300)]
gRPC: RST_STREAM(NO_ERROR) handling micro-optimization.

After 2096b21fcd10, a single RST_STREAM(NO_ERROR) may not result in an error.
This change removes several unnecessary ctx->type checks for such a case.

4 years agogRPC: handling GOAWAY with a higher last stream identifier.
Sergey Kandaurov [Thu, 17 Jun 2021 08:43:55 +0000 (11:43 +0300)]
gRPC: handling GOAWAY with a higher last stream identifier.

Previously, once received from upstream, it couldn't limit
opening additional streams in a cached keepalive connection.

4 years agoFixed SSL logging with lingering close.
Maxim Dounin [Tue, 1 Jun 2021 14:37:51 +0000 (17:37 +0300)]
Fixed SSL logging with lingering close.

Recent fixes to SSL shutdown with lingering close (554c6ae25ffc, 1.19.5)
broke logging of SSL variables.  To make sure logging of SSL variables
works properly, avoid freeing c->ssl when doing an SSL shutdown before
lingering close.

Reported by Reinis Rozitis
(http://mailman.nginx.org/pipermail/nginx/2021-May/060670.html).

4 years agoSSL: ngx_ssl_shutdown() rework.
Maxim Dounin [Tue, 1 Jun 2021 14:37:49 +0000 (17:37 +0300)]
SSL: ngx_ssl_shutdown() rework.

Instead of calling SSL_free() with each return point, introduced a single
place where cleanup happens.  As a positive side effect, this fixes two
potential memory leaks on ngx_handle_read_event() and ngx_handle_write_event()
errors where there were no SSL_free() calls (though unlikely practical,
as errors there are only expected to happen due to bugs or kernel issues).

4 years agoContrib: vim syntax, update core and 3rd party module directives.
Gena Makhomed [Sun, 30 May 2021 09:26:00 +0000 (12:26 +0300)]
Contrib: vim syntax, update core and 3rd party module directives.

4 years agoCore: disabled SO_REUSEADDR on UDP sockets while testing config.
Maxim Dounin [Mon, 31 May 2021 13:36:51 +0000 (16:36 +0300)]
Core: disabled SO_REUSEADDR on UDP sockets while testing config.

On Linux, SO_REUSEADDR allows completely duplicate UDP sockets, so using
SO_REUSEADDR when testing configuration results in packets being dropped
if there is an existing traffic on the sockets being tested (ticket #2187).
While dropped packets are expected with UDP, it is better to avoid this
when possible.

With this change, SO_REUSEADDR is no longer set on datagram sockets when
testing configuration.

4 years agoCore: disabled cloning sockets when testing config (ticket #2188).
Maxim Dounin [Mon, 31 May 2021 13:36:37 +0000 (16:36 +0300)]
Core: disabled cloning sockets when testing config (ticket #2188).

Since we anyway do not set SO_REUSEPORT when testing configuration
(see ecb5cd305b06), trying to open additional sockets does not make much
sense, as all these additional sockets are expected to result in EADDRINUSE
errors from bind().  On the other hand, there are reports that trying
to open these sockets takes significant time under load: total configuration
testing time greater than 15s was observed in ticket #2188, compared to less
than 1s without load.

With this change, no additional sockets are opened during testing
configuration.

4 years agoVersion bump.
Maxim Dounin [Mon, 31 May 2021 13:36:12 +0000 (16:36 +0300)]
Version bump.

4 years agorelease-1.21.0 tag
Maxim Dounin [Tue, 25 May 2021 12:28:56 +0000 (15:28 +0300)]
release-1.21.0 tag

4 years agonginx-1.21.0-RELEASE
Maxim Dounin [Tue, 25 May 2021 12:28:55 +0000 (15:28 +0300)]
nginx-1.21.0-RELEASE

4 years agoResolver: explicit check for compression pointers in question.
Maxim Dounin [Tue, 25 May 2021 12:17:50 +0000 (15:17 +0300)]
Resolver: explicit check for compression pointers in question.

Since nginx always uses exactly one entry in the question section of
a DNS query, and never uses compression pointers in this entry, parsing
of a DNS response in ngx_resolver_process_response() does not expect
compression pointers to appear in the question section of the DNS
response.  Indeed, compression pointers in the first name of a DNS response
hardly make sense, do not seem to be allowed by RFC 1035 (which says
"a pointer to a prior occurance of the same name", note "prior"), and
were never observed in practice.

Added an explicit check to ngx_resolver_process_response()'s parsing
of the question section to properly report an error if compression pointers
nevertheless appear in the question section.

4 years agoResolver: simplified ngx_resolver_copy().
Maxim Dounin [Tue, 25 May 2021 12:17:45 +0000 (15:17 +0300)]
Resolver: simplified ngx_resolver_copy().

Instead of checking on each label if we need to place a dot or not,
now it always adds a dot after a label, and reduces the resulting
length afterwards.

4 years agoResolver: reworked ngx_resolver_copy() copy loop.
Maxim Dounin [Tue, 25 May 2021 12:17:43 +0000 (15:17 +0300)]
Resolver: reworked ngx_resolver_copy() copy loop.

To make the code easier to read, reworked the ngx_resolver_copy()
copy loop to match the one used to calculate length.  No functional
changes.

4 years agoResolver: fixed label types handling in ngx_resolver_copy().
Maxim Dounin [Tue, 25 May 2021 12:17:41 +0000 (15:17 +0300)]
Resolver: fixed label types handling in ngx_resolver_copy().

Previously, anything with any of the two high bits set were interpreted
as compression pointers.  This is incorrect, as RFC 1035 clearly states
that "The 10 and 01 combinations are reserved for future use".  Further,
the 01 combination is actually allocated for EDNS extended label type
(see RFC 2671 and RFC 6891), not really used though.

Fix is to reject unrecognized label types rather than misinterpreting
them as compression pointers.

4 years agoResolver: fixed off-by-one read in ngx_resolver_copy().
Maxim Dounin [Tue, 25 May 2021 12:17:38 +0000 (15:17 +0300)]
Resolver: fixed off-by-one read in ngx_resolver_copy().

It is believed to be harmless, and in the worst case it uses some
uninitialized memory as a part of the compression pointer length,
eventually leading to the "name is out of DNS response" error.

4 years agoResolver: fixed off-by-one write in ngx_resolver_copy().
Maxim Dounin [Tue, 25 May 2021 12:17:36 +0000 (15:17 +0300)]
Resolver: fixed off-by-one write in ngx_resolver_copy().

Reported by Luis Merino, Markus Vervier, Eric Sesterhenn, X41 D-Sec GmbH.

4 years agoLocation header escaping in redirects (ticket #882).
Ruslan Ermilov [Mon, 24 May 2021 18:55:20 +0000 (21:55 +0300)]
Location header escaping in redirects (ticket #882).

The header is escaped in redirects based on request URI or
location name (auto redirect).

4 years agoFixed log action when using SSL certificates with variables.
Maxim Dounin [Mon, 24 May 2021 15:23:42 +0000 (18:23 +0300)]
Fixed log action when using SSL certificates with variables.

When variables are used in ssl_certificate or ssl_certificate_key, a request
is created in the certificate callback to evaluate the variables, and then
freed.  Freeing it, however, updates c->log->action to "closing request",
resulting in confusing error messages like "client timed out ... while
closing request" when a client times out during the SSL handshake.

Fix is to restore c->log->action after calling ngx_http_free_request().

4 years agoStream: the "fastopen" parameter of the "listen" directive.
Ruslan Ermilov [Thu, 20 May 2021 16:59:16 +0000 (19:59 +0300)]
Stream: the "fastopen" parameter of the "listen" directive.

Based on a patch by Anbang Wen.

4 years agoCore: fixed comment about msie_refresh escaping.
Ruslan Ermilov [Wed, 19 May 2021 13:24:13 +0000 (16:24 +0300)]
Core: fixed comment about msie_refresh escaping.

After 12a656452ad1, the "%" character is no longer escaped by
ngx_escape_uri(NGX_ESCAPE_REFRESH).

4 years agoMail: max_errors directive.
Maxim Dounin [Wed, 19 May 2021 00:13:31 +0000 (03:13 +0300)]
Mail: max_errors directive.

Similarly to smtpd_hard_error_limit in Postfix and smtp_max_unknown_commands
in Exim, specifies the number of errors after which the connection is closed.

4 years agoMail: IMAP pipelining support.
Maxim Dounin [Wed, 19 May 2021 00:13:28 +0000 (03:13 +0300)]
Mail: IMAP pipelining support.

The change is mostly the same as the SMTP one (04e43d03e153 and 3f5d0af4e40a),
and ensures that nginx is able to properly handle or reject multiple IMAP
commands.  The s->cmd field is not really used and set for consistency.

Non-synchronizing literals handling in invalid/unknown commands is limited,
so when a non-synchronizing literal is detected at the end of a discarded
line, the connection is closed.

4 years agoMail: stricter checking of IMAP tags.
Maxim Dounin [Wed, 19 May 2021 00:13:26 +0000 (03:13 +0300)]
Mail: stricter checking of IMAP tags.

Only "A-Za-z0-9-._" characters now allowed (which is stricter than what
RFC 3501 requires, but expected to be enough for all known clients),
and tags shouldn't be longer than 32 characters.

4 years agoMail: fixed backslash handling in IMAP literals.
Maxim Dounin [Wed, 19 May 2021 00:13:23 +0000 (03:13 +0300)]
Mail: fixed backslash handling in IMAP literals.

Previously, s->backslash was set if any of the arguments was a quoted
string with a backslash character.  After successful command parsing
this resulted in all arguments being filtered to remove backslashes.
This is, however, incorrect, as backslashes should not be removed from
IMAP literals.  For example:

   S: * OK IMAP4 ready
   C: a01 login {9}
   S: + OK
   C: user\name "pass\"word"
   S: * BAD internal server error

resulted in "Auth-User: username" instead of "Auth-User: user\name"
as it should.

Fix is to apply backslash filtering on per-argument basis during parsing.

4 years agoMail: removed dead s->arg_start handling.
Maxim Dounin [Wed, 19 May 2021 00:13:22 +0000 (03:13 +0300)]
Mail: removed dead s->arg_start handling.

As discussed in the previous change, s->arg_start handling in the "done"
labels of ngx_mail_pop3_parse_command(), ngx_mail_imap_parse_command(),
and ngx_mail_smtp_parse_command() is wrong: s->arg_start cannot be
set there, as it is handled and cleared on all code paths where the
"done" labels are reached.  The relevant code is dead and now removed.

4 years agoMail: fixed s->arg_start clearing on invalid IMAP commands.
Maxim Dounin [Wed, 19 May 2021 00:13:20 +0000 (03:13 +0300)]
Mail: fixed s->arg_start clearing on invalid IMAP commands.

Previously, s->arg_start was left intact after invalid IMAP commands,
and this might result in an argument incorrectly added to the following
command.  Similarly, s->backslash was left intact as well, leading
to unneeded backslash removal.

For example (LFs from the client are explicitly shown as "<LF>"):

  S: * OK IMAP4 ready
  C: a01 login "\<LF>
  S: a01 BAD invalid command
  C: a0000000000\2 authenticate <LF>
  S: a00000000002 aBAD invalid command

The backslash followed by LF generates invalid command with s->arg_start
and s->backslash set, the following command incorrectly treats anything
from the old s->arg_start to the space after the command as an argument,
and removes the backslash from the tag.  If there is no space, s->arg_end
will be NULL.

Both things seem to be harmless though.  In particular:

- This can be used to provide an incorrect argument to a command without
  arguments.  The only command which seems to look at the single argument
  is AUTHENTICATE, and it checks the argument length before trying to
  access it.

- Backslash removal uses the "end" pointer, and stops due to "src < end"
  condition instead of scanning all the process memory if s->arg_end is
  NULL (and arg[0].len is huge).

- There should be no backslashes in unquoted strings.

An obvious fix is to clear s->arg_start and s->backslash on invalid commands,
similarly to how it is done in POP3 parsing (added in 810:e3aa8f305d21) and
SMTP parsing.

This, however, makes it clear that s->arg_start handling in the "done"
label is wrong: s->arg_start cannot be legitimately set there, as it
is expected to be cleared in all possible cases when the "done" label is
reached.  The relevant code is dead and will be removed by the following
change.

4 years agoMail: POP3 pipelining support.
Maxim Dounin [Wed, 19 May 2021 00:13:18 +0000 (03:13 +0300)]
Mail: POP3 pipelining support.

The change is mostly the same as the SMTP one (04e43d03e153 and 3f5d0af4e40a),
and ensures that nginx is able to properly handle or reject multiple POP3
commands, as required by the PIPELINING capability (RFC 2449).  The s->cmd
field is not really used and set for consistency.

4 years agoMail: optimized discarding invalid SMTP commands.
Maxim Dounin [Wed, 19 May 2021 00:13:17 +0000 (03:13 +0300)]
Mail: optimized discarding invalid SMTP commands.

There is no need to scan buffer from s->buffer->pos, as we already scanned
the buffer till "p" and wasn't able to find an LF.

There is no real need for this change in SMTP, since it is at most a
microoptimization of a non-common code path.  Similar code in IMAP, however,
will have to start scanning from "p" to be correct, since there can be
newlines in IMAP literals.

4 years agoMail: fixed handling of invalid SMTP commands split between reads.
Maxim Dounin [Wed, 19 May 2021 00:13:15 +0000 (03:13 +0300)]
Mail: fixed handling of invalid SMTP commands split between reads.

Previously, if an invalid SMTP command was split between reads, nginx failed
to wait for LF before returning an error, and interpreted the rest of the
command received later as a separate command.

The sw_invalid state in ngx_mail_smtp_parse_command(), introduced in
04e43d03e153, did not work, since ngx_mail_smtp_auth_state() clears
s->state when returning an error due to NGX_MAIL_PARSE_INVALID_COMMAND.
And not clearing s->state will introduce another problem: the rest
of the command would trigger duplicate error when rest of the command is
received.

Fix is to return NGX_AGAIN from ngx_mail_smtp_parse_command() until full
command is received.

4 years agoMail: fixed SMTP pipelining to send the response immediately.
Maxim Dounin [Wed, 19 May 2021 00:13:12 +0000 (03:13 +0300)]
Mail: fixed SMTP pipelining to send the response immediately.

Previously, if there were some pipelined SMTP data in the buffer when
a proxied connection with the backend was established, nginx called
ngx_mail_proxy_handler() to send these data, and not tried to send the
response to the last command.  In most cases, this response was later sent
along with the response to the pipelined command, but if for some reason
client decides to wait for the response before finishing the next command
this might result in a connection hang.

Fix is to always call ngx_mail_proxy_handler() to send the response, and
additionally post an event to send the pipelined data if needed.

4 years agoMIME: added application/wasm type (ticket #1606).
Maxim Dounin [Sat, 8 May 2021 17:31:03 +0000 (20:31 +0300)]
MIME: added application/wasm type (ticket #1606).

4 years agoUpstream: variables support in certificates.
Maxim Dounin [Wed, 5 May 2021 23:22:09 +0000 (02:22 +0300)]
Upstream: variables support in certificates.

4 years agoAuth basic: changed alcf->user_file to be a pointer.
Maxim Dounin [Wed, 5 May 2021 23:22:07 +0000 (02:22 +0300)]
Auth basic: changed alcf->user_file to be a pointer.

This saves some memory in typical case when auth_basic_user_file is not
explicitly set, and unifies the code with alcf->realm.

4 years agoChanged complex value slots to use NGX_CONF_UNSET_PTR.
Maxim Dounin [Wed, 5 May 2021 23:22:03 +0000 (02:22 +0300)]
Changed complex value slots to use NGX_CONF_UNSET_PTR.

With this change, it is now possible to use ngx_conf_merge_ptr_value()
to merge complex values.  This change follows much earlier changes in
ngx_conf_merge_ptr_value() and ngx_conf_set_str_array_slot()
in 1452:cd586e963db0 (0.6.10) and 1701:40d004d95d88 (0.6.22), and the
change in ngx_conf_set_keyval_slot() (7728:485dba3e2a01, 1.19.4).

To preserve compatibility with existing 3rd party modules, both NULL
and NGX_CONF_UNSET_PTR are accepted for now.

4 years agoRestored zeroing of ngx_channel_t in ngx_pass_open_channel().
Ruslan Ermilov [Thu, 22 Apr 2021 13:12:52 +0000 (16:12 +0300)]
Restored zeroing of ngx_channel_t in ngx_pass_open_channel().

Due to structure's alignment, some uninitialized memory contents may have
been passed between processes.

Zeroing was removed in 0215ec9aaa8a.

Reported by Johnny Wang.

4 years agoMail: fixed reading with fully filled buffer (ticket #2159).
Maxim Dounin [Wed, 21 Apr 2021 20:24:59 +0000 (23:24 +0300)]
Mail: fixed reading with fully filled buffer (ticket #2159).

With SMTP pipelining, ngx_mail_read_command() can be called with s->buffer
without any space available, to parse additional commands received to the
buffer on previous calls.  Previously, this resulted in recv() being called
with zero length, resulting in zero being returned, which was interpreted
as a connection close by the client, so nginx silently closed connection.

Fix is to avoid calling c->recv() if there is no free space in the buffer,
but continue parsing of the already received commands.