Skip to content

Commit

Permalink
Fixed issue #1819: Allow a list of headers in 'xdebug.client_discover…
Browse files Browse the repository at this point in the history
…y_header'
  • Loading branch information
derickr committed Jun 7, 2022
1 parent 8a6e116 commit e553f4d
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 30 deletions.
41 changes: 22 additions & 19 deletions src/debugger/com.c
Original file line number Diff line number Diff line change
Expand Up @@ -418,32 +418,33 @@ void xdebug_close_socket(int socketfd)
SCLOSE(socketfd);
}

static zval *get_client_discovery_address(const char **header)
static zval *get_client_discovery_address(char **header)
{
zval *remote_addr = NULL;
xdebug_arg *headers;
int i;
zval *remote_addr = NULL;

xdebug_log(XLOG_CHAN_DEBUG, XLOG_INFO, "Checking remote connect back address.");
xdebug_log(XLOG_CHAN_DEBUG, XLOG_INFO, "Checking for client discovery headers: '%s'.", XINI_DBG(client_discovery_header));

if (XINI_DBG(client_discovery_header) && XINI_DBG(client_discovery_header)[0]) {
*header = XINI_DBG(client_discovery_header);
xdebug_log(XLOG_CHAN_DEBUG, XLOG_INFO, "Checking user configured header '%s'.", XINI_DBG(client_discovery_header));
headers = xdebug_arg_ctor();
xdebug_explode(",", XINI_DBG(client_discovery_header), headers, -1);

remote_addr = zend_hash_str_find(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]), XINI_DBG(client_discovery_header), HASH_KEY_STRLEN(XINI_DBG(client_discovery_header)));
}
if (!remote_addr) {
*header = "HTTP_X_FORWARDED_FOR";
xdebug_log(XLOG_CHAN_DEBUG, XLOG_INFO, "Checking header 'HTTP_X_FORWARDED_FOR'.");
for (i = 0; i < headers->c; ++i) {
char *header_name = xdebug_trim(headers->args[i]);

remote_addr = zend_hash_str_find(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]), "HTTP_X_FORWARDED_FOR", HASH_KEY_SIZEOF("HTTP_X_FORWARDED_FOR"));
}
if (!remote_addr) {
*header = "REMOTE_ADDR";
xdebug_log(XLOG_CHAN_DEBUG, XLOG_INFO, "Checking header 'REMOTE_ADDR'.");
xdebug_log(XLOG_CHAN_DEBUG, XLOG_INFO, "Checking header '%s'.", header_name);
remote_addr = zend_hash_str_find(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]), header_name, HASH_KEY_STRLEN(header_name));

if (remote_addr) {
*header = header_name;
xdebug_arg_dtor(headers);
return remote_addr;
}

remote_addr = zend_hash_str_find(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]), "REMOTE_ADDR", HASH_KEY_SIZEOF("REMOTE_ADDR"));
xdfree(header_name);
}

return remote_addr;
return NULL;
}

/* Starting the debugger */
Expand All @@ -452,7 +453,7 @@ static void xdebug_init_normal_debugger(xdebug_str *connection_attempts)
zval *remote_addr = NULL;
char *cp = NULL;
int cp_found = 0;
const char *header = NULL;
char *header = NULL;

if (!XINI_DBG(discover_client_host)) {
char *pseudo_hostname = resolve_pseudo_hosts(XINI_DBG(client_host));
Expand All @@ -473,6 +474,7 @@ static void xdebug_init_normal_debugger(xdebug_str *connection_attempts)
return;
}

/* Discover client host section */
remote_addr = get_client_discovery_address(&header);

if (remote_addr && strstr(Z_STRVAL_P(remote_addr), "://")) {
Expand All @@ -499,6 +501,7 @@ static void xdebug_init_normal_debugger(xdebug_str *connection_attempts)

xdebug_str_add_fmt(connection_attempts, "%s:%ld (from %s HTTP header)", Z_STRVAL_P(remote_addr), XINI_DBG(client_port), header);
xdebug_log(XLOG_CHAN_DEBUG, XLOG_INFO, "Client host discovered through HTTP header, connecting to %s:%ld.", Z_STRVAL_P(remote_addr), (long int) XINI_DBG(client_port));
xdfree(header);

XG_DBG(context).socket = xdebug_create_socket(Z_STRVAL_P(remote_addr), XINI_DBG(client_port), XINI_DBG(connect_timeout_ms));

Expand Down
4 changes: 2 additions & 2 deletions tests/debugger/remote_log-unix-2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ unlink (sys_get_temp_dir() . '/' . getenv('UNIQ_RUN_ID') . 'remote-unix.txt' );
--EXPECTF--
3
[%d] Log opened at %d-%d-%d %d:%d:%d.%d
[%d] [Step Debug] INFO: Checking remote connect back address.
[%d] [Step Debug] INFO: Checking user configured header 'I_LIKE_COOKIES'.
[%d] [Step Debug] INFO: Checking for client discovery headers: 'I_LIKE_COOKIES'.
[%d] [Step Debug] INFO: Checking header 'I_LIKE_COOKIES'.
[%d] [Step Debug] WARN: Invalid remote address provided containing URI spec 'unix:///tmp/haxx0r.sock'.
[%d] [Step Debug] WARN: Could not discover client host through HTTP headers, connecting to configured address/port: unix:///tmp/xdbg.sock:0.
[%d] [Step Debug] WARN: Creating socket for 'unix:///tmp/xdbg.sock', connect: No such file or directory.
Expand Down
4 changes: 2 additions & 2 deletions tests/debugger/remote_log-win.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ echo file_get_contents("C:\\Windows\\Temp\\remote-log4.txt");
--EXPECTF--
3
[%d] Log opened at %d-%d-%d %d:%d:%d.%d
[%d] [Step Debug] INFO: Checking remote connect back address.
[%d] [Step Debug] INFO: Checking user configured header 'I_LIKE_COOKIES'.
[%d] [Step Debug] INFO: Checking for client discovery headers: 'I_LIKE_COOKIES'.
[%d] [Step Debug] INFO: Checking header 'I_LIKE_COOKIES'.
[%d] [Step Debug] INFO: Client host discovered through HTTP header, connecting to doesnotexist3:9003.
[%d] [Step Debug] WARN: Creating socket for 'doesnotexist3:9003', getaddrinfo: %d.
[%d] [Step Debug] WARN: Could not connect to client host discovered through HTTP headers, connecting to configured address/port: doesnotexist2:9003.
Expand Down
2 changes: 1 addition & 1 deletion tests/debugger/remote_log2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ unlink (sys_get_temp_dir() . '/' . getenv('UNIQ_RUN_ID') . 'remote-log2.txt' );
--EXPECTF--
3
[%d] Log opened at %d-%d-%d %d:%d:%d.%d
[%d] [Step Debug] INFO: Checking remote connect back address.
[%d] [Step Debug] INFO: Checking for client discovery headers: 'HTTP_X_FORWARDED_FOR,REMOTE_ADDR'.
[%d] [Step Debug] INFO: Checking header 'HTTP_X_FORWARDED_FOR'.
[%d] [Step Debug] INFO: Checking header 'REMOTE_ADDR'.
[%d] [Step Debug] WARN: Could not discover client host through HTTP headers, connecting to configured address/port: doesnotexist2:9003.
Expand Down
6 changes: 3 additions & 3 deletions tests/debugger/remote_log3.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ xdebug.log={TMPDIR}/{RUNID}remote-log3.txt
xdebug.discover_client_host=1
xdebug.client_host=doesnotexist2
xdebug.client_port=9003
xdebug.client_discovery_header=I_LIKE_COOKIES
xdebug.client_discovery_header=I_LIKE_COOKIES,HTTP_X_FORWARDED_FOR,REMOTE_ADDR
--FILE--
<?php
echo strlen("foo"), "\n";
Expand All @@ -22,8 +22,8 @@ unlink (sys_get_temp_dir() . '/' . getenv('UNIQ_RUN_ID') . 'remote-log3.txt' );
--EXPECTF--
3
[%d] Log opened at %d-%d-%d %d:%d:%d.%d
[%d] [Step Debug] INFO: Checking remote connect back address.
[%d] [Step Debug] INFO: Checking user configured header 'I_LIKE_COOKIES'.
[%d] [Step Debug] INFO: Checking for client discovery headers: 'I_LIKE_COOKIES,HTTP_X_FORWARDED_FOR,REMOTE_ADDR'.
[%d] [Step Debug] INFO: Checking header 'I_LIKE_COOKIES'.
[%d] [Step Debug] INFO: Checking header 'HTTP_X_FORWARDED_FOR'.
[%d] [Step Debug] INFO: Checking header 'REMOTE_ADDR'.
[%d] [Step Debug] WARN: Could not discover client host through HTTP headers, connecting to configured address/port: doesnotexist2:9003.
Expand Down
4 changes: 2 additions & 2 deletions tests/debugger/remote_log4.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ unlink (sys_get_temp_dir() . '/' . getenv('UNIQ_RUN_ID') . 'remote-log4.txt' );
--EXPECTF--
3
[%d] Log opened at %d-%d-%d %d:%d:%d.%d
[%d] [Step Debug] INFO: Checking remote connect back address.
[%d] [Step Debug] INFO: Checking user configured header 'I_LIKE_COOKIES'.
[%d] [Step Debug] INFO: Checking for client discovery headers: 'I_LIKE_COOKIES'.
[%d] [Step Debug] INFO: Checking header 'I_LIKE_COOKIES'.
[%d] [Step Debug] INFO: Client host discovered through HTTP header, connecting to cookiehost:9003.
[%d] [Step Debug] WARN: Creating socket for 'cookiehost:9003', getaddrinfo: %s.
[%d] [Step Debug] WARN: Could not connect to client host discovered through HTTP headers, connecting to configured address/port: doesnotexist2:9003.
Expand Down
2 changes: 1 addition & 1 deletion xdebug.c
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("xdebug.client_host", "localhost", PHP_INI_ALL, OnUpdateString, settings.debugger.client_host, zend_xdebug_globals, xdebug_globals)
STD_PHP_INI_ENTRY("xdebug.client_port", "9003", PHP_INI_ALL, OnUpdateLong, settings.debugger.client_port, zend_xdebug_globals, xdebug_globals)
STD_PHP_INI_BOOLEAN("xdebug.discover_client_host", "0", PHP_INI_ALL, OnUpdateBool, settings.debugger.discover_client_host, zend_xdebug_globals, xdebug_globals)
STD_PHP_INI_ENTRY("xdebug.client_discovery_header", "", PHP_INI_ALL, OnUpdateString, settings.debugger.client_discovery_header, zend_xdebug_globals, xdebug_globals)
STD_PHP_INI_ENTRY("xdebug.client_discovery_header", "HTTP_X_FORWARDED_FOR,REMOTE_ADDR", PHP_INI_ALL, OnUpdateString, settings.debugger.client_discovery_header, zend_xdebug_globals, xdebug_globals)
STD_PHP_INI_ENTRY("xdebug.idekey", "", PHP_INI_ALL, OnUpdateString, settings.debugger.ide_key_setting, zend_xdebug_globals, xdebug_globals)
STD_PHP_INI_ENTRY("xdebug.connect_timeout_ms", "200", PHP_INI_ALL, OnUpdateLong, settings.debugger.connect_timeout_ms, zend_xdebug_globals, xdebug_globals)

Expand Down

0 comments on commit e553f4d

Please sign in to comment.