Skip to content

Commit 067f5cf

Browse files
authored
Enhance robustness of data scrubbing with improved error handling (#60)
* Simplify replacement value logic in ScrubberService::autoSanitize using a ternary operator for better readability. * Add try-catch in ScrubberService::autoSanitize to skip invalid regex patterns, preventing loop breaks. * Update ScrubberService::patternChecker to safely update content only with non-null results from checkAndSanitize. * Standardize nested structure handling in ProcessArrayTrait::processArrayRecursively to use recursive processing consistently. * Wrap string sanitization in ProcessArrayTrait::processArrayRecursively with try-catch to skip problematic values. * Enhance ProcessArrayTrait::processArray with comprehensive fallbacks for JSON encoding failures, sanitization exceptions, non-string content, and invalid JSON decoding, ensuring an array is always returned. These changes are attempting to resolve the type exception reported in issue #50
1 parent 7dea161 commit 067f5cf

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

src/Services/ScrubberService.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,25 @@ public static function autoSanitize(string &$jsonContent): void
4949
? Secret::decrypt($regexClass->getPattern())
5050
: $regexClass->getPattern();
5151

52-
if (method_exists($regexClass, 'getReplacementValue')) {
53-
// Check if getReplacementValue() exists on the regex class and if it does, use it.
54-
$replace = $regexClass->getReplacementValue();
55-
} else {
56-
// Otherwise, use the default replacement pattern.
57-
$replace = config('scrubber.redaction');
58-
}
52+
$replace = method_exists($regexClass, 'getReplacementValue')
53+
? $regexClass->getReplacementValue()
54+
: config('scrubber.redaction');
5955

60-
self::patternChecker($pattern, $jsonContent, $replace);
56+
try {
57+
self::patternChecker($pattern, $jsonContent, $replace);
58+
} catch (\Exception $e) {
59+
// Skip this regex $pattern to prevent breaking the autoSanitizer loop.
60+
}
6161
});
6262
}
6363

6464
protected static function patternChecker(string $regexPattern, string &$jsonContent, string $replace): void
6565
{
6666
$hits = 0;
67-
$jsonContent = RegexRepository::checkAndSanitize($regexPattern, $replace, $jsonContent, $hits);
67+
$result = RegexRepository::checkAndSanitize($regexPattern, $replace, $jsonContent, $hits);
68+
if (! is_null($result)) {
69+
$jsonContent = $result;
70+
}
6871

6972
/**
7073
* @todo

src/Strategies/ContentProcessingStrategy/Traits/ProcessArrayTrait.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ public function processArrayRecursively(array $content): array
1111
foreach ($content as $key => $value) {
1212
if ($value !== null) {
1313
if (is_array($value)) {
14-
$content[$key] = $this->processArray($value);
14+
$content[$key] = $this->processArrayRecursively($value);
1515
} elseif (is_object($value) && ! method_exists($value, '__toString')) {
16-
$content[$key] = $this->processArray((array) $value);
16+
$content[$key] = $this->processArrayRecursively((array) $value);
1717
} else {
1818
$value = (string) $value;
19-
20-
ScrubberService::autoSanitize($value);
21-
19+
try {
20+
ScrubberService::autoSanitize($value);
21+
} catch (\Exception $e) {
22+
// Skip sanitization for this value to prevent breaking the array
23+
}
2224
$content[$key] = $value;
2325
}
2426
}
@@ -35,8 +37,22 @@ public function processArray(array $content): array
3537
return $this->processArrayRecursively($content);
3638
}
3739

38-
ScrubberService::autoSanitize($jsonContent);
40+
try {
41+
ScrubberService::autoSanitize($jsonContent);
42+
43+
} catch (\Exception $e) {
44+
return $this->processArrayRecursively($content);
45+
}
46+
47+
if (! is_string($jsonContent)) {
48+
return $this->processArrayRecursively($content);
49+
}
50+
51+
$decoded = ScrubberService::decodeRecord($jsonContent);
52+
if ($decoded === null) {
53+
return $this->processArrayRecursively($content);
54+
}
3955

40-
return ScrubberService::decodeRecord($jsonContent);
56+
return $decoded;
4157
}
4258
}

0 commit comments

Comments
 (0)