mirror of
https://github.com/LamGC/wp-asciiplayer.git
synced 2026-11-26 20:55:48 +00:00
fix: bound recording resources and add security regressions (#41)
Build and WordPress integration / Build and publish pristine package (push) Canceled after 0s
Build and WordPress integration / PHP 8.3 syntax and regression tests (push) Canceled after 0s
Build and WordPress integration / PHP 7.4 syntax and regression tests (push) Canceled after 0s
Build and WordPress integration / WordPress 6.3 / PHP 7.4 (push) Canceled after 0s
Build and WordPress integration / WordPress latest / PHP 8.2 (push) Canceled after 0s
Build and WordPress integration / Plugin Check (advisory) (push) Canceled after 0s
Build and WordPress integration / Build and publish pristine package (push) Canceled after 0s
Build and WordPress integration / PHP 8.3 syntax and regression tests (push) Canceled after 0s
Build and WordPress integration / PHP 7.4 syntax and regression tests (push) Canceled after 0s
Build and WordPress integration / WordPress 6.3 / PHP 7.4 (push) Canceled after 0s
Build and WordPress integration / WordPress latest / PHP 8.2 (push) Canceled after 0s
Build and WordPress integration / Plugin Check (advisory) (push) Canceled after 0s
- Bound recording bytes, JSON structure, events, markers, terminal dimensions and timestamp arithmetic before unsafe player allocation or scheduling. - Share validated playback data and metadata; cancel stale, failed and timed-out loads. - Add PHP 7.4-compatible predecode budgets and preserve supported v1/v2/v3 behavior. - Add localized errors, documented limits and 110 official/seeded recording fixtures. Verified 325 Node tests, native PHP 7.4/8.3 regression suites and 55 Chromium tests on each WordPress CI matrix. Preserve plugin version, vendor files and isolated package artifacts.
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
<?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,155 @@
|
||||
<?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 function LamGC\Asciiplayer\is_cast_time;
|
||||
use const LamGC\Asciiplayer\CAST_MAX_JSON_CONTAINERS;
|
||||
use const LamGC\Asciiplayer\CAST_MAX_JSON_TOKENS;
|
||||
use const LamGC\Asciiplayer\CAST_MAX_TIME_MS;
|
||||
|
||||
function cast_budget_accepts( $text ) {
|
||||
$containers = 0;
|
||||
$tokens = 0;
|
||||
return is_cast_json_within_budget( $text, $containers, $tokens );
|
||||
}
|
||||
|
||||
function cast_time_contents( $version, $intervals, $metadata = array() ) {
|
||||
$header = 3 === $version ? array( 'version' => 3, 'term' => array( 'cols' => 80, 'rows' => 24 ) ) : array( 'version' => $version, 'width' => 80, 'height' => 24 );
|
||||
$header = array_merge( $header, $metadata );
|
||||
$frames = array();
|
||||
foreach ( $intervals as $time ) {
|
||||
$frames[] = 1 === $version ? array( $time, 'x' ) : array( $time, 'o', 'x' );
|
||||
}
|
||||
if ( 1 === $version ) {
|
||||
$header['stdout'] = $frames;
|
||||
return json_encode( $header );
|
||||
}
|
||||
return json_encode( $header ) . "\n" . implode( "\n", array_map( 'json_encode', $frames ) );
|
||||
}
|
||||
|
||||
same( 2147483647, CAST_MAX_TIME_MS, 'The timeline ceiling matches the signed browser timer range.' );
|
||||
$max_seconds = CAST_MAX_TIME_MS / 1000;
|
||||
foreach ( array( 1, 2, 3 ) as $version ) {
|
||||
same( true, cast_contents_are_valid( cast_time_contents( $version, array( $max_seconds ) ) ), 'Accept the exact v' . $version . ' timer horizon.' );
|
||||
same( false, cast_contents_are_valid( cast_time_contents( $version, array( $max_seconds + 0.001 ) ) ), 'Reject v' . $version . ' one millisecond beyond the horizon.' );
|
||||
same( false, cast_contents_are_valid( cast_time_contents( $version, array( -0.001 ) ) ), 'Reject negative v' . $version . ' time.' );
|
||||
foreach ( array( 'duration', 'idle_time_limit' ) as $key ) {
|
||||
same( true, cast_contents_are_valid( cast_time_contents( $version, array( 0 ), array( $key => $max_seconds ) ) ), 'Accept exact v' . $version . ' ' . $key . ' horizon.' );
|
||||
foreach ( array( $max_seconds + 0.001, -0.001, '1', null ) as $value ) {
|
||||
same( false, cast_contents_are_valid( cast_time_contents( $version, array( 0 ), array( $key => $value ) ) ), 'Reject invalid v' . $version . ' ' . $key . '.' );
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach ( array( INF, -INF, NAN, -1, '1', null ) as $value ) {
|
||||
same( false, is_cast_time( $value ), 'Time validation rejects nonfinite, negative, and nonnumeric values.' );
|
||||
}
|
||||
$rounding_intervals = array( 307699.2378567164, 1839784.4091432837 );
|
||||
same( (float) CAST_MAX_TIME_MS, ( $rounding_intervals[0] + $rounding_intervals[1] ) * 1000, 'Seconds-first rounding hides the over-limit result.' );
|
||||
same( true, $rounding_intervals[0] * 1000 + $rounding_intervals[1] * 1000 > CAST_MAX_TIME_MS, 'The actual player millisecond sum is over the horizon.' );
|
||||
foreach ( array( 1, 3 ) as $version ) {
|
||||
same( true, cast_contents_are_valid( cast_time_contents( $version, array( 1, $max_seconds - 1 ) ) ), 'Accept cumulative v' . $version . ' at the horizon.' );
|
||||
foreach ( array( array( 1, $max_seconds ), $rounding_intervals, array( 2.996155224770526e303, 1.7677315826146105e305 ) ) as $intervals ) {
|
||||
same( false, cast_contents_are_valid( cast_time_contents( $version, $intervals ) ), 'Reject v' . $version . ' cumulative upstream millisecond overflow.' );
|
||||
}
|
||||
}
|
||||
same( false, cast_contents_are_valid( cast_time_contents( 2, array( 1e20 ) ) ), 'Reject a tiny v2 cast whose output batching cannot make progress.' );
|
||||
same( false, cast_contents_are_valid( cast_time_contents( 2, array( 10004.674, 10004.673999999999 ) ) ), 'Reject backward v2 seconds even when conversion rounds to equal milliseconds.' );
|
||||
same( true, cast_contents_are_valid( cast_time_contents( 2, array( 1, 1 ) ) ), 'Equal v2 timestamps remain accepted.' );
|
||||
|
||||
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,8 @@ 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';
|
||||
require __DIR__ . '/security-fixtures.php';
|
||||
|
||||
echo "OK: {$assertions} assertions passed.\n";
|
||||
} catch ( Throwable $error ) {
|
||||
|
||||
@@ -0,0 +1,236 @@
|
||||
<?php
|
||||
/**
|
||||
* Run the shared, deterministic security corpus through the real upload gates.
|
||||
*
|
||||
* The PHP-only CI job needs no Node installation or pre-generated large files.
|
||||
* Expand one manifest recipe to one temporary file, in at most 64 KiB chunks,
|
||||
* verify its bytes/hash, exercise each gate, and delete it before the next case.
|
||||
*/
|
||||
|
||||
use function LamGC\Asciiplayer\is_valid_cast_file;
|
||||
use const LamGC\Asciiplayer\CAST_MAX_BYTES;
|
||||
|
||||
/** Write a bounded chunk completely, including on a short filesystem write. */
|
||||
function security_fixture_write_chunk( $handle, $chunk, $hash, &$bytes ) {
|
||||
$length = strlen( $chunk );
|
||||
if ( $bytes + $length > CAST_MAX_BYTES + 1 ) {
|
||||
throw new RuntimeException( 'A security fixture exceeded the 32 MiB + 1 byte generation cap.' );
|
||||
}
|
||||
$offset = 0;
|
||||
while ( $offset < $length ) {
|
||||
$written = fwrite( $handle, substr( $chunk, $offset ) );
|
||||
if ( false === $written || 0 === $written ) {
|
||||
throw new RuntimeException( 'Could not write a security fixture.' );
|
||||
}
|
||||
$offset += $written;
|
||||
}
|
||||
hash_update( $hash, $chunk );
|
||||
$bytes += $length;
|
||||
}
|
||||
|
||||
/** Expand only literal/repeat recipes; never interpret executable generators. */
|
||||
function security_fixture_materialize( $fixture, $root, $path ) {
|
||||
$handle = fopen( $path, 'wb' );
|
||||
if ( false === $handle ) {
|
||||
throw new RuntimeException( 'Could not open the security fixture temporary file.' );
|
||||
}
|
||||
$hash = hash_init( 'sha256' );
|
||||
$bytes = 0;
|
||||
try {
|
||||
$source = $fixture['source'];
|
||||
if ( isset( $source['file'] ) ) {
|
||||
$file = realpath( $root . '/' . $source['file'] );
|
||||
if ( false === $file || 0 !== strpos( $file, $root . DIRECTORY_SEPARATOR ) || ! is_file( $file ) ) {
|
||||
throw new RuntimeException( 'Security fixture files must stay inside their corpus directory.' );
|
||||
}
|
||||
$input = fopen( $file, 'rb' );
|
||||
if ( false === $input ) {
|
||||
throw new RuntimeException( 'Could not open a checked-in security fixture.' );
|
||||
}
|
||||
try {
|
||||
while ( ! feof( $input ) ) {
|
||||
$chunk = fread( $input, 65536 );
|
||||
if ( false === $chunk || ( '' === $chunk && ! feof( $input ) ) ) {
|
||||
throw new RuntimeException( 'Could not read a checked-in security fixture.' );
|
||||
}
|
||||
security_fixture_write_chunk( $handle, $chunk, $hash, $bytes );
|
||||
}
|
||||
} finally {
|
||||
fclose( $input );
|
||||
}
|
||||
} else {
|
||||
foreach ( $source['segments'] as $segment ) {
|
||||
$text = $segment['text'];
|
||||
$remaining = $segment['repeat'];
|
||||
if ( ! is_string( $text ) || '' === $text || ! is_int( $remaining ) || $remaining < 1 ) {
|
||||
throw new RuntimeException( 'Security recipes require nonempty text and a positive integer repeat.' );
|
||||
}
|
||||
$length = strlen( $text );
|
||||
if ( $length <= 65536 ) {
|
||||
$batch = min( $remaining, intdiv( 65536, $length ) );
|
||||
$chunk = str_repeat( $text, $batch );
|
||||
while ( $remaining >= $batch ) {
|
||||
security_fixture_write_chunk( $handle, $chunk, $hash, $bytes );
|
||||
$remaining -= $batch;
|
||||
}
|
||||
if ( $remaining > 0 ) {
|
||||
security_fixture_write_chunk( $handle, str_repeat( $text, $remaining ), $hash, $bytes );
|
||||
}
|
||||
} else {
|
||||
while ( $remaining-- > 0 ) {
|
||||
for ( $offset = 0; $offset < $length; $offset += 65536 ) {
|
||||
security_fixture_write_chunk( $handle, substr( $text, $offset, 65536 ), $hash, $bytes );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
fclose( $handle );
|
||||
}
|
||||
clearstatcache( true, $path );
|
||||
same( $fixture['bytes'], $bytes, $fixture['id'] . ': generated byte count matches the shared manifest.' );
|
||||
same( $bytes, filesize( $path ), $fixture['id'] . ': the actual upload file has those bytes.' );
|
||||
same( $fixture['sha256'], hash_final( $hash ), $fixture['id'] . ': generated bytes match the shared SHA-256.' );
|
||||
}
|
||||
|
||||
/** Verify ordering at the real decoder, not merely a generic false result. */
|
||||
function security_fixture_assert_decodes( $fixture, $label ) {
|
||||
$lengths = $GLOBALS['cast_decode_lengths'];
|
||||
if ( $fixture['expected']['accepted'] ) {
|
||||
same( true, count( $lengths ) > 0, $label . ': a valid control actually reaches the native JSON decoder.' );
|
||||
}
|
||||
$threshold = $fixture['expected']['phpNoDecodeAtOrAboveBytes'] ?? $fixture['expected']['noDecodeAtOrAboveBytes'] ?? null;
|
||||
if ( null !== $threshold ) {
|
||||
same( true, is_int( $threshold ) && $threshold > 0 && $threshold <= $fixture['bytes'], $label . ': decoder witness is bounded by the rejected file.' );
|
||||
$largest = empty( $lengths ) ? 0 : max( $lengths );
|
||||
same( true, $largest < $threshold, $label . ': rejected payload never reaches the full decoder (largest decode: ' . $largest . ' bytes).' );
|
||||
}
|
||||
}
|
||||
|
||||
$security_fixture_root = realpath( dirname( __DIR__ ) . '/fixtures/security' );
|
||||
$security_fixture_manifest = json_decode( file_get_contents( $security_fixture_root . '/manifest.json' ), true );
|
||||
same( JSON_ERROR_NONE, json_last_error(), 'The shared security manifest is valid JSON.' );
|
||||
same( 1, $security_fixture_manifest['schemaVersion'], 'PHP consumes the versioned shared security corpus.' );
|
||||
same( true, count( $security_fixture_manifest['cases'] ) > 0, 'The security corpus cannot silently become empty.' );
|
||||
|
||||
// Independent policy pins keep a production constant change from moving the
|
||||
// goalposts, including cell limits whose next representable grid may skip one.
|
||||
foreach ( array(
|
||||
'CAST_MAX_BYTES' => 33554432,
|
||||
'CAST_MAX_LINE_BYTES' => 262144,
|
||||
'CAST_MAX_V1_BYTES' => 2097152,
|
||||
'CAST_MAX_EVENTS' => 100000,
|
||||
'CAST_MAX_DIMENSION' => 1000,
|
||||
'CAST_MAX_CELLS' => 40000,
|
||||
'CAST_MAX_MARKERS' => 1000,
|
||||
'CAST_MAX_JSON_TOKENS' => 400000,
|
||||
'CAST_MAX_JSON_LINE_TOKENS' => 20000,
|
||||
'CAST_MAX_JSON_CONTAINERS' => 100016,
|
||||
'CAST_JSON_DEPTH' => 16,
|
||||
'CAST_MAX_TIME_MS' => 2147483647,
|
||||
) as $constant => $limit ) {
|
||||
same( $limit, constant( 'LamGC\\Asciiplayer\\' . $constant ), $constant . ': the documented production limit is unchanged.' );
|
||||
}
|
||||
|
||||
$security_fixture_callbacks = array();
|
||||
foreach ( $GLOBALS['ap_test']['filters'] as $filter ) {
|
||||
if ( in_array( $filter[0], array( 'wp_handle_upload_prefilter', 'wp_handle_sideload_prefilter' ), true ) ) {
|
||||
$security_fixture_callbacks[ $filter[0] ] = $filter[1];
|
||||
}
|
||||
}
|
||||
same( 2, count( $security_fixture_callbacks ), 'Both registered upload and sideload paths consume the security corpus.' );
|
||||
|
||||
$security_fixture_wp_state = $GLOBALS['ap_test'];
|
||||
$GLOBALS['ap_test']['capabilities'] = array( 'upload_files' => true, 'unfiltered_upload' => true );
|
||||
$GLOBALS['ap_test']['multisite'] = false;
|
||||
unset( $GLOBALS['ap_test']['allowed_mimes'] );
|
||||
$security_fixture_ids = array();
|
||||
$security_fixture_records = array();
|
||||
try {
|
||||
foreach ( $security_fixture_manifest['cases'] as $fixture ) {
|
||||
same( false, isset( $security_fixture_ids[ $fixture['id'] ] ), 'Each security fixture has a unique ID.' );
|
||||
$security_fixture_ids[ $fixture['id'] ] = $fixture['expected'];
|
||||
$security_fixture_records[ $fixture['id'] ] = $fixture;
|
||||
same( true, isset( $fixture['source']['file'] ) !== isset( $fixture['source']['segments'] ), $fixture['id'] . ': exactly one fixture source is defined.' );
|
||||
same( true, is_bool( $fixture['expected']['accepted'] ), $fixture['id'] . ': the expected upload result is explicit.' );
|
||||
$path = tempnam( sys_get_temp_dir(), 'ap-security-' );
|
||||
if ( false === $path ) {
|
||||
throw new RuntimeException( 'Could not create the security fixture temporary file.' );
|
||||
}
|
||||
try {
|
||||
security_fixture_materialize( $fixture, $security_fixture_root, $path );
|
||||
$GLOBALS['cast_decode_lengths'] = array();
|
||||
same( $fixture['expected']['accepted'], is_valid_cast_file( $path ), $fixture['id'] . ': actual file validator result.' );
|
||||
security_fixture_assert_decodes( $fixture, $fixture['id'] . ' / file validator' );
|
||||
// Browser MIME and size are deliberately wrong: inspect the actual file.
|
||||
$upload = array( 'name' => $fixture['id'] . '.cast', 'tmp_name' => $path, 'error' => 0, 'type' => 'image/png', 'size' => 1 );
|
||||
foreach ( $security_fixture_callbacks as $hook => $callback ) {
|
||||
$GLOBALS['cast_decode_lengths'] = array();
|
||||
$result = call_user_func( $callback, $upload );
|
||||
if ( $fixture['expected']['accepted'] ) {
|
||||
same( $upload, $result, $fixture['id'] . ' / ' . $hook . ': valid content passes unchanged.' );
|
||||
} else {
|
||||
same( true, is_string( $result['error'] ) && 0 === strpos( $result['error'], 'Upload a valid Asciicast' ), $fixture['id'] . ' / ' . $hook . ': content rejection occurs even for unfiltered_upload.' );
|
||||
$result['error'] = 0;
|
||||
same( $upload, $result, $fixture['id'] . ' / ' . $hook . ': only the upload error changes.' );
|
||||
}
|
||||
security_fixture_assert_decodes( $fixture, $fixture['id'] . ' / ' . $hook );
|
||||
}
|
||||
} finally {
|
||||
unlink( $path );
|
||||
}
|
||||
}
|
||||
// Required numeric witnesses cannot disappear while the remaining generic
|
||||
// manifest loop still passes. These expectations do not import guard limits.
|
||||
same( 2147483647, $security_fixture_manifest['policy']['timelineMs'] ?? null, 'The corpus independently pins the signed 32-bit millisecond horizon.' );
|
||||
foreach ( array( 1, 2, 3 ) as $version ) {
|
||||
foreach ( array( 'timeline', 'duration', 'idle-limit' ) as $purpose ) {
|
||||
foreach ( array( 'at' => 2147483647, 'over' => 2147483648 ) as $suffix => $milliseconds ) {
|
||||
$id = $purpose . '-v' . $version . '-' . $suffix;
|
||||
$fixture = $security_fixture_records[ $id ] ?? array();
|
||||
same( array( 'metric' => 'timelineMs', 'limit' => 2147483647, 'value' => $milliseconds ), $fixture['boundary'] ?? null, $id . ': the exact timing boundary remains in the shared corpus.' );
|
||||
same( 'at' === $suffix, $fixture['expected']['accepted'] ?? null, $id . ': both sides retain their independently pinned acceptance result.' );
|
||||
same( 'over' === $suffix ? 'time' : null, $fixture['expected']['jsError'] ?? null, $id . ': timing rejection reason is explicit.' );
|
||||
}
|
||||
}
|
||||
foreach ( array( 'duration', 'idle-limit' ) as $purpose ) {
|
||||
$id = $purpose . '-v' . $version . '-zero';
|
||||
same( true, $security_fixture_ids[ $id ]['accepted'] ?? null, $id . ': zero timing metadata remains supported.' );
|
||||
}
|
||||
}
|
||||
foreach ( array( 1, 3 ) as $version ) {
|
||||
foreach ( array( 'at', 'over' ) as $suffix ) {
|
||||
$id = 'interval-timeline-v' . $version . '-' . $suffix;
|
||||
same( 'at' === $suffix, $security_fixture_ids[ $id ]['accepted'] ?? null, $id . ': interval cumulative boundary remains covered.' );
|
||||
}
|
||||
}
|
||||
foreach ( array(
|
||||
'time-v2-output-nonprogress',
|
||||
'time-v1-ms-sum-overflow', 'time-v3-ms-sum-overflow',
|
||||
'time-v1-ms-rounding-over', 'time-v3-ms-rounding-over',
|
||||
) as $id ) {
|
||||
same( false, $security_fixture_ids[ $id ]['accepted'] ?? null, $id . ': the required actual-player arithmetic witness remains rejected.' );
|
||||
same( 'time', $security_fixture_ids[ $id ]['jsError'] ?? null, $id . ': both languages retain the same explicit timestamp expectation.' );
|
||||
same( true, ( $security_fixture_records[ $id ]['bytes'] ?? 256 ) < 256, $id . ': the numerical regression payload remains tiny.' );
|
||||
}
|
||||
|
||||
foreach ( array( 'bytes-v2-over', 'bytes-v3-over' ) as $id ) {
|
||||
same( 1, $security_fixture_ids[ $id ]['phpNoDecodeAtOrAboveBytes'] ?? null, $id . ': the PHP file-stat gate must reject before any native decode.' );
|
||||
}
|
||||
// These cases must keep decoder witnesses: a false validator result alone
|
||||
// would not prove that allocation was prevented before the native decode.
|
||||
foreach ( array(
|
||||
'bytes-v2-over', 'bytes-v3-over', 'v1-bytes-over',
|
||||
'line-v2-over', 'line-v3-over',
|
||||
'tokens-v1-over', 'containers-v1-over',
|
||||
'depth-v1-over', 'depth-v2-over', 'depth-v3-over',
|
||||
'line-tokens-v2-over', 'line-tokens-v3-over',
|
||||
) as $id ) {
|
||||
same( true, isset( $security_fixture_ids[ $id ]['noDecodeAtOrAboveBytes'] ), $id . ': required predecode witness remains in the corpus.' );
|
||||
same( false, $security_fixture_ids[ $id ]['accepted'], $id . ': the predecode witness remains a rejection case.' );
|
||||
}
|
||||
} finally {
|
||||
$GLOBALS['ap_test'] = $security_fixture_wp_state;
|
||||
$GLOBALS['cast_decode_lengths'] = array();
|
||||
}
|
||||
+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