Skip to content

Commit 7eeacbf

Browse files
committed
Enhance type validation to support sensitive parameter redaction
1 parent 8160f85 commit 7eeacbf

16 files changed

Lines changed: 427 additions & 282 deletions

‎src/Internal/Checker/ParamChecker.php‎

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
use TypePHP\Internal\Validator\TypeValidatorRegistry;
3131

3232
/**
33+
* @phpstan-import-type FunctionContract from DocblockParser
34+
*
3335
* @internal Evaluates function and method parameter contract validations (including dynamic @method calls via __call / __callStatic).
3436
*/
3537
final class ParamChecker
@@ -102,23 +104,7 @@ private static function isUnconstrained(TypeNode $typeNode): bool
102104

103105
/**
104106
* @param array<string, mixed> $vars
105-
* @param array{
106-
* types: array<string, TypeNode>,
107-
* templates: array<string, TemplateTagValueNode>,
108-
* classTemplates: array<string, TemplateTagValueNode>,
109-
* return: ?TypeNode,
110-
* aliases: array<string, TypeNode>,
111-
* hasParamContract: bool,
112-
* hasReturnContract: bool,
113-
* paramsUseGenerics: bool,
114-
* returnUsesGenerics: bool,
115-
* returnUsesMethodTemplates: bool,
116-
* returnIsThis: bool,
117-
* returnIsDynamic: bool,
118-
* allParamsUnconstrained: bool,
119-
* returnUnconstrained: bool,
120-
* isSimple: bool
121-
* }|null $contract Pre-resolved contract to avoid re-parsing
107+
* @param FunctionContract|null $contract Pre-resolved contract to avoid re-parsing
122108
*/
123109
public static function checkParams(
124110
string $function,
@@ -170,9 +156,10 @@ public static function checkParams(
170156
$classTemplates = $contract['classTemplates'] ?? [];
171157
$aliases = $contract['aliases'];
172158
$hasMethodTemplates = (\count($methodTemplates) > 0);
159+
$sensitiveParams = $contract['sensitiveParams'] ?? [];
173160

174161
if (! $paramsUseGenerics && ! $hasMethodTemplates && \count($aliases) === 0) {
175-
return self::validateSimpleParams($contract['types'], $vars, $effectiveFunction, $registry);
162+
return self::validateSimpleParams($contract['types'], $vars, $effectiveFunction, $registry, $sensitiveParams);
176163
}
177164

178165
self::prepareGenericBindings($effectiveFunction, $methodTemplates, $thisObj, $classTemplates, $thisOrClass);
@@ -203,7 +190,8 @@ public static function checkParams(
203190
$declaredTemplates,
204191
$registry,
205192
$classTemplates,
206-
$thisOrClass
193+
$thisOrClass,
194+
$sensitiveParams
207195
);
208196
}
209197

@@ -212,12 +200,14 @@ public static function checkParams(
212200
*
213201
* @param array<string, TypeNode> $types
214202
* @param array<string, mixed> $vars
203+
* @param array<string, bool> $sensitiveParams
215204
*/
216205
private static function validateSimpleParams(
217206
array $types,
218207
array $vars,
219208
string $effectiveFunction,
220-
TypeValidatorRegistry $registry
209+
TypeValidatorRegistry $registry,
210+
array $sensitiveParams = []
221211
): ?ErrorMessage {
222212
foreach ($types as $paramName => $typeNode) {
223213
if (isset($vars[$paramName]) || \array_key_exists($paramName, $vars)) {
@@ -231,7 +221,9 @@ private static function validateSimpleParams(
231221
if (self::isUnconstrained($typeNode)) {
232222
continue;
233223
}
234-
$err = $registry->validate($vars[$paramName], $typeNode, '');
224+
225+
$isSensitive = $sensitiveParams[$paramName] ?? false;
226+
$err = $registry->validate($vars[$paramName], $typeNode, '', $isSensitive);
235227
if ($err !== null) {
236228
return ErrorFactory::createError($effectiveFunction . '(): Argument $' . $paramName . $err->getMessage());
237229
}
@@ -310,6 +302,7 @@ private static function resolveBaseTypes(
310302
* @param array<string, TypeNode> $boundTemplates
311303
* @param array<string, TemplateTagValueNode> $declaredTemplates
312304
* @param array<string, TemplateTagValueNode> $classTemplates
305+
* @param array<string, bool> $sensitiveParams
313306
*/
314307
private static function validateAllParameters(
315308
array $contractTypes,
@@ -323,7 +316,8 @@ private static function validateAllParameters(
323316
array $declaredTemplates,
324317
TypeValidatorRegistry $registry,
325318
array $classTemplates,
326-
object|string|null $thisOrClass = null
319+
object|string|null $thisOrClass = null,
320+
array $sensitiveParams = []
327321
): ?ErrorMessage {
328322
foreach ($contractTypes as $paramName => $_) {
329323
if (! isset($vars[$paramName]) && ! \array_key_exists($paramName, $vars)) {
@@ -334,6 +328,8 @@ private static function validateAllParameters(
334328
? TemplateManager::getBoundTemplates($effectiveFunction, $thisOrClass, $allTemplates)
335329
: $boundTemplates;
336330

331+
$isSensitive = $sensitiveParams[$paramName] ?? false;
332+
337333
$err = self::validateSingleParam(
338334
$paramName,
339335
$baseTypes[$paramName],
@@ -347,7 +343,8 @@ private static function validateAllParameters(
347343
$registry,
348344
$classTemplates,
349345
$vars,
350-
$thisOrClass
346+
$thisOrClass,
347+
$isSensitive
351348
);
352349

353350
if ($err !== null) {
@@ -911,7 +908,8 @@ private static function validateSingleParam(
911908
TypeValidatorRegistry $registry,
912909
array $classTemplates = [],
913910
array $vars = [],
914-
object|string|null $thisOrClass = null
911+
object|string|null $thisOrClass = null,
912+
bool $isSensitive = false
915913
): ?ErrorMessage {
916914
if (
917915
$typeNode instanceof ConditionalTypeForParameterNode ||
@@ -941,14 +939,14 @@ private static function validateSingleParam(
941939
}
942940

943941
if ($typeNode instanceof GenericTypeNode && self::isClassStringTemplate($typeNode, $templates)) {
944-
return self::resolveClassStringTemplate($typeNode, $val, $paramName, $effectiveFunction, $thisObj, $templates, $classTemplates);
942+
return self::resolveClassStringTemplate($typeNode, $val, $paramName, $effectiveFunction, $thisObj, $templates, $classTemplates, $isSensitive);
945943
}
946944

947945
if (self::getTemplateName($typeNode, $templates) !== null) {
948946
return self::resolveTemplateParam($typeNode, $val, $paramName, $effectiveFunction, $thisObj, $templates, $registry, $classTemplates, $thisOrClass);
949947
}
950948

951-
return $registry->validate($val, $typeNode, $effectiveFunction . '(): Argument $' . $paramName);
949+
return $registry->validate($val, $typeNode, $effectiveFunction . '(): Argument $' . $paramName, $isSensitive);
952950
}
953951

954952
/**
@@ -1078,7 +1076,8 @@ private static function resolveClassStringTemplate(
10781076
string $function,
10791077
?object $thisObj,
10801078
array $templates,
1081-
array $classTemplates = []
1079+
array $classTemplates = [],
1080+
bool $isSensitive = false
10821081
): ?ErrorMessage {
10831082
/** @var IdentifierTypeNode $innerType */
10841083
$innerType = $typeNode->genericTypes[0];
@@ -1089,7 +1088,7 @@ private static function resolveClassStringTemplate(
10891088

10901089
if (! TemplateManager::isBound($function, $targetObj, $templateName)) {
10911090
if (! \is_string($val) || ! ClassNameValidator::isValidClassString($val)) {
1092-
return ErrorFactory::createError($function . '(): Argument $' . $paramName . ' must be a valid class-string, ' . TypeFormatter::formatGivenValue($val) . ' given');
1091+
return ErrorFactory::createError($function . '(): Argument $' . $paramName . ' must be a valid class-string, ' . TypeFormatter::formatGivenValue($val, $isSensitive) . ' given');
10931092
}
10941093

10951094
if ($templateNode->bound !== null) {
@@ -1098,8 +1097,9 @@ private static function resolveClassStringTemplate(
10981097
$resolvedBound = SpecialTypeResolver::resolve($templateNode->bound, $function, $thisObj);
10991098
if (! self::checkClassStringSatisfiesBound($val, $resolvedBound)) {
11001099
$boundDisplay = (string) $resolvedBound;
1100+
$displayVal = $isSensitive ? 'string given' : "'" . $val . "' given";
11011101

1102-
return ErrorFactory::createError($function . '(): Argument $' . $paramName . ' (class-string<' . $templateName . '>) must be a class-string of ' . $boundDisplay . ", '" . $val . "' given");
1102+
return ErrorFactory::createError($function . '(): Argument $' . $paramName . ' (class-string<' . $templateName . '>) must be a class-string of ' . $boundDisplay . ', ' . $displayVal);
11031103
}
11041104
}
11051105
}
@@ -1110,7 +1110,7 @@ private static function resolveClassStringTemplate(
11101110
$expectedTypeNode = TemplateManager::getBoundType($function, $targetObj, $templateName);
11111111
if ($expectedTypeNode !== null) {
11121112
if (! \is_string($val) || ! self::checkClassStringSatisfiesBound($val, $expectedTypeNode)) {
1113-
$valStr = TypeFormatter::formatGivenValue($val);
1113+
$valStr = TypeFormatter::formatGivenValue($val, $isSensitive);
11141114
$targetDisplay = (string) $expectedTypeNode;
11151115

11161116
return ErrorFactory::createError($function . '(): Argument $' . $paramName . ' must be a class-string of ' . $targetDisplay . ', ' . $valStr . ' given');

‎src/Internal/Checker/ReturnChecker.php‎

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
use TypePHP\Internal\Wrapper\CallableWrapper;
2222

2323
/**
24+
* @phpstan-import-type FunctionContract from DocblockParser
25+
*
2426
* @internal Evaluates function and method return contract validations (including dynamic @method calls via __call / __callStatic).
2527
*/
2628
final class ReturnChecker
@@ -81,23 +83,7 @@ public static function isReturnUnconstrained(string $effectiveFunction): bool
8183

8284
/**
8385
* @param array<string, mixed> $vars
84-
* @param array{
85-
* types: array<string, TypeNode>,
86-
* templates: array<string, TemplateTagValueNode>,
87-
* classTemplates: array<string, TemplateTagValueNode>,
88-
* return: ?TypeNode,
89-
* aliases: array<string, TypeNode>,
90-
* hasParamContract: bool,
91-
* hasReturnContract: bool,
92-
* paramsUseGenerics: bool,
93-
* returnUsesGenerics: bool,
94-
* returnUsesMethodTemplates: bool,
95-
* returnIsThis: bool,
96-
* returnIsDynamic: bool,
97-
* allParamsUnconstrained: bool,
98-
* returnUnconstrained: bool,
99-
* isSimple: bool
100-
* }|null $contract Pre-resolved contract to avoid re-parsing
86+
* @param FunctionContract|null $contract Pre-resolved contract to avoid re-parsing
10187
*/
10288
public static function checkReturn(
10389
string $function,
@@ -145,7 +131,6 @@ public static function checkReturn(
145131
return $value;
146132
}
147133

148-
// Use pre-resolved contract or parse
149134
$contract ??= DocblockParser::parse($effectiveFunction);
150135

151136
if (! ($contract['hasReturnContract'] ?? ($contract['return'] !== null))) {
@@ -236,18 +221,7 @@ private static function handleMagicReturn(
236221
* @param array<int|string, mixed> $vars
237222
* @param array<string, TypeNode> $aliases
238223
* @param array<string, TemplateTagValueNode> $templates
239-
* @param array{
240-
* return?: TypeNode|null,
241-
* hasReturnContract?: bool,
242-
* returnIsThis?: bool,
243-
* returnIsDynamic?: bool,
244-
* aliases?: array<string, TypeNode>,
245-
* templates?: array<string, TemplateTagValueNode>,
246-
* classTemplates?: array<string, TemplateTagValueNode>,
247-
* allParamsUnconstrained?: bool,
248-
* returnUnconstrained?: bool,
249-
* isSimple?: bool
250-
* } $contract
224+
* @param FunctionContract|array{} $contract
251225
*/
252226
private static function evaluateReturn(
253227
TypeNode $returnTypeNode,
@@ -348,7 +322,6 @@ private static function evaluateReturn(
348322
}
349323
}
350324

351-
// Fast-path: if return type is mixed or array, skip validation.
352325
if ($resolvedType instanceof IdentifierTypeNode) {
353326
$lower = strtolower($resolvedType->name);
354327
if ($lower === 'mixed' || $lower === 'array') {

‎src/Internal/Diagnostic/TypeFormatter.php‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@
99
*/
1010
final class TypeFormatter
1111
{
12-
public static function formatGivenValue(mixed $value): string
12+
public static function formatGivenValue(mixed $value, bool $isSensitive = false): string
1313
{
14+
if ($isSensitive) {
15+
return get_debug_type($value);
16+
}
17+
1418
if (\is_int($value)) {
1519
if ($value < 0) {
1620
return "negative int ($value)";

0 commit comments

Comments
 (0)