Bound recording loading, terminal allocation, markers and upload JSON complexity

This commit is contained in:
2026-10-15 10:13:55 +08:00
parent ffff9704e5
commit 92de7f86f4
24 changed files with 1831 additions and 115 deletions
+174 -17
View File
@@ -17,6 +17,143 @@ 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;
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 ) {
@@ -54,6 +191,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 +207,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,7 +223,12 @@ 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;
}
@@ -108,7 +258,7 @@ function is_cast_event( $event, $version, &$time ) {
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 +291,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,25 +311,30 @@ function is_valid_cast_file( $path ) {
}
$bytes = strlen( $line );
$count = 0;
$markers = 0;
$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 );
$event = json_decode( $line, false, CAST_JSON_DEPTH );
if ( JSON_ERROR_NONE !== json_last_error() || ! is_cast_event( $event, $header->version, $time ) ) {
return false;
}
if ( 'm' === $event[1] && ++$markers > CAST_MAX_MARKERS ) {
return false;
}
}
return feof( $handle );
} finally {
@@ -194,7 +351,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;
}