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

- 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:
2026-10-15 11:42:07 +08:00
committed by GitHub
parent ffff9704e5
commit 6f8182db41
113 changed files with 7121 additions and 126 deletions
+192 -28
View File
@@ -17,6 +17,146 @@ const CAST_MAX_LINE_BYTES = 262144; // Bound individual JSON decodes to 256 KiB.
const CAST_MAX_V1_BYTES = 2097152; // Legacy v1 is a single JSON document, not a stream.
const CAST_MAX_EVENTS = 100000;
const CAST_MAX_DIMENSION = 1000;
const CAST_MAX_CELLS = 40000;
const CAST_MAX_MARKERS = 1000;
// Browser timers use signed 32-bit millisecond delays (~24.85 days). Keep the
// raw timeline in this range; playback additionally checks the selected speed.
const CAST_MAX_TIME_MS = 2147483647;
const CAST_MAX_JSON_TOKENS = 400000;
const CAST_MAX_JSON_LINE_TOKENS = 20000;
const CAST_MAX_JSON_CONTAINERS = 100016;
const CAST_JSON_DEPTH = 16;
/**
* Reject allocation-heavy JSON before decoding it, without building a graph.
*
* Count container openings and scalar/key tokens, including duplicate keys.
* Escapes and punctuation inside strings do not contribute to the budget.
* Counters may be shared across JSONL records. This lexical resource gate is
* not a JSON validator: json_decode remains responsible for grammar and UTF-8.
*/
function is_cast_json_within_budget( $text, &$containers, &$tokens, $token_limit = CAST_MAX_JSON_TOKENS ) {
$length = strlen( $text );
$depth = 0;
$in_string = false;
$escaped = false;
$in_atom = false;
for ( $index = 0; $index < $length; ++$index ) {
$char = $text[ $index ];
if ( $in_string ) {
if ( $escaped ) {
$escaped = false;
} elseif ( '\\' === $char ) {
$escaped = true;
} elseif ( '"' === $char ) {
$in_string = false;
}
continue;
}
if ( '"' === $char ) {
$in_string = true;
$in_atom = false;
++$tokens;
} elseif ( '{' === $char || '[' === $char ) {
$in_atom = false;
++$containers;
++$depth;
// json_decode's depth includes the scalar level inside containers.
if ( $depth >= CAST_JSON_DEPTH ) {
return false;
}
} elseif ( '}' === $char || ']' === $char ) {
--$depth;
$in_atom = false;
if ( $depth < 0 ) {
return false;
}
} elseif ( ',' === $char || ':' === $char || ' ' === $char || "\n" === $char || "\r" === $char || "\t" === $char ) {
$in_atom = false;
} elseif ( ! $in_atom ) {
++$tokens;
$in_atom = true;
}
if ( $containers > CAST_MAX_JSON_CONTAINERS || $tokens > $token_limit ) {
return false;
}
}
return true;
}
/**
* Identify the last top-level version key without decoding a document graph.
*
* Run only after the lexical budget. Short JSON key/number decodes correctly
* handle escaped keys and duplicate-key last-wins semantics. Nested metadata
* never selects the format. This is a discriminator, not a grammar validator.
*/
function cast_top_level_version( $text ) {
$length = strlen( $text );
$depth = 0;
$expect_key = false;
$version = null;
for ( $index = 0; $index < $length; ++$index ) {
$char = $text[ $index ];
if ( '"' === $char ) {
$start = $index;
$escaped = false;
while ( ++$index < $length ) {
if ( $escaped ) {
$escaped = false;
} elseif ( '\\' === $text[ $index ] ) {
$escaped = true;
} elseif ( '"' === $text[ $index ] ) {
break;
}
}
if ( 1 !== $depth || ! $expect_key ) {
continue;
}
$expect_key = false;
// Seven fully escaped characters plus quotes need at most 44 bytes.
$key_length = $index - $start + 1;
if ( $key_length < 9 || $key_length > 44 || 'version' !== json_decode( substr( $text, $start, $key_length ) ) ) {
continue;
}
$version = null;
$cursor = $index + 1;
while ( $cursor < $length && false !== strpos( " \t\r\n", $text[ $cursor ] ) ) {
++$cursor;
}
if ( $cursor >= $length || ':' !== $text[ $cursor++ ] ) {
continue;
}
while ( $cursor < $length && false !== strpos( " \t\r\n", $text[ $cursor ] ) ) {
++$cursor;
}
$start = $cursor;
while ( $cursor < $length && false === strpos( ",}] \t\r\n", $text[ $cursor ] ) ) {
++$cursor;
if ( $cursor - $start > 32 ) {
break;
}
}
if ( $cursor > $start && $cursor - $start <= 32 ) {
$value = json_decode( substr( $text, $start, $cursor - $start ) );
$version = is_int( $value ) ? $value : null;
}
} elseif ( '{' === $char || '[' === $char ) {
++$depth;
if ( 1 === $depth ) {
if ( '{' !== $char ) {
return null;
}
$expect_key = true;
}
} elseif ( '}' === $char || ']' === $char ) {
--$depth;
} elseif ( ',' === $char && 1 === $depth ) {
$expect_key = true;
}
}
return $version;
}
/** Whether this user and, on multisite, the network permit .cast uploads. */
function can_upload_cast( $user = null ) {
@@ -44,9 +184,9 @@ function is_cast_filename( $filename ) {
return is_string( $filename ) && 1 === preg_match( '/\.cast\z/i', $filename );
}
/** Finite, nonnegative seconds that remain finite in the player's milliseconds. */
/** Nonnegative seconds within the supported browser timer/precision horizon. */
function is_cast_time( $value ) {
return ( is_int( $value ) || is_float( $value ) ) && $value >= 0 && is_finite( $value * 1000 );
return ( is_int( $value ) || is_float( $value ) ) && $value >= 0 && $value * 1000 <= CAST_MAX_TIME_MS;
}
/** Bound both initial and resize dimensions before they reach the terminal. */
@@ -54,6 +194,11 @@ function is_cast_dimension( $value ) {
return is_int( $value ) && $value > 0 && $value <= CAST_MAX_DIMENSION;
}
/** Bound the cell product as well as each independent axis. */
function is_cast_size( $cols, $rows ) {
return is_cast_dimension( $cols ) && is_cast_dimension( $rows ) && $cols * $rows <= CAST_MAX_CELLS;
}
/** Validate metadata used by asciinema-player; unknown fields remain compatible. */
function is_cast_header( $header ) {
if ( ! is_object( $header ) || ! isset( $header->version ) || ! in_array( $header->version, array( 1, 2, 3 ), true ) ) {
@@ -65,7 +210,10 @@ function is_cast_header( $header ) {
}
$cols = 3 === $header->version ? ( $terminal->cols ?? null ) : ( $terminal->width ?? null );
$rows = 3 === $header->version ? ( $terminal->rows ?? null ) : ( $terminal->height ?? null );
if ( ! is_cast_dimension( $cols ) || ! is_cast_dimension( $rows ) ) {
// The bundled player treats explicit zero header dimensions as defaults.
$cols = 0 === $cols ? 80 : $cols;
$rows = 0 === $rows ? 24 : $rows;
if ( ! is_cast_size( $cols, $rows ) ) {
return false;
}
foreach ( array( 'duration', 'idle_time_limit' ) as $key ) {
@@ -78,17 +226,23 @@ function is_cast_header( $header ) {
/** Validate a whole bounded legacy v1 document, including every stdout frame. */
function is_cast_v1( $text ) {
$recording = json_decode( $text, false, 16 );
$containers = 0;
$tokens = 0;
if ( strlen( $text ) > CAST_MAX_V1_BYTES || ! is_cast_json_within_budget( $text, $containers, $tokens ) || 1 !== cast_top_level_version( $text ) ) {
return false;
}
$recording = json_decode( $text, false, CAST_JSON_DEPTH );
if ( JSON_ERROR_NONE !== json_last_error() || ! is_cast_header( $recording ) || 1 !== $recording->version || ! isset( $recording->stdout ) || ! is_array( $recording->stdout ) || count( $recording->stdout ) > CAST_MAX_EVENTS ) {
return false;
}
$time = 0;
$time_ms = 0;
foreach ( $recording->stdout as $frame ) {
if ( ! is_array( $frame ) || 2 !== count( $frame ) || ! is_cast_time( $frame[0] ) || ! is_string( $frame[1] ) ) {
return false;
}
$time += $frame[0];
if ( ! is_cast_time( $time ) ) {
// Match upstream's arithmetic order, including floating-point rounding.
$time_ms += $frame[0] * 1000;
if ( ! is_finite( $time_ms ) || $time_ms > CAST_MAX_TIME_MS ) {
return false;
}
}
@@ -96,19 +250,21 @@ function is_cast_v1( $text ) {
}
/** Validate one JSONL event, with absolute v2 time or relative v3 intervals. */
function is_cast_event( $event, $version, &$time ) {
function is_cast_event( $event, $version, &$time_ms, &$previous_time ) {
if ( ! is_array( $event ) || 3 !== count( $event ) || ! is_cast_time( $event[0] ) || ! is_string( $event[1] ) || '' === $event[1] || ! is_string( $event[2] ) ) {
return false;
}
if ( 2 === $version && $event[0] < $time ) {
$event_ms = $event[0] * 1000;
if ( 2 === $version && $event[0] < $previous_time ) {
return false;
}
$time = 2 === $version ? $event[0] : $time + $event[0];
if ( ! is_cast_time( $time ) ) {
$previous_time = $event[0];
$time_ms = 2 === $version ? $event_ms : $time_ms + $event_ms;
if ( ! is_finite( $time_ms ) || $time_ms > CAST_MAX_TIME_MS ) {
return false;
}
if ( 'r' === $event[1] ) {
if ( 1 !== preg_match( '/^([1-9][0-9]{0,3})x([1-9][0-9]{0,3})$/D', $event[2], $matches ) || ! is_cast_dimension( (int) $matches[1] ) || ! is_cast_dimension( (int) $matches[2] ) ) {
if ( 1 !== preg_match( '/^([1-9][0-9]{0,3})x([1-9][0-9]{0,3})$/D', $event[2], $matches ) || ! is_cast_size( (int) $matches[1], (int) $matches[2] ) ) {
return false;
}
}
@@ -141,16 +297,18 @@ function is_valid_cast_file( $path ) {
return false;
}
$line = fgets( $handle, CAST_MAX_LINE_BYTES + 2 );
if ( false === $line || strlen( $line ) > CAST_MAX_LINE_BYTES ) {
if ( false === $line ) {
return false;
}
$header = json_decode( $line, false, 16 );
// Player 3.17 interprets a complete first-line object as JSONL and rejects
// version 1 there. Keep uploads playable: legacy v1 must be pretty-printed.
if ( is_object( $header ) && isset( $header->version ) && 1 === $header->version ) {
return false;
$containers = 0;
$tokens = 0;
$header = null;
if ( strlen( $line ) <= CAST_MAX_LINE_BYTES && is_cast_json_within_budget( $line, $containers, $tokens, CAST_MAX_JSON_LINE_TOKENS ) ) {
$header = json_decode( $line, false, CAST_JSON_DEPTH );
}
if ( ! is_cast_header( $header ) ) {
if ( ! is_cast_header( $header ) || 1 === $header->version ) {
// Legacy v1 may be compact or pretty-printed. Its whole-document
// byte and structure budgets apply before any complete decode.
if ( $stat['size'] > CAST_MAX_V1_BYTES || 0 !== fseek( $handle, 0 ) ) {
return false;
}
@@ -159,23 +317,29 @@ function is_valid_cast_file( $path ) {
}
$bytes = strlen( $line );
$count = 0;
$time = 0;
$markers = 0;
$time_ms = 0;
$previous_time = 0;
while ( false !== ( $line = fgets( $handle, CAST_MAX_LINE_BYTES + 2 ) ) ) {
$bytes += strlen( $line );
if ( $bytes > CAST_MAX_BYTES || strlen( $line ) > CAST_MAX_LINE_BYTES || ++$count > CAST_MAX_EVENTS ) {
return false;
}
if ( 3 === $header->version && '#' === $line[0] ) {
// Match the bundled player's parser: only lines beginning with '['
// are events. Ignored lines still consume byte and line budgets.
if ( '[' !== $line[0] ) {
continue;
}
// The bundled player's JSONL parser only consumes lines starting with
// '['; reject indented events rather than silently losing their output.
if ( '[' !== $line[0] ) {
$containers = 0;
$tokens = 0;
if ( ! is_cast_json_within_budget( $line, $containers, $tokens, CAST_MAX_JSON_LINE_TOKENS ) ) {
return false;
}
// Empty extra lines and non-event trailing content are not valid JSONL.
$event = json_decode( $line, false, 16 );
if ( JSON_ERROR_NONE !== json_last_error() || ! is_cast_event( $event, $header->version, $time ) ) {
$event = json_decode( $line, false, CAST_JSON_DEPTH );
if ( JSON_ERROR_NONE !== json_last_error() || ! is_cast_event( $event, $header->version, $time_ms, $previous_time ) ) {
return false;
}
if ( 'm' === $event[1] && ++$markers > CAST_MAX_MARKERS ) {
return false;
}
}
@@ -194,7 +358,7 @@ function validate_cast_upload( $file ) {
if ( ! can_upload_cast() || ! isset( $allowed['cast'] ) || CAST_MIME !== $allowed['cast'] ) {
$file['error'] = __( 'You are not allowed to upload Asciicast files.', 'wp-asciiplayer' );
} elseif ( ! is_valid_cast_file( $file['tmp_name'] ?? null ) ) {
$file['error'] = __( 'Upload a valid Asciicast v2/v3 JSONL or pretty-printed v1 .cast recording (up to 32 MiB; v1 up to 2 MiB, 100,000 events, 256 KiB per JSONL line, and 1,000 columns or rows).', 'wp-asciiplayer' );
$file['error'] = __( 'Upload a valid Asciicast v2/v3 JSONL or v1 .cast recording (up to 32 MiB; v1 up to 2 MiB, 100,000 events, 256 KiB per JSONL line, 1,000 markers, 1,000 columns or rows, 40,000 terminal cells, and bounded JSON structure).', 'wp-asciiplayer' );
}
return $file;
}