|
| 1 | + |
| 2 | +# HG changeset patch |
| 3 | +# User Maxim Dounin <[email protected]> |
| 4 | +# Date 1696940019 -10800 |
| 5 | +# Node ID cdda286c0f1b4b10f30d4eb6a63fefb9b8708ecc |
| 6 | +# Parent 3db945fda515014d220151046d02f3960bcfca0a |
| 7 | +HTTP/2: per-iteration stream handling limit. |
| 8 | + |
| 9 | +To ensure that attempts to flood servers with many streams are detected |
| 10 | +early, a limit of no more than 2 * max_concurrent_streams new streams per one |
| 11 | +event loop iteration was introduced. This limit is applied even if |
| 12 | +max_concurrent_streams is not yet reached - for example, if corresponding |
| 13 | +streams are handled synchronously or reset. |
| 14 | + |
| 15 | +Further, refused streams are now limited to maximum of max_concurrent_streams |
| 16 | +and 100, similarly to priority_limit initial value, providing some tolerance |
| 17 | +to clients trying to open several streams at the connection start, yet |
| 18 | +low tolerance to flooding attempts. |
| 19 | + |
| 20 | +diff -r 3db945fda515 -r cdda286c0f1b src/http/v2/ngx_http_v2.c |
| 21 | +--- a/src/http/v2/ngx_http_v2.c Fri Sep 22 19:23:57 2023 +0400 |
| 22 | ++++ b/src/http/v2/ngx_http_v2.c Tue Oct 10 15:13:39 2023 +0300 |
| 23 | +@@ -347,6 +347,7 @@ |
| 24 | + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http2 read handler"); |
| 25 | + |
| 26 | + h2c->blocked = 1; |
| 27 | ++ h2c->new_streams = 0; |
| 28 | + |
| 29 | + if (c->close) { |
| 30 | + c->close = 0; |
| 31 | +@@ -1284,6 +1285,14 @@ |
| 32 | + goto rst_stream; |
| 33 | + } |
| 34 | + |
| 35 | ++ if (h2c->new_streams++ >= 2 * h2scf->concurrent_streams) { |
| 36 | ++ ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0, |
| 37 | ++ "client sent too many streams at once"); |
| 38 | ++ |
| 39 | ++ status = NGX_HTTP_V2_REFUSED_STREAM; |
| 40 | ++ goto rst_stream; |
| 41 | ++ } |
| 42 | ++ |
| 43 | + if (!h2c->settings_ack |
| 44 | + && !(h2c->state.flags & NGX_HTTP_V2_END_STREAM_FLAG) |
| 45 | + && h2scf->preread_size < NGX_HTTP_V2_DEFAULT_WINDOW) |
| 46 | +@@ -1349,6 +1358,12 @@ |
| 47 | + |
| 48 | + rst_stream: |
| 49 | + |
| 50 | ++ if (h2c->refused_streams++ > ngx_max(h2scf->concurrent_streams, 100)) { |
| 51 | ++ ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0, |
| 52 | ++ "client sent too many refused streams"); |
| 53 | ++ return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_NO_ERROR); |
| 54 | ++ } |
| 55 | ++ |
| 56 | + if (ngx_http_v2_send_rst_stream(h2c, h2c->state.sid, status) != NGX_OK) { |
| 57 | + return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR); |
| 58 | + } |
| 59 | +diff -r 3db945fda515 -r cdda286c0f1b src/http/v2/ngx_http_v2.h |
| 60 | +--- a/src/http/v2/ngx_http_v2.h Fri Sep 22 19:23:57 2023 +0400 |
| 61 | ++++ b/src/http/v2/ngx_http_v2.h Tue Oct 10 15:13:39 2023 +0300 |
| 62 | +@@ -131,6 +131,8 @@ |
| 63 | + ngx_uint_t processing; |
| 64 | + ngx_uint_t frames; |
| 65 | + ngx_uint_t idle; |
| 66 | ++ ngx_uint_t new_streams; |
| 67 | ++ ngx_uint_t refused_streams; |
| 68 | + ngx_uint_t priority_limit; |
| 69 | + |
| 70 | + size_t send_window; |
| 71 | + |
0 commit comments