Skip to content

Commit b6ee006

Browse files
committed
Store the original value in SensitiveParameterValue
1 parent 0ce7327 commit b6ee006

File tree

6 files changed

+179
-36
lines changed

6 files changed

+179
-36
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
SensitiveParameterValue behaves as expected.
3+
--FILE--
4+
<?php
5+
6+
$v = new SensitiveParameterValue('secret');
7+
8+
var_dump($v);
9+
var_dump(serialize($v));
10+
var_dump(json_encode($v));
11+
var_dump($v->getValue());
12+
?>
13+
--EXPECTF--
14+
object(SensitiveParameterValue)#%d (0) {
15+
}
16+
string(35) "O:23:"SensitiveParameterValue":0:{}"
17+
string(2) "{}"
18+
string(6) "secret"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
SensitiveParameterValue may not be unserialized.
3+
--FILE--
4+
<?php
5+
6+
$v = new SensitiveParameterValue('secret');
7+
8+
var_dump(unserialize(serialize($v)));
9+
?>
10+
--EXPECTF--
11+
Fatal error: Uncaught Exception: Unserializing objects of class SensitiveParameterValue is not supported. in %ssensitive_parameter_value_unserialize.php:5
12+
Stack trace:
13+
#0 [internal function]: SensitiveParameterValue->__unserialize(Array)
14+
#1 %ssensitive_parameter_value_unserialize.php(5): unserialize('O:23:"Sensitive...')
15+
#2 {main}
16+
thrown in %ssensitive_parameter_value_unserialize.php on line 5

Zend/zend_attributes.c

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "zend_API.h"
2222
#include "zend_attributes.h"
2323
#include "zend_attributes_arginfo.h"
24+
#include "zend_exceptions.h"
2425
#include "zend_smart_str.h"
2526

2627
ZEND_API zend_class_entry *zend_ce_attribute;
@@ -100,9 +101,52 @@ ZEND_METHOD(SensitiveParameter, __construct)
100101

101102
ZEND_METHOD(SensitiveParameterValue, __construct)
102103
{
103-
ZEND_PARSE_PARAMETERS_NONE();
104+
zval *value;
105+
106+
ZEND_PARSE_PARAMETERS_START(1, 1)
107+
Z_PARAM_ZVAL(value)
108+
ZEND_PARSE_PARAMETERS_END();
109+
110+
ZVAL_COPY(OBJ_PROP_NUM(Z_OBJ_P(ZEND_THIS), 0), value);
104111
}
105112

113+
/* {{{ */
114+
ZEND_METHOD(SensitiveParameterValue, getValue)
115+
{
116+
ZEND_PARSE_PARAMETERS_NONE();
117+
118+
ZVAL_COPY(return_value, OBJ_PROP_NUM(Z_OBJ_P(ZEND_THIS), 0));
119+
} /* }}} */
120+
121+
/* {{{ */
122+
ZEND_METHOD(SensitiveParameterValue, __debugInfo)
123+
{
124+
ZEND_PARSE_PARAMETERS_NONE();
125+
126+
array_init(return_value);
127+
} /* }}} */
128+
129+
/* {{{ */
130+
ZEND_METHOD(SensitiveParameterValue, __serialize)
131+
{
132+
ZEND_PARSE_PARAMETERS_NONE();
133+
134+
array_init(return_value);
135+
} /* }}} */
136+
137+
/* {{{ */
138+
ZEND_METHOD(SensitiveParameterValue, __unserialize)
139+
{
140+
HashTable *data;
141+
142+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "h", &data) == FAILURE) {
143+
RETURN_THROWS();
144+
}
145+
146+
zend_throw_exception(NULL, "Unserializing objects of class SensitiveParameterValue is not supported.", 0);
147+
RETURN_THROWS();
148+
} /* }}} */
149+
106150
static zend_attribute *get_attribute(HashTable *attributes, zend_string *lcname, uint32_t offset)
107151
{
108152
if (attributes) {

Zend/zend_attributes.stub.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,15 @@ public function __construct() {}
2626

2727
final class SensitiveParameterValue
2828
{
29-
public function __construct() {}
29+
private mixed $value;
30+
31+
public function __construct(mixed $value) {}
32+
33+
public function getValue(): mixed {}
34+
35+
public function __serialize(): array {}
36+
37+
public function __unserialize(array $data): void {}
38+
39+
public function __debugInfo(): array{}
3040
}

Zend/zend_attributes_arginfo.h

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 5cc7e0e910c5d61926451412e176fa00a92b3239 */
2+
* Stub hash: f5eecf818174dad27449a9f1c944f47a17d63c30 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Attribute___construct, 0, 0, 0)
55
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, flags, IS_LONG, 0, "Attribute::TARGET_ALL")
@@ -12,14 +12,32 @@ ZEND_END_ARG_INFO()
1212

1313
#define arginfo_class_SensitiveParameter___construct arginfo_class_ReturnTypeWillChange___construct
1414

15-
#define arginfo_class_SensitiveParameterValue___construct arginfo_class_ReturnTypeWillChange___construct
15+
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_SensitiveParameterValue___construct, 0, 0, 1)
16+
ZEND_ARG_TYPE_INFO(0, value, IS_MIXED, 0)
17+
ZEND_END_ARG_INFO()
18+
19+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_SensitiveParameterValue_getValue, 0, 0, IS_MIXED, 0)
20+
ZEND_END_ARG_INFO()
21+
22+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_SensitiveParameterValue___serialize, 0, 0, IS_ARRAY, 0)
23+
ZEND_END_ARG_INFO()
24+
25+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_SensitiveParameterValue___unserialize, 0, 1, IS_VOID, 0)
26+
ZEND_ARG_TYPE_INFO(0, data, IS_ARRAY, 0)
27+
ZEND_END_ARG_INFO()
28+
29+
#define arginfo_class_SensitiveParameterValue___debugInfo arginfo_class_SensitiveParameterValue___serialize
1630

1731

1832
ZEND_METHOD(Attribute, __construct);
1933
ZEND_METHOD(ReturnTypeWillChange, __construct);
2034
ZEND_METHOD(AllowDynamicProperties, __construct);
2135
ZEND_METHOD(SensitiveParameter, __construct);
2236
ZEND_METHOD(SensitiveParameterValue, __construct);
37+
ZEND_METHOD(SensitiveParameterValue, getValue);
38+
ZEND_METHOD(SensitiveParameterValue, __serialize);
39+
ZEND_METHOD(SensitiveParameterValue, __unserialize);
40+
ZEND_METHOD(SensitiveParameterValue, __debugInfo);
2341

2442

2543
static const zend_function_entry class_Attribute_methods[] = {
@@ -48,6 +66,10 @@ static const zend_function_entry class_SensitiveParameter_methods[] = {
4866

4967
static const zend_function_entry class_SensitiveParameterValue_methods[] = {
5068
ZEND_ME(SensitiveParameterValue, __construct, arginfo_class_SensitiveParameterValue___construct, ZEND_ACC_PUBLIC)
69+
ZEND_ME(SensitiveParameterValue, getValue, arginfo_class_SensitiveParameterValue_getValue, ZEND_ACC_PUBLIC)
70+
ZEND_ME(SensitiveParameterValue, __serialize, arginfo_class_SensitiveParameterValue___serialize, ZEND_ACC_PUBLIC)
71+
ZEND_ME(SensitiveParameterValue, __unserialize, arginfo_class_SensitiveParameterValue___unserialize, ZEND_ACC_PUBLIC)
72+
ZEND_ME(SensitiveParameterValue, __debugInfo, arginfo_class_SensitiveParameterValue___debugInfo, ZEND_ACC_PUBLIC)
5173
ZEND_FE_END
5274
};
5375

@@ -109,5 +131,11 @@ static zend_class_entry *register_class_SensitiveParameterValue(void)
109131
class_entry = zend_register_internal_class_ex(&ce, NULL);
110132
class_entry->ce_flags |= ZEND_ACC_FINAL;
111133

134+
zval property_value_default_value;
135+
ZVAL_UNDEF(&property_value_default_value);
136+
zend_string *property_value_name = zend_string_init("value", sizeof("value") - 1, 1);
137+
zend_declare_typed_property(class_entry, property_value_name, &property_value_default_value, ZEND_ACC_PRIVATE, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_ANY));
138+
zend_string_release(property_value_name);
139+
112140
return class_entry;
113141
}

Zend/zend_builtin_functions.c

Lines changed: 59 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "zend_builtin_functions.h"
2525
#include "zend_constants.h"
2626
#include "zend_ini.h"
27+
#include "zend_interfaces.h"
2728
#include "zend_exceptions.h"
2829
#include "zend_extensions.h"
2930
#include "zend_closures.h"
@@ -1567,25 +1568,35 @@ static void debug_backtrace_get_args(zend_execute_data *call, zval *arg_array) /
15671568
);
15681569

15691570
if (attribute != NULL) {
1570-
zval arg;
1571-
object_init_ex(&arg, zend_ce_sensitive_parameter_value);
1572-
ZEND_HASH_FILL_SET(&arg);
15731571
last_was_sensitive = 1;
15741572
} else {
15751573
last_was_sensitive = 0;
1576-
if (arg) {
1577-
ZVAL_DEREF(arg);
1578-
Z_TRY_ADDREF_P(arg);
1579-
ZEND_HASH_FILL_SET(arg);
1580-
} else {
1581-
ZEND_HASH_FILL_SET_NULL();
1582-
}
1574+
15831575
}
1576+
1577+
if (arg) {
1578+
ZVAL_DEREF(arg);
1579+
Z_TRY_ADDREF_P(arg);
1580+
} else {
1581+
ZVAL_NULL(arg);
1582+
}
1583+
1584+
if (last_was_sensitive) {
1585+
zval new_arg;
1586+
object_init_ex(&new_arg, zend_ce_sensitive_parameter_value);
1587+
zend_call_method_with_1_params(Z_OBJ_P(&new_arg), zend_ce_sensitive_parameter_value, &zend_ce_sensitive_parameter_value->constructor, "__construct", NULL, arg);
1588+
ZEND_HASH_FILL_SET(&new_arg);
1589+
} else {
1590+
ZEND_HASH_FILL_SET(arg);
1591+
}
1592+
15841593
ZEND_HASH_FILL_NEXT();
15851594
i++;
15861595
}
15871596
} else {
15881597
while (i < first_extra_arg) {
1598+
zval *arg;
1599+
15891600
zend_attribute *attribute = zend_get_parameter_attribute_str(
15901601
call->func->op_array.attributes,
15911602
"sensitiveparameter",
@@ -1594,22 +1605,28 @@ static void debug_backtrace_get_args(zend_execute_data *call, zval *arg_array) /
15941605
);
15951606

15961607
if (attribute != NULL) {
1597-
zval arg;
1598-
object_init_ex(&arg, zend_ce_sensitive_parameter_value);
1599-
ZEND_HASH_FILL_SET(&arg);
16001608
last_was_sensitive = 1;
16011609
} else {
16021610
last_was_sensitive = 0;
1611+
}
1612+
1613+
if (EXPECTED(Z_TYPE_INFO_P(p) != IS_UNDEF)) {
1614+
arg = p;
1615+
ZVAL_DEREF(arg);
1616+
Z_TRY_ADDREF_P(arg);
1617+
} else {
1618+
ZVAL_NULL(arg);
1619+
}
16031620

1604-
if (EXPECTED(Z_TYPE_INFO_P(p) != IS_UNDEF)) {
1605-
zval *arg = p;
1606-
ZVAL_DEREF(arg);
1607-
Z_TRY_ADDREF_P(arg);
1608-
ZEND_HASH_FILL_SET(arg);
1609-
} else {
1610-
ZEND_HASH_FILL_SET_NULL();
1611-
}
1621+
if (last_was_sensitive) {
1622+
zval new_arg;
1623+
object_init_ex(&new_arg, zend_ce_sensitive_parameter_value);
1624+
zend_call_method_with_1_params(Z_OBJ_P(&new_arg), zend_ce_sensitive_parameter_value, &zend_ce_sensitive_parameter_value->constructor, "__construct", NULL, arg);
1625+
ZEND_HASH_FILL_SET(&new_arg);
1626+
} else {
1627+
ZEND_HASH_FILL_SET(arg);
16121628
}
1629+
16131630
ZEND_HASH_FILL_NEXT();
16141631
p++;
16151632
i++;
@@ -1619,6 +1636,8 @@ static void debug_backtrace_get_args(zend_execute_data *call, zval *arg_array) /
16191636
}
16201637

16211638
while (i < num_args) {
1639+
zval *arg;
1640+
16221641
zend_attribute *attribute = zend_get_parameter_attribute_str(
16231642
call->func->op_array.attributes,
16241643
"sensitiveparameter",
@@ -1630,22 +1649,30 @@ static void debug_backtrace_get_args(zend_execute_data *call, zval *arg_array) /
16301649
(i > call->func->op_array.num_args && last_was_sensitive)
16311650
|| attribute != NULL
16321651
) {
1633-
zval arg;
1634-
object_init_ex(&arg, zend_ce_sensitive_parameter_value);
1635-
ZEND_HASH_FILL_SET(&arg);
1652+
16361653
last_was_sensitive = 1;
16371654
} else {
16381655
last_was_sensitive = 0;
1656+
}
16391657

1640-
if (EXPECTED(Z_TYPE_INFO_P(p) != IS_UNDEF)) {
1641-
zval *arg = p;
1642-
ZVAL_DEREF(arg);
1643-
Z_TRY_ADDREF_P(arg);
1644-
ZEND_HASH_FILL_SET(arg);
1645-
} else {
1646-
ZEND_HASH_FILL_SET_NULL();
1647-
}
1658+
1659+
if (EXPECTED(Z_TYPE_INFO_P(p) != IS_UNDEF)) {
1660+
arg = p;
1661+
ZVAL_DEREF(arg);
1662+
Z_TRY_ADDREF_P(arg);
1663+
} else {
1664+
ZVAL_NULL(arg);
1665+
}
1666+
1667+
if (last_was_sensitive) {
1668+
zval new_arg;
1669+
object_init_ex(&new_arg, zend_ce_sensitive_parameter_value);
1670+
zend_call_method_with_1_params(Z_OBJ_P(&new_arg), zend_ce_sensitive_parameter_value, &zend_ce_sensitive_parameter_value->constructor, "__construct", NULL, arg);
1671+
ZEND_HASH_FILL_SET(&new_arg);
1672+
} else {
1673+
ZEND_HASH_FILL_SET(arg);
16481674
}
1675+
16491676
ZEND_HASH_FILL_NEXT();
16501677
p++;
16511678
i++;

0 commit comments

Comments
 (0)