Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Zend/tests/display_error_function_args.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
Displaying function arguments in errors
--INI--
display_error_function_args=On
--FILE--
<?php

// A function that sets its own parameters in docref call, to compare
unlink('/');
// Something with sensitive parameters that exists in a minimal build,
// and also doesn't set anything in the docref call
// XXX: May be better to provide a new zend_test func?
password_hash("test", PASSWORD_BCRYPT, array("salt" => "123456789012345678901" . chr(0)));

ini_set("display_error_function_args", "Off");

unlink('/');
password_hash("test", PASSWORD_BCRYPT, array("salt" => "123456789012345678901" . chr(0)));

?>
--EXPECTF--
Warning: unlink('/'): %s in %s on line %d

Warning: password_hash(Object(SensitiveParameterValue), '2y', Array): The "salt" option has been ignored, since providing a custom salt is no longer supported in %s on line %d

Warning: unlink(/): %s in %s on line %d

Warning: password_hash(): The "salt" option has been ignored, since providing a custom salt is no longer supported in %s on line %d
82 changes: 63 additions & 19 deletions Zend/zend_exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,31 @@ static void _build_trace_args(zval *arg, smart_str *str) /* {{{ */
}
/* }}} */

static void _build_trace_args_list(zval *tmp, smart_str *str) /* {{{ */
{
if (EXPECTED(Z_TYPE_P(tmp) == IS_ARRAY)) {
size_t last_len = ZSTR_LEN(str->s);
zend_string *name;
zval *arg;

ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(tmp), name, arg) {
if (name) {
smart_str_append(str, name);
smart_str_appends(str, ": ");
}
_build_trace_args(arg, str);
} ZEND_HASH_FOREACH_END();

if (last_len != ZSTR_LEN(str->s)) {
ZSTR_LEN(str->s) -= 2; /* remove last ', ' */
}
} else {
/* only happens w/ reflection abuse (Zend/tests/bug63762.phpt) */
zend_error(E_WARNING, "args element is not an array");
}
}
/* }}} */

static void _build_trace_string(smart_str *str, const HashTable *ht, uint32_t num) /* {{{ */
{
zval *file, *tmp;
Expand Down Expand Up @@ -579,30 +604,49 @@ static void _build_trace_string(smart_str *str, const HashTable *ht, uint32_t nu
smart_str_appendc(str, '(');
tmp = zend_hash_find_known_hash(ht, ZSTR_KNOWN(ZEND_STR_ARGS));
if (tmp) {
if (EXPECTED(Z_TYPE_P(tmp) == IS_ARRAY)) {
size_t last_len = ZSTR_LEN(str->s);
zend_string *name;
zval *arg;

ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(tmp), name, arg) {
if (name) {
smart_str_append(str, name);
smart_str_appends(str, ": ");
}
_build_trace_args(arg, str);
} ZEND_HASH_FOREACH_END();

if (last_len != ZSTR_LEN(str->s)) {
ZSTR_LEN(str->s) -= 2; /* remove last ', ' */
}
} else {
zend_error(E_WARNING, "args element is not an array");
}
_build_trace_args_list(tmp, str);
}
smart_str_appends(str, ")\n");
}
/* }}} */

/* {{{ Gets the function arguments printed as a string from a backtrace frame. */
ZEND_API zend_string *zend_trace_function_args_to_string(const HashTable *frame) {
zval *tmp;
smart_str str = {0};
/* init because ASan will complain */
smart_str_appends(&str, "");

tmp = zend_hash_find_known_hash(frame, ZSTR_KNOWN(ZEND_STR_ARGS));
if (tmp) {
_build_trace_args_list(tmp, &str);
}

smart_str_0(&str);
return str.s ? str.s : ZSTR_EMPTY_ALLOC();
}
/* }}} */

/* {{{ Gets the currently executing function's arguments as a string. Used by php_verror. */
ZEND_API zend_string *zend_trace_current_function_args_string(void) {
zend_string *dynamic_params = NULL;
/* get a backtrace to snarf function args */
zval backtrace, *first_frame;
zend_fetch_debug_backtrace(&backtrace, /* skip_last */ 0, /* options */ 0, /* limit */ 1);
/* can fail esp if low memory condition */
if (Z_TYPE(backtrace) != IS_ARRAY) {
return NULL; /* don't need to free */
}
first_frame = zend_hash_index_find(Z_ARRVAL(backtrace), 0);
if (first_frame) {
dynamic_params = zend_trace_function_args_to_string(Z_ARRVAL_P(first_frame));
}
zval_ptr_dtor(&backtrace);
/* free the string after we use it */
return dynamic_params;
}
/* }}} */

ZEND_API zend_string *zend_trace_to_string(const HashTable *trace, bool include_main) {
zend_ulong index;
zval *frame;
Expand Down
2 changes: 2 additions & 0 deletions Zend/zend_exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ ZEND_API zend_result zend_update_exception_properties(zend_execute_data *execute
/* show an exception using zend_error(severity,...), severity should be E_ERROR */
ZEND_API ZEND_COLD zend_result zend_exception_error(zend_object *exception, int severity);
ZEND_NORETURN void zend_exception_uncaught_error(const char *prefix, ...) ZEND_ATTRIBUTE_FORMAT(printf, 1, 2);
ZEND_API zend_string *zend_trace_function_args_to_string(const HashTable *frame);
ZEND_API zend_string *zend_trace_current_function_args_string(void);
ZEND_API zend_string *zend_trace_to_string(const HashTable *trace, bool include_main);

ZEND_API ZEND_COLD zend_object *zend_create_unwind_exit(void);
Expand Down
15 changes: 14 additions & 1 deletion main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
#include "win32/php_registry.h"
#include "ext/standard/flock_compat.h"
#endif
#include "Zend/zend_builtin_functions.h"
#include "Zend/zend_exceptions.h"

#if PHP_SIGCHILD
Expand Down Expand Up @@ -802,6 +803,7 @@ PHP_INI_BEGIN()

STD_PHP_INI_ENTRY_EX("display_errors", "1", PHP_INI_ALL, OnUpdateDisplayErrors, display_errors, php_core_globals, core_globals, display_errors_mode)
STD_PHP_INI_BOOLEAN("display_startup_errors", "1", PHP_INI_ALL, OnUpdateBool, display_startup_errors, php_core_globals, core_globals)
STD_PHP_INI_BOOLEAN("display_error_function_args", "0", PHP_INI_ALL, OnUpdateBool, display_error_function_args, php_core_globals, core_globals)
STD_PHP_INI_BOOLEAN("enable_dl", "1", PHP_INI_SYSTEM, OnUpdateBool, enable_dl, php_core_globals, core_globals)
STD_PHP_INI_BOOLEAN("expose_php", "1", PHP_INI_SYSTEM, OnUpdateBool, expose_php, php_core_globals, core_globals)
STD_PHP_INI_ENTRY("docref_root", "", PHP_INI_ALL, OnUpdateString, docref_root, php_core_globals, core_globals)
Expand Down Expand Up @@ -1134,7 +1136,18 @@ PHPAPI ZEND_COLD void php_verror(const char *docref, const char *params, int typ

/* if we still have memory then format the origin */
if (is_function) {
origin_len = spprintf(&origin, 0, "%s%s%s(%s)", class_name, space, function, params);
zend_string *dynamic_params = NULL;
/*
* By default, this is disabled because it requires rewriting
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest taking a look at https://wiki.php.net/rfc/error_backtraces_v2 and its discussion - you can have the default be true generally, and then false for tests

* almost all PHPT files.
*/
if (PG(display_error_function_args)) {
dynamic_params = zend_trace_current_function_args_string();
}
origin_len = spprintf(&origin, 0, "%s%s%s(%s)", class_name, space, function, dynamic_params ? ZSTR_VAL(dynamic_params) : params);
if (dynamic_params) {
zend_string_release(dynamic_params);
}
} else {
origin_len = strlen(function);
origin = estrndup(function, origin_len);
Expand Down
1 change: 1 addition & 0 deletions main/php_globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ struct _php_core_globals {

uint8_t display_errors;
bool display_startup_errors;
bool display_error_function_args;
bool log_errors;
bool ignore_repeated_errors;
bool ignore_repeated_source;
Expand Down
9 changes: 9 additions & 0 deletions php.ini-development
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,15 @@ ignore_repeated_source = Off
; Production Value: On
;fatal_error_backtraces = On

; This directive controls whether PHP will print the actual arguments of a
; function upon an error. If disabled (or there was an error fetching the
; arguments), the function providing the error may optionally provide some
; additional information after the problem function's name.
; Default Value: Off
; Development Value: On
; Production Value: On
display_error_function_args = On
Comment on lines +618 to +621
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related to Daniel's comment: Please avoid further INIs where the builtin default differs from the recommended “production default”.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Relatedly, we should probably finally drop zend.exception_ignore_args=Off from php.ini-production /cc @bwoebi.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean zend.exception_ignore_args=On? Because that's what currently is in php.ini-production if I'm not mistaken. Do you think the information disclosure is no longer an issue?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean zend.exception_ignore_args=On?

Yes, thank you.

Do you think the information disclosure is no longer an issue?

display_errors=Off should remain and for parameters in logs there is #[\SensitiveParameter] and “just changing the INI”. The Docker images also don't load any INI by default, thus for them the builtin default is already being used.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going back to the original comment of avoiding defaults which are different from production recommendations: Is there a consensus that the default should be production settings?

Because some of the main settings like display_errors and error_reporting have Development as a default and I probably missed the discussion on whether a default configuration is aimed at development or production, i.e. prioritize providing as much information to the developers as possible or try to prevent information leakage on production systems as much as possible.

I can see arguments for both and don't have a strong opinion, I'm just pointing out that I'm unaware of a clear decision.


;;;;;;;;;;;;;;;;;
; Data Handling ;
;;;;;;;;;;;;;;;;;
Expand Down
9 changes: 9 additions & 0 deletions php.ini-production
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,15 @@ ignore_repeated_source = Off
; Production Value: On
;fatal_error_backtraces = On

; This directive controls whether PHP will print the actual arguments of a
; function upon an error. If disabled (or there was an error fetching the
; arguments), the function providing the error may optionally provide some
; additional information after the problem function's name.
; Default Value: Off
; Development Value: On
; Production Value: On
display_error_function_args = On

;;;;;;;;;;;;;;;;;
; Data Handling ;
;;;;;;;;;;;;;;;;;
Expand Down
1 change: 1 addition & 0 deletions run-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ function main(): void
'fatal_error_backtraces=Off',
'display_errors=1',
'display_startup_errors=1',
'display_error_function_args=0',
'log_errors=0',
'html_errors=0',
'track_errors=0',
Expand Down
4 changes: 4 additions & 0 deletions scripts/dev/bless_tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ function normalizeOutput(string $out): string {
'Resource ID#%d used as offset, casting to integer (%d)',
$out);
$out = preg_replace('/string\(\d+\) "([^"]*%d)/', 'string(%d) "$1', $out);
// Inside of strings, replace absolute paths that have been truncated with
// any string. These tend to contain homedirs with usernames, not good.
$out = preg_replace("/'\\/.*\.\\.\\.'/", "'%s'", $out);
$out = preg_replace("/'file:\/\\/.*\.\\.\\.'/", "'%s'", $out);
$out = str_replace("\0", '%0', $out);
return $out;
}
Expand Down
Loading