From ee2077f969cd8c5eb76b515e2ad59bf7a3560780 Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Mon, 17 Jan 2022 17:54:01 +0100 Subject: [PATCH 1/3] Fix GH-7953: ob_clean() only does not set Content-Encoding If an output handler has not yet been started, calling `ob_clean()` causes it to start. If that happens, we must not forget to set the `Content-Encoding` and `Vary` headers. --- ext/zlib/tests/gh7953.phpt | 21 +++++++++++++++++++++ ext/zlib/zlib.c | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 ext/zlib/tests/gh7953.phpt diff --git a/ext/zlib/tests/gh7953.phpt b/ext/zlib/tests/gh7953.phpt new file mode 100644 index 0000000000000..020bf57fa6804 --- /dev/null +++ b/ext/zlib/tests/gh7953.phpt @@ -0,0 +1,21 @@ +--TEST-- +GH-7953 (ob_clean() only does not set Content-Encoding) +--SKIPIF-- + +--CGI-- +--ENV-- +HTTP_ACCEPT_ENCODING=gzip +--FILE-- + +--EXPECTF-- +%a +--EXPECTHEADERS-- +Content-Encoding: gzip +Vary: Accept-Encoding diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c index 75bd273526eb6..f7cf0d5dac76a 100644 --- a/ext/zlib/zlib.c +++ b/ext/zlib/zlib.c @@ -281,7 +281,7 @@ static int php_zlib_output_handler(void **handler_context, php_output_context *o return FAILURE; } - if (!(output_context->op & PHP_OUTPUT_HANDLER_CLEAN)) { + if (!(output_context->op & PHP_OUTPUT_HANDLER_CLEAN) || (output_context->op & PHP_OUTPUT_HANDLER_START)) { int flags; if (SUCCESS == php_output_handler_hook(PHP_OUTPUT_HANDLER_HOOK_GET_FLAGS, &flags)) { From c77380cd819c9fe4dfc1c7e045dcadb8c4458cd4 Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Thu, 20 Jan 2022 14:08:47 +0100 Subject: [PATCH 2/3] ob_iconv_handler() needs the same fix --- ext/iconv/iconv.c | 2 +- ext/iconv/tests/gh7953.phpt | 21 +++++++++++++++++++++ ext/zlib/tests/gh7953.phpt | 2 +- 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 ext/iconv/tests/gh7953.phpt diff --git a/ext/iconv/iconv.c b/ext/iconv/iconv.c index 90c209cbcbf00..edcedd437e8e1 100644 --- a/ext/iconv/iconv.c +++ b/ext/iconv/iconv.c @@ -311,7 +311,7 @@ static int php_iconv_output_handler(void **nothing, php_output_context *output_c mimetype = SG(default_mimetype) ? SG(default_mimetype) : SAPI_DEFAULT_MIMETYPE; } - if (mimetype != NULL && !(output_context->op & PHP_OUTPUT_HANDLER_CLEAN)) { + if (mimetype != NULL && !(output_context->op & PHP_OUTPUT_HANDLER_CLEAN) || (output_context->op & PHP_OUTPUT_HANDLER_START)) { size_t len; char *p = strstr(get_output_encoding(), "//"); diff --git a/ext/iconv/tests/gh7953.phpt b/ext/iconv/tests/gh7953.phpt new file mode 100644 index 0000000000000..bfc249fb9f21e --- /dev/null +++ b/ext/iconv/tests/gh7953.phpt @@ -0,0 +1,21 @@ +--TEST-- +GH-7953 (ob_clean() only may not set Content-* header) +--SKIPIF-- + +--INI-- +input_encoding=UTF-8 +output_encoding=ISO-8859-1 +--CGI-- +--FILE-- + +--EXPECTF-- +%a +--EXPECTHEADERS-- +Content-Type: text/html; charset=ISO-8859-1 diff --git a/ext/zlib/tests/gh7953.phpt b/ext/zlib/tests/gh7953.phpt index 020bf57fa6804..d5d9011c03281 100644 --- a/ext/zlib/tests/gh7953.phpt +++ b/ext/zlib/tests/gh7953.phpt @@ -1,5 +1,5 @@ --TEST-- -GH-7953 (ob_clean() only does not set Content-Encoding) +GH-7953 (ob_clean() only may not set Content-* header) --SKIPIF-- Date: Thu, 20 Jan 2022 16:55:57 +0100 Subject: [PATCH 3/3] fix precedence --- ext/iconv/iconv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/iconv/iconv.c b/ext/iconv/iconv.c index edcedd437e8e1..d0693c08f2062 100644 --- a/ext/iconv/iconv.c +++ b/ext/iconv/iconv.c @@ -311,7 +311,7 @@ static int php_iconv_output_handler(void **nothing, php_output_context *output_c mimetype = SG(default_mimetype) ? SG(default_mimetype) : SAPI_DEFAULT_MIMETYPE; } - if (mimetype != NULL && !(output_context->op & PHP_OUTPUT_HANDLER_CLEAN) || (output_context->op & PHP_OUTPUT_HANDLER_START)) { + if (mimetype != NULL && (!(output_context->op & PHP_OUTPUT_HANDLER_CLEAN) || (output_context->op & PHP_OUTPUT_HANDLER_START))) { size_t len; char *p = strstr(get_output_encoding(), "//");