diff --git a/NEWS b/NEWS index c40618e166e..fcbdcc9c24e 100644 --- a/NEWS +++ b/NEWS @@ -6,6 +6,9 @@ PHP NEWS . Fixed bug #80092 (ZTS + preload = segfault on shutdown). (nielsdos) . Fixed buffer underflow when compiling memoized expression. (ilutov) +- CLI: + . Ensure a single Date header is present. (coppolafab) + - CType: . Fixed bug GH-11997 (ctype_alnum 5 times slower in PHP 8.1 or greater). (nielsdos) diff --git a/sapi/cli/php_cli_server.c b/sapi/cli/php_cli_server.c index 202fa8ea7ed..ab181f5b03a 100644 --- a/sapi/cli/php_cli_server.c +++ b/sapi/cli/php_cli_server.c @@ -356,10 +356,25 @@ static void append_http_status_line(smart_str *buffer, int protocol_version, int smart_str_appendl_ex(buffer, "\r\n", 2, persistent); } /* }}} */ -static void append_essential_headers(smart_str* buffer, php_cli_server_client *client, bool persistent) /* {{{ */ +static void append_essential_headers(smart_str* buffer, php_cli_server_client *client, bool persistent, sapi_headers_struct *sapi_headers) /* {{{ */ { zval *val; struct timeval tv = {0}; + bool append_date_header = true; + + if (sapi_headers != NULL) { + zend_llist_position pos; + sapi_header_struct *h = (sapi_header_struct*)zend_llist_get_first_ex(&sapi_headers->headers, &pos); + while (h) { + if (h->header_len > strlen("Date:")) { + if (strncasecmp(h->header, "Date:", strlen("Date:")) == 0) { + append_date_header = false; + break; + } + } + h = (sapi_header_struct*)zend_llist_get_next_ex(&sapi_headers->headers, &pos); + } + } if (NULL != (val = zend_hash_find(&client->request.headers, ZSTR_KNOWN(ZEND_STR_HOST)))) { smart_str_appends_ex(buffer, "Host: ", persistent); @@ -367,7 +382,7 @@ static void append_essential_headers(smart_str* buffer, php_cli_server_client *c smart_str_appends_ex(buffer, "\r\n", persistent); } - if (!gettimeofday(&tv, NULL)) { + if (append_date_header && !gettimeofday(&tv, NULL)) { zend_string *dt = php_format_date("D, d M Y H:i:s", sizeof("D, d M Y H:i:s") - 1, tv.tv_sec, 0); smart_str_appends_ex(buffer, "Date: ", persistent); smart_str_append_ex(buffer, dt, persistent); @@ -551,7 +566,7 @@ static int sapi_cli_server_send_headers(sapi_headers_struct *sapi_headers) /* {{ append_http_status_line(&buffer, client->request.protocol_version, SG(sapi_headers).http_response_code, 0); } - append_essential_headers(&buffer, client, 0); + append_essential_headers(&buffer, client, 0, sapi_headers); h = (sapi_header_struct*)zend_llist_get_first_ex(&sapi_headers->headers, &pos); while (h) { @@ -2049,7 +2064,7 @@ static zend_result php_cli_server_send_error_page(php_cli_server *server, php_cl /* out of memory */ goto fail; } - append_essential_headers(&buffer, client, 1); + append_essential_headers(&buffer, client, 1, NULL); smart_str_appends_ex(&buffer, "Content-Type: text/html; charset=UTF-8\r\n", 1); smart_str_appends_ex(&buffer, "Content-Length: ", 1); smart_str_append_unsigned_ex(&buffer, php_cli_server_buffer_size(&client->content_sender.buffer), 1); @@ -2165,7 +2180,7 @@ static zend_result php_cli_server_begin_send_static(php_cli_server *server, php_ php_cli_server_log_response(client, 500, NULL); return FAILURE; } - append_essential_headers(&buffer, client, 1); + append_essential_headers(&buffer, client, 1, NULL); if (mime_type) { smart_str_appendl_ex(&buffer, "Content-Type: ", sizeof("Content-Type: ") - 1, 1); smart_str_appends_ex(&buffer, mime_type, 1); diff --git a/sapi/cli/tests/gh12363.phpt b/sapi/cli/tests/gh12363.phpt new file mode 100644 index 00000000000..5093030642b --- /dev/null +++ b/sapi/cli/tests/gh12363.phpt @@ -0,0 +1,39 @@ +--TEST-- +Ensure a single Date header is present +--SKIPIF-- + +--FILE-- + +--EXPECTF-- +HTTP/1.1 200 OK +Host: %s +Connection: close +X-Powered-By: %s +Date: Mon, 25 Mar 1985 00:20:00 GMT +Content-type: text/html; charset=UTF-8 +