Skip to content

Commit 8dd0ba7

Browse files
naxvogclaude
andcommitted
Keep audit report export working when a violation carries invalid UTF-8
TypeFormatter previews string values with a byte-based substr(), which can split a multibyte character. json_encode() then fails on the malformed UTF-8 and exportReport()/writeWorkerShard() drop the whole report without any signal. Encode with JSON_INVALID_UTF8_SUBSTITUTE so the offending bytes become U+FFFD and the report is still written. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
1 parent 77920b2 commit 8dd0ba7

2 files changed

Lines changed: 51 additions & 2 deletions

File tree

‎src/Internal/Reporting/ViolationCollector.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ public static function writeWorkerShard(string $reportFile): void
267267
'violations' => array_values(self::$violations),
268268
'instrumented_files' => StreamWrapper::getInstrumentedFiles(),
269269
];
270-
$content = json_encode($payload, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
270+
$content = json_encode($payload, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_SUBSTITUTE);
271271

272272
if ($content !== false) {
273273
@file_put_contents($shardFile, $content);
@@ -294,7 +294,7 @@ public static function exportReport(?string $filePath = null): ?string
294294
}
295295

296296
$document = self::buildReportDocument($allViolations);
297-
$json = json_encode($document, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
297+
$json = json_encode($document, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_SUBSTITUTE);
298298

299299
if ($json === false || @file_put_contents($targetFile, $json) === false) {
300300
return null;

‎tests/Internal/Reporting/ViolationCollectorTest.php‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace TypePHP\Tests\Internal\Reporting;
66

77
use TypePHP\Internal\Diagnostic\ErrorMessage;
8+
use TypePHP\Internal\Diagnostic\TypeFormatter;
89
use TypePHP\Internal\Reporting\ViolationCollector;
910
use TypePHP\Internal\Reporting\ViolationRecord;
1011
use TypePHP\Internal\Util\Config;
@@ -491,6 +492,54 @@ function: 'test',
491492
@rmdir($tempDir);
492493
}
493494
});
495+
496+
test('exports report and worker shard when a violation carries invalid UTF-8', function () {
497+
$tempDir = sys_get_temp_dir() . '/typephp_utf8_test_' . uniqid();
498+
mkdir($tempDir, 0777, true);
499+
$reportFile = $tempDir . '/typephp-report.json';
500+
501+
// 16 ASCII bytes followed by 2-byte characters: the 17-byte preview cut splits the first "ä".
502+
$given = TypeFormatter::formatGivenValue(str_repeat('a', 16) . 'ääääää');
503+
expect(preg_match('//u', $given))->toBeFalse();
504+
505+
putenv('TEST_TOKEN=7');
506+
507+
try {
508+
Config::set([
509+
'on_violation' => 'report',
510+
'report_file' => $reportFile,
511+
]);
512+
513+
$err = new ErrorMessage("Argument \$name must be of type int, {$given} given");
514+
ViolationCollector::handle($err, 'parameter', null, 'src/Action.php', 10);
515+
516+
expect(ViolationCollector::exportReport($reportFile))->toBe($reportFile);
517+
518+
$doc = json_decode((string) file_get_contents($reportFile), true);
519+
expect($doc['summary']['total_violations'])->toBe(1)
520+
->and($doc['violations'][0]['message'])->toContain('aaaaaaaaaaaaaaaa')
521+
;
522+
523+
ViolationCollector::writeWorkerShard($reportFile);
524+
525+
$shards = glob($tempDir . '/.typephp-shards/shard_7_*.json');
526+
expect($shards)->toHaveCount(1);
527+
528+
$shard = json_decode((string) file_get_contents($shards[0]), true);
529+
expect($shard['violations'])->toHaveCount(1);
530+
} finally {
531+
putenv('TEST_TOKEN');
532+
$files = glob($tempDir . '/.typephp-shards/*');
533+
if ($files !== false) {
534+
foreach ($files as $f) {
535+
@unlink($f);
536+
}
537+
}
538+
@rmdir($tempDir . '/.typephp-shards');
539+
@unlink($reportFile);
540+
@rmdir($tempDir);
541+
}
542+
});
494543
});
495544

496545
describe('Metadata Parser', function () {

0 commit comments

Comments
 (0)