fix: bound Symcon API responses below the kernel output limit

This commit is contained in:
jhartworks committed 2026-11-09 14:37:21 +01:00
1 parent eff9eb83d7
commit 804f2d0494
9 files changed
+470 -110

No files matched your search

+72
View File
@@ -0,0 +1,72 @@
<?php
declare(strict_types=1);
class IPSModuleStrict
{
protected int $InstanceID;
public function __construct(int $id) { $this->InstanceID = $id; }
}
function check(bool $condition, string $message): void { if (!$condition) throw new RuntimeException($message); }
require __DIR__ . '/../NextFloor/module.php';
$module = new NextFloor(random_int(1000000, 2000000));
$send = new ReflectionMethod($module, 'sendApiResult');
$read = new ReflectionMethod($module, 'readTransfer');
$release = new ReflectionMethod($module, 'releaseTransfer');
$directoryMethod = new ReflectionMethod($module, 'transferDirectory');
$directory = $directoryMethod->invoke($module);
$capture = static function (array $data, bool $chunked = true) use ($send, $module): string {
ob_start();
try { $send->invoke($module, $data, $chunked); return (string) ob_get_contents(); }
finally { ob_end_clean(); }
};
$fixture = ['variables' => [], 'objects' => []];
for ($index = 0; $index < 10000; $index++) {
$variable = ['entity_id' => 'sensor.' . $index, 'name' => 'Zähler Küche ÄÖÜ 🌞 ' . $index,
'path' => 'Haus / Küche / ' . str_repeat('Unterkategorie / ', 8) . $index, 'state' => '21.5', 'attributes' => ['unit_of_measurement' => '°C']];
$fixture['variables'][] = $variable;
$fixture['objects'][] = ['object_id' => $index + 1, 'parent_id' => 0, 'name' => $variable['name'], 'path' => $variable['path'], 'object_type' => 2];
}
$raw = json_encode($fixture, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
check(strlen($raw) > 1048576, 'Large fixture must exceed the reported kernel limit');
$small = $capture(['revision' => 1]);
check(json_decode($small, true) === ['revision' => 1], 'Small responses remain ordinary JSON');
$manifestJson = $capture($fixture);
$manifest = json_decode($manifestJson, true, 512, JSON_THROW_ON_ERROR)['_nextfloor_transfer'];
$file = $directory . DIRECTORY_SEPARATOR . $manifest['id'] . '.json';
try {
$assembled = '';
$largest = strlen($manifestJson);
for ($index = 0; $index < $manifest['chunks']; $index++) {
$chunk = $read->invoke($module, $manifest['id'], $index);
$wire = $capture($chunk);
$largest = max($largest, strlen($wire));
check(strlen($wire) <= 256 * 1024, 'Every wire response must stay safely below 1 MiB');
$decoded = json_decode($wire, true, 512, JSON_THROW_ON_ERROR);
$assembled .= base64_decode($decoded['data'], true);
}
check($assembled === $raw, 'All variables and objects must survive the transfer byte-for-byte');
check(strlen($assembled) === $manifest['bytes'], 'Manifest size must match the actual JSON bytes');
$release->invoke($module, $manifest['id']);
check(!is_file($file), 'Completed transfers must be removed');
foreach (['../escape', str_repeat('a', 31), str_repeat('z', 32)] as $badID) {
try { $read->invoke($module, $badID, 0); throw new LogicException('Invalid transfer ID was accepted'); }
catch (InvalidArgumentException $expected) {}
}
$expired = json_decode($capture($fixture), true)['_nextfloor_transfer'];
$expiredFile = $directory . DIRECTORY_SEPARATOR . $expired['id'] . '.json';
touch($expiredFile, time() - 180);
clearstatcache(true, $expiredFile);
try { $read->invoke($module, $expired['id'], 0); throw new LogicException('Expired transfer was accepted'); }
catch (RuntimeException $expected) { check(str_starts_with($expected->getMessage(), 'transfer_expired'), 'Report transfer expiry'); }
check(!is_file($expiredFile), 'Expired snapshots must be deleted');
$legacy = json_decode($capture($fixture, false), true);
check(http_response_code() === 409 && str_contains($legacy['error'], 'client_update_required'), 'A stale browser must get a small reload error, never an oversized response');
echo 'OK: 10000 variables and objects (' . strlen($raw) . ' bytes), largest response ' . $largest . " bytes; exact reassembly, expiry, cleanup and legacy-client guard\n";
} finally {
$release->invoke($module, $manifest['id']);
// Only this fixture's freshly-created directory, and only if empty (no recursive cleanup).
if ((glob($directory . DIRECTORY_SEPARATOR . '*') ?: []) === []) rmdir($directory);
}