Skip to content

Commit

Permalink
Fixed issue #2239: Add 'XDEBUG_IGNORE' GET/POST/COOKIE/ENV to make th…
Browse files Browse the repository at this point in the history
…e step debugger ignore that specific request
  • Loading branch information
derickr committed Jan 23, 2024
1 parent 50a9203 commit 2678a4b
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 25 deletions.
38 changes: 38 additions & 0 deletions src/debugger/com.c
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,40 @@ void xdebug_mark_debug_connection_not_active()
XG_DBG(remote_connection_pid) = 0;
}

bool xdebug_should_ignore(void)
{
const char *ignore_value;
const char *found_in_global;

ignore_value = xdebug_lib_find_in_globals("XDEBUG_IGNORE", &found_in_global);

if (!ignore_value) {
return false;
}

if ((strcmp(ignore_value, "no") == 0) || (strcmp(ignore_value, "0") == 0)) {
xdebug_log_ex(XLOG_CHAN_DEBUG, XLOG_INFO, "IGN", "Not ignoring present 'XDEBUG_IGNORE' %s variable, because the value is '%s'.", found_in_global, ignore_value);
return false;
}

xdebug_log_ex(XLOG_CHAN_DEBUG, XLOG_DEBUG, "IGN", "Not activating because an 'XDEBUG_IGNORE' %s variable is present, with value '%s'.", found_in_global, ignore_value);
return true;
}


void xdebug_debug_init_if_requested_on_connect_to_client()
{
RETURN_IF_MODE_IS_NOT(XDEBUG_MODE_STEP_DEBUG);

if (xdebug_should_ignore()) {
return;
}

if (!xdebug_is_debug_connection_active()) {
xdebug_init_debugger();
}
}

void xdebug_debug_init_if_requested_on_error()
{
RETURN_IF_MODE_IS_NOT(XDEBUG_MODE_STEP_DEBUG);
Expand Down Expand Up @@ -771,6 +805,10 @@ void xdebug_debug_init_if_requested_at_startup(void)
return;
}

if (xdebug_should_ignore()) {
return;
}

if (
xdebug_lib_start_with_request(XDEBUG_MODE_STEP_DEBUG) ||
(!xdebug_lib_never_start_with_request() && xdebug_handle_start_session()) ||
Expand Down
1 change: 1 addition & 0 deletions src/debugger/com.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ void xdebug_mark_debug_connection_active(void);
void xdebug_mark_debug_connection_not_active(void);
void xdebug_mark_debug_connection_pending(void);
void xdebug_debug_init_if_requested_at_startup(void);
void xdebug_debug_init_if_requested_on_connect_to_client(void);
void xdebug_debug_init_if_requested_on_error(void);
void xdebug_debug_init_if_requested_on_xdebug_break(void);

Expand Down
2 changes: 1 addition & 1 deletion src/debugger/debugger.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ void xdebug_debugger_statement_call(zend_string *filename, int lineno)
XG_DBG(context).do_connect_to_client = 0;

if (!xdebug_is_debug_connection_active()) {
xdebug_debug_init_if_requested_on_xdebug_break();
xdebug_debug_init_if_requested_on_connect_to_client();
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/debugger/debugger_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,6 @@ struct _xdebug_lines_list {
#define XG_DBG(v) (XG(globals.debugger.v))
#define XINI_DBG(v) (XG(settings.debugger.v))

bool xdebug_should_ignore(void);

#endif
64 changes: 50 additions & 14 deletions src/lib/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -325,26 +325,61 @@ const char *xdebug_lib_mode_from_value(int mode)
}
}

static const char *find_in_globals(const char *element)
const char *xdebug_lib_find_in_globals(const char *element, const char **found_in_global)
{
zval *trigger_val = NULL;
const char *env_value = getenv(element);
zval *st;

/* Elements in Superglobal Symbols */
st = zend_hash_str_find(&EG(symbol_table), "_GET", strlen("_GET"));
if (st && (trigger_val = zend_hash_str_find(Z_ARRVAL_P(st), element, strlen(element))) != NULL) {
*found_in_global = "GET";
return Z_STRVAL_P(trigger_val);
}

st = zend_hash_str_find(&EG(symbol_table), "_POST", strlen("_POST"));
if (st && (trigger_val = zend_hash_str_find(Z_ARRVAL_P(st), element, strlen(element))) != NULL) {
*found_in_global = "POST";
return Z_STRVAL_P(trigger_val);
}

st = zend_hash_str_find(&EG(symbol_table), "_COOKIE", strlen("_COOKIE"));
if (st && (trigger_val = zend_hash_str_find(Z_ARRVAL_P(st), element, strlen(element))) != NULL) {
*found_in_global = "COOKIE";
return Z_STRVAL_P(trigger_val);
}

/* Actual Superglobals */
if ((trigger_val = zend_hash_str_find(Z_ARR(PG(http_globals)[TRACK_VARS_GET]), element, strlen(element))) != NULL) {
*found_in_global = "GET";
return Z_STRVAL_P(trigger_val);
}

if ((trigger_val = zend_hash_str_find(Z_ARR(PG(http_globals)[TRACK_VARS_POST]), element, strlen(element))) != NULL) {
*found_in_global = "POST";
return Z_STRVAL_P(trigger_val);
}

if ((trigger_val = zend_hash_str_find(Z_ARR(PG(http_globals)[TRACK_VARS_COOKIE]), element, strlen(element))) != NULL) {
*found_in_global = "COOKIE";
return Z_STRVAL_P(trigger_val);
}

/* Environment. This goes last. */
if (env_value) {
*found_in_global = "ENV";
return env_value;
}

if (
(
(trigger_val = zend_hash_str_find(Z_ARR(PG(http_globals)[TRACK_VARS_ENV]), element, strlen(element))) != NULL
) || (
(trigger_val = zend_hash_str_find(Z_ARR(PG(http_globals)[TRACK_VARS_GET]), element, strlen(element))) != NULL
) || (
(trigger_val = zend_hash_str_find(Z_ARR(PG(http_globals)[TRACK_VARS_POST]), element, strlen(element))) != NULL
) || (
(trigger_val = zend_hash_str_find(Z_ARR(PG(http_globals)[TRACK_VARS_COOKIE]), element, strlen(element))) != NULL
)
) {
st = zend_hash_str_find(&EG(symbol_table), "_ENV", strlen("_ENV"));
if (st && (trigger_val = zend_hash_str_find(Z_ARRVAL_P(st), element, strlen(element))) != NULL) {
*found_in_global = "ENV";
return Z_STRVAL_P(trigger_val);
}

if ((trigger_val = zend_hash_str_find(Z_ARR(PG(http_globals)[TRACK_VARS_ENV]), element, strlen(element))) != NULL) {
*found_in_global = "ENV";
return Z_STRVAL_P(trigger_val);
}

Expand Down Expand Up @@ -431,11 +466,12 @@ static int trigger_enabled(int for_mode, char **found_trigger_value)
{
const char *trigger_value = NULL;
const char *trigger_name = "XDEBUG_TRIGGER";
const char *found_in_global;

xdebug_log(XLOG_CHAN_CONFIG, XLOG_DEBUG, "Checking if trigger 'XDEBUG_TRIGGER' is enabled for mode '%s'", xdebug_lib_mode_from_value(for_mode));

/* First we check for the generic 'XDEBUG_TRIGGER' option */
trigger_value = find_in_globals(trigger_name);
trigger_value = xdebug_lib_find_in_globals(trigger_name, &found_in_global);

/* If not found, we fall back to the per-mode name for backwards compatibility reasons */
if (!trigger_value) {
Expand All @@ -449,7 +485,7 @@ static int trigger_enabled(int for_mode, char **found_trigger_value)

if (trigger_name) {
xdebug_log(XLOG_CHAN_CONFIG, XLOG_INFO, "Trigger value for 'XDEBUG_TRIGGER' not found, falling back to '%s'", trigger_name);
trigger_value = find_in_globals(trigger_name);
trigger_value = xdebug_lib_find_in_globals(trigger_name, &found_in_global);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/lib/lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ int xdebug_lib_never_start_with_request(void);
int xdebug_lib_get_start_with_request(void);
int xdebug_lib_has_shared_secret(void);

const char *xdebug_lib_find_in_globals(const char *element, const char **found_in_global);

#define XDEBUG_START_UPON_ERROR_DEFAULT 1
#define XDEBUG_START_UPON_ERROR_YES 2
#define XDEBUG_START_UPON_ERROR_NO 3
Expand Down
4 changes: 2 additions & 2 deletions tests/debugger/bug00627.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ check_reqs('dbgp; !win');
<?php
require 'dbgp/dbgpclient.php';
$dir = dirname(__FILE__);
putenv("XDEBUG_TEST_DIR=$dir");

$filename = $dir . '/bug00627.inc';

$commands = array(
Expand All @@ -20,7 +20,7 @@ $commands = array(
'detach',
);

dbgpRunFile( $filename, $commands );
dbgpRunFile( $filename, $commands, [], ['env' => ['XDEBUG_TEST_DIR' => $dir]] );
?>
--EXPECT--
<?xml version="1.0" encoding="iso-8859-1"?>
Expand Down
3 changes: 1 addition & 2 deletions tests/debugger/bug00886.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ check_reqs('ext phar; dbgp; slow; !osx');
require 'dbgp/dbgpclient.php';

$dir = dirname(__FILE__);
putenv("XDEBUG_TEST_DIR=$dir");
$pharFile = str_replace('\\', '/', "phar://{$dir}/bug00886.phar");
$filename = dirname(__FILE__) . '/bug00886.inc';

Expand All @@ -27,7 +26,7 @@ $commands = array(
'detach',
);

dbgpRunFile( $filename, $commands );
dbgpRunFile( $filename, $commands, [], ['env' => ['XDEBUG_TEST_DIR' => $dir]] );
?>
--EXPECTF--
<?xml version="1.0" encoding="iso-8859-1"?>
Expand Down
3 changes: 1 addition & 2 deletions tests/debugger/bug01007.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ check_reqs('dbgp');
require 'dbgp/dbgpclient.php';

$dir = dirname(__FILE__);
putenv("XDEBUG_TEST_DIR=$dir");

$filename = dirname(__FILE__) . '/bug01007-index.inc';

Expand All @@ -22,7 +21,7 @@ $commands = array(
'property_get -n ::hello'
);

dbgpRunFile( $filename, $commands );
dbgpRunFile( $filename, $commands, [], ['env' => ['XDEBUG_TEST_DIR' => $dir]] );
?>
--EXPECT--
<?xml version="1.0" encoding="iso-8859-1"?>
Expand Down
18 changes: 14 additions & 4 deletions tests/debugger/dbgp/dbgpclient.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private function open( &$errno, &$errstr )
return $socket;
}

private function launchPhp( &$pipes, $filename, array $ini_options = [] )
private function launchPhp( &$pipes, $filename, array $ini_options = [], array $extra_options = [] )
{
@unlink( $this->tmpDir . 'error-output.txt' );
@unlink( $this->tmpDir . 'remote_log.txt' );
Expand All @@ -73,21 +73,31 @@ private function launchPhp( &$pipes, $filename, array $ini_options = [] )
"xdebug.client_port" => $this->getPort(),
);

$env_vars = array_key_exists( 'env', $extra_options ) ? $extra_options['env'] : [];
$env_vars += $_ENV;

$options = (getenv('TEST_PHP_ARGS') ?: '');
$ini_options = array_merge( $default_options, $ini_options );
foreach ( $ini_options as $key => $value )
{
$options .= " -d{$key}=$value";
}

if ( array_key_exists( 'auto_prepend', $extra_options ) )
{
$prependFile = "{$this->tmpDir}auto-prepend.inc";
file_put_contents( $prependFile, $extra_options['auto_prepend'] );
$options .= " -dauto_prepend_file={$prependFile}";
}

$php = getenv( 'TEST_PHP_EXECUTABLE' );
$cmd = "{$php} $options {$filename} >{$this->tmpDir}php-stdout.txt 2>{$this->tmpDir}php-stderr.txt";
if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
$cmd = "exec {$cmd}";
}
$cwd = dirname( __FILE__ );

$process = proc_open( $cmd, $descriptorspec, $pipes, $cwd );
$process = proc_open( $cmd, $descriptorspec, $pipes, $cwd, $env_vars );
return $process;
}

Expand Down Expand Up @@ -174,8 +184,8 @@ function start( $filename, array $ini_options = [], array $options = [])
echo "Address: {$this->getAddress()}\n";
return false;
}
$this->php = $this->launchPhp( $this->ppipes, $filename, $ini_options );
$conn = @stream_socket_accept( $this->socket, isset( $options['timeout'] ) ? $options['timeout'] : 20 );
$this->php = $this->launchPhp( $this->ppipes, $filename, $ini_options, $options );
$conn = @stream_socket_accept( $this->socket, isset( $options['timeout'] ) ? $options['timeout'] : 5 );

if ( $conn === false )
{
Expand Down

0 comments on commit 2678a4b

Please sign in to comment.