|
| 1 | +--TEST-- |
| 2 | +GH-24493: upload progress preserves application-generated session records across serializers |
| 3 | +--EXTENSIONS-- |
| 4 | +session |
| 5 | +--SKIPIF-- |
| 6 | +<?php |
| 7 | +include('skipif.inc'); |
| 8 | +if (!getenv('TEST_PHP_CGI_EXECUTABLE')) { |
| 9 | + die('skip CGI not available'); |
| 10 | +} |
| 11 | +?> |
| 12 | +--FILE-- |
| 13 | +<?php |
| 14 | +$dir = __DIR__ . '/gh24493_upload_progress_session_roundtrip'; |
| 15 | +@mkdir($dir); |
| 16 | +$worker = "$dir/worker.php"; |
| 17 | +file_put_contents($worker, <<<'CODE' |
| 18 | +<?php |
| 19 | +$property = getenv('TEST_PROPERTY'); |
| 20 | +$action = getenv('TEST_ACTION'); |
| 21 | +if ($action === 'upload') { |
| 22 | + echo 'UPLOAD:', $_FILES['file']['error'], ':', |
| 23 | + isset($_SESSION) ? 'exposed' : 'clean', "\n"; |
| 24 | + return; |
| 25 | +} |
| 26 | +session_start(); |
| 27 | +if ($action === 'seed') { |
| 28 | + $object = new stdClass; |
| 29 | + $object->{$property} = 'BUSINESS_LITERAL'; |
| 30 | + $_SESSION = ['object' => $object, 'alias' => $object, 'sentinel' => 'KEEP_LITERAL']; |
| 31 | + session_write_close(); |
| 32 | + echo "SEEDED\n"; |
| 33 | + return; |
| 34 | +} |
| 35 | +$object = $_SESSION['object'] ?? null; |
| 36 | +$ok = $object instanceof stdClass |
| 37 | + && ($object->{$property} ?? null) === 'BUSINESS_LITERAL' |
| 38 | + && ($_SESSION['sentinel'] ?? null) === 'KEEP_LITERAL' |
| 39 | + && $object === ($_SESSION['alias'] ?? null); |
| 40 | +session_write_close(); |
| 41 | +echo $ok ? "PRESERVED\n" : "LOST\n"; |
| 42 | +CODE); |
| 43 | +$properties = ['normal', '__PHP_Incomplete_Class_Name', |
| 44 | + '__PHP_Incomplete_Class_Serialized', '__PHP_Incomplete_Enum_Case']; |
| 45 | +$boundary = 'gh24493boundary'; |
| 46 | +$body = "--$boundary\r\nContent-Disposition: form-data; name=\"PHP_SESSION_UPLOAD_PROGRESS\"\r\n\r\nkey\r\n" |
| 47 | + . "--$boundary\r\nContent-Disposition: form-data; name=\"file\"; filename=\"normal.txt\"\r\n\r\nabc\r\n" |
| 48 | + . "--$boundary--\r\n"; |
| 49 | +$ini = ['session.save_path' => $dir, 'session.save_handler' => 'files', |
| 50 | + 'session.use_strict_mode' => '0', 'session.use_cookies' => '1', |
| 51 | + 'session.use_only_cookies' => '1', 'session.cache_limiter' => '', |
| 52 | + 'session.name' => 'PHPSESSID', 'session.upload_progress.enabled' => '1', |
| 53 | + 'session.upload_progress.name' => 'PHP_SESSION_UPLOAD_PROGRESS', |
| 54 | + 'session.upload_progress.min_freq' => '0', 'session.upload_progress.freq' => '1', |
| 55 | + 'file_uploads' => '1', 'display_errors' => '1', 'html_errors' => '0']; |
| 56 | +$extensionDir = ini_get('extension_dir'); |
| 57 | +foreach (['session.' . PHP_SHLIB_SUFFIX, 'php_session.' . PHP_SHLIB_SUFFIX] as $module) { |
| 58 | + if ($extensionDir !== false && file_exists("$extensionDir/$module")) { |
| 59 | + $ini['extension_dir'] = $extensionDir; |
| 60 | + $ini['extension'] = $module; |
| 61 | + break; |
| 62 | + } |
| 63 | +} |
| 64 | +foreach (['php', 'php_binary', 'php_serialize'] as $serializer) { |
| 65 | + foreach ([0, 1] as $cleanup) { |
| 66 | + foreach ($properties as $index => $property) { |
| 67 | + $sid = 'roundtrip' . $cleanup . $index; |
| 68 | + @unlink("$dir/sess_$sid"); |
| 69 | + $cmd = [getenv('TEST_PHP_CGI_EXECUTABLE'), '-n', '-q']; |
| 70 | + $settings = $ini + ['session.serialize_handler' => $serializer, |
| 71 | + 'session.upload_progress.cleanup' => (string) $cleanup]; |
| 72 | + foreach ($settings as $name => $value) { |
| 73 | + $cmd[] = '-d'; |
| 74 | + $cmd[] = "$name=$value"; |
| 75 | + } |
| 76 | + $cmd[] = '-f'; |
| 77 | + $cmd[] = $worker; |
| 78 | + $ok = true; |
| 79 | + foreach (['seed', 'upload', 'read'] as $action) { |
| 80 | + $env = getenv(); |
| 81 | + $env['REDIRECT_STATUS'] = '1'; |
| 82 | + $env['REQUEST_METHOD'] = $action === 'upload' ? 'POST' : 'GET'; |
| 83 | + $env['SCRIPT_FILENAME'] = $worker; |
| 84 | + $env['PATH_TRANSLATED'] = $worker; |
| 85 | + $env['QUERY_STRING'] = ''; |
| 86 | + $env['HTTP_COOKIE'] = "PHPSESSID=$sid"; |
| 87 | + $env['CONTENT_TYPE'] = "multipart/form-data; boundary=$boundary"; |
| 88 | + $env['CONTENT_LENGTH'] = $action === 'upload' ? (string) strlen($body) : '0'; |
| 89 | + $env['TEST_PROPERTY'] = $property; |
| 90 | + $env['TEST_ACTION'] = $action; |
| 91 | + $proc = proc_open($cmd, [0 => ['pipe','r'], 1 => ['pipe','w'], 2 => ['pipe','w']], $pipes, $dir, $env); |
| 92 | + if ($action === 'upload') { |
| 93 | + fwrite($pipes[0], $body); |
| 94 | + } |
| 95 | + fclose($pipes[0]); |
| 96 | + $output = stream_get_contents($pipes[1]); |
| 97 | + $error = stream_get_contents($pipes[2]); |
| 98 | + fclose($pipes[1]); |
| 99 | + fclose($pipes[2]); |
| 100 | + $exit = proc_close($proc); |
| 101 | + $expected = match ($action) { |
| 102 | + 'seed' => 'SEEDED', 'upload' => 'UPLOAD:0:clean', 'read' => 'PRESERVED' |
| 103 | + }; |
| 104 | + $ok = $ok && $exit === 0 && $error === '' && str_contains($output, $expected) |
| 105 | + && !str_contains($output, 'Warning:'); |
| 106 | + if ($action === 'seed') { |
| 107 | + $record = file_get_contents("$dir/sess_$sid"); |
| 108 | + } elseif ($action === 'upload' && $property !== 'normal') { |
| 109 | + /* A rejected progress update must not rewrite even a partial record. */ |
| 110 | + $ok = $ok && file_exists("$dir/sess_$sid") |
| 111 | + && file_get_contents("$dir/sess_$sid") === $record; |
| 112 | + } |
| 113 | + } |
| 114 | + printf("%s cleanup=%d property=%d %s\n", $serializer, $cleanup, $index, $ok ? 'OK' : 'FAIL'); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | +?> |
| 119 | +--CLEAN-- |
| 120 | +<?php |
| 121 | +$dir = __DIR__ . '/gh24493_upload_progress_session_roundtrip'; |
| 122 | +foreach (glob("$dir/*") as $file) { |
| 123 | + unlink($file); |
| 124 | +} |
| 125 | +rmdir($dir); |
| 126 | +?> |
| 127 | +--EXPECT-- |
| 128 | +php cleanup=0 property=0 OK |
| 129 | +php cleanup=0 property=1 OK |
| 130 | +php cleanup=0 property=2 OK |
| 131 | +php cleanup=0 property=3 OK |
| 132 | +php cleanup=1 property=0 OK |
| 133 | +php cleanup=1 property=1 OK |
| 134 | +php cleanup=1 property=2 OK |
| 135 | +php cleanup=1 property=3 OK |
| 136 | +php_binary cleanup=0 property=0 OK |
| 137 | +php_binary cleanup=0 property=1 OK |
| 138 | +php_binary cleanup=0 property=2 OK |
| 139 | +php_binary cleanup=0 property=3 OK |
| 140 | +php_binary cleanup=1 property=0 OK |
| 141 | +php_binary cleanup=1 property=1 OK |
| 142 | +php_binary cleanup=1 property=2 OK |
| 143 | +php_binary cleanup=1 property=3 OK |
| 144 | +php_serialize cleanup=0 property=0 OK |
| 145 | +php_serialize cleanup=0 property=1 OK |
| 146 | +php_serialize cleanup=0 property=2 OK |
| 147 | +php_serialize cleanup=0 property=3 OK |
| 148 | +php_serialize cleanup=1 property=0 OK |
| 149 | +php_serialize cleanup=1 property=1 OK |
| 150 | +php_serialize cleanup=1 property=2 OK |
| 151 | +php_serialize cleanup=1 property=3 OK |
0 commit comments