mirror of
https://github.com/LamGC/wp-asciiplayer.git
synced 2026-11-26 20:55:48 +00:00
Bound recording loading, terminal allocation, markers and upload JSON complexity
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
/** Test-only decoder instrumentation, loaded before production calls. */
|
||||
|
||||
namespace LamGC\Asciiplayer {
|
||||
// Instrument the real decoder without adding a production test hook. A gate
|
||||
// failure must not build the rejected whole-document object graph.
|
||||
function json_decode( $json, $associative = null, $depth = 512, $flags = 0 ) {
|
||||
$GLOBALS['cast_decode_lengths'][] = strlen( $json );
|
||||
return \json_decode( $json, $associative, $depth, $flags );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
/** Resource-bound regressions; included after the existing upload contracts. */
|
||||
|
||||
|
||||
namespace {
|
||||
use function LamGC\Asciiplayer\is_cast_json_within_budget;
|
||||
use function LamGC\Asciiplayer\is_cast_v1;
|
||||
use const LamGC\Asciiplayer\CAST_MAX_JSON_CONTAINERS;
|
||||
use const LamGC\Asciiplayer\CAST_MAX_JSON_TOKENS;
|
||||
|
||||
function cast_budget_accepts( $text ) {
|
||||
$containers = 0;
|
||||
$tokens = 0;
|
||||
return is_cast_json_within_budget( $text, $containers, $tokens );
|
||||
}
|
||||
|
||||
foreach ( array(
|
||||
'zero v2 dimensions' => '{"version":2,"width":0,"height":0}',
|
||||
'zero v3 dimensions' => '{"version":3,"term":{"cols":0,"rows":0}}',
|
||||
'40,000 cells' => '{"version":2,"width":1000,"height":40}',
|
||||
'normal ignored lines' => $v2 . "\n\n# comment\nnot an event\n [broken ignored JSON\n[0,\"o\",\"works\"]\n",
|
||||
'1,000 markers' => $v2 . "\n" . str_repeat( "[0,\"m\",\"mark\"]\n", 1000 ),
|
||||
'40,000-cell resize' => $v2 . "\n[0,\"r\",\"200x200\"]\n",
|
||||
) as $description => $contents ) {
|
||||
same( true, cast_contents_are_valid( $contents ), 'Accept ' . $description . '.' );
|
||||
}
|
||||
foreach ( array(
|
||||
'cell-product overflow' => '{"version":2,"width":201,"height":200}',
|
||||
'zero width defaults before cell check' => '{"version":2,"width":0,"height":501}',
|
||||
'zero height defaults before cell check' => '{"version":2,"width":1000,"height":0}' . "\n[0,\"r\",\"1000x41\"]\n",
|
||||
'v3 cell-product overflow' => '{"version":3,"term":{"cols":201,"rows":200}}',
|
||||
'1,001 markers' => $v3 . "\n" . str_repeat( "[0,\"m\",\"mark\"]\n", 1001 ),
|
||||
'resize cell-product overflow' => $v2 . "\n[0,\"r\",\"201x200\"]\n",
|
||||
'zero resize has no header defaults' => $v3 . "\n[0,\"r\",\"80x0\"]\n",
|
||||
'ignored line count overflow' => $v2 . "\n" . str_repeat( "\n", 100001 ),
|
||||
'ignored line byte overflow' => $v2 . "\n#" . str_repeat( 'x', 262144 ),
|
||||
'missing dimensions' => '{"version":2}',
|
||||
'empty malformed event' => $v2 . "\n[]\n",
|
||||
) as $description => $contents ) {
|
||||
same( false, cast_contents_are_valid( $contents ), 'Reject ' . $description . '.' );
|
||||
}
|
||||
|
||||
$containers = 0;
|
||||
$tokens = 0;
|
||||
$escaped = json_encode( array( 'key"\\' => '[{}], : \\" braces', 'value' => true ) );
|
||||
same( true, is_cast_json_within_budget( " \n\t" . $escaped . "\r\n", $containers, $tokens ), 'Escapes and JSON whitespace are scanned.' );
|
||||
same( 1, $containers, 'String punctuation does not inflate containers.' );
|
||||
same( 4, $tokens, 'Keys and scalar values each consume a token.' );
|
||||
same( true, cast_budget_accepts( str_repeat( '[', 15 ) . '0' . str_repeat( ']', 15 ) ), 'Container nesting 15 fits decoder depth 16.' );
|
||||
same( false, cast_budget_accepts( str_repeat( '[', 16 ) . '0' . str_repeat( ']', 16 ) ), 'Reject depth before decoder allocation.' );
|
||||
same( true, cast_budget_accepts( '[' . str_repeat( '0,', CAST_MAX_JSON_TOKENS - 1 ) . '0]' ), 'Scalar token budget is inclusive.' );
|
||||
same( false, cast_budget_accepts( '[' . str_repeat( '0,', CAST_MAX_JSON_TOKENS ) . '0]' ), 'Reject excessive scalar tokens.' );
|
||||
same( true, cast_budget_accepts( '[' . str_repeat( '{},', CAST_MAX_JSON_CONTAINERS - 2 ) . '{}]' ), 'Container budget is inclusive.' );
|
||||
same( false, cast_budget_accepts( '[' . str_repeat( '{},', CAST_MAX_JSON_CONTAINERS - 1 ) . '{}]' ), 'Reject excessive empty containers.' );
|
||||
$containers = 0;
|
||||
$tokens = CAST_MAX_JSON_TOKENS - 2;
|
||||
same( true, is_cast_json_within_budget( '[0,"o"]', $containers, $tokens ), 'Shared record budget reaches its boundary.' );
|
||||
same( false, is_cast_json_within_budget( '[0]', $containers, $tokens ), 'Shared record budget cannot reset per event.' );
|
||||
|
||||
foreach ( array(
|
||||
'wide metadata scalars' => '[' . str_repeat( '0,', CAST_MAX_JSON_TOKENS ) . '0]',
|
||||
'wide metadata containers' => '[' . str_repeat( '{},', CAST_MAX_JSON_CONTAINERS ) . '{}]',
|
||||
'duplicate metadata keys' => '{' . str_repeat( '"x":0,', 200000 ) . '"x":0}',
|
||||
'deep metadata' => str_repeat( '[', 16 ) . '0' . str_repeat( ']', 16 ),
|
||||
) as $description => $metadata ) {
|
||||
$text = "{\n\"version\":1,\"width\":80,\"height\":24,\"stdout\":[],\"metadata\":" . $metadata . '}';
|
||||
$GLOBALS['cast_decode_lengths'] = array();
|
||||
same( false, is_cast_v1( $text ), 'Reject ' . $description . ' before decoding.' );
|
||||
same( array(), $GLOBALS['cast_decode_lengths'], 'No native decode runs for ' . $description . '.' );
|
||||
same( false, cast_contents_are_valid( $text ), 'Upload path rejects ' . $description . '.' );
|
||||
same( false, in_array( strlen( $text ), $GLOBALS['cast_decode_lengths'], true ), 'Upload path never decodes the full rejected document.' );
|
||||
}
|
||||
$prefix = '{"version":2,"width":80,"height":24,"metadata":[';
|
||||
same( true, cast_contents_are_valid( $prefix . str_repeat( '0,', 19992 ) . '0]}' ), 'JSONL header accepts exactly 20,000 scalar/key tokens.' );
|
||||
same( false, cast_contents_are_valid( $prefix . str_repeat( '0,', 19993 ) . '0]}' ), 'JSONL header rejects 20,001 scalar/key tokens.' );
|
||||
foreach ( array( 2, 3 ) as $version ) {
|
||||
$prefix = 2 === $version ? '{"version":2,"width":80,"height":24,"metadata":[' : '{"version":3,"term":{"cols":80,"rows":24},"metadata":[';
|
||||
$wide_header = $prefix . str_repeat( '0,', 20000 ) . '0]}';
|
||||
$GLOBALS['cast_decode_lengths'] = array();
|
||||
same( false, cast_contents_are_valid( $wide_header ), 'Wide JSONL header is rejected without v1 budget fallback.' );
|
||||
same( false, in_array( strlen( $wide_header ), $GLOBALS['cast_decode_lengths'], true ), 'Over-budget v2/v3 header never reaches full decoder.' );
|
||||
}
|
||||
foreach ( array(
|
||||
'escaped version key' => '{"ver\\u0073ion":1,"width":80,"height":24,"stdout":[]}',
|
||||
'duplicate version last wins' => '{"version":2,"version":1,"width":80,"height":24,"stdout":[]}',
|
||||
'nested version ignored' => '{"version":1,"meta":{"version":2},"width":80,"height":24,"stdout":[]}',
|
||||
'frame-rich first line' => '{"stdout":[' . str_repeat( '[0,""],', 10000 ) . '[0,""]],'. "\n" .'"version":1,"width":80,"height":24}',
|
||||
'title-rich first line' => '{"title":"' . str_repeat( 'x', 262144 ) . '",'. "\n" .'"version":1,"width":80,"height":24,"stdout":[]}',
|
||||
) as $description => $text ) {
|
||||
same( true, cast_contents_are_valid( $text ), 'Format discriminator preserves ' . $description . '.' );
|
||||
}
|
||||
$text = '{"version":1,"version":2,"width":80,"height":24,"stdout":[],"metadata":[' . str_repeat( '0,', 20000 ) . '0]}';
|
||||
$GLOBALS['cast_decode_lengths'] = array();
|
||||
same( false, cast_contents_are_valid( $text ), 'Last duplicate version cannot receive a legacy budget.' );
|
||||
same( false, in_array( strlen( $text ), $GLOBALS['cast_decode_lengths'], true ), 'Last duplicate version rejects before full decode.' );
|
||||
|
||||
$wide_event = '[0,"o",[' . str_repeat( '0,', 19999 ) . '0]]';
|
||||
$GLOBALS['cast_decode_lengths'] = array();
|
||||
same( false, cast_contents_are_valid( $v2 . "\n" . $wide_event ), 'Reject wide event data with the per-line gate.' );
|
||||
same( false, in_array( strlen( $wide_event ), $GLOBALS['cast_decode_lengths'], true ), 'Wide event never reaches the native decoder.' );
|
||||
$deep_header = '{"version":2,"width":80,"height":24,"metadata":' . str_repeat( '[', 14 ) . '0' . str_repeat( ']', 14 ) . '}';
|
||||
same( true, cast_contents_are_valid( $deep_header ), 'Decoder and lexical gate agree on depth boundary.' );
|
||||
|
||||
$GLOBALS['cast_decode_lengths'] = array();
|
||||
same( false, is_cast_v1( '{"version":1,broken}' ), 'Final decoder still rejects malformed JSON.' );
|
||||
same( true, in_array( strlen( '{"version":1,broken}' ), $GLOBALS['cast_decode_lengths'], true ), 'Within-budget grammar errors reach the real decoder.' );
|
||||
$text = '{"version":1,"width":0,"height":0,"stdout":[' . str_repeat( '[0,""],', 99999 ) . '[0,""]]}';
|
||||
same( true, cast_contents_are_valid( $text ), '100,000 compact v1 frames fit byte, structure and event budgets.' );
|
||||
$GLOBALS['cast_decode_lengths'] = array();
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
/** Run with PHP 7.4+: php tests/php/run.php */
|
||||
|
||||
require __DIR__ . '/cast-budget-decoder.php';
|
||||
require __DIR__ . '/bootstrap.php';
|
||||
|
||||
use function LamGC\Asciiplayer\enqueue_content_assets;
|
||||
@@ -179,6 +180,7 @@ try {
|
||||
same( array( 'lamgc-asciiplayer-style' ), $GLOBALS['ap_test']['style_print_calls'], 'Late/widget styles print once in the footer.' );
|
||||
|
||||
require __DIR__ . '/uploads.php';
|
||||
require __DIR__ . '/cast-budget.php';
|
||||
|
||||
echo "OK: {$assertions} assertions passed.\n";
|
||||
} catch ( Throwable $error ) {
|
||||
|
||||
+11
-11
@@ -56,6 +56,16 @@ $v2 = '{"version":2,"width":80,"height":24}';
|
||||
$v3 = '{"version":3,"term":{"cols":100,"rows":30}}';
|
||||
$v1 = array( 'version' => 1, 'width' => 80, 'height' => 24, 'duration' => 1.5, 'stdout' => array( array( 0.1, "Hello\r\n" ), array( 1.4, 'World' ) ) );
|
||||
foreach ( array(
|
||||
'zero width' => '{"version":2,"width":0,"height":24}',
|
||||
'event object' => $v2 . "\n{\"0\":0,\"1\":\"o\",\"2\":\"bad\"}\n",
|
||||
'trailing HTML' => $v2 . "\n[0,\"o\",\"ok\"]\n<script>alert(1)</script>\n",
|
||||
'trailing JSON object' => $v2 . "\n[0,\"o\",\"ok\"]\n{\"unrelated\":true}\n",
|
||||
'blank event line' => $v2 . "\n\n",
|
||||
'indented event ignored by bundled player' => $v2 . "\n [0,\"o\",\"invisible\"]\n",
|
||||
'v2 comment' => $v2 . "\n# comment\n",
|
||||
'v3 indented comment' => $v3 . "\n # not a comment\n",
|
||||
'compact v1' => json_encode( $v1 ),
|
||||
'large compact v1' => json_encode( array_merge( $v1, array( 'title' => str_repeat( 'a', CAST_MAX_LINE_BYTES + 1 ) ) ) ),
|
||||
'pretty v1' => json_encode( $v1, JSON_PRETTY_PRINT ),
|
||||
'v2 output' => $v2 . "\n[0.1,\"o\",\"Hello\\r\\n\"]\n",
|
||||
'v3 relative events' => $v3 . "\n[5,\"o\",\"Hello\"]\n[0.1,\"r\",\"120x40\"]\n[0,\"x\",\"0\"]\n",
|
||||
@@ -80,7 +90,6 @@ foreach ( array(
|
||||
'SVG' => '<svg xmlns="http://www.w3.org/2000/svg"><script>alert(1)</script></svg>',
|
||||
'unsupported version' => '{"version":4,"width":80,"height":24}',
|
||||
'string version' => '{"version":"2","width":80,"height":24}',
|
||||
'zero width' => '{"version":2,"width":0,"height":24}',
|
||||
'oversized width' => '{"version":2,"width":1001,"height":24}',
|
||||
'negative height' => '{"version":2,"width":80,"height":-1}',
|
||||
'fractional width' => '{"version":2,"width":80.5,"height":24}',
|
||||
@@ -89,8 +98,6 @@ foreach ( array(
|
||||
'v3 terminal array' => '{"version":3,"term":[100,30]}',
|
||||
'v1 absent stdout' => "{\n\"version\":1,\"width\":80,\"height\":24\n}",
|
||||
'v1 stdout object' => "{\n\"version\":1,\"width\":80,\"height\":24,\"stdout\":{\"0\":[0,\"x\"]}\n}",
|
||||
'compact v1 unsupported by bundled player' => json_encode( $v1 ),
|
||||
'large compact v1 unsupported by bundled player' => json_encode( array_merge( $v1, array( 'title' => str_repeat( 'a', CAST_MAX_LINE_BYTES + 1 ) ) ) ),
|
||||
'v1 negative delay' => json_encode( array_merge( $v1, array( 'stdout' => array( array( -1, 'x' ) ) ) ), JSON_PRETTY_PRINT ),
|
||||
'v1 malformed later frame' => json_encode( array_merge( $v1, array( 'stdout' => array( array( 0, 'ok' ), array( 0, array( 'x' ) ) ) ) ), JSON_PRETTY_PRINT ),
|
||||
'v1 extra frame element' => json_encode( array_merge( $v1, array( 'stdout' => array( array( 0, 'ok', 'extra' ) ) ) ), JSON_PRETTY_PRINT ),
|
||||
@@ -102,7 +109,6 @@ foreach ( array(
|
||||
'milliseconds overflow' => $v2 . "\n[1e308,\"o\",\"bad\"]\n",
|
||||
'v3 cumulative milliseconds overflow' => $v3 . "\n[1e305,\"o\",\"first\"]\n[1e305,\"o\",\"second\"]\n",
|
||||
'v2 out of order' => $v2 . "\n[2,\"o\",\"first\"]\n[1,\"o\",\"second\"]\n",
|
||||
'event object' => $v2 . "\n{\"0\":0,\"1\":\"o\",\"2\":\"bad\"}\n",
|
||||
'missing event data' => $v2 . "\n[0,\"o\"]\n",
|
||||
'extra event element' => $v2 . "\n[0,\"o\",\"bad\",1]\n",
|
||||
'event data array' => $v2 . "\n[0,\"o\",[\"bad\"]]\n",
|
||||
@@ -111,12 +117,6 @@ foreach ( array(
|
||||
'oversized resize' => $v2 . "\n[0,\"r\",\"2000x20\"]\n",
|
||||
'malformed resize' => $v2 . "\n[0,\"r\",\"80,24\"]\n",
|
||||
'bad later JSON' => $v2 . "\n[0,\"o\",\"ok\"]\n[1,\"o\",\"truncated\"\n",
|
||||
'trailing HTML' => $v2 . "\n[0,\"o\",\"ok\"]\n<script>alert(1)</script>\n",
|
||||
'trailing JSON object' => $v2 . "\n[0,\"o\",\"ok\"]\n{\"unrelated\":true}\n",
|
||||
'blank event line' => $v2 . "\n\n",
|
||||
'indented event ignored by bundled player' => $v2 . "\n [0,\"o\",\"invisible\"]\n",
|
||||
'v2 comment' => $v2 . "\n# comment\n",
|
||||
'v3 indented comment' => $v3 . "\n # not a comment\n",
|
||||
'UTF-8 BOM' => "\xef\xbb\xbf" . $v2,
|
||||
'invalid UTF-8' => $v2 . "\n[0,\"o\",\"\xff\"]\n",
|
||||
'oversized JSONL line' => $v2 . "\n[0,\"o\",\"" . str_repeat( 'a', CAST_MAX_LINE_BYTES ) . "\"]\n",
|
||||
@@ -166,7 +166,7 @@ try {
|
||||
same( true, is_string( validate_cast_upload( $upload )['error'] ), 'Upload permissions are enforced for valid content too.' );
|
||||
same( $denied, check_cast_filetype( $approved, $path, 'recording.cast' ), 'MIME override never grants upload capability.' );
|
||||
$GLOBALS['ap_test']['capabilities'] = array( 'upload_files' => true, 'unfiltered_upload' => true );
|
||||
file_put_contents( $path, $v2 . "\n<script>alert(1)</script>\n" );
|
||||
file_put_contents( $path, $v2 . "\n[invalid JSON\n" );
|
||||
clearstatcache( true, $path );
|
||||
same( true, is_string( validate_cast_upload( $upload )['error'] ), 'Malformed content fails even with unfiltered_upload.' );
|
||||
same( $denied, check_cast_filetype( $approved, $path, 'recording.cast' ), 'Invalid content cannot keep a previous MIME approval.' );
|
||||
|
||||
Reference in New Issue
Block a user