comparison src/http/v2/ngx_http_v2.c @ 9165:cdda286c0f1b

HTTP/2: per-iteration stream handling limit. To ensure that attempts to flood servers with many streams are detected early, a limit of no more than 2 * max_concurrent_streams new streams per one event loop iteration was introduced. This limit is applied even if max_concurrent_streams is not yet reached - for example, if corresponding streams are handled synchronously or reset. Further, refused streams are now limited to maximum of max_concurrent_streams and 100, similarly to priority_limit initial value, providing some tolerance to clients trying to open several streams at the connection start, yet low tolerance to flooding attempts.
author Maxim Dounin <mdounin@mdounin.ru>
date Tue, 10 Oct 2023 15:13:39 +0300
parents 262c01782566
children ea1f29c2010c
comparison
equal deleted inserted replaced
9164:3db945fda515 9165:cdda286c0f1b
345 } 345 }
346 346
347 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http2 read handler"); 347 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http2 read handler");
348 348
349 h2c->blocked = 1; 349 h2c->blocked = 1;
350 h2c->new_streams = 0;
350 351
351 if (c->close) { 352 if (c->close) {
352 c->close = 0; 353 c->close = 0;
353 354
354 if (c->error) { 355 if (c->error) {
1282 1283
1283 status = NGX_HTTP_V2_REFUSED_STREAM; 1284 status = NGX_HTTP_V2_REFUSED_STREAM;
1284 goto rst_stream; 1285 goto rst_stream;
1285 } 1286 }
1286 1287
1288 if (h2c->new_streams++ >= 2 * h2scf->concurrent_streams) {
1289 ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
1290 "client sent too many streams at once");
1291
1292 status = NGX_HTTP_V2_REFUSED_STREAM;
1293 goto rst_stream;
1294 }
1295
1287 if (!h2c->settings_ack 1296 if (!h2c->settings_ack
1288 && !(h2c->state.flags & NGX_HTTP_V2_END_STREAM_FLAG) 1297 && !(h2c->state.flags & NGX_HTTP_V2_END_STREAM_FLAG)
1289 && h2scf->preread_size < NGX_HTTP_V2_DEFAULT_WINDOW) 1298 && h2scf->preread_size < NGX_HTTP_V2_DEFAULT_WINDOW)
1290 { 1299 {
1291 ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0, 1300 ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
1346 } 1355 }
1347 1356
1348 return ngx_http_v2_state_header_block(h2c, pos, end); 1357 return ngx_http_v2_state_header_block(h2c, pos, end);
1349 1358
1350 rst_stream: 1359 rst_stream:
1360
1361 if (h2c->refused_streams++ > ngx_max(h2scf->concurrent_streams, 100)) {
1362 ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
1363 "client sent too many refused streams");
1364 return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_NO_ERROR);
1365 }
1351 1366
1352 if (ngx_http_v2_send_rst_stream(h2c, h2c->state.sid, status) != NGX_OK) { 1367 if (ngx_http_v2_send_rst_stream(h2c, h2c->state.sid, status) != NGX_OK) {
1353 return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR); 1368 return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
1354 } 1369 }
1355 1370