@@ -1746,7 +1746,6 @@ int main(int argc, char *argv[])
17461746 int status = 0 ;
17471747#endif
17481748 char * query_string ;
1749- char * decoded_query_string ;
17501749 int skip_getopt = 0 ;
17511750
17521751#if defined(SIGPIPE ) && defined(SIG_IGN )
@@ -1801,10 +1800,15 @@ int main(int argc, char *argv[])
18011800 * the executable. Ideally we skip argument parsing when we're in cgi or fastcgi mode,
18021801 * but that breaks PHP scripts on Linux with a hashbang: `#!/php-cgi -d option=value`.
18031802 * Therefore, this code only prevents passing arguments if the query string starts with a '-'.
1804- * Similarly, scripts spawned in subprocesses on Windows may have the same issue. */
1803+ * Similarly, scripts spawned in subprocesses on Windows may have the same issue.
1804+ * However, Windows has lots of conversion rules and command line parsing rules that
1805+ * are too difficult and dangerous to reliably emulate. */
18051806 if ((query_string = getenv ("QUERY_STRING" )) != NULL && strchr (query_string , '=' ) == NULL ) {
1807+ #ifdef PHP_WIN32
1808+ skip_getopt = cgi || fastcgi ;
1809+ #else
18061810 unsigned char * p ;
1807- decoded_query_string = strdup (query_string );
1811+ char * decoded_query_string = strdup (query_string );
18081812 php_url_decode (decoded_query_string , strlen (decoded_query_string ));
18091813 for (p = (unsigned char * )decoded_query_string ; * p && * p <= ' ' ; p ++ ) {
18101814 /* skip all leading spaces */
@@ -1813,22 +1817,8 @@ int main(int argc, char *argv[])
18131817 skip_getopt = 1 ;
18141818 }
18151819
1816- /* On Windows we have to take into account the "best fit" mapping behaviour. */
1817- #ifdef PHP_WIN32
1818- if (* p >= 0x80 ) {
1819- wchar_t wide_buf [1 ];
1820- wide_buf [0 ] = * p ;
1821- char char_buf [4 ];
1822- size_t wide_buf_len = sizeof (wide_buf ) / sizeof (wide_buf [0 ]);
1823- size_t char_buf_len = sizeof (char_buf ) / sizeof (char_buf [0 ]);
1824- if (WideCharToMultiByte (CP_ACP , 0 , wide_buf , wide_buf_len , char_buf , char_buf_len , NULL , NULL ) == 0
1825- || char_buf [0 ] == '-' ) {
1826- skip_getopt = 1 ;
1827- }
1828- }
1829- #endif
1830-
18311820 free (decoded_query_string );
1821+ #endif
18321822 }
18331823
18341824 php_ini_builder_init (& ini_builder );
@@ -1895,18 +1885,17 @@ int main(int argc, char *argv[])
18951885
18961886 /* check force_cgi after startup, so we have proper output */
18971887 if (cgi && CGIG (force_redirect )) {
1898- /* Apache will generate REDIRECT_STATUS,
1899- * Netscape and redirect.so will generate HTTP_REDIRECT_STATUS.
1900- * redirect.so and installation instructions available from
1901- * http://www.koehntopp.de/php.
1902- 1903- */
1904- if (!getenv ("REDIRECT_STATUS" ) &&
1905- !getenv ("HTTP_REDIRECT_STATUS" ) &&
1906- /* this is to allow a different env var to be configured
1907- * in case some server does something different than above */
1908- (!CGIG (redirect_status_env ) || !getenv (CGIG (redirect_status_env )))
1909- ) {
1888+ /* This is to allow a different environment variable to be configured
1889+ * in case the we cannot auto-detect which environment variable to use.
1890+ * Checking this first to allow user overrides in case the environment
1891+ * variable can be set by an untrusted party. */
1892+ const char * redirect_status_env = CGIG (redirect_status_env );
1893+ if (!redirect_status_env ) {
1894+ /* Apache will generate REDIRECT_STATUS. */
1895+ redirect_status_env = "REDIRECT_STATUS" ;
1896+ }
1897+
1898+ if (!getenv (redirect_status_env )) {
19101899 zend_try {
19111900 SG (sapi_headers ).http_response_code = 400 ;
19121901 PUTS ("<b>Security Alert!</b> The PHP CGI cannot be accessed directly.\n\n\
0 commit comments