Commit 3a1a8997e5f for php

commit 3a1a8997e5fc3762a22652c0c9363dd1ea92b784
Author: Jorg Adam Sowa <jorg.sowa@gmail.com>
Date:   Mon Nov 30 20:57:54 2026 +0100

    ext/session: Reject non-array php_serialize session data (#25950)

    With session.serialize_handler=php_serialize, stored data whose top-level value isn't an array (i:1;, s:3:"abc";, b:1;, N;, an object) was accepted and became $_SESSION as that type. Decoding reported success, and writes were then silently skipped, so the bad record stayed in storage.

    That data now takes the same path as data that can't be unserialized: $_SESSION is reset to an empty array, decoding fails with "Failed to decode session object. Session has been destroyed", and the session is destroyed. This matches the php and php_binary handlers, which can only ever produce an array. Valid sessions are unaffected, including a reference-wrapped array.

diff --git a/NEWS b/NEWS
index ed39303f708..3f40bf7a2d7 100644
--- a/NEWS
+++ b/NEWS
@@ -168,6 +168,8 @@ PHP                                                                        NEWS
     set to an empty string. (Ilia Alshanetsky)

 - Session:
+  . Fixed php_serialize session data with a non-array top-level value being
+    accepted as $_SESSION. (jorgsowa)
   . Fixed session_start() continuing after a failed create_sid() when
     session.use_strict_mode rejects the supplied ID. (Ilia Alshanetsky)
   . Fixed exceptions from user-defined create_sid() handlers being replaced
diff --git a/ext/session/session.c b/ext/session/session.c
index a15c053fa8e..08d0a57fa85 100644
--- a/ext/session/session.c
+++ b/ext/session/session.c
@@ -1098,17 +1098,19 @@ PS_SERIALIZER_DECODE_FUNC(php_serialize)
 	result = php_var_unserialize(
 		&session_vars, (const unsigned char **)&val, (const unsigned char *)endptr, &var_hash);
 	PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
+	if (result) {
+		const zval *data = &session_vars;
+		ZVAL_DEREF(data);
+		result = Z_TYPE_P(data) == IS_ARRAY;
+	}
 	if (!result) {
 		zval_ptr_dtor(&session_vars);
-		ZVAL_NULL(&session_vars);
+		array_init(&session_vars);
 	}

 	if (!Z_ISUNDEF(PS(http_session_vars))) {
 		zval_ptr_dtor(&PS(http_session_vars));
 	}
-	if (Z_TYPE(session_vars) == IS_NULL) {
-		array_init(&session_vars);
-	}
 	ZVAL_NEW_REF(&PS(http_session_vars), &session_vars);
 	Z_ADDREF_P(&PS(http_session_vars));
 	zend_hash_update_ind(&EG(symbol_table), var_name, &PS(http_session_vars));
diff --git a/ext/session/tests/session_decode_php_serialize_non_array.phpt b/ext/session/tests/session_decode_php_serialize_non_array.phpt
new file mode 100644
index 00000000000..3d81fced972
--- /dev/null
+++ b/ext/session/tests/session_decode_php_serialize_non_array.phpt
@@ -0,0 +1,78 @@
+--TEST--
+php_serialize handler rejects session data that is not an array
+--EXTENSIONS--
+session
+--INI--
+session.use_cookies=0
+session.cache_limiter=
+session.serialize_handler=php_serialize
+--FILE--
+<?php
+$cases = [
+    'N;',
+    'b:0;',
+    'b:1;',
+    'i:1;',
+    'd:1.5;',
+    's:3:"abc";',
+    'O:8:"stdClass":1:{s:1:"a";i:1;}',
+    'a:1:{s:1:"a";i:1;}',
+];
+
+foreach ($cases as $data) {
+    echo "$data\n";
+    session_start();
+    var_dump(session_decode($data));
+    var_dump($_SESSION);
+    session_abort();
+}
+?>
+--EXPECTF--
+N;
+
+Warning: session_decode(): Failed to decode session object. Session has been destroyed in %s on line %d
+bool(false)
+array(0) {
+}
+b:0;
+
+Warning: session_decode(): Failed to decode session object. Session has been destroyed in %s on line %d
+bool(false)
+array(0) {
+}
+b:1;
+
+Warning: session_decode(): Failed to decode session object. Session has been destroyed in %s on line %d
+bool(false)
+array(0) {
+}
+i:1;
+
+Warning: session_decode(): Failed to decode session object. Session has been destroyed in %s on line %d
+bool(false)
+array(0) {
+}
+d:1.5;
+
+Warning: session_decode(): Failed to decode session object. Session has been destroyed in %s on line %d
+bool(false)
+array(0) {
+}
+s:3:"abc";
+
+Warning: session_decode(): Failed to decode session object. Session has been destroyed in %s on line %d
+bool(false)
+array(0) {
+}
+O:8:"stdClass":1:{s:1:"a";i:1;}
+
+Warning: session_decode(): Failed to decode session object. Session has been destroyed in %s on line %d
+bool(false)
+array(0) {
+}
+a:1:{s:1:"a";i:1;}
+bool(true)
+array(1) {
+  ["a"]=>
+  int(1)
+}
diff --git a/ext/standard/tests/serialize/bug70219_1.phpt b/ext/standard/tests/serialize/bug70219_1.phpt
index 846c147a3f2..004ab7b4867 100644
--- a/ext/standard/tests/serialize/bug70219_1.phpt
+++ b/ext/standard/tests/serialize/bug70219_1.phpt
@@ -31,6 +31,10 @@ function unserialize($data) {
 ?>
 --EXPECTF--
 Deprecated: %s implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in %s on line %d
+
+Warning: session_decode(): Failed to decode session object. Session has been destroyed in %s on line %d
+
+Warning: session_decode(): Session data cannot be decoded when there is no active session in %s on line %d
 array(2) {
   [0]=>
   object(obj)#%d (1) {
@@ -43,7 +47,5 @@ function unserialize($data) {
     NULL
   }
 }
-object(obj)#1 (1) {
-  ["data"]=>
-  NULL
+array(0) {
 }