fix: bound recording timestamps before playback

Match upstream millisecond accumulation and bound native timer horizons.
Add deterministic rounding, overflow and non-progress regression casts,
with captured-player and WordPress playback/upload coverage.
This commit is contained in:
2026-10-15 11:30:31 +08:00
parent 487eedf0ba
commit e1cd87f87d
49 changed files with 1273 additions and 33 deletions
-1
View File
@@ -9,4 +9,3 @@ namespace LamGC\Asciiplayer {
return \json_decode( $json, $associative, $depth, $flags );
}
}
+45
View File
@@ -5,8 +5,10 @@
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;
@@ -14,6 +16,49 @@ namespace {
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}}',
+37
View File
@@ -128,6 +128,7 @@ foreach ( array(
'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.' );
}
@@ -145,10 +146,12 @@ $GLOBALS['ap_test']['capabilities'] = array( 'upload_files' => true, 'unfiltered
$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-' );
@@ -178,6 +181,40 @@ try {
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.' );
}