Skip to content

Commit 491716b

Browse files
committed
By default strip sensitive data from serialized exceptions
1 parent 2cafec0 commit 491716b

File tree

3 files changed

+63
-21
lines changed

3 files changed

+63
-21
lines changed

src/Exception/JSONModelValidationException.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,35 @@
1616
*/
1717
abstract class JSONModelValidationException extends Exception implements JsonSerializable, SerializationInterface
1818
{
19-
use SerializableTrait;
19+
use SerializableTrait {
20+
toArray as protected _toArray;
21+
}
22+
23+
/**
24+
* @inheritDoc
25+
*
26+
* @param bool $stripSensitiveData By default the file and the line of the exception will not be serialized
27+
*/
28+
public function toArray(array $except = [], int $depth = 512, bool $stripSensitiveData = true)
29+
{
30+
if ($stripSensitiveData && !in_array('__KEEP_SENSITIVE_DATA__', $except)) {
31+
$except = array_merge($except, ['file', 'line']);
32+
} else {
33+
array_push($except, '__KEEP_SENSITIVE_DATA__');
34+
}
35+
36+
return $this->_toArray($except, $depth);
37+
}
38+
39+
/**
40+
* @inheritDoc
41+
*/
42+
public function toJSON(array $except = [], int $options = 0, int $depth = 512, bool $stripSensitiveData = true)
43+
{
44+
if ($depth < 1) {
45+
return false;
46+
}
47+
48+
return json_encode($this->toArray($except, $depth, $stripSensitiveData), $options, $depth);
49+
}
2050
}

src/Traits/SerializableTrait.php

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,43 +61,47 @@ public function toArray(array $except = [], int $depth = 512)
6161

6262
$depth--;
6363
$modelData = [];
64-
array_push($except, 'rawModelDataInput', 'errorRegistry');
64+
array_push($except, 'rawModelDataInput', 'errorRegistry', 'customSerializer');
6565

66-
foreach (get_object_vars($this) as $key => $value) {
66+
foreach (get_class_vars(get_class($this)) as $key => $value) {
6767
if (in_array($key, $except)) {
6868
continue;
6969
}
7070

7171
if ($customSerializer = $this->getCustomSerializerMethod($key)) {
72-
$this->handleSerializedValue($modelData, $key, $this->{$customSerializer}());
72+
$this->handleSerializedValue($modelData, $key, $this->{$customSerializer}(), $depth, $except);
7373
continue;
7474
}
7575

76-
if (is_array($value)) {
77-
$subData = [];
78-
foreach ($value as $subKey => $element) {
79-
$subData[$subKey] = $this->evaluateAttribute($element, $depth, $except);
80-
}
81-
$modelData[$key] = $subData;
82-
} else {
83-
$modelData[$key] = $this->evaluateAttribute($value, $depth, $except);
84-
}
76+
$modelData[$key] = $this->serializeValue($this->$key, $depth, $except);
8577
}
8678

8779
return $modelData;
8880
}
8981

90-
private function handleSerializedValue(array &$data, $key, $serializedValue): void
82+
private function handleSerializedValue(array &$data, $key, $serializedValue, int $depth, array $except): void
9183
{
9284
if ($serializedValue instanceof SerializedValue &&
9385
$serializedValue->getSerializationStrategy() === SerializedValue::STRATEGY_MERGE_VALUE
9486
) {
95-
$data = array_merge($data, $serializedValue->getSerializedValue());
87+
$data = array_merge($data, $this->serializeValue($serializedValue->getSerializedValue(), $depth, $except));
9688

9789
return;
9890
}
9991

100-
$data[$key] = $serializedValue;
92+
$data[$key] = $this->serializeValue($serializedValue, $depth, $except);
93+
}
94+
95+
private function serializeValue($value, int $depth, array $except) {
96+
if (is_array($value)) {
97+
$subData = [];
98+
foreach ($value as $subKey => $element) {
99+
$subData[$subKey] = $this->evaluateAttribute($element, $depth, $except);
100+
}
101+
return $subData;
102+
}
103+
104+
return $this->evaluateAttribute($value, $depth, $except);
101105
}
102106

103107
private function evaluateAttribute($attribute, int $depth, array $except)
@@ -113,9 +117,9 @@ private function evaluateAttribute($attribute, int $depth, array $except)
113117
return (0 >= $depth)
114118
? null
115119
: (
116-
method_exists($attribute, 'toArray')
117-
? $attribute->toArray($except, $depth - 1)
118-
: get_object_vars($attribute)
120+
method_exists($attribute, 'toArray')
121+
? $attribute->toArray($except, $depth - 1)
122+
: get_object_vars($attribute)
119123
);
120124
}
121125

tests/Exception/ErrorRegistryExceptionTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,16 @@ public function testErrorRegistryExceptionCollectsMessages(): void
6565
'message' => "Value for test1 must not be larger than 2\nValue for test2 must not be larger than 2",
6666
];
6767

68-
$this->assertSame($expectedOutput, $errorRegistry->toArray(['file', 'line', 'code']));
69-
$this->assertSame(json_encode($expectedOutput), $errorRegistry->toJSON(['file', 'line', 'code']));
68+
$this->assertSame($expectedOutput, $errorRegistry->toArray(['code']));
69+
$this->assertSame(json_encode($expectedOutput), $errorRegistry->toJSON(['code']));
70+
71+
$serialized = $errorRegistry->toArray(['code'], 512, false);
72+
$this->assertArrayHasKey('file', $serialized);
73+
$this->assertArrayHasKey('line', $serialized);
74+
$this->assertArrayHasKey('file', $serialized['errors'][0]);
75+
$this->assertArrayHasKey('line', $serialized['errors'][0]);
76+
$this->assertArrayHasKey('file', json_decode($errorRegistry->toJSON(['code'], 0, 512, false), true));
77+
$this->assertArrayHasKey('line', json_decode($errorRegistry->toJSON(['code'], 0, 512, false), true));
7078

7179
throw $errorRegistry;
7280
}

0 commit comments

Comments
 (0)