Repository navigation
fix: a Map or a Set serializes and reads back, as a queued job or a cached value holds one - #158
Merged
Merged
Conversation
…ached value holds one
Problem
Laravel's queue, cache and sessions run serialize() on the value they keep.
A value that holds a PHP# Map or Set, itself or in a field of a struct or a
class, threw "Serialization of 'Sharp\Map' is not allowed", because the
engine's table class carried ZEND_ACC_NOT_SERIALIZABLE. Spec section 11
already assumed the opposite: "an object serialized as it is keeps its type
arguments".
Solution
Sharp\Map and Sharp\Set serialize through the engine's own hooks, as
ArrayObject and SplObjectStorage do. Map::__serialize writes each key then
its value, the list the constructor takes, and Set::__serialize is toList.
Each __unserialize fills the table through the code its constructor runs,
so a later key replaces an earlier one, and an odd or non-list Map list
throws the constructor's ValueError. unserialize runs __unserialize once
the whole payload is read, so a struct or class key is complete when the
table hashes it.
A Map carries no type arguments of its own, so the copy is a
Map<string, int> exactly when its entries are. An enum key comes back as
the same case, a struct key or a key of a class that declares == as an
equal value, and a plain PHP object key as the object read back beside it.
table_serialize.phpt runs each row with and without preloading: a
Map<string, int>, a Map keyed by an enum, by a struct and by a class, a Map
in a List, a struct field, a class field, a Set, an empty Map, the bytes
serialize writes, a read-back in a fresh process, a bad key count and a Set
where a Map is declared.
php-sharp/
├── docs/spec.md section 11: serialize keeps a collection
├── ext/sharp/sharp.c __serialize and __unserialize on Sharp\Map and Sharp\Set
└── Zend/tests/sharp/
├── Queued/ new: Speed, Parcel, Shipment, Ledger, Courier, Payloads
├── table_serialize.inc new: the rows
├── table_serialize.phpt new: the rows without preloading
├── table_serialize_preload.inc new: the preload script
├── table_serialize_preload.phpt new: the rows under preloading
└── table_crossings.phpt the row that expected the refusal goes
…holds them, and unserialize always allows them
Problem
unserialize checks each PHP# field when it reads it, but fills a Map or a
Set only once the whole payload is read. So a tampered payload whose
Map<string, int> field held a string value or an int key, whose Set<Speed>
field held an int, or whose List<Map<string, int>> field held a wrong
value, was accepted, where the same tamper in a List<int> field throws.
The check of a generic class's open field ran at the same point, and
passed the same way.
unserialize with allowed_classes, which Sharp.Data.PhpSerializer passes as
[] by default, gave a Map back as __PHP_Incomplete_Class, and PhpSerializer
took only PHP's names for the classes it allows.
A second constructor call or a direct __unserialize call on a Map or a Set
that held entries wrote through every holder of a shared one.
Solution
Each object with a PHP# field queues a check when its last property is
read. var_destroy runs it after the __unserialize of each Map and Set the
object holds, which their own data queued first, so every entry is in
place. sharp_object_properties_accept checks each PHP#-typed field there,
the open fields of a generic class included, and a wrong entry throws the
"Cannot assign" TypeError a write from plain PHP throws.
unserialize allows Sharp\Map and Sharp\Set whatever allowed_classes says,
as it allows an array, and checks each key's own class as before.
PhpSerializer turns each PHP# name it is given, as `Queued.Crate`, into the
class PHP names.
A serialized Map that is not each key followed by its value throws "A
serialized Map must hold each key followed by its value", and a Set's data
that is no list throws "A serialized Set must hold a list of its elements".
Filling a Map or a Set that holds entries throws the Error offsetSet's
refusal words.
The checker's stub declares __serialize and __unserialize on Sharp\Map and
Sharp\Set.
php-sharp/
├── docs/
│ ├── spec.md section 11: whatever classes unserialize allows, and a wrong entry throws
│ └── architecture/slices/11-generics.md the delayed check
├── ext/
│ ├── sharp/
│ │ ├── php_sharp.h sharp_object_properties_accept replaces sharp_object_open_properties_accept
│ │ └── sharp.c the messages and the refusal to fill a table again
│ └── standard/var_unserializer.re VAR_SHARP_CHECK_FLAG, and Map and Set always allowed
├── sharp/
│ ├── composer/library/Sharp/Data/PhpSerializer.sharp PHP# class names in allowedClasses
│ └── mago/crates/prelude/assets/extensions/sharp.php __serialize and __unserialize
└── Zend/tests/sharp/
├── Queued/ new: Crate, Box; Payloads reads through PhpSerializer
├── table_serialize.inc rows for each tamper, the allowed classes and the refusals
├── table_serialize.phpt
├── table_serialize_preload.inc
└── table_serialize_preload.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A queued job, a cached value or a session that holds a PHP#
MaporSetthrewException: Serialization of 'Sharp\Map' is not allowed, alone or as a field of a struct or a class. A real Laravel app keeps these values throughserialize(), so moving any code that keeps aMapin a job or a cache broke. Spec section 11 already assumed it works: "an object serialized as it is keeps its type arguments".What a PHP# developer can now write
Mapor aSetserializes and reads back as itself, with its entries in the same order, alone, in aList, in a struct field or in a class field, in the same process or a fresh one, with and without preloading. The PHP# reader gets aMap, never an array.==are equal values, so a lookup with a fresh equal key finds them. A plain PHP object key, which has no==, is the object read back beside it in the same payload.is Map<string, int>exactly when the original is.unserializealways allows aMapand aSet, as it allows an array, whateverallowed_classessays. Each key's own class still goes through the check.Sharp.Data.PhpSerializertakes PHP# class names in its allowed list, as"Queued.Crate".Map,SetorList<Map<…>>holds a wrong key, value or element throws theTypeErrora write from plain PHP throws. A Map's data that is not each key followed by its value, and a Set's that is no list, throw aValueErrorthat says so.__unserializeon aMaporSetthat holds entries throws, since it would write through every holder of a shared one.Engine
ext/sharp/sharp.c:Sharp\MapandSharp\SetdropZEND_ACC_NOT_SERIALIZABLEand serialize through__serializeand__unserialize, asArrayObjectandSplObjectStoragedo.Map::__serializewrites each key then its value, the list the constructor takes, andSet::__serializeistoList.ext/standard/var_unserializer.re: each object with a PHP# field queues a check thatvar_destroyruns after the__unserializeof everyMapandSetinside it.sharp_object_properties_acceptreplacessharp_object_open_properties_acceptand checks every PHP#-typed field there, the open fields of a generic class included.Sharp\MapandSharp\Setpass theallowed_classescheck.sharp/mago: the checker's stub declares__serializeand__unserialize.docs/spec.mdsection 11 states the rule.Tests
table_serialize.phptandtable_serialize_preload.phptrun the same rows without and with preloading: aMap<string, int>, aMapkeyed by an enum, by a struct, by a class that declares==and by a plain PHP object, aMapin aList, a struct field, a class field, aSet, an emptyMap, the bytesserializewrites, a read-back in a fresh process, a bad key count, aSetunder named keys, aSetwhere aMapis declared, a wrong value in a class field, a wrong key in a struct field, a wrong element in aSetfield, a wrong value inside aList<Map<…>>field, a generic class field,PhpSerializer's default,PhpSerializerallowing a PHP# name,allowed_classes => false, and the two refusals to fill a table again.table_crossings.phptloses the row that expected the refusal.Not in this PR, after v1
var_exportof aMapor aSetprints an empty__set_state(array()).NANkey reads back as a key no lookup finds.type_arguments_out_of_memory.phptfails underUSE_ZEND_ALLOC=0, which enforces nomemory_limit.