From f8a4e9204f9d6cd501fd8718242911bf508ba446 Mon Sep 17 00:00:00 2001 From: Jakub Zelenka Date: Sun, 12 Mar 2023 19:30:16 +0000 Subject: [PATCH] Fix bug #74129: Incorrect SCRIPT_NAME with apache ProxyPassMatch when spaces are in path --- sapi/fpm/fpm/fpm_main.c | 24 ++++++++- ...gi-env-pif-apache-pp-sn-strip-encoded.phpt | 54 +++++++++++++++++++ 2 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 sapi/fpm/tests/fcgi-env-pif-apache-pp-sn-strip-encoded.phpt diff --git a/sapi/fpm/fpm/fpm_main.c b/sapi/fpm/fpm/fpm_main.c index 7cb0a0a33b814..fb3d0e8117c83 100644 --- a/sapi/fpm/fpm/fpm_main.c +++ b/sapi/fpm/fpm/fpm_main.c @@ -1157,12 +1157,32 @@ static void init_request_info(void) * As we can extract PATH_INFO from PATH_TRANSLATED * it is probably also in SCRIPT_NAME and need to be removed */ + char *decoded_path_info = NULL; + size_t decoded_path_info_len = 0; + if (strchr(path_info, '%')) { + decoded_path_info = estrdup(path_info); + decoded_path_info_len = php_url_decode(decoded_path_info, strlen(path_info)); + } int snlen = strlen(env_script_name); - if (snlen>slen && !strcmp(env_script_name+snlen-slen, path_info)) { + int env_script_file_info_start = 0; + if ( + ( + snlen > slen && + !strcmp(env_script_name + (env_script_file_info_start = snlen - slen), path_info) + ) || + ( + decoded_path_info && + snlen > decoded_path_info_len && + !strcmp(env_script_name + (env_script_file_info_start = snlen - decoded_path_info_len), decoded_path_info) + ) + ) { FCGI_PUTENV(request, "ORIG_SCRIPT_NAME", orig_script_name); - env_script_name[snlen-slen] = 0; + env_script_name[env_script_file_info_start] = 0; SG(request_info).request_uri = FCGI_PUTENV(request, "SCRIPT_NAME", env_script_name); } + if (decoded_path_info) { + efree(decoded_path_info); + } } env_path_info = FCGI_PUTENV(request, "PATH_INFO", path_info); } diff --git a/sapi/fpm/tests/fcgi-env-pif-apache-pp-sn-strip-encoded.phpt b/sapi/fpm/tests/fcgi-env-pif-apache-pp-sn-strip-encoded.phpt new file mode 100644 index 0000000000000..22114e1abde47 --- /dev/null +++ b/sapi/fpm/tests/fcgi-env-pif-apache-pp-sn-strip-encoded.phpt @@ -0,0 +1,54 @@ +--TEST-- +FPM: FastCGI env var path info fix for Apache ProxyPass SCRIPT_NAME stripping with encoded path (bug #74129) +--SKIPIF-- + +--FILE-- +createSourceFileAndScriptName(); +$tester->start(); +$tester->expectLogStartNotices(); +$tester + ->request( + uri: $scriptName . '/1%202', + scriptFilename: "proxy:fcgi://" . $tester->getAddr() . $sourceFilePath . '/1%202', + scriptName: $scriptName . '/1 2' + ) + ->expectBody([$scriptName, $scriptName . '/1 2', $sourceFilePath, '/1%202', $scriptName . '/1%202']); +$tester->terminate(); +$tester->close(); + +?> +Done +--EXPECT-- +Done +--CLEAN-- +