Skip to content

Commit ea50df8

Browse files
programsurfdaeungdaeungyoonshlubroai
committed
Fix GH-24493: preserve session records and release aborted upload buffers
Co-authored-by: daeungdaeung <41292577+daeungdaeung@users.noreply.github.com> Co-authored-by: Seunghyun Yoon <7575205+yoonsh@users.noreply.github.com> Co-authored-by: lubroai <90449417+lubroai@users.noreply.github.com>
1 parent 030d2f2 commit ea50df8

4 files changed

Lines changed: 301 additions & 10 deletions

File tree

‎ext/session/session.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,17 @@ static zend_string *php_session_encode(void) /* {{{ */
266266

267267
static ZEND_COLD void php_session_cancel_decode(void)
268268
{
269+
if (PS(in_rfc1867_update)) {
270+
/* The upload progress decoder is deliberately more restrictive than
271+
* session_start(). Leave the stored record for the application to read. */
272+
php_session_abort();
273+
/* An aborted update will not use the lazy-write comparison buffer. */
274+
if (PS(session_vars)) {
275+
zend_string_release_ex(PS(session_vars), 0);
276+
PS(session_vars) = NULL;
277+
}
278+
return;
279+
}
269280
php_session_destroy();
270281
php_session_track_init();
271282
php_error_docref(NULL, E_WARNING, "Failed to decode session object. Session has been destroyed");
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
--TEST--
2+
GH-24493: aborted upload progress updates release the lazy-write buffer
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+
if (!function_exists('proc_open')) {
12+
die('skip proc_open not available');
13+
}
14+
?>
15+
--FILE--
16+
<?php
17+
$dir = __DIR__ . '/gh24493_upload_progress_memory';
18+
@mkdir($dir);
19+
$worker = "$dir/worker.php";
20+
file_put_contents($worker, <<<'CODE'
21+
<?php
22+
$action = getenv('TEST_ACTION');
23+
if ($action === 'upload') {
24+
echo 'UPLOAD:', $_FILES['file']['error'], ':',
25+
file_get_contents($_FILES['file']['tmp_name']), ':',
26+
isset($_SESSION) ? 'exposed' : 'clean', "\n";
27+
return;
28+
}
29+
session_start();
30+
if ($action === 'seed') {
31+
$object = new stdClass;
32+
$object->__PHP_Incomplete_Class_Name = 'application value';
33+
/* The restricted decoder rejects the object before reading the payload. */
34+
$_SESSION = ['object' => $object, 'payload' => str_repeat('A', 5 * 1024 * 1024)];
35+
session_write_close();
36+
echo "SEEDED\n";
37+
return;
38+
}
39+
$ok = ($_SESSION['object']->__PHP_Incomplete_Class_Name ?? null) === 'application value'
40+
&& ($_SESSION['payload'] ?? null) === str_repeat('A', 5 * 1024 * 1024);
41+
session_write_close();
42+
echo $ok ? "PRESERVED\n" : "LOST\n";
43+
CODE);
44+
$boundary = 'sessionmemoryboundary';
45+
$body = "--$boundary\r\nContent-Disposition: form-data; name=\"PHP_SESSION_UPLOAD_PROGRESS\"\r\n\r\nkey\r\n"
46+
. "--$boundary\r\nContent-Disposition: form-data; name=\"file\"; filename=\"file.txt\"\r\n\r\nabc\r\n"
47+
. "--$boundary--\r\n";
48+
$ini = ['session.save_path' => $dir, 'session.save_handler' => 'files',
49+
'session.gc_probability' => '0', 'session.use_strict_mode' => '0',
50+
'session.use_cookies' => '1', 'session.use_only_cookies' => '1',
51+
'session.cache_limiter' => '', 'session.name' => 'PHPSESSID',
52+
'session.lazy_write' => '1', 'session.upload_progress.enabled' => '1',
53+
'session.upload_progress.name' => 'PHP_SESSION_UPLOAD_PROGRESS',
54+
'session.upload_progress.freq' => '1', 'session.upload_progress.min_freq' => '0',
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+
$sid = 'memory' . $cleanup;
67+
@unlink("$dir/sess_$sid");
68+
$ok = true;
69+
foreach (['seed', 'upload', 'read'] as $action) {
70+
$cmd = [getenv('TEST_PHP_CGI_EXECUTABLE'), '-n', '-q'];
71+
$settings = $ini + ['session.serialize_handler' => $serializer,
72+
'session.upload_progress.cleanup' => (string) $cleanup,
73+
'memory_limit' => $action === 'upload' ? '8M' : '64M'];
74+
foreach ($settings as $name => $value) {
75+
$cmd[] = '-d';
76+
$cmd[] = "$name=$value";
77+
}
78+
$cmd[] = '-f';
79+
$cmd[] = $worker;
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_ACTION'] = $action;
90+
$proc = proc_open($cmd, [0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes, $dir, $env);
91+
if ($action === 'upload') {
92+
fwrite($pipes[0], $body);
93+
}
94+
fclose($pipes[0]);
95+
$output = stream_get_contents($pipes[1]);
96+
$error = stream_get_contents($pipes[2]);
97+
fclose($pipes[1]);
98+
fclose($pipes[2]);
99+
$exit = proc_close($proc);
100+
$expected = match ($action) {
101+
'seed' => 'SEEDED', 'upload' => 'UPLOAD:0:abc:clean', 'read' => 'PRESERVED'
102+
};
103+
$ok = $ok && $exit === 0 && $error === '' && str_contains($output, $expected)
104+
&& !str_contains($output, 'Warning:') && !str_contains($output, 'Fatal error:');
105+
if ($action === 'seed') {
106+
$recordHash = hash_file('sha256', "$dir/sess_$sid");
107+
} elseif ($action === 'upload') {
108+
$ok = $ok && file_exists("$dir/sess_$sid")
109+
&& hash_file('sha256', "$dir/sess_$sid") === $recordHash;
110+
}
111+
}
112+
printf("%s cleanup=%d %s\n", $serializer, $cleanup, $ok ? 'OK' : 'FAIL');
113+
}
114+
}
115+
?>
116+
--CLEAN--
117+
<?php
118+
$dir = __DIR__ . '/gh24493_upload_progress_memory';
119+
foreach (glob("$dir/*") as $file) {
120+
unlink($file);
121+
}
122+
rmdir($dir);
123+
?>
124+
--EXPECT--
125+
php cleanup=0 OK
126+
php cleanup=1 OK
127+
php_binary cleanup=0 OK
128+
php_binary cleanup=1 OK
129+
php_serialize cleanup=0 OK
130+
php_serialize cleanup=1 OK
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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

‎ext/session/tests/rfc1867_incomplete_class_reserved_props.phpt‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
session rfc1867: the upload progress update rejects session data whose objects carry the incomplete class bookkeeping property names
2+
session rfc1867: the upload progress update preserves records it cannot decode without user code
33
--EXTENSIONS--
44
session
55
--SKIPIF--
@@ -23,7 +23,8 @@ file_put_contents($worker, '<?php');
2323
* __PHP_Incomplete_Class, which stores the class name of each object and the
2424
* payload of C:/E: entries under these three property names. Data that already
2525
* carries one of them cannot be told apart from that bookkeeping, so the decode
26-
* has to fail rather than let the two meanings be confused. */
26+
* has to fail rather than let the two meanings be confused. The progress
27+
* update must abort without deleting or rewriting the stored record. */
2728
$cases = [
2829
'control' => 'x|O:8:"stdClass":1:{s:1:"a";i:1;}',
2930
'Class_Name' => 'x|O:8:"stdClass":1:{s:27:"__PHP_Incomplete_Class_Name";s:3:"foo";}',
@@ -109,9 +110,7 @@ foreach ($cases as $label => $record) {
109110
fclose($pipes[2]);
110111
proc_close($proc);
111112

112-
/* A refused decode must leave exactly one warning: the handler stops there
113-
* instead of writing the progress into the empty session it was left with,
114-
* which would add a second warning about the save path. */
113+
/* A progress-only decode failure must leave the stored session intact. */
115114
$kept = file_exists($file) && str_starts_with(file_get_contents($file), $record);
116115
printf("%-16s warnings: %d, decode refused: %-5s record kept: %s\n", $label,
117116
substr_count($out, 'Warning:'),
@@ -131,9 +130,9 @@ foreach (glob("$dir/sess_*") as $f) {
131130
?>
132131
--EXPECT--
133132
control warnings: 0, decode refused: false record kept: true
134-
Class_Name warnings: 1, decode refused: true record kept: false
135-
Class_Serialized warnings: 1, decode refused: true record kept: false
136-
Enum_Case warnings: 1, decode refused: true record kept: false
137-
nested warnings: 1, decode refused: true record kept: false
133+
Class_Name warnings: 0, decode refused: false record kept: true
134+
Class_Serialized warnings: 0, decode refused: false record kept: true
135+
Enum_Case warnings: 0, decode refused: false record kept: true
136+
nested warnings: 0, decode refused: false record kept: true
138137
mangled warnings: 0, decode refused: false record kept: true
139-
corrupt warnings: 1, decode refused: true record kept: false
138+
corrupt warnings: 0, decode refused: false record kept: true

0 commit comments

Comments
 (0)